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

Commit a5a6caf

Browse files
committed
implement high score system
1 parent 39e023b commit a5a6caf

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

projects/Snake Game/src/display.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ def __init__(self):
1212
pygame.display.set_caption("Snake")
1313
self.clock = pygame.time.Clock()
1414

15-
def update_ui(self, snake, food, score):
15+
def update_ui(self, snake, food, score, high_score):
1616
self.window.fill(RgbColors.BLACK)
1717
self.draw_snake(snake)
1818
self.draw_food(food)
1919
self.draw_score(score)
20+
self.render_high_score(high_score)
2021
pygame.display.flip()
2122

2223
def draw_snake(self, snake):
@@ -37,6 +38,7 @@ def draw_food(self, food):
3738
)
3839

3940
def draw_score(self, score):
41+
self.font = pygame.font.Font(None, 25)
4042
score_display = self.font.render(f"Score: {score}", True, RgbColors.WHITE)
4143
self.window.blit(score_display, [0, 0]) # score in top left window corner
4244

@@ -57,3 +59,17 @@ def render_play_again(self):
5759
display_box = play_again_display.get_rect(center=(self.width // 2, self.height // 2))
5860
self.window.blit(play_again_display, display_box)
5961
pygame.display.flip()
62+
63+
def render_high_score(self, high_score):
64+
high_score_display = self.font.render(f"High Score: {high_score}", True, RgbColors.WHITE)
65+
self.window.blit(high_score_display, [self.width - high_score_display.get_width(), 0])
66+
67+
def render_new_high_score(self, new_high_score):
68+
new_high_score_display = self.font.render(f"New High Score: {new_high_score}", True, RgbColors.WHITE)
69+
text_width = new_high_score_display.get_width()
70+
text_height = new_high_score_display.get_height()
71+
text_x = (self.width - text_width) // 2
72+
text_y = (self.height // 3) - (text_height // 2)
73+
self.window.blit(new_high_score_display,
74+
[text_x, text_y])
75+
pygame.display.flip()

projects/Snake Game/src/game.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pygame
22
import random
3+
import json
34
from snake import Snake, Direction, Point
45
from display import Display
56
from constants import GameSettings
@@ -12,18 +13,23 @@ def __init__(self):
1213
self.score = 0
1314
self.food = None
1415
self.place_food()
16+
self.high_score = self.load_high_score()
1517

1618
def game_loop(self):
1719
while True:
1820
self.play_step()
1921
game_over, score = self.play_step()
22+
self.update_high_score(self.high_score)
2023
if game_over:
2124
self.display.render_game_over()
25+
if score > self.high_score:
26+
self.display.render_new_high_score(score)
27+
self.update_high_score(score)
28+
self.high_score = self.load_high_score()
2229
self.display.render_play_again()
2330
if not self.play_again():
2431
break
2532
self.restart_game()
26-
print("Final Score:", self.score)
2733
pygame.quit()
2834

2935
def is_collision(self):
@@ -67,7 +73,7 @@ def play_step(self):
6773
else:
6874
self.snake.blocks.pop()
6975
# Update UI and Clock
70-
self.display.update_ui(self.snake, self.food, self.score)
76+
self.display.update_ui(self.snake, self.food, self.score, self.high_score)
7177
self.display.clock.tick(GameSettings.SPEED)
7278
game_over = self.is_collision()
7379
return game_over, self.score
@@ -96,3 +102,19 @@ def restart_game(self):
96102
self.snake = Snake()
97103
self.score = 0
98104
self.place_food()
105+
self.high_score = self.load_high_score()
106+
107+
def load_high_score(self):
108+
try:
109+
with open('high_score.json', 'r') as file:
110+
data = json.load(file)
111+
return data.get('high_score')
112+
except FileNotFoundError:
113+
return 0
114+
115+
def update_high_score(self, new_score):
116+
high_score = self.load_high_score()
117+
if new_score > high_score:
118+
data = {"high_score": new_score}
119+
with open('high_score.json', 'w') as file:
120+
json.dump(data, file)

0 commit comments

Comments
 (0)