Implement food spawning system and basic scoring
This commit is contained in:
1 parent
520eba279b
commit
424cd4c727
3 files changed
+134
-42
No files matched your search
+52
@@ -0,0 +1,52 @@
|
||||
import pygame
|
||||
import random
|
||||
from typing import Tuple, List
|
||||
|
||||
class Food:
|
||||
def __init__(self, block_size: int, color: Tuple[int, int, int] = (255, 0, 0)):
|
||||
self.block_size = block_size
|
||||
self.color = color
|
||||
self.position = (0, 0) # Will be set by spawn()
|
||||
|
||||
def spawn(self, width: int, height: int, occupied_positions: List[Tuple[int, int]]) -> None:
|
||||
"""
|
||||
Spawn food at a random position that isn't occupied by the snake.
|
||||
|
||||
Args:
|
||||
width: Game area width
|
||||
height: Game area height
|
||||
occupied_positions: List of positions (typically snake body positions) where food shouldn't spawn
|
||||
"""
|
||||
# Calculate valid grid positions
|
||||
valid_x = range(0, width, self.block_size)
|
||||
valid_y = range(0, height, self.block_size)
|
||||
|
||||
# Create list of all possible positions
|
||||
all_positions = [
|
||||
(x, y) for x in valid_x for y in valid_y
|
||||
if (x, y) not in occupied_positions
|
||||
]
|
||||
|
||||
if not all_positions:
|
||||
# No valid positions (snake fills screen) - game should be won
|
||||
return
|
||||
|
||||
# Choose random position
|
||||
self.position = random.choice(all_positions)
|
||||
|
||||
def draw(self, screen: pygame.Surface) -> None:
|
||||
"""Draw the food on the screen"""
|
||||
pygame.draw.rect(
|
||||
screen,
|
||||
self.color,
|
||||
pygame.Rect(
|
||||
self.position[0],
|
||||
self.position[1],
|
||||
self.block_size,
|
||||
self.block_size
|
||||
)
|
||||
)
|
||||
|
||||
def check_collision(self, position: Tuple[int, int]) -> bool:
|
||||
"""Check if the given position collides with the food"""
|
||||
return self.position == position
|
||||
+59
-23
@@ -1,6 +1,8 @@
|
||||
import pygame
|
||||
import sys
|
||||
from enum import Enum, auto
|
||||
from snake import Snake, Direction
|
||||
from food import Food
|
||||
|
||||
class GameState(Enum):
|
||||
MENU = auto()
|
||||
@@ -20,9 +22,18 @@ class Game:
|
||||
self.clock = pygame.time.Clock()
|
||||
self.fps = 60
|
||||
|
||||
# Game objects
|
||||
self.block_size = 20
|
||||
self.snake = Snake((self.width // 2, self.height // 2), self.block_size)
|
||||
self.food = Food(self.block_size)
|
||||
|
||||
# Spawn initial food
|
||||
self.food.spawn(self.width, self.height, self.snake.body)
|
||||
|
||||
# Game state
|
||||
self.state = GameState.MENU
|
||||
self.state = GameState.PLAYING # Changed to start in playing state for now
|
||||
self.running = True
|
||||
self.score = 0
|
||||
|
||||
def handle_events(self):
|
||||
for event in pygame.event.get():
|
||||
@@ -34,37 +45,62 @@ class Game:
|
||||
self.state = GameState.PAUSED
|
||||
elif self.state == GameState.PAUSED:
|
||||
self.state = GameState.PLAYING
|
||||
elif self.state == GameState.PLAYING:
|
||||
# Handle snake direction
|
||||
if event.key == pygame.K_UP:
|
||||
self.snake.change_direction(Direction.UP)
|
||||
elif event.key == pygame.K_DOWN:
|
||||
self.snake.change_direction(Direction.DOWN)
|
||||
elif event.key == pygame.K_LEFT:
|
||||
self.snake.change_direction(Direction.LEFT)
|
||||
elif event.key == pygame.K_RIGHT:
|
||||
self.snake.change_direction(Direction.RIGHT)
|
||||
|
||||
def update(self):
|
||||
if self.state == GameState.PLAYING:
|
||||
# Update game logic here
|
||||
pass
|
||||
elif self.state == GameState.MENU:
|
||||
# Update menu logic here
|
||||
pass
|
||||
elif self.state == GameState.GAME_OVER:
|
||||
# Update game over logic here
|
||||
pass
|
||||
elif self.state == GameState.PAUSED:
|
||||
# Update pause menu logic here
|
||||
pass
|
||||
# Move snake
|
||||
if self.snake.move(pygame.time.get_ticks()):
|
||||
# Check wall collision
|
||||
if self.snake.check_collision(self.width, self.height):
|
||||
self.state = GameState.GAME_OVER
|
||||
return
|
||||
|
||||
# Check food collision
|
||||
if self.food.check_collision(self.snake.body[0]):
|
||||
self.snake.grow()
|
||||
self.score += 1
|
||||
self.food.spawn(self.width, self.height, self.snake.body)
|
||||
|
||||
def render(self):
|
||||
# Clear screen
|
||||
self.screen.fill((0, 0, 0)) # Black background
|
||||
|
||||
if self.state == GameState.PLAYING:
|
||||
# Render game here
|
||||
pass
|
||||
elif self.state == GameState.MENU:
|
||||
# Render menu here
|
||||
pass
|
||||
if self.state == GameState.PLAYING or self.state == GameState.PAUSED:
|
||||
# Draw game objects
|
||||
self.snake.draw(self.screen)
|
||||
self.food.draw(self.screen)
|
||||
|
||||
# Draw score
|
||||
font = pygame.font.Font(None, 36)
|
||||
score_text = font.render(f'Score: {self.score}', True, (255, 255, 255))
|
||||
self.screen.blit(score_text, (10, 10))
|
||||
|
||||
# Draw pause indicator
|
||||
if self.state == GameState.PAUSED:
|
||||
pause_text = font.render('PAUSED', True, (255, 255, 255))
|
||||
text_rect = pause_text.get_rect(center=(self.width//2, self.height//2))
|
||||
self.screen.blit(pause_text, text_rect)
|
||||
|
||||
elif self.state == GameState.GAME_OVER:
|
||||
# Render game over screen here
|
||||
pass
|
||||
elif self.state == GameState.PAUSED:
|
||||
# Render pause menu here
|
||||
pass
|
||||
font = pygame.font.Font(None, 74)
|
||||
text = font.render('Game Over!', True, (255, 0, 0))
|
||||
score_text = font.render(f'Score: {self.score}', True, (255, 255, 255))
|
||||
|
||||
text_rect = text.get_rect(center=(self.width//2, self.height//2 - 50))
|
||||
score_rect = score_text.get_rect(center=(self.width//2, self.height//2 + 50))
|
||||
|
||||
self.screen.blit(text, text_rect)
|
||||
self.screen.blit(score_text, score_rect)
|
||||
|
||||
# Update display
|
||||
pygame.display.flip()
|
||||
|
||||
Reference in new issue
Block a user