完善记分系统

  • 1确保难度升级分值跟着升级
  • 2将分值显示为10的整数倍
  • 3显示最高分
  • 4显示等级
  • 5显示剩余飞船数

确保难度升级分值跟着升级

self.alien_points = int(self.alien_points * self.score_scale)
print(self.alien_points)

print确保分值变化,确保后删除

Python从入门到实践project飞船射击外星人3_重置

将分值显示为10的整数倍

rounded_score = round(self.stats.score, -1)
score_str = f"{rounded_score:,}"

Python从入门到实践project飞船射击外星人3_Group_02

显示最高分

# 在任何情况下都不应重置最⾼分
self.high_score = 0

Python从入门到实践project飞船射击外星人3_重置_03

self.prep_high_score()

Python从入门到实践project飞船射击外星人3_重置_04

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

Python从入门到实践project飞船射击外星人3_Group_05

self.screen.blit(self.high_score_image, self.high_score_rect)

Python从入门到实践project飞船射击外星人3_Group_06

检测是否显示最高得分

def check_high_score(self):
    """检查是否诞⽣了新的最⾼分"""
    if self.stats.score > self.stats.high_score:
        self.stats.high_score = self.stats.score
        self.prep_high_score()

Python从入门到实践project飞船射击外星人3_Group_07

self.sb.check_high_score()

Python从入门到实践project飞船射击外星人3_Group_08

显示等级

self.level = 1

Python从入门到实践project飞船射击外星人3_重置_09

self.prep_high_score()
self.prep_level()

Python从入门到实践project飞船射击外星人3_重置_10

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

Python从入门到实践project飞船射击外星人3_Group_11

self.screen.blit(self.level_image, self.level_rect)

Python从入门到实践project飞船射击外星人3_Group_12

# 提⾼等级
self.stats.level += 1
self.sb.prep_level()

Python从入门到实践project飞船射击外星人3_重置_13

self.sb.prep_level()

Python从入门到实践project飞船射击外星人3_重置_14

显示剩余飞船数

from pygame.sprite import Sprite
uper().__init__()

Python从入门到实践project飞船射击外星人3_重置_15

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)

Python从入门到实践project飞船射击外星人3_Group_16

绘制飞船

self.ships.draw(self.screen)

Python从入门到实践project飞船射击外星人3_Group_17

self.sb.prep_ships()

Python从入门到实践project飞船射击外星人3_Group_18

self.sb.prep_ships()

Python从入门到实践project飞船射击外星人3_Group_19