在《Pygame中Sprite实现逃亡游戏2》中实现了飞龙和飞火的显示。接下来实现玩家跳跃效果,如图1所示。
图1 玩家跳跃效果
1 按键响应
当玩家按下键盘中空格按键后,游戏中的玩家就会跳跃,用于躲避飞火。在while True循环判断按键的代码中,通过如图2所示的代码实现对空格按键的响应。
图2 按键响应
其中,第120行中的keys是通过pygame.key.get_pressed()获取到的按键信息,如果keys[K_SPACE]的值是True,则表示键盘中的空格按键被按下;第121行中的player_jumping是玩家是否处于跳跃状态的标志,该标志初始值是False,对player_jumping进行判断,如果该值是False,则说明玩家处于奔跑状态,而并没有处于跳跃状态,接下来的代码将玩家设置为跳远状态,而如果player_jumping的值是True,则说明此时玩家已经在跳跃状态,如果此时再对玩家进行处理,那么玩家就会在空中实现再次跳跃。如果玩家处于奔跑状态时按下了空格按键,接下来第122行代码将player_jumping的值设置为True,即跳跃状态,之后第123行将跳跃速度jump_vel的值设置为-8.0,因为玩家向上跳跃时,其纵坐标是变小的。
2 玩家跳跃效果的实现
在修改了跳跃速度之后,继续在while True循环中修改玩家的纵坐标,实现跳跃效果,如图3所示。
图3 实现玩家跳跃效果的代码
第129行代码对玩家的状态进行判断,只有在处于跳跃状态时,才实现跳跃效果;第130行代码修改玩家的纵坐标player.Y,Y是精灵类MySprite的属性,在图4中显示的代码中定义。
图4 精灵类MySprite的属性
修改player.Y的值,实际上通过精灵类的_sety()方法修改了rect.y的值,也就是显示帧图像起始位置的纵坐标。图3中第131行代码改变jump_vel的值,也就是跳跃的高度,跳跃的高度应该是随着时间推移而减小的(因为jump_vel的值是-8.0),直到jump_vel的值变为0,则玩家达到跳跃的最高位置,接下来jump_vel变为正数,随着时间推移逐渐变大,玩家开始下落;第132-135行代码表示当玩家下落到地面(玩家初始位置)之下时,将玩家的纵坐标设置为地面player_start_y(保证玩家下落后在地面上),之后将玩家的状态设置为非跳跃即奔跑状态,最后跳跃速度jump_vel的值恢复为0。变量player_start_y在while True循环之前设置为玩家初始状态的纵坐标player.Y。
3 完整代码
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
screen.blit(bg, (0,0))
group.update(ticks, 50)
group.draw(screen)
pygame.display.update()