# 昨日知识点回顾
添加Play按钮创建Button类绘制按钮开始游戏、游戏结束重制游戏影藏鼠标光标
# 今日知识点学习
14.2 提高等级
14.2.1 修改速度设置
# Settings.py
# 加快游戏节奏的速度
self.speedup_scale = 1.1
self.initialize_dynamic_settings()
def initialize_dynamic_settings(self):
"""初始化随游戏进行而变化的设置"""
self.ship_speed = 1.5
self.bullet_speed = 3.0
self.alien_speed = 1.0
# fleet_direction为1表示向右移,为-1表示向左移
self.fleet_direction = 1
def increase_speed(self):
"""提高速度设置"""
self.ship_speed *= self.speedup_scale
self.bullet_speed *= self.speedup_scale
self.alien_speed *= self.speedup_scale
# 主程序
---snip---
def _check_bullet_alien_collisions(self):
"""相应子弹和外星人碰撞"""
# 删除发生碰撞的子弹和外星人
collisions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True)
if not self.aliens:
# 删除现有的子弹并新建一群外星人
self.bullets.empty()
self._create_fleet()
# 调用increase_speed()加快游戏节奏,加大游戏难度
self.settings.increase_speed()
---snip---
14.2.2 重置速度
# 主程序
def _check_play_button(self, mouse_pos):
"""在玩家单击Play按钮时开始新游戏"""
# game_active为False时游戏才开始并显示Play按钮
button_clicked = self.play_button.rect.collidepoint(mouse_pos)
if button_clicked and not self.stats.game_active:
# 重置游戏设置
self.settings.initialize_dynamic_settings()
# 重置游戏统计信息
self.stats.reset_stats()
self.stats.game_active = True
14.3 记分
# game_stats.py
class GameStats:
"""跟踪游戏的统计信息"""
def __init__(self, ai_game):
"""初始化统计信息"""
self.settings = ai_game.settings
self.reset_stats()
# 游戏在刚启动时处于非活动状态
self.game_active = False
def reset_stats(self):
"""初始化在游戏运行期间可能变化的统计信息"""
self.ships_left = self.settings.ship_limit
self.score = 0
14.3.1 显示得分
# scoreboard.py
import pygame.font
class Scoreboard:
"""显示得分信息的类"""
def __init__(self, ai_game):
"""初始化显示得分涉及的属性"""
self.screen = ai_game.screen
self.screen_rect = self.screen.get_rect()
self.settings = ai_game.settings
self.stats = ai_game.stats
# 显示得分信息时使用的字体设置
self.text_color = (30, 30, 30)
self.font = pygame.font.SysFont(None, 48)
# 初始化得分图像
self.prep_score()
def prep_score(self):
"""将得分转换为一幅渲染的图像"""
score_str = str(self.stats.score)
self.score_image = self.font.render(score_str, True,
self.text_color, self.settings.bg_color)
# 在屏幕右上角显示得分
self.score_rect = self.score_image.get_rect()
# score_rect右边缘与屏幕右边缘始终相距20像素,上边缘与屏幕上边缘相距20像素
self.score_rect.right = self.screen_rect.right - 20
self.score_rect.top = 20
def show_score(self):
"""在屏幕上显示得分"""
self.screen.blit(self.score_image, self.score_rect)
14.3.2 创建记分牌
# 主程序
---snip---
# 创建一个用于存储游戏统计信息的实例
self.stats = GameStats(self)
# 创建记分牌
self.sb = Scoreboard(self)
self.ship = Ship(self)
def _update_screen(self):
"""更新屏幕上的图像,并切换到新屏幕。"""
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.aliens.draw(self.screen)
# 显示得分
self.sb.show_score()
# 如果游戏处于非活动状态,就绘制Play按钮
if not self.stats.game_active:
self.play_button.draw_button()
# 让最近绘制的屏幕可见
pygame.display.flip()
---snip---
运行结果:(右上角显示得分)
14.3.3 在外星人被消灭时更新得分
# Settings.py
def initialize_dynamic_settings(self):
"""初始化随游戏进行而变化的设置"""
self.ship_speed = 1.5
self.bullet_speed = 3.0
self.alien_speed = 1.0
# fleet_direction为1表示向右移,为-1表示向左移
self.fleet_direction = 1
# 记分
self.alien_points = 50
# 主程序
---snip---
def _check_bullet_alien_collisions(self):
"""响应子弹和外星人碰撞"""
# 删除发生碰撞的子弹和外星人
collisions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True)
if collisions:
self.stats.score += self.settings.alien_points
self.sb.prep_score()
if not self.aliens:
# 删除现有的子弹并新建一群外星人
self.bullets.empty()
self._create_fleet()
# 调用increase_speed()加快游戏节奏,加大游戏难度
self.settings.increase_speed()
---snip---
14.3.4 重置得分
# 主程序
---snip---
def _check_play_button(self, mouse_pos):
"""在玩家单击Play按钮时开始新游戏"""
# game_active为False时游戏才开始并显示Play按钮
button_clicked = self.play_button.rect.collidepoint(mouse_pos)
if button_clicked and not self.stats.game_active:
# 重置游戏设置
self.settings.initialize_dynamic_settings()
# 重置游戏统计信息
self.stats.reset_stats()
self.stats.game_active = True
# 重置得分为0
self.sb.prep_score()
# 清空余下的外星人和子弹
self.aliens.empty()
self.bullets.empty()
# 创建一群新的外星人并让飞船居中
self._create_fleet()
self.ship.center_ship()
# 隐藏鼠标光标
pygame.mouse.set_visible(False)
---snip---
14.3.5 将消灭的每个外星人都计入得分
# 主程序
---snip---
def _check_bullet_alien_collisions(self):
"""响应子弹和外星人碰撞"""
# 删除发生碰撞的子弹和外星人
collisions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True)
if collisions:
for aliens in collisions.values():
self.stats.score += self.settings.alien_points * len(aliens)
self.sb.prep_score()
# self.stats.score += self.settings.alien_points
# self.sb.prep_score()
---snip---
14.3.6 提高分数
# Settings.py
# 加快游戏节奏的速度
self.speedup_scale = 1.1
# 外星人分数的提高速度
self.score_scale = 1.5
---snip---
def increase_speed(self):
"""提高速度设置和外星人分数"""
self.ship_speed *= self.speedup_scale
self.bullet_speed *= self.speedup_scale
self.alien_speed *= self.speedup_scale
self.alien_points = int(self.alien_points * self.score_scale)
# 没提高一个等级,在终端窗口看到新的分数值,确认分数不断增加后
# 删除print()函数,否则会影响游戏性能
print(self.alien_points)
14.3.7 舍入得分
# scoreboard.py
---snip---
def prep_score(self):
"""将得分转换为一幅渲染的图像"""
# round()函数让小数精确到小数点后一位,小数位由第二位实参指定
# 如果第二位实参为负数,将舍入到最近的10的整数倍
rounded_score = round(self.stats.score, -1)
# 将数值转换为字符串时在其中插入逗号
score_str = "{:,}".format(rounded_score)
# score_str = str(self.stats.score)
---snip---
运行结果:
14.3.8 最高得分
# game_stats.py
class GameStats:
"""跟踪游戏的统计信息"""
def __init__(self, ai_game):
"""初始化统计信息"""
self.settings = ai_game.settings
self.reset_stats()
# 游戏在刚启动时处于非活动状态
self.game_active = False
# 任何情况下都不应该重置最高得分
self.high_score = 0
---snip---
# scoreboard.py
---snip---
# 初始化得分图像
self.prep_score()
self.prep_high_score()
---snip---
def prep_high_score(self):
"""将最高得分转换为渲染的图像"""
high_score = round(self.stats.high_score, -1)
high_score_str = "{:,}".format(high_score)
self.prep_high_score_image = self.font.render(high_score_str, True, self.text_color, self.settings.bg_color)
# 将最高得分放在屏幕顶端中央
self.high_score_rect = self.prep_high_score_image.get_rect()
self.high_score_rect.centerx = self.screen_rect.centerx
self.high_score_rect.top = self.score_rect.top
def show_score(self):
"""在屏幕上显示得分"""
self.screen.blit(self.score_image, self.score_rect)
self.screen.blit(self.prep_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()
运行结果:
14.3.9 显示等级
# game_stats
---snip---
def reset_stats(self):
"""初始化在游戏运行期间可能变化的统计信息"""
self.ships_left = self.settings.ship_limit
# 设置当前游戏等级
self.level = 1
# scoreboard.py
---snip---
# 初始化得分图像
self.prep_score()
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
def show_score(self):
"""在屏幕上显示得分和等级"""
self.screen.blit(self.score_image, self.score_rect)
self.screen.blit(self.prep_high_score_image, self.high_score_rect)
self.score_rect.blit(self.level_image, self.level_rect)
---snip---
# 主程序
def _check_bullet_alien_collisions(self):
"""响应子弹和外星人碰撞"""
---snip---
if not self.aliens:
# 删除现有的子弹并新建一群外星人
self.bullets.empty()
self._create_fleet()
# 调用increase_speed()加快游戏节奏,加大游戏难度
self.settings.increase_speed()
# 提高等级
self.stats.level += 1
self.sb.prep_level()
def _check_play_button(self, mouse_pos):
"""在玩家单击Play按钮时开始新游戏"""
# game_active为False时游戏才开始并显示Play按钮
button_clicked = self.play_button.rect.collidepoint(mouse_pos)
if button_clicked and not self.stats.game_active:
# 重置游戏设置
self.settings.initialize_dynamic_settings()
# 重置游戏统计信息
self.stats.reset_stats()
self.stats.game_active = True
self.sb.prep_score()
self.sb.prep_level()
---snip---
运行结果:
14.3.10 显示余下的飞船数
# ship.py
import pygame
from pygame.sprite import Sprite
class Ship(Sprite):
"""管理飞船的类"""
def __init__(self, ai_game):
"""初始化飞船并设置其初始位置"""
super().__init__()
---snip---
# scoreboard.py
import pygame.font
from pygame.sprite import Group
from Ship import Ship
class Scoreboard:
"""显示得分信息的类"""
def __init__(self, ai_game):
"""初始化显示得分涉及的属性"""
self.ai_game = ai_game
self.screen = ai_game.screen
self.screen_rect = self.screen.get_rect()
self.settings = ai_game.settings
self.stats = ai_game.stats
# 显示得分信息时使用的字体设置
self.text_color = (30, 30, 30)
self.font = pygame.font.SysFont(None, 48)
# 初始化得分图像
self.prep_score()
self.prep_high_score()
self.prep_level()
self.prep_ships()
---snip---
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)
def show_score(self):
"""在屏幕上绘制得分、等级和余下的飞船数"""
self.screen.blit(self.score_image, self.score_rect)
self.screen.blit(self.prep_high_score_image, self.high_score_rect)
self.screen.blit(self.level_image, self.level_rect)
self.ships.draw(self.screen)
---snip---
# 主程序
---snip---
def _check_play_button(self, mouse_pos):
"""在玩家单击Play按钮时开始新游戏"""
# game_active为False时游戏才开始并显示Play按钮
button_clicked = self.play_button.rect.collidepoint(mouse_pos)
if button_clicked and not self.stats.game_active:
# 重置游戏设置
self.settings.initialize_dynamic_settings()
# 重置游戏统计信息
self.stats.reset_stats()
self.stats.game_active = True
self.sb.prep_score()
self.sb.prep_level()
self.sb.prep_ships()
---snip---
def _ship_hit(self):
"""响应飞船被外星人撞到"""
if self.stats.ships_left > 0:
# 将Ship_left减1并更新记分牌
self.stats.ships_left -= 1
self.sb.prep_ships()
---snip---
运行结果: