103 lines
3.1 KiB
Python
103 lines
3.1 KiB
Python
|
import pygame
|
||
|
import sys
|
||
|
import random
|
||
|
|
||
|
# Initialize Pygame
|
||
|
pygame.init()
|
||
|
|
||
|
# Constants
|
||
|
SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480
|
||
|
GRID_SIZE = 20
|
||
|
FPS = 10
|
||
|
|
||
|
# Colors
|
||
|
WHITE = (255, 255, 255)
|
||
|
BLACK = (0, 0, 0)
|
||
|
GREEN = (0, 255, 0)
|
||
|
RED = (255, 0, 0)
|
||
|
|
||
|
|
||
|
class Game:
|
||
|
def __init__(self):
|
||
|
# Set up the display
|
||
|
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||
|
self.clock = pygame.time.Clock()
|
||
|
self.snake = Snake([(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)], (0, -1), 1)
|
||
|
self.food = Food(self.generate_food_position())
|
||
|
|
||
|
def generate_food_position(self):
|
||
|
return (random.randint(0, (SCREEN_WIDTH // GRID_SIZE) - 1) * GRID_SIZE,
|
||
|
random.randint(0, (SCREEN_HEIGHT // GRID_SIZE) - 1) * GRID_SIZE)
|
||
|
|
||
|
def main_loop(self):
|
||
|
while True:
|
||
|
for event in pygame.event.get():
|
||
|
if event.type == pygame.QUIT:
|
||
|
pygame.quit()
|
||
|
sys.exit()
|
||
|
|
||
|
# Handle user input and update game state
|
||
|
self.handle_input()
|
||
|
self.update_game_state()
|
||
|
|
||
|
# Draw everything
|
||
|
self.screen.fill(BLACK)
|
||
|
self.draw_snake()
|
||
|
self.draw_food()
|
||
|
pygame.display.flip()
|
||
|
|
||
|
# Cap the frame rate
|
||
|
self.clock.tick(FPS)
|
||
|
|
||
|
def handle_input(self):
|
||
|
keys = pygame.key.get_pressed()
|
||
|
if keys[pygame.K_UP] and self.snake.direction != (0, 1):
|
||
|
self.snake.change_direction((0, -1))
|
||
|
elif keys[pygame.K_DOWN] and self.snake.direction != (0, -1):
|
||
|
self.snake.change_direction((0, 1))
|
||
|
elif keys[pygame.K_LEFT] and self.snake.direction != (1, 0):
|
||
|
self.snake.change_direction((-1, 0))
|
||
|
elif keys[pygame.K_RIGHT] and self.snake.direction != (-1, 0):
|
||
|
self.snake.change_direction((1, 0))
|
||
|
|
||
|
def update_game_state(self):
|
||
|
self.snake.move()
|
||
|
if self.snake.position[0] == self.food.position:
|
||
|
self.snake.grow()
|
||
|
self.food.position = self.generate_food_position()
|
||
|
|
||
|
def draw_snake(self):
|
||
|
for segment in self.snake.position:
|
||
|
pygame.draw.rect(self.screen, GREEN, (segment[0], segment[1], GRID_SIZE, GRID_SIZE))
|
||
|
|
||
|
def draw_food(self):
|
||
|
pygame.draw.rect(self.screen, RED, (self.food.position[0], self.food.position[1], GRID_SIZE, GRID_SIZE))
|
||
|
|
||
|
|
||
|
class Snake:
|
||
|
def __init__(self, position, direction, length):
|
||
|
self.position = position
|
||
|
self.direction = direction
|
||
|
self.length = length
|
||
|
|
||
|
def move(self):
|
||
|
head_x, head_y = self.position[0]
|
||
|
new_dir_x, new_dir_y = self.direction
|
||
|
new_head = (head_x + new_dir_x * GRID_SIZE, head_y + new_dir_y * GRID_SIZE)
|
||
|
self.position.insert(0, new_head)
|
||
|
if len(self.position) > self.length:
|
||
|
self.position.pop()
|
||
|
|
||
|
def change_direction(self, new_direction):
|
||
|
self.direction = new_direction
|
||
|
|
||
|
def grow(self):
|
||
|
self.length += 1
|
||
|
|
||
|
|
||
|
class Food:
|
||
|
def __init__(self, position):
|
||
|
self.position = position
|
||
|
|
||
|
def spawn(self, board_size):
|
||
|
pass # This method is not used in this implementation
|