Pygame - Python 游戏编程入门 class4-加入玩家飞机坠毁并产生动画
前言
今天我们继续完成前面的任务,不知道大家有木有发现之前的飞机撞到敌人是不会爆炸的,这很不符合规律,今天我们加入这个小功能,玩家飞机坠毁并产生动画。(°∀°)ノ
正片开始~
## 1. 判断飞机是否坠毁
关于碰撞检测,我们在上一节的内容中就作了简单介绍了,这一节中我们使用一个新函数,用于判断玩家是否被敌机击中:
pygame.sprite.spritecollide()——检测sprite与group之间的碰撞
spritecollide(sprite, group, dokill, collided = None) -> Sprite_list
参数跟上一节中参数说明相同,函数返回group中与sprite发生碰撞的所有精灵,以列表(list)形式
我们现在加入几代码
enemy1_down_list = pygame.sprite.spritecollide(hero, enemy1_group, True)
if len(enemy1_down_list) > 0: # 不空
enemy1_down_group.add(enemy1_down_list)
hero.is_hit = True
如果玩家被击中(即enemy1_down_list不空),就将击中玩家的敌机加入到enemy1_down_group中,将渲染enemy1坠毁动画的过程交给该group;最后将hero的is_hit属性更改为True(Hero类新加入的属性,便于进行后续的逻辑判断)。
## 2. 制造玩家飞机坠毁动画
当然玩家被击中了也是不可能凭空消失的,我们将以前在主循环中制造玩家飞机动画的代码更新一下,即将:
hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]
改为:
if hero.is_hit:
if ticks%(ANIMATE_CYCLE//2) == 0:
hero_down_index += 1
hero.image = hero_surface[hero_down_index]
if hero_down_index == 5:
break
else:
hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]
如果玩家没有被击中,则按以前普通的玩家图片制造动画;若玩家被击中,则按每15tick换一次图片的频率制造爆炸动画,当图片索引增加至上限时(爆炸动画显示完成),跳出主循环,即结束游戏。
3. 完整的结束游戏
如果你只完成了上面的代码,你会发现,玩家被击中了之后,飞机爆炸,之后画面静止,然后所有动作都进行不了,包括右上角红叉。这是怎么回事?(╯°口°)╯(┴—┴ 认真思考过后你会发现,你此时已经跳出了主循环,而捕捉击键事件和点击事件的功能却写在主循环中~就是这样啦~
无奈之下只能再写一段捕捉事件的代码(#-_-)┯━┯
# 跳出主循环
screen.blit(gameover, (0, 0))
# 玩家坠毁后退出游戏
while True:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
跳出主循环后,我们重写绘制一个“gameover”的背景,用户通过右上角红叉结束游戏~
这样就算是一个完整的游戏了吧(°∀°)ノ
附上此节的完整代码:
# -*- coding = utf-8 -*-
"""
@author: Will Wu
增加功能:
1. 玩家碰到敌人会坠毁
2. 游戏结束
"""
import pygame # 导入pygame库
from pygame.locals import * # 导入pygame库中的一些常量
from sys import exit # 导入sys库中的exit函数
from random import randint
# 定义窗口的分辨率
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640
# 子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self, bullet_surface, bullet_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_surface
self.rect = self.image.get_rect()
self.rect.topleft = bullet_init_pos
self.speed = 8
# 控制子弹移动
def update(self):
self.rect.top -= self.speed
if self.rect.bottom < 0:
self.kill()
# 玩家类
class Hero(pygame.sprite.Sprite):
def __init__(self, hero_surface, hero_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = hero_surface
self.rect = self.image.get_rect()
self.rect.topleft = hero_init_pos
self.speed = 6
self.is_hit = False # new
# 子弹1的Group
self.bullets1 = pygame.sprite.Group()
# 控制射击行为
def single_shoot(self, bullet1_surface):
bullet1 = Bullet(bullet1_surface, self.rect.midtop)
self.bullets1.add(bullet1)
# 控制飞机移动
def move(self, offset):
x = self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]
if x < 0:
self.rect.left = 0
elif x > SCREEN_WIDTH - self.rect.width:
self.rect.left = SCREEN_WIDTH - self.rect.width
else:
self.rect.left = x
if y < 0:
self.rect.top = 0
elif y > SCREEN_HEIGHT - self.rect.height:
self.rect.top = SCREEN_HEIGHT - self.rect.height
else:
self.rect.top = y
# 敌人类
class Enemy(pygame.sprite.Sprite):
def __init__(self, enemy_surface, enemy_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_surface
self.rect = self.image.get_rect()
self.rect.topleft = enemy_init_pos
self.speed = 2
# 爆炸动画画面索引
self.down_index = 0
def update(self):
self.rect.top += self.speed
if self.rect.top > SCREEN_HEIGHT:
self.kill()
###########################################################################
# 定义画面帧率
FRAME_RATE = 60
# 定义动画周期(帧数)
ANIMATE_CYCLE = 30
ticks = 0
clock = pygame.time.Clock()
offset = {pygame.K_LEFT:0, pygame.K_RIGHT:0, pygame.K_UP:0, pygame.K_DOWN:0}
# 玩家坠毁图片索引
hero_down_index = 1 # new
# 初始化游戏
pygame.init() # 初始化pygame
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) # 初始化窗口
pygame.display.set_caption('This is my first pygame-program') # 设置窗口标题
# 载入背景图
background = pygame.image.load('resources/image/background.png')
# 游戏结束图
gameover = pygame.image.load('resources/image/gameover.png') # new
# 载入资源图片
shoot_img = pygame.image.load('resources/image/shoot.png')
# 用subsurface剪切读入的图片
# Hero图片
hero_surface = []
hero_surface.append(shoot_img.subsurface(pygame.Rect(0, 99, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 360, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 234, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 624, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 498, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(432, 624, 102, 126)))
hero_pos = [200, 500]
# bullet1图片
bullet1_surface = shoot_img.subsurface(pygame.Rect(1004, 987, 9, 21))
# enemy1图片
enemy1_surface = shoot_img.subsurface(pygame.Rect(534, 612, 57, 43))
enemy1_down_surface = []
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 347, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(873, 697, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 296, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(930, 697, 57, 43)))
# 创建玩家
hero = Hero(hero_surface[0], hero_pos)
# 创建敌人组
enemy1_group = pygame.sprite.Group()
# 创建击毁敌人组
enemy1_down_group = pygame.sprite.Group()
# 事件循环(main loop)
while True:
# 控制游戏最大帧率
clock.tick(FRAME_RATE)
# 绘制背景
screen.blit(background, (0, 0))
# 改变飞机图片制造动画
if ticks >= ANIMATE_CYCLE:
ticks = 0
# 制造飞机动画 ######################################
# 更新的代码段
if hero.is_hit:
if ticks%(ANIMATE_CYCLE//2) == 0:
hero_down_index += 1
hero.image = hero_surface[hero_down_index]
if hero_down_index == 5:
break
else:
hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]
# #################################################
# 射击
if ticks % 10 == 0:
hero.single_shoot(bullet1_surface)
# 控制子弹
hero.bullets1.update()
# 绘制子弹
hero.bullets1.draw(screen)
# 产生敌机
if ticks % 30 == 0:
enemy = Enemy(enemy1_surface, [randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])
enemy1_group.add(enemy)
# 控制敌机
enemy1_group.update()
# 绘制敌机
enemy1_group.draw(screen)
# 检测敌机与子弹的碰撞
enemy1_down_group.add(pygame.sprite.groupcollide(enemy1_group, hero.bullets1, True, True))
for enemy1_down in enemy1_down_group:
screen.blit(enemy1_down_surface[enemy1_down.down_index], enemy1_down.rect)
if ticks % (ANIMATE_CYCLE//2) == 0:
if enemy1_down.down_index < 3:
enemy1_down.down_index += 1
else:
enemy1_down_group.remove(enemy1_down)
# 检测敌机与玩家的碰撞 #################################
# 更新的代码段
enemy1_down_list = pygame.sprite.spritecollide(hero, enemy1_group, True)
if len(enemy1_down_list) > 0: # 不空
enemy1_down_group.add(enemy1_down_list)
hero.is_hit = True
# ###################################################
# 绘制飞机
screen.blit(hero.image, hero.rect)
ticks += 1 # python已略去自增运算符
# 更新屏幕
pygame.display.update()
# 处理游戏退出
# 从消息队列中循环取
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# ※ Python中没有switch-case 多用字典类型替代
# 控制方向
if event.type == pygame.KEYDOWN:
if event.key in offset:
offset[event.key] = hero.speed
elif event.type == pygame.KEYUP:
if event.key in offset:
offset[event.key] = 0
# 移动飞机
hero.move(offset)
# 更新的代码段 ###############################
# 跳出主循环
screen.blit(gameover, (0, 0))
# 玩家坠毁后退出游戏
while True:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
############################################
本作品采用《CC 协议》,转载必须注明作者和本文链接