You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1020 B
33 lines
1020 B
# 开发日期: 2024/4/26
|
|
import pickle
|
|
class GameStats():
|
|
"""跟踪游戏的统计信息"""
|
|
|
|
def __init__(self, ai_settings):
|
|
"""初始化统计信息"""
|
|
self.ai_settings = ai_settings
|
|
self.reset_stats()
|
|
# 游戏刚启动的时候处于活动状态
|
|
self.game_active = False
|
|
# 在任何情况下都不应该重置最高分
|
|
self.high_score = 0
|
|
|
|
def reset_stats(self):
|
|
"""初始化在游戏运行期间可能变化的统计信息"""
|
|
self.ships_left = self.ai_settings.ship_limit
|
|
self.score = 0
|
|
self.level = 1
|
|
def save_high_score(self):
|
|
f = open("high_score.pk1", 'wb')
|
|
pickle.dump(str(self.high_score), f, 0)
|
|
f.close()
|
|
def load_high_score(self):
|
|
f = open("high_score.pk1", 'rb')
|
|
try:
|
|
str_high_score = pickle.load(f)
|
|
self.high_score = int(str_high_score)
|
|
except EOFError:
|
|
self.high_score = 0
|
|
finally:
|
|
f.close()
|