14-1 按 P 开始新游戏 :鉴于游戏《外星人入侵》使用键盘来控制飞船,最好让玩家也能够通过按键来开始游戏。请添加让玩家在按 P 时开始游戏的代码。也许这样做会有所帮助:将 check_play_button() 的一些代码提取出来,放到一个名为start_game() 的函数中,并在 check_play_button()和 check_keydown_events() 中调用这个函数。
14-2 射击练习 :创建一个矩形,它在屏幕右边缘以固定的速度上下移动。然后,在屏幕左边缘创建一艘飞船,玩家可上下移动该飞船,并射击前述矩形目标。添加一个用于开始游戏的 Play 按钮,在玩家三次未击中目标时结束游戏,并重新显示 Play 按钮,让玩家能够通过单击该按钮来重新开始游戏。
14-3 有一定难度的射击练习 :以你为完成练习 14-2 而做的工作为基础,让标靶的移动速度随游戏进行而加快,并在玩家单击 Play 按钮时将其重置为初始值。
14-4 历史最高分 :每当玩家关闭并重新开始游戏《外星人入侵》时,最高分都将被重置。请修复这个问题,调用 sys.exit() 前将最高分写入文件,并当在 GameStats 中初始化最高分时从文件中读取它。
14-6 扩展游戏《外星人入侵》 :想想如何扩展游戏《外星人入侵》。例如,可让外星人也能够向飞船射击,或者添加盾牌,让飞船躲到它后面,使得只有从两边射来的子弹才能摧毁飞船。另外,还可以使用像 pygame.mixer 这样的模块来添加音效,如爆炸声和射击声。
合并为一个游戏“飞船射击矩形”,效果图如下:
代码:
file.py(存储信息到文件和获取文件的信息)
import pickle
# filename = 'file/stats.pkl'
# 存储信息到文件
def save_file(obj, filename):
statsObj = load_file(filename)
if statsObj == 0:
# 不存在文件时,直接保存字典
with open(filename, 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
else:
# 存在文件时,只修改文件中的最高分
for key, val in statsObj.items():
# 获取文件最高分的值(当文件字段不止一个时候使用)
if key == 'highScore':
statsObj[key] = obj['highScore']
obj = statsObj
with open(filename, 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
# 读取信息
def load_file(filename):
try:
with open(filename, 'rb') as f:
return pickle.load(f)
except FileNotFoundError:
# 不存在文件则输入错误信息
msg = "Sorry, the file " + filename + " does not exist."
print(msg)
return 0
# obj = {'highScore': 20, 'points': 5}
# obj = {'highScore': 50}
# save_file(obj, filename)
# filedata = load_file(filename)
# print(filedata)
settings.py(定义一些必须的基本属性和初始值)。
Tips:注意最高分“high_score”从文件读取,所以关闭程序再打开也能显示以前的最高分。
import file as f
class Settings():
def __init__(self):
self.screen_width = 1000
self.screen_height = 600
self.bg_color = (230, 230, 230)
# 设置矩形尺寸、颜色
self.rect_width = 50
self.rect_height = 60
self.rect_color = (255, 255, 255)
# 子弹设置(宽、高、颜色、最大数量)
self.bullet_width = 15
self.bullet_height = 5
self.bullet_color = 60, 60, 60
# 加快游戏节奏的速度
self.speedup_scale = 1.1
# 分数的提高速度
self.score_scale = 1.5
self.initialize_settings()
# 初始化统计信息
self.reset_stats()
# 统计信息文件路径
self.filename = 'file/stats.pkl'
# 游戏刚启动时处于非活动状态
self.game_active = False
# 读取文件的最高分,在任何情况下都不应重置最高得分
statsObj = f.load_file(self.filename)
if statsObj == 0:
# 不存在文件则显示最高分0
highScore = 0
else:
for key, val in statsObj.items():
# 获取文件最高分的值(当文件字段不止一个时候使用)
if key == 'highScore':
highScore = val
self.high_score = highScore
def initialize_settings(self):
"""初始化随游戏进行而变化的设置"""
self.ship_move_speed = 1.5
self.bullet_speed = 3
self.rectangle_move_speed = 0.5
# 记分
self.one_points = 50
def increase_speed(self):
"""提高速度设置"""
self.ship_move_speed *= self.speedup_scale
self.bullet_speed *= self.speedup_scale
self.rectangle_move_speed *= self.speedup_scale
self.one_points = int(self.one_points * self.score_scale)
def reset_stats(self):
"""初始化在游戏运行期间可能变化的统计信息"""
# 可射失的数量
self.ship_limit = 3
# 射击分数
self.score = 0
# 等级
self.level = 1
# 打中多少矩形升一级
self.level_number = 3
ship.py(创建飞船类,并定义飞船的属性和方法(旋转rotate、缩放scale、更新update(键盘上下键控制上下移动)和绘制draw_ship))。
import pygame
from pygame.sprite import Sprite
class Ship(Sprite):
def __init__(self, settings, screen):
super(Ship, self).__init__()
self.settings = settings
self.screen = screen
self.screen_rect = self.screen.get_rect()
# 引入飞船图片并定位
self.image = pygame.image.load('../images/ship.png')
self.rect = self.image.get_rect()
self.rect.centery = self.screen_rect.centery
self.rect.left = 0
# 在矩形的属性 center 中存储小数值
self.center = float(self.rect.centery)
# 移动标志
self.move_down = False
self.move_up = False
def rotate(self, angle):
# 图片旋转
self.image = pygame.transform.rotate(self.image, angle)
def scale(self, multiple):
# 图片缩放
self.image = pygame.transform.smoothscale(self.image, (multiple, multiple))
def update(self):
if self.move_down and self.rect.bottom < self.screen_rect.bottom:
self.center += self.settings.ship_move_speed
if self.move_up and self.rect.top > 0:
self.center -= self.settings.ship_move_speed
self.rect.centery = self.center
def draw_ship(self):
"""绘制飞船到屏幕"""
self.screen.blit(self.image, self.rect)
bullet.py(创建子弹类,并定义子弹的属性和方法(更新update(按空格键发射子弹由左到右)和绘制draw_bullet))。
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
""" 一个对飞船发射的子弹进行管理的类 """
def __init__(self, settings, screen, ship):
""" 在飞船所处的位置创建一个子弹对象 """
super(Bullet, self).__init__()
self.screen = screen
# 在 (0,0) 处创建一个表示子弹的矩形,再设置正确的位置
self.rect = pygame.Rect(0, 0, settings.bullet_width, settings.bullet_height)
self.rect.centery = ship.rect.centery
# 飞船顶部
self.rect.left = ship.rect.right
# 存储用小数表示的子弹位置
self.x = float(self.rect.x)
self.color = settings.bullet_color
self.speed = settings.bullet_speed
def update(self):
"""向上移动子弹"""
# 更新表示子弹位置的小数值(子弹往右)
self.x += self.speed
# 更新表示子弹的rect的位置
self.rect.x = self.x
def draw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(self.screen, self.color, self.rect)
rectangle.py(创建矩形类,并定义矩形的属性和方法(更新update(自动上下移动)和绘制draw_rect))。
import pygame
from pygame.sprite import Sprite
class Rectangle(Sprite):
def __init__(self, settings, screen):
super(Rectangle, self).__init__()
self.settings = settings
self.screen = screen
self.screen_rect = screen.get_rect()
# 创建rect对象并定位到右边缘顶部
self.rect = pygame.Rect(0, 0, self.settings.rect_width, self.settings.rect_height)
self.rect.y = 0
self.rect.right = self.screen_rect.right
# 在矩形的属性 center 中存储小数值
self.center = float(self.rect.centery)
# 默认先往下移动
self.move_down = True
def update(self):
if self.move_down:
self.center += self.settings.rectangle_move_speed
# 到底就往上移动
if self.rect.bottom >= self.screen_rect.bottom:
self.move_down = False
else:
self.center -= self.settings.rectangle_move_speed
# 到顶就往下移动
if self.rect.top <= 0:
self.move_down = True
self.rect.centery = self.center
def draw_rect(self):
pygame.draw.rect(self.screen, self.settings.rect_color, self.rect)
button.py(创建按钮类,并定义按钮的属性和方法(渲染prep_msg(把字符串渲染成图片)和绘制draw_button))。
import pygame.font
class Button():
def __init__(self, screen, msg):
"""初始化按钮的属性"""
self.screen = screen
self.screen_rect = screen.get_rect()
# 设置按钮的尺寸和其他属性
self.width, self.height = 200, 50
self.button_color = (216, 30, 6)
self.text_color = (255, 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)
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
def draw_button(self):
# 绘制一个用颜色填充的按钮,再绘制文本
self.screen.fill(self.button_color, self.rect)
self.screen.blit(self.msg_image, self.msg_image_rect)
scoreboard.py(创建记分牌类,并定义记分牌的属性和方法(渲染prep_score(把分数渲染成图片)、prep_high_score(把最高分渲染成图片)、prep_level(把等级渲染成图片)、prep_ships(把生命数(飞船)渲染成图片)和显示统计信息show_score))。
import pygame.font
from pygame.sprite import Group
from ship import Ship
class Scoreboard():
"""显示得分信息的类"""
def __init__(self, settings, screen):
"""初始化显示得分涉及的属性"""
self.screen = screen
self.screen_rect = screen.get_rect()
self.settings = settings
# 显示得分信息时使用的字体设置
self.text_color = (30, 30, 30)
self.font = pygame.font.SysFont(None, 30)
# 飞船缩放值
self.scaleValue = 20
# 准备初始得分图像\最高得分\等级
self.prep_score()
self.prep_high_score()
self.prep_level()
self.prep_ships()
def prep_score(self):
"""将得分转换为渲染的图像"""
rounded_score = int(round(self.settings.score, -1))
score_str = '{:,}'.format(rounded_score)
self.score_image = self.font.render(score_str, True, self.text_color)
# 将得分放在屏幕右上角
self.score_rect = self.score_image.get_rect()
self.score_rect.right = self.screen_rect.right -20
self.score_rect.top = 10
def prep_high_score(self):
""" 将最高得分转换为渲染的图像 """
high_score = int(round(self.settings.high_score, -1))
high_score_str = "{:,}".format(high_score)
self.high_score_image = self.font.render(high_score_str, True, self.text_color)
# 将最高得分放在屏幕顶部中央
self.high_score_rect = self.high_score_image.get_rect()
self.high_score_rect.centerx = self.screen_rect.centerx
self.high_score_rect.top = self.score_rect.top
def prep_level(self):
"""将等级转换为渲染的图像"""
self.level_image = self.font.render(str(self.settings.level), True, self.text_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 prep_ships(self):
""" 显示还余下多少艘飞船 """
self.ships = Group()
for ship_number in range(self.settings.ship_limit):
ship = Ship(self.settings, self.screen)
# 缩放球大小并赋值位置
ship.scale(self.scaleValue)
ship.rect.x = 10 + ship.rect.width * ship_number * 0.5
ship.rect.y = self.score_rect.top
self.ships.add(ship)
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)
# 绘制飞船
self.ships.draw(self.screen)
game_functions.py(放置跟业务逻辑有关的函数)
import sys
import pygame
from rectangle import Rectangle
from bullet import Bullet
import file as f
# 事件
def check_events(settings, screen, ship, play_button, scoreboard, rects, bullets, fireSound):
""" 响应按键和鼠标事件 """
for event in pygame.event.get():
if event.type == pygame.QUIT:
save_file(settings)
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, settings, screen, ship, scoreboard, rects, bullets, fireSound)
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
check_play_button(settings, play_button, scoreboard, rects, bullets, mouse_x, mouse_y)
def check_keydown_events(event, settings, screen, ship, scoreboard, rects, bullets, fireSound):
""" 响应按键 """
if event.key == pygame.K_DOWN:
ship.move_down = True
elif event.key == pygame.K_UP:
ship.move_up = True
elif event.key == pygame.K_SPACE:
fireSound.play()
# 点击空格键创建一颗子弹
fire_bullet(settings, screen, ship, bullets)
elif event.key == pygame.K_p:
start_game(settings, scoreboard, rects, bullets)
elif event.key == pygame.K_q:
save_file(settings)
sys.exit()
def check_keyup_events(event, ship):
""" 响应松开 """
if event.key == pygame.K_DOWN:
ship.move_down = False
elif event.key == pygame.K_UP:
ship.move_up = False
def check_play_button(settings, play_button, scoreboard, rects, bullets, mouse_x, mouse_y):
"""在玩家单击Play按钮时开始新游戏"""
button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
if button_clicked and not settings.game_active:
start_game(settings, scoreboard, rects, bullets)
def start_game(settings, scoreboard, rects, bullets):
"""开始游戏"""
# 重置游戏设置
settings.initialize_settings()
# 隐藏光标
pygame.mouse.set_visible(False)
# 重置游戏统计信息
settings.reset_stats()
settings.game_active = True
# 重置记分牌图像
scoreboard.prep_score()
scoreboard.prep_high_score()
scoreboard.prep_level()
scoreboard.prep_ships()
# 清空矩形列表和子弹列表
rects.empty()
bullets.empty()
# 文件
def save_file(settings):
obj = {'highScore': settings.high_score}
f.save_file(obj, settings.filename)
# 矩形
def create_rect(settings, screen, rects):
"""当没有矩形才能创建一个矩形"""
if len(rects) == 0:
new_rect = Rectangle(settings, screen)
rects.add(new_rect)
# 子弹
def fire_bullet(settings, screen, ship, bullets):
"""当没有子弹才能创建一颗子弹"""
if len(bullets) == 0:
new_bullet = Bullet(settings, screen, ship)
bullets.add(new_bullet)
def update_bullets(settings, screen, scoreboard, rects, bullets, explosiveSound):
"""更新子弹的位置,并删除已消失的子弹"""
# 更新子弹的位置
bullets.update()
# 删除已消失的子弹并同时更新飞船的生命
for bullet in bullets.copy():
if bullet.rect.right > screen.get_rect().right:
bullets.remove(bullet)
settings.ship_limit -= 1
scoreboard.prep_ships()
# 当三条命都没了,游戏结束
if settings.ship_limit == 0:
settings.game_active = False
# 子弹打到矩形
check_bullet_rect_collisions(settings, scoreboard, rects, bullets, explosiveSound)
def check_bullet_rect_collisions(settings, scoreboard, rects, bullets, explosiveSound):
# 检查是否有子弹击中了矩形
# 如果是这样,就删除相应的子弹和矩形(两个True分别删除子弹和矩形,不删除就改为False)
collisions = pygame.sprite.groupcollide(bullets, rects, True, True)
if collisions:
# pygame.mixer.music.play()
explosiveSound.play()
# 计算打击到多少矩形(这里一直只有一个),并计算分数并渲染
for rects in collisions.values():
settings.score += settings.one_points * len(rects)
scoreboard.prep_score()
# 渲染最高分
check_high_score(settings, scoreboard)
# 等达到等级数量升级并渲染新等级
settings.level_number -= 1
if settings.level_number == 0:
settings.increase_speed()
settings.level += 1
scoreboard.prep_level()
# 还原为3(同settings一致)
settings.level_number = 3
# 分数
def check_high_score(settings, scoreboard):
"""检查是否诞生了新的最高得分"""
if settings.score > settings.high_score:
settings.high_score = settings.score
scoreboard.prep_high_score()
# 屏幕
def update_screen(settings, screen, ship, play_button, scoreboard, rects, bullets):
""" 更新屏幕上的图像,并切换到新屏幕 """
# 每次循环时都重绘屏幕
screen.fill(settings.bg_color)
# 绘制飞船到屏幕
ship.draw_ship()
# 绘制矩形到屏幕
create_rect(settings, screen, rects)
for rect in rects.sprites():
rect.draw_rect()
# 绘制子弹到屏幕
for bullet in bullets.sprites():
bullet.draw_bullet()
# 渲染记分牌信息
scoreboard.show_score()
# 如果游戏处于非活动状态,就绘制 Play 按钮
if not settings.game_active:
play_button.draw_button()
# 让最近绘制的屏幕可见
pygame.display.flip()
shootingrectangle.py(程序主函数)
Tips:由于“pygame.mixer.music”一次只能播放一个音乐,为了满足播放背景音乐同时播放音效,需要混合使用“pygame.mixer.Sound”(Sound不能使用mp3)。
import pygame
from pygame.sprite import Group
from settings import Settings
from button import Button
from ship import Ship
from bullet import Bullet
import game_functions as gf
from scoreboard import Scoreboard
def run_game():
pygame.init()
# 初始化全部音频,并加载爆炸声音乐
pygame.mixer.init()
# 等待1s
pygame.time.delay(1000)
pygame.mixer.music.load('file/bgsound.mp3')
# -1代表无限循环(背景音乐)
pygame.mixer.music.play(-1)
# 爆炸声
explosiveSound = pygame.mixer.Sound('file/explosiveSound.wav')
# 枪声
fireSound = pygame.mixer.Sound('file/fireSound.wav')
settings = Settings()
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
# 全屏显示
# screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption('射击矩形')
# 创建Play按钮
play_button = Button(screen, 'Play')
# 创建飞船、矩形、子弹
ship = Ship(settings, screen)
ship.rotate(-90)
# 创建子弹的编组
bullets = Group()
rects = Group()
# 创建记分牌
scoreboard = Scoreboard(settings, screen)
while True:
# 检查玩家输入(不加会导致一直加载)
gf.check_events(settings, screen, ship, play_button, scoreboard, rects, bullets, fireSound)
if settings.game_active:
# 更新飞船位置
ship.update()
# 更新矩形位置
rects.update()
# 更新子弹位置
gf.update_bullets(settings, screen, scoreboard, rects, bullets, explosiveSound)
# 更新屏幕信息
gf.update_screen(settings, screen, ship, play_button, scoreboard, rects, bullets)
run_game()