pygame飞机大战用精灵组层编写英雄系列(七)英雄战队开组,左右僚机护法

本文介绍了如何在pygame飞机大战游戏中为英雄添加僚机,通过使用精灵组来实现僚机的自动发射子弹功能。作者在升级pygame到1.9.5时遇到提示功能缺失的问题,退回到1.9.4版本后问题解决。僚机的实现结合了lifebar的概念和子弹发射逻辑,使得英雄两侧有了护航的僚机。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题外话,下载了pygame1.9.5,发现自动提示功能没有了,函数的功能提示不全,显示 missingmodule,非常不方便,卸载,下载whl安装也不行。

重新安装了1.9.4,又可以了。

准备给英雄加个僚机,僚机自动发射子弹。

听起来复杂,其实实现起来不难,lifebar的原理加上子弹发射就OK了。

from setting import *
from herobullet import *


class WingPlane(pygame.sprite.Sprite):
    """
        僚机,分为左右两个
         """

    def __init__(self, boss, direction="LEFT"):
        self.groups = allgroup
        self.boss = boss
        self._layer = self.boss._layer
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.direction = direction
        if direction == "LEFT":
            self.image = pygame.image.load('images/hero/leftwingplane.png')
        elif direction == 'RIGHT':
            self.image = pygame.image.load('images/hero/rightwingplane.png')
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)
        self.fire_interval = 300
        self.start_time = pygame.time.get_ticks()

    def update(self):
        if self.direction == "LEFT":
            self.rect.centerx = self.boss.rect.centerx - self.boss.rect.width
        elif self.direction == "RIGHT":
            self.rect.centerx = self.boss.rect.centerx + self.boss.rect.width
        self.rect.centery = self.boss.rect.centery
        self.fire()

    def fire(self, firetype=0):
        current_time = pygame.time.get_ticks()
        pass_time = current_time - self.start_time
        if pass_time < self.fire_interval:
            return
        self.start_time = current_time
        #hero 子弹的发射位置当然是头部中心
        wingplane_fire_pos = vect(self.rect.midtop[0], self.rect.midtop[1])
        #单颗子弹
        if firetype == 0:
            bullet = HeroBullet()
            bullet.set_bullet_type(4)
            pos_x = wingplane_fire_pos.x - bullet.rect.width // 2
            pos_y = wingplane_fire_pos.y - 50
            bullet.set_speed(-4, angle=90)
            bullet.set_pos(pos_x, pos_y)

在heroplane里把左右护法加上。初始化函数里添加


wingplane_left = WingPlane(self,'LEFT')

wingplane_right =WingPlane(self,'RIGHT')

开心上路喽。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值