Skip to content

Commit bcd3d35

Browse files
Merge pull request #49 from chris-greening/add-stats-to-game-over
Add stats to game over screen
2 parents c695e71 + 8613837 commit bcd3d35

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

planetoids/core/game_state.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def __init__(self, screen, settings, clock):
3737
logger.info("GameState instantiated")
3838
self.score_popups = []
3939
self.debris = []
40+
self.shots_fired = 0
41+
self.asteroids_destroyed = 0
42+
self.shots_hit = 0
43+
self.start_time = pygame.time.get_ticks()
4044

4145
@property
4246
def font(self):

planetoids/core/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.1
1+
0.7.2

planetoids/entities/asteroid.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def split(self):
6464
angle = random.uniform(0, 360) # Random scatter direction
6565
speed = random.uniform(1, 3) # Small speed variation
6666
self.game_state.debris.append(Debris(self.x, self.y, angle, speed))
67+
68+
self.game_state.asteroids_destroyed += 1
69+
self.game_state.shots_hit += 1
6770
return asteroids
6871

6972
@classmethod

planetoids/entities/player.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def shoot(self):
6666
radius = 7
6767
for angle in angles:
6868
bullets.append(Bullet(self.game_state, self.x, self.y, angle, color=color, radius=radius))
69+
self.game_state.shots_fired += 1
6970

7071
return bullets
7172

planetoids/ui/game_over.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ def _display_game_over(self, screen, dt):
3030
start_time = pygame.time.get_ticks()
3131
game_over = True
3232

33+
accuracy = (
34+
(self.game_state.shots_hit / self.game_state.shots_fired) * 100
35+
if self.game_state.shots_fired > 0 else 0
36+
)
37+
38+
# Time survived calculation
39+
elapsed_ms = pygame.time.get_ticks() - self.game_state.start_time
40+
seconds = elapsed_ms // 1000
41+
minutes = seconds // 60
42+
hours = minutes // 60
43+
formatted_time = f"{hours:02}:{minutes % 60:02}:{seconds % 60:02}"
44+
3345
while game_over:
3446
screen.fill(config.BLACK)
3547

@@ -38,6 +50,7 @@ def _display_game_over(self, screen, dt):
3850
asteroid.draw(screen)
3951

4052
self.game_state.score.draw(screen, show_multiplier=False)
53+
self.game_state.level.draw(screen)
4154

4255
screen.blit(text, text_rect)
4356

@@ -52,6 +65,20 @@ def _display_game_over(self, screen, dt):
5265
pixelation=self.settings.get("pixelation")
5366
)
5467

68+
# Additional stats display
69+
stat_font = pygame.font.Font(self.settings.FONT_PATH, font_size)
70+
stats = [
71+
f"Time Survived: {formatted_time}",
72+
f"Shots Fired: {self.game_state.shots_fired}",
73+
f"Asteroids Destroyed: {self.game_state.asteroids_destroyed}",
74+
f"Accuracy: {accuracy:.1f}%"
75+
]
76+
77+
for i, line in enumerate(stats):
78+
stat_text = stat_font.render(line, True, config.WHITE)
79+
stat_rect = stat_text.get_rect(center=(config.WIDTH // 2, config.HEIGHT // 2 + 200 + i * 40))
80+
screen.blit(stat_text, stat_rect)
81+
5582
pygame.display.flip()
5683
self.game_state.clock.tick(config.FPS)
5784

0 commit comments

Comments
 (0)