Add game restart functionality and update roadmap

This commit is contained in:
Rbanh committed 2025-02-23 12:13:24 -05:00
1 parent dda11c6dc5
commit 18ff38fe47
2 files changed
+29 -19

No files matched your search

+14 -5
View File
@@ -24,15 +24,17 @@ class Game:
# 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)
self.reset_game()
# Game state
self.state = GameState.PLAYING # Changed to start in playing state for now
self.running = True
def reset_game(self):
"""Reset the game state for a new game"""
self.snake = Snake((self.width // 2, self.height // 2), self.block_size)
self.food = Food(self.block_size)
self.food.spawn(self.width, self.height, self.snake.body)
self.score = 0
def handle_events(self):
@@ -55,6 +57,10 @@ class Game:
self.snake.change_direction(Direction.LEFT)
elif event.key == pygame.K_RIGHT:
self.snake.change_direction(Direction.RIGHT)
elif self.state == GameState.GAME_OVER:
# Any key to restart in game over state
self.reset_game()
self.state = GameState.PLAYING
def update(self):
if self.state == GameState.PLAYING:
@@ -95,12 +101,15 @@ class Game:
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))
restart_text = font.render('Press any key to restart', 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))
restart_rect = restart_text.get_rect(center=(self.width//2, self.height//2 + 150))
self.screen.blit(text, text_rect)
self.screen.blit(score_text, score_rect)
self.screen.blit(restart_text, restart_rect)
# Update display
pygame.display.flip()