完善记分系统
- 1确保难度升级分值跟着升级
- 2将分值显示为10的整数倍
- 3显示最高分
- 4显示等级
- 5显示剩余飞船数
确保难度升级分值跟着升级
self.alien_points = int(self.alien_points * self.score_scale)
print(self.alien_points)
print确保分值变化,确保后删除
将分值显示为10的整数倍
rounded_score = round(self.stats.score, -1)
score_str = f"{rounded_score:,}"
显示最高分
# 在任何情况下都不应重置最⾼分
self.high_score = 0
self.prep_high_score()
def prep_high_score(self):
"""将最⾼分渲染为图像"""
high_score = round(self.stats.high_score, -1)
high_score_str = f"{high_score:,}"
self.high_score_image = self.font.render(high_score_str,True,
self.text_color, self.settings.bg_color)
# 将最⾼分放在屏幕顶部的中央
self.high_score_rect = self.high_score_image.get_rect()
self.high_score_rect.center = self.screen_rect.center
self.high_score_rect.top = self.score_rect.top
self.screen.blit(self.high_score_image, self.high_score_rect)
检测是否显示最高得分
def check_high_score(self):
"""检查是否诞⽣了新的最⾼分"""
if self.stats.score > self.stats.high_score:
self.stats.high_score = self.stats.score
self.prep_high_score()
self.sb.check_high_score()
显示等级
self.level = 1
self.prep_high_score()
self.prep_level()
def prep_level(self):
"""将等级渲染为图像"""
level_str = str(self.stats.level)
self.level_image = self.font.render(level_str, True,self.text_color, self.settings.bg_color)
# 将等级放在得分下⽅
self.level_rect = self.level_image.get_rect()
self.level_rect.right = self.score_rect.right
self.level_rect.top = self.score_rect.bottom + 10
self.screen.blit(self.level_image, self.level_rect)
# 提⾼等级
self.stats.level += 1
self.sb.prep_level()
self.sb.prep_level()
显示剩余飞船数
from pygame.sprite import Sprite
uper().__init__()
def prep_ships(self):
"""显⽰还余下多少艘飞船"""
self.ships = Group()
for ship_number in range(self.stats.ships_left):
ship = Ship(self.ai_game)
ship.rect.x = 10 + ship_number * ship.rect.width
ship.rect.y = 10
self.ships.add(ship)
绘制飞船
self.ships.draw(self.screen)
self.sb.prep_ships()
self.sb.prep_ships()