Python 外星人入侵游戏(三):记分(上)

来源:《Python编程:从入门到实践》

文章目录

  • 1 添加Play按钮
    • 1.1 创建Button类
    • 1.2 在屏幕上绘制按钮
    • 1.3 开始游戏
    • 1.4 重置游戏
    • 1.5 将Play按钮切换到非活动状态
    • 1.6 隐藏光标
  • 2 提高等级
    • 2.1 修改速度设置
    • 2.2 重置速度

1 添加Play按钮

  • 添加一个Play按钮,它在游戏开始前出现,并在游戏结束后再次出现,让玩家能够开始新游戏
  • 当前,玩家在运行alien_invasion.py时就开始了
  • 下面让游戏一开始处于非活跃状态,并提示玩家单机Play按钮来开始游戏

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--
  • 现在游戏一开始将处于非活动状态,后面创建了Play按钮后,玩家才能开始游戏

1.1 创建Button类

  • Pygame没有内置创建按钮的方法,下面创建一个Button类,用于创建带标签的实心矩形

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关闭反锯齿功能(反锯齿让文本的边缘更平滑)
    • 余下的两个实参分别是 文本颜色 & 背景色
  • 最后创建方法draw_button(),通过它将按钮显示到屏幕上:

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对象,从而在屏幕上绘制文本图像
  • 至此,Button类创建ok

1.2 在屏幕上绘制按钮

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()
  • 导入Button类,创建了一个名为play_button的实例,然后将play_button传递给update_screen(),以便能够在屏幕更新时显示按钮
  • 下面修改update_screen(),以便在游戏非活动状态时显示Play按钮:

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()
  • 为让Play按钮位于其他所有屏幕元素上面,我们在绘制其他所有元素后再绘制这个按钮,然后切换到新屏幕

Python 外星人入侵游戏(三):记分(上)_第1张图片

1.3 开始游戏

  • 添加监视这个按钮的鼠标事件:

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内
  • 如果是这样的,我们将game_active设置为True,让游戏就此开始
  • 别忘了更新alien_invasion.py的check_events(),需要传递另外两个实参——stats和play_button:

alien_invasion.py

	while True:
        gf.check_events(ai_settings, screen, stats, play_button, ship, 
            bullets)
        --snip--
  • 现在可以开始这个游戏了,游戏结束时,重新显示Play按钮
    Python 外星人入侵游戏(三):记分(上)_第2张图片

1.4 重置游戏

  • 前面只处理了第一次单击Play按钮的情况,没有处理游戏结束的情况,因为没有重置导致游戏结束的条件
  • 为在玩家每次单机Play按钮时都重置游戏,需要重置统计信息、删除现有的外星人和子弹、创建一群新的外星人,并让飞船居中:

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()
  • 同时更新check_events()

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中调用check_events()的代码

alien_invasion.py

    # 开始游戏的主循环
    while True:
        gf.check_events(ai_settings, screen, stats, play_button, ship, aliens,
        	bullets)
        --snip--
  • 现在运行游戏,每当单机Play按钮,游戏将正确地重置

1.5 将Play按钮切换到非活动状态

  • 做完上一步的朋友们,应该都发现一个问题,就是即便Play按钮不见了,单击其原来所在的区域时,游戏依然会作出响应
  • 下面来修复这个bug:

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--
  • 标志button_clicked的值为True/False,仅当玩家单击了Play按钮且游戏处于非活动状态时,游戏才重新开始
  • 现在运行游戏,这个问题应该已经解决了

1.6 隐藏光标

  • 游戏开始后,光标也就没有什么L用了,只会添乱
  • 下面让游戏在活动状态时让光标不可见:

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在光标位于游戏窗口内时将其隐藏
  • 游戏结束后,重新显示光标,让玩家能够单机Play按钮来开始新游戏
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)

2 提高等级

  • 将整群外星人都消灭干净后,玩家将提高一个等级,但游戏的难度并没有变
  • 下面来做一些改变:玩家消灭掉屏幕的外星人群后,加快游戏的节奏,不断让游戏更难

2.1 修改速度设置

  • 下面重新组织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)

2.2 重置速度

  • 每当开始新游戏时,为了防止速度是前一次游戏增加了的值,需要将发生变化的设置重置为初始值:

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--
  • 现在,《外星人入侵》 has bocome more interesting and more challenging~
  • Python 外星人入侵游戏(三):记分(下)

你可能感兴趣的:(Python学习)