来源:《Python编程:从入门到实践》
game_stats.py
def __init__(self, ai_settings):
"""初始化统计信息"""
self.ai_settings = ai_settings
self.reset_stats()
# 游戏刚启动时处于活动状态
self.game_active = False
def reset_stats(self):
--snip--
button.py
import pygame.font
class Button():
def __init__(self, ai_settings, screen, msg):
"""初始化按钮的属性"""
self.screen = screen
self.screen_rect = screen.get_rect()
# 设置按钮的尺寸和其他属性
self.width, self.height = 200, 50
self.button_color = (0, 255, 0)
self.text_color = (225, 255, 255)
self.font = pygame.font.SysFont(None, 48)
# 创建按钮的rect对象,并使其居中
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = self.screen_rect.center
# 按钮的标签只需创建一次
self.prep_msg(msg)
模块pygame.font,它让Pygame能够将文本渲染到屏幕上
方法__init__()接受参数self,对象ai_settings和screen,以及msg(要在按钮中显示的文本)
self.font = pygame.font.SysFont(None, 48):指定使用什么字体来渲染文本;实参None让Pygame使用默认字体,48指定了文本的字号
Pygame通过将要显示的字符串渲染为图像来处理文本
。调用prep_msg()来处理这样的渲染
button.py
def prep_msg(self, msg):
"""将msg渲染为图像,并使其在按钮上居中"""
self.msg_image = self.font.render(msg, True, self.text_color,
self.button_color)
self.msg_image_rect = self.msg_image.get_rect()
self.msg_image_rect.center = self.rect.center
prep_msg()接受实参self以及要渲染为图像的文本(msg)
调用font.render()将存储在msg中的文本转换为图像,然后将图像存储在msg_image中
方法font.render()还接受一个布尔实参,其指定开启or关闭反锯齿功能(反锯齿让文本的边缘更平滑)
余下的两个实参分别是 文本颜色 & 背景色
button.py
def draw_button(self):
# 绘制一个用颜色填充的按钮,再绘制文本
self.screen.fill(self.button_color, self.rect)
self.screen.blit(self.msg_image, self.msg_image_rect)
screen.fill():绘制表示按钮的矩形
screen.blit():向它传递一幅图像及与其关联的rect对象,从而在屏幕上绘制文本图像
alien_invasion.py
--snip--
from game_stats import GameStats
from button import Button
--snip--
def run_game():
--snip--
pygame.display.set_caption("Alien Invasion")
# 创建Play按钮
play_button = Button(ai_settings, screen, "Play")
--snip--
# 开始游戏的主循环
while True:
--snip--
gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets,
play_button)
run_game()
game_functions.py
def update_screen(ai_settings, screen, stats, ship, aliens, bullets,
play_button):
"""更新屏幕上的图像,并切换到新屏幕"""
--snip--
# 如果游戏处于非活动状态,就绘制Play按钮
if not stats.game_active:
play_button.draw_button()
# 让最近绘制的屏幕可见
pygame.display.flip()
game_functions.py
def check_events(ai_settings, screen, stats, play_button, ship, 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(stats, play_button, mouse_x, mouse_y)
def check_play_button(stats, play_button, mouse_x, mouse_y):
"""单机Play按钮时开始新游戏"""
if play_button.rect.collidepoint(mouse_x, mouse_y):
stats.game_active = True
check_events()中添加了形参stats(来访问标志game_active)和play_button(检查玩家是否单机了Play按钮)
无论单机屏幕什么地方,Pygame都将检测一个MOUSEBUTTONDOWN事件
使用pygame.mouse.get_pos(),它返回一个元组(包含单击时鼠标的x、y坐标)
将这些值传递给check_play_button(),而这个函数使用collidepoint()检查鼠标单机位置是否在Play按钮的rect内
alien_invasion.py
while True:
gf.check_events(ai_settings, screen, stats, play_button, ship,
bullets)
--snip--
game_functions.py
def check_play_button(ai_settings, screen, stats, play_button, ship, aliens,
bullets, mouse_x, mouse_y):
"""单机Play按钮时开始新游戏"""
if play_button.rect.collidepoint(mouse_x, mouse_y):
# 重置游戏统计信息
stats.reset_stats()
stats.game_active = True
# 情况外星人列表和子弹列表
aliens.empty()
bullets.empty()
# 创建一群新的外星人,并让飞船居中
create_fleet(ai_settings, screen, ship, aliens)
ship.center_ship()
game_functions.py
def check_events(ai_settings, screen, stats, play_button, ship, aliens,
bullets):
--snip--
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(ai_settings, screen, stats, play_button, ship,
aliens, bullets, mouse_x, mouse_y)
alien_invasion.py
# 开始游戏的主循环
while True:
gf.check_events(ai_settings, screen, stats, play_button, ship, aliens,
bullets)
--snip--
game_functions.py
def check_play_button(ai_settings, screen, stats, 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--
game_functions.py
def check_play_button(ai_settings, screen, stats, 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:
# 隐藏光标
pygame.mouse.set_visible(False)
--snip--
向set_visible()传递False,让Pygame在光标位于游戏窗口内时将其隐藏
def ship_hit(ai_settings, stats, screen, ship, aliens, bullets):
"""响应被外星人撞到的飞船"""
if stats.ships_left > 0:
--snip--
else:
stats.game_active = False
pygame.mouse.set_visible(True)
下面重新组织Settings类,将游戏设置 划分为静态的和动态的两组
settings.py
class Settings():
"""一个存储《外星人入侵》游戏的所有设置的类"""
def __init__(self):
"""初始化游戏的静态设置"""
# 屏幕设置
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230, 230, 230)
# 飞船的设置
self.ship_limit = 3
# 子弹设置
self.bullet_width = 3
self.bullet_height = 15
self.bullet_color = 60, 60, 60
self.bullets_allowed = 3
# 外星人设置
self.fleet_drop_speed = 10
# 以什么样的速度加快游戏节奏
self.speedup_scale = 1.1
self.initialize_dynamic_settings()
def initialize_dynamic_settings(self):
"""初始化随游戏进行而变化的设置"""
self.ship_speed_factor = 1.5
self.bullet_speed_factor = 2
self.alien_speed_factor = 1
# fleet_direction为1表示向右移,为-1表示向左移
self.fleet_direction = 1
使用increase_speed()来提高飞船、子弹和外星人的速度
:settings.py
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
在check_bullet_alien_collisions()中,我们在整群外星人都被消灭后,调用increase_speed()
game_functions.py
def check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets):
--snip--
if len(aliens) == 0:
# 删除现有的子弹,加快游戏节奏,并新建一群新外星人
bullets.empty()
ai_settings.increase_speed()
create_fleet(ai_settings, screen, ship, aliens)
game_functions.py
def check_play_button(ai_settings, screen, stats, 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:
# 重置游戏设置
ai_settings.initialize_dynamic_settings()
# 隐藏光标
pygame.mouse.set_visible(False)
--snip--