来源:《Python编程:从入门到实践》
得分是游戏的一项统计信息,在GameStats中添加一个score属性
:game_stats.py
class GameStats():
--snip--
def reset_stats(self):
"""初始化在游戏运行期间可能变化的统计信息"""
self.ships_left = self.ai_settings.ship_limit
self.score = 0
reset_stats()而不是__init__()中初始化score
在屏幕上显示得分,现在创建一个新类Scoreboard
scoreboard.py
import pygame.font
class Scoreboard():
"""显示得分信息的类"""
def __init__(self, ai_settings, screen, stats):
"""初始化显示得分涉及的属性"""
self.screen = screen
self.screen_rect = screen.get_rect()
self.ai_settings = ai_settings
self.stats = stats
# 显示得分信息时使用的字体设置
self.text_color = (30, 30, 30)
self.font = pygame.font.SysFont(None, 48)
# 准备初始得分图像
self.prep_score()
prep_score()
:scoreboard.py
def prep_score(self):
"""将得分转换为一幅渲染的图像"""
score_str = str(self.stats.score)
self.score_image = self.font.render(score_str, True, self.text_color,
self.ai_settings.bg_color)
# 将得分放在屏幕右上角
self.score_rect = self.score_image.get_rect()
self.score_rect.right = self.screen_rect.right - 20
self.score_rect.top = 20
方法show_score()
:scoreboard.py
def show_score(self):
"""在屏幕上显示得分"""
self.screen.blit(self.score_image, self.score_rect)
在alien_invasion.py中创建一个Scoreboard实例
:alien_invasion.py
--snip--
from game_stats import GameStats
from scoreboard import Scoreboard
--snip--
def run_game():
--snip--
# 创建存储游戏统计信息的实例,并创建记分牌
stats = GameStats(ai_settings)
sb = Scoreboard(ai_settings, screen, stats)
--snip--
# 开始游戏的主循环
while True:
--snip--
gf.update_screen(ai_settings, screen, stats, sb, ship, aliens,
bullets, play_button)
run_game()
导入新类Scoreboard,并创建一个名为sb的Scoreboard实例
将sb传递给update_screen(),让它能够在屏幕上显示得分
game_functions.py
def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
play_button):
--snip--
# 显示得分
sb.show_score()
# 如果游戏处于非活动状态,就绘制Play按钮
if not stats.game_active:
play_button.draw_button()
# 让最近绘制的屏幕可见
pygame.display.flip()
上面在update_screen()的形参列表添加了sb,并在绘制Play按钮前调用show_score()
stats.score
的值,再调用prep_score()更新得分图像
settings.py
def initialize_dynamic_settings(self):
--snip--
# 记分
self.alien_points = 50
check_bullet_alien_collisions()中,每当击中外星人时,都更新得分
game_functions.py
def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship,
aliens, bullets):
"""响应子弹和外星人的碰撞"""
# 删除发生碰撞的子弹和外星人
collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
if collisions:
stats.score += ai_settings.alien_points
sb.prep_score()
--snip--
update_bullets(),确保函数之间传递合适的实参
game_functions.py
def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets):
"""更新子弹的位置,并删除已消失的子弹"""
--snip--
check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship,
aliens, bullets)
需要更新主while循环调用update_bullets()的代码
:alien_invasion.py
# 开始游戏的主循环
while True:
gf.check_events(ai_settings, screen, stats, play_button, ship, aliens,
bullets)
if stats.game_active:
ship.update()
gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens,
bullets)
--snip--
game_functions.py
def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship,
aliens, bullets):
--snip--
if collisions:
for aliens in collisions.values():
stats.score += ai_settings.alien_points * len(aliens)
sb.prep_score()
--snip--
settings.py
class Settings():
"""一个存储《外星人入侵》游戏的所有设置的类"""
def __init__(self):
--snip--
# 以什么样的速度加快游戏节奏
self.speedup_scale = 1.1
# 外星人点数的提高速度
self.score_scale = 1.5
self.initialize_dynamic_settings()
--snip--
def increase_speed(self):
"""提高速度设置 & 外星人点数"""
self.ship_speed_factor *= self.speedup_scale
self.bullet_speed_factor *= self.speedup_scale
self.alien_speed_factor *= self.speedup_scale
self.alien_points = int(self.alien_points * self.score_scale)
为让点数为整数,使用函数int()
scoreboard.py
def prep_score(self):
"""将得分转换为渲染的图像"""
rounded_score = int(round(self.stats.score, -1))
score_str = "{:,}".format(rounded_score)
self.score_image = self.font.render(score_str, True, self.text_color,
self.ai_settings.bg_color)
--snip--
函数round()通常让小数精确到小数点后多少位,其中小数位数是由第二个实参指定的
负数
,round()将圆整到最近的10、100、1000等整数倍
game_stats.py
class GameStats():
--snip--
# 在任何情况下都不应重置最高得分
self.high_score = 0
在任何情况下都不会重置最高得分,所以在__init__()中初始化high_score
,而不是reset_stats()__init__()
scoreboard.py
def __init__(self, ai_settings, screen, stats):
--snip--
# 准备包含最高得分和当前得分的图像
self.prep_score()
self.prep_high_score()
prep_high_score()用于准备最高得分的图像
scoreboard.py
def prep_high_score(self):
"""将最高得分转换为渲染的图像"""
high_score = int(round(self.stats.high_score, -1))
high_score_str = "{:,}".format(high_score)
self.high_score_image = self.font.render(high_score_str, True,
self.text_color, self.ai_settings.bg_color)
# 将最高得分放在屏幕顶部中央
self.high_score_rect = self.self.high_score_image.get_rect()
self.high_score_rect.centerx = self.screen_rect.centerx
self.high_score_rect.top = self.score_rect.top
方法show_score()
scoreboard.py
def show_score(self):
"""在屏幕上显示当前得分 & 最高得分"""
self.screen.blit(self.score_image, self.score_rect)
self.screen.blit(self.high_score_image, self.high_score_rect)
check_high_score()——检查是否诞生了新的最高得分
game_functions.py
def check_high_score(stats, sb):
"""检查是否诞生了新的最高得分"""
if stats.score > stats.high_score:
stats.high_score = stats.score
sb.prep_high_score()
game_functions.py
def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship,
aliens, bullets):
--snip--
if collisions:
for aliens in collisions.values():
stats.score += ai_settings.alien_points * len(aliens)
sb.prep_score()
check_high_score(stats, sb)
--snip--
因为每次开始新游戏都需要重置等级,所以必须在reset_stats()中初始化它
game_stats.py
def reset_stats(self):
"""初始化在游戏运行期间可能变化的统计信息"""
self.ships_left = self.ai_settings.ship_limit
self.score = 0
self.level = 1
让Scoreboard在当前得分下方显示当前等级,在__init__()调用一个新方法prep_level()
scoreboard.py
def __init__(self, ai_settings, screen, stats):
--snip--
# 准备包含最高得分和当前得分的图像
self.prep_score()
self.prep_high_score()
self.prep_level()
--snip--
def prep_level(self):
"""将等级转换为渲染的图像"""
self.level_image = self.font.render(str(self.stats.level), True,
self.text_color, self.ai_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
更新show_score()
scoreboard.py
def show_score(self):
"""在屏幕上显示当前得分 & 最高得分 & 等级"""
self.screen.blit(self.score_image, self.score_rect)
self.screen.blit(self.high_score_image, self.high_score_rect)
self.screen.blit(self.level_image, self.level_rect)
在check_bullet_alien_collisions()中提高等级,并更新等级图像
game_functions.py
def check_bullet_alien_collisions(ai_settings, screen, stats, sb, ship,
aliens, bullets):
--snip--
if len(aliens) == 0:
# 如果整群外星人都被消灭, 就提高一个level
bullets.empty()
ai_settings.increase_speed()
# 提高等级
stats.level += 1
sb.prep_level()
create_fleet(ai_settings, screen, ship, aliens)
game_functions.py
def check_play_button(ai_settings, screen, stats, sb, play_button, ship,
aliens, bullets, mouse_x, mouse_y):
"""单机Play按钮时开始新游戏"""
button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
if button_clicked and not stats.game_active:
--snip--
# 重置游戏统计信息
stats.reset_stats()
stats.game_active = True
# 重置记分牌图像
sb.prep_score()
sb.prep_high_score()
sb.prep_level()
--snip--
check_play_button()的定义包含对象sb
更新check_events()
的代码game_functions.py
def check_events(ai_settings, screen, stats, sb, play_button, ship, aliens,
bullets):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
--snip--
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(ai_settings, screen, stats, sb, play_button,
ship, aliens, bullets, mouse_x, mouse_y)
check_events()的定义需要包含形参sb
更新alien_invasion.py中调用check_events()的代码
alien_invasion.py
# 开始游戏的主循环
while True:
gf.check_events(ai_settings, screen, stats, sb, play_button, ship,
aliens, bullets)
--snip--
ship.py
import pygame
from pygame.sprite import Sprite
class Ship(Sprite):
def __init__(self, ai_settings, screen):
"""初始化飞船并设置其初始位置"""
super().__init__()
--snip--
scoreboard.py
import pygame.font
from pygame.sprite import Group
from ship import Ship
class Scoreboard():
"""显示得分信息的类"""
def __init__(self, ai_settings, screen, stats):
--snip--
self.prep_level()
self.prep_ships()
调用prep_level()后,调用prep_ships()
scoreboard.py
def prep_ships(self):
"""显示还有多少艘飞船"""
self.ships = Group()
for ship_number in range(self.stats.ships_left):
ship = Ship(self.ai_settings, self.screen)
ship.rect.x = 10 + ship_number * ship.rect.width
ship.rect.y = 10
self.ships.add(ship)
方法prep_ships()创建一个空编组self.ships,存储飞船实例
scoreboard.py
def show_score(self):
--snip--
self.screen.blit(self.level_image, self.level_rect)
# 绘制飞船
self.ships.draw(self.screen)
game_functions.py
def check_play_button(ai_settings, screen, stats, sb, play_button, ship,
aliens, bullets, mouse_x, mouse_y):
"""单机Play按钮时开始新游戏"""
button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
if button_clicked and not stats.game_active:
--snip--
# 重置记分牌图像
sb.prep_score()
sb.prep_high_score()
sb.prep_level()
sb.prep_ships()
--snip--
在飞船被外星人撞到时调用prep_ships()
game_functions.py
def update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets):
--snip--
# 检测外星人和飞船之间的碰撞
if pygame.sprite.spritecollideany(ship, aliens):
ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)
# 检查是否有外星人到达屏幕底端
check_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets)
def ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets):
"""响应被外星人撞到的飞船"""
if stats.ships_left > 0:
# 将ship_left减去1
stats.ships_left -= 1
# 更新记分牌
sb.prep_ships()
# 清空外星人列表和子弹列表
--snip--
更新上面的方法,注意新增的形参
game_functions.py
def check_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets):
"""检查是否有外星人到达了屏幕底端"""
screen_rect = screen.get_rect()
for alien in aliens.sprites():
if alien.rect.bottom >= screen_rect.bottom:
# 像飞船被撞到一样进行处理
ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)
break
check_aliens_bottom()传递实参sb
最后,在alien_invasion.py中修改调用update_aliens()的代码,传递实参sb
alien_invasion.py
# 开始游戏的主循环
while True:
--snip--
if stats.game_active:
ship.update()
gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens,
bullets)
gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens,
bullets)
--snip--