Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit fe6666c

Browse files
committed
implement play again feature - resolves #743
1 parent c973a2c commit fe6666c

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

projects/Snake Game/src/display.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ def __init__(self):
1414

1515
def update_ui(self, snake, food, score):
1616
self.window.fill(RgbColors.BLACK)
17-
# Draw snake
17+
self.draw_snake(snake)
18+
self.draw_food(food)
19+
self.draw_score(score)
20+
pygame.display.flip()
21+
22+
def draw_snake(self, snake):
1823
for block in snake.blocks:
1924
pygame.draw.rect(
2025
self.window, RgbColors.BLUE1,
@@ -23,13 +28,32 @@ def update_ui(self, snake, food, score):
2328
pygame.draw.rect(
2429
self.window, RgbColors.BLUE2, pygame.Rect(block.x + 4, block.y + 4, 12, 12)
2530
)
26-
# Draw food
31+
32+
def draw_food(self, food):
2733
pygame.draw.rect(
2834
self.window,
2935
RgbColors.RED,
3036
pygame.Rect(food.x, food.y, GameSettings.BLOCK_SIZE, GameSettings.BLOCK_SIZE),
3137
)
32-
# Draw score
38+
39+
def draw_score(self, score):
3340
score_display = self.font.render(f"Score: {score}", True, RgbColors.WHITE)
34-
self.window.blit(score_display, [0, 0]) # score in top left corner of window
41+
self.window.blit(score_display, [0, 0]) # score in top left window corner
42+
43+
def render_game_over(self):
44+
self.font = pygame.font.Font(None, 48)
45+
game_over_display = self.font.render("GAME OVER", True, RgbColors.WHITE)
46+
text_width = game_over_display.get_width()
47+
text_height = game_over_display.get_height()
48+
text_x = (self.width - text_width) // 2
49+
text_y = (self.height // 4) - (text_height // 2)
50+
self.window.blit(game_over_display,
51+
[text_x, text_y])
52+
pygame.display.flip()
53+
54+
def render_play_again(self):
55+
self.font = pygame.font.Font(None, 32)
56+
play_again_display = self.font.render("Play again? (Y/N)", True, RgbColors.WHITE)
57+
display_box = play_again_display.get_rect(center=(self.width // 2, self.height // 2))
58+
self.window.blit(play_again_display, display_box)
3559
pygame.display.flip()

projects/Snake Game/src/game.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ def game_loop(self):
1818
self.play_step()
1919
game_over, score = self.play_step()
2020
if game_over:
21-
break
21+
self.display.render_game_over()
22+
self.display.render_play_again()
23+
if not self.play_again():
24+
break
25+
self.restart_game()
2226
print("Final Score:", self.score)
2327
pygame.quit()
2428

@@ -77,5 +81,18 @@ def place_food(self):
7781
if self.food in self.snake.blocks:
7882
self.place_food()
7983

80-
def get_score(self):
81-
return self.score
84+
def play_again(self):
85+
while True:
86+
for event in pygame.event.get():
87+
if event.type == pygame.QUIT:
88+
return False
89+
if event.type == pygame.KEYDOWN:
90+
if event.key in [pygame.K_n, pygame.K_ESCAPE]:
91+
return False
92+
if event.key in [pygame.K_y, pygame.K_RETURN]:
93+
return True
94+
95+
def restart_game(self):
96+
self.snake = Snake()
97+
self.score = 0
98+
self.place_food()

0 commit comments

Comments
 (0)