python及pygame雷霆战机游戏项目实战11 玩家多条命

项目详细介绍

项目详细介绍

在这个系列中,将制作一个雷霆战机游戏。

GIF.gif

玩家爆炸

将使用不同的爆炸动画来制作玩家的死亡。

只需像其他爆炸那样加载那些帧。现在加载代码如下所示:

explosion_anim = {}
explosion_anim['lg'] = []
explosion_anim['sm'] = []
explosion_anim['player'] = []
for i in range(9):
    filename = 'regularExplosion0{}.png'.format(i)
    img = pygame.image.load(path.join(img_dir, filename)).convert()
    img.set_colorkey(BLACK)
    img_lg = pygame.transform.scale(img, (75, 75))
    explosion_anim['lg'].append(img_lg)
    img_sm = pygame.transform.scale(img, (32, 32))
    explosion_anim['sm'].append(img_sm)
    filename = 'sonicExplosion0{}.png'.format(i)
    img = pygame.image.load(path.join(img_dir, filename)).convert()
    img.set_colorkey(BLACK)
    explosion_anim['player'].append(img)

不需要更改Explosionsprite类中的任何内容,因此只需要在玩家的血条耗尽时创建玩家爆炸。可以在检查玩家与流星碰撞的游戏循环中添加:

# check to see if a mob hit the player
hits = pygame.sprite.spritecollide(player, mobs, True, pygame.sprite.collide_circle)
for hit in hits:
    player.shield -= hit.radius * 2
    expl = Explosion(hit.rect.center, 'sm')
    all_sprites.add(expl)
    newmob()
    if player.shield <= 0:
        death_explosion = Explosion(player.rect.center, 'player')
        all_sprites.add(death_explosion)
		running = False

但是,如果你运行程序,你会看到现在有一个问题:当玩家死亡时,设置runningFalse使游戏结束,没有机会看到漂亮爆炸!

要解决这个问题,需要在爆炸完成之前不要结束游戏。所以将删除玩家,但running在爆炸消失之前不会设置为False

    if player.shield <= 0:
        death_explosion = Explosion(player.rect.center, 'player')
        all_sprites.add(death_explosion)
	player.kill()

# if the player died and the explosion has finished playing
if not player.alive() and not death_explosion.alive():
	running = False

alive()函数只返回特定精灵是否存活。调用kill()函数后alive()函数返回False。

多条生命

现在将为玩家提供多条生命。可以使用变量跟踪它,但也希望能够在屏幕上显示它。使用较小的玩家飞船图像来表示剩下多少生命。首先,将创建较小的图像:

player_img = pygame.image.load(path.join(img_dir, "playerShip1_orange.png")).convert()
player_mini_img = pygame.transform.scale(player_img, (25, 19))
player_mini_img.set_colorkey(BLACK)

现在将在Player类中添加一些新参数__init__():生命计数器self.lives,一个标记self.hidden(可以是True或者变量False)隐藏/显示玩家,以及一个控制玩家隐藏时间的计时器self.hide_timer:

self.lives = 3
self.hidden = False
self.hide_timer = pygame.time.get_ticks()

现在,当玩家死亡时不是使用kill(),将隐藏玩家并从中减去1 lives。还为下一次生命重置血条:

if player.shield <= 0:
        death_explosion = Explosion(player.rect.center, 'player')
        all_sprites.add(death_explosion)
        player.hide()
        player.lives -= 1
        player.shield = 100

# if the player died and the explosion has finished playing
if player.lives == 0 and not death_explosion.alive():
    running = False

接下来,需要定义 hide()方法。Player类中将添加一个hide()方法,该方法将hidden标志设置为True并启动计时器。还需要确保在玩家被隐藏时,它不会被流星击中。有几种方法可以做到这一点,一个不需要添加/删除组等的简单方法就是暂时将玩家从屏幕底部移开self.rect.center = (WIDTH / 2, HEIGHT + 200)

def hide(self):
    # hide the player temporarily
    self.hidden = True
    self.hide_timer = pygame.time.get_ticks()
    self.rect.center = (WIDTH / 2, HEIGHT + 200)

在玩家的update()方法中,需要判断是否已经过了足够的时间(现在使用1秒),如果是,取消隐藏:

def update(self):
	# unhide if hidden
	if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000:
	    self.hidden = False
	    self.rect.centerx = WIDTH / 2
	    self.rect.bottom = HEIGHT - 10

生命计数器显示

为了显示生命,将创建一个类似draw_shield_bar()的功能,这将让将生命计数器放在给定的位置:

def draw_lives(surf, x, y, lives, img):
    for i in range(lives):
        img_rect = img.get_rect()
        img_rect.x = x + 30 * i
        img_rect.y = y
        surf.blit(img, img_rect)

然后,在游戏循环的绘图部分添加对此函数的调用:

draw_lives(screen, WIDTH - 100, 5, player.lives,
	   player_mini_img)

现在可以看到最终结果:
image.png

在下一课中,将为游戏添加一些加强功能。

项目源码及资源

获取地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值