-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_stats.py
More file actions
29 lines (23 loc) · 832 Bytes
/
game_stats.py
File metadata and controls
29 lines (23 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import json
class GameStats:
"""Track statistics for Alien Invasion."""
def __init__(self, ai_game):
"""Initialize statistics."""
self.settings = ai_game.settings
self.reset_stats()
# Start game in an inactive state.
self.game_active = False
# High score should never be reset.
self.high_score = self.get_saved_high_score()
def get_saved_high_score(self):
"""Gets high score from file, if it exists."""
try:
with open('high_score.json') as f:
return json.load(f)
except FileNotFoundError:
return 0
def reset_stats(self):
"""Initialize statistics that can change during the game."""
self.ships_left = self.settings.ship_limit
self.score = 0
self.level = 1