在《Pygame中Sprite实现逃亡游戏3》中实现了玩家跳跃飞火的效果,接下来通过精灵类的碰撞检测来判断飞火是否击中玩家、飞火是否击中飞龙以及飞龙是否抓住玩家。
1 飞火是否击中玩家的判断
判断飞火是否击中玩家的代码如图1所示。
图1 判断飞火是否击中玩家的代码
第137行代码使用精灵类的collide_rect()方法判断飞火是否击中玩家,该方法的参数arrow表示飞火,参数player表示玩家,当飞火击中玩家时,collide_rect()方法返回True,第138代码在屏幕的右侧重置飞火,第139行代码将玩家的横坐标向左移动(玩家被飞火击中后要退后)。
相关链接1 reset_arrow()方法以及player.X属性请参考《Pygame中Sprite实现逃亡游戏2》
注意1 判断飞火是否击中玩家、飞火是否击中飞龙以及飞龙是否抓住玩家的代码均在while True循环中。
2 飞火是否击中飞龙的判断
判断飞火是否击中飞龙的代码如图2所示。
图2 判断飞火是否击中飞龙的代码
从图2中可知,当飞火是否击中飞龙后,需要重置飞火并且将飞龙的横坐标向左移动。
3 飞龙是否抓住玩家
判断飞龙是否抓住玩家的代码如图3所示。
图3 判断飞龙是否抓住玩家的代码
从图3中可以看出,当飞龙抓住玩家,此时游戏结束,game_over标志设置为True。
4 程序运行效果
程序运行效果如图4所示。
图4 程序运行效果
5 完整代码
import pygame
import os
import time
import random
from pygame.locals import *
class MySprite(pygame.sprite.Sprite):
def __init__(self, target):
pygame.sprite.Sprite.__init__(self) #extend the base Sprite class
self.master_image = None
self.frame = 0
self.old_frame = -1
self.frame_width = 1
self.frame_height = 1
self.first_frame = 0
self.last_frame = 0
self.columns = 1
self.last_time = 0
#X property
def _getx(self):
return self.rect.x
def _setx(self,value):
self.rect.x = value
X = property(_getx,_setx)
#Y property
def _gety(self):
return self.rect.y
def _sety(self,value):
self.rect.y = value
Y = property(_gety,_sety)
#position property
def _getpos(self):
return self.rect.topleft
def _setpos(self,pos):
self.rect.topleft = pos
position = property(_getpos,_setpos)
def load(self, filename, width, height, columns):
self.master_image = pygame.image.load(filename).convert_alpha()
self.frame_width = width
self.frame_height = height
self.rect = Rect(0,0,width,height)
self.columns = columns
#try to auto-calculate total frames
rect = self.master_image.get_rect()
self.last_frame = (rect.width // width) * (rect.height // height) - 1
def update(self, current_time, rate=30):
#update animation frame number
if current_time > self.last_time + rate:
self.frame += 1
if self.frame > self.last_frame:
self.frame = self.first_frame
self.last_time = current_time
#build current frame only if it changed
if self.frame != self.old_frame:
frame_x = (self.frame % self.columns) * self.frame_width
frame_y = (self.frame // self.columns) * self.frame_height
rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height)
self.image = self.master_image.subsurface(rect)
self.old_frame = self.frame
def __str__(self):
return str(self.frame) + "," + str(self.first_frame) + \
"," + str(self.last_frame) + "," + str(self.frame_width) + \
"," + str(self.frame_height) + "," + str(self.columns) + \
"," + str(self.rect)
def reset_arrow():
y = random.randint(250,350)
arrow.position = 800,y
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('Escape The Dragon Game')
font = pygame.font.Font('1.ttf', 18)
framerate = pygame.time.Clock()
group = pygame.sprite.Group()
player = MySprite(screen)
player.load('caveman.png', 50, 64, 8)
player.first_frame = 1
player.last_frame = 7
player.position = 400, 303
group.add(player)
bg = pygame.image.load('background.png').convert_alpha()
#create the dragon sprite
dragon = MySprite(screen)
dragon.load("dragon.png", 260, 150, 3)
dragon.position = 100, 230
group.add(dragon)
#create the arrow sprite
arrow = MySprite(screen)
arrow.load("flame.png", 40, 16, 1)
arrow.position = 800,320
group.add(arrow)
game_over = False
arrow_vel = 8.0
player_jumping = False
jump_vel = 0.0
player_start_y = player.Y
while True:
framerate.tick(30)
ticks = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
os.sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
pygame.quit()
os.sys.exit()
elif keys[K_SPACE]:
if not player_jumping:
player_jumping = True
jump_vel = -8.0
#update the arrow
if not game_over:
arrow.X -= arrow_vel
if arrow.X < 0:
reset_arrow()
if player_jumping:
player.Y += jump_vel
jump_vel += 0.5
if player.Y > player_start_y:
player.Y = player_start_y
player_jumping = False
jump_vel = 0.0
if pygame.sprite.collide_rect(arrow, player):
reset_arrow()
player.X -= 10
if pygame.sprite.collide_rect(arrow, dragon):
reset_arrow()
dragon.X -= 10
if pygame.sprite.collide_rect(player, dragon):
game_over = True
screen.blit(bg, (0,0))
group.update(ticks, 50)
group.draw(screen)
pygame.display.update()