python - 发射多颗子弹并通过按键改变它们的位置

标签 python pygame

我正在为自己的娱乐创建一个 SHMUP 类型的游戏,但每当我按下一个键时,我都无法将“子弹生成器”位置更改为另一个固定位置,假设我想要一种进攻性的游戏风格,并且防守型,我想要视觉上的差异,决定拉近与玩家的子弹距离,但我不知道如何做,愿意帮助我吗?

现状:

class MainFire(pygame.sprite.Sprite):
    def __init__(self, x, y, filename, posx, posy):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join(img_folder, filename)).convert()
        self.image.set_colorkey(WHITE)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.bottom = y
        self.speedy = - 20
        posx += self.rect.centerx
        posy += self.rect.bottom

    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom < 0:
            self.kill()

class SubFire(pygame.sprite.Sprite):
    def __init__(self, x, y, filename, posx, posy):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join(img_folder, filename)).convert()
        self.image.set_colorkey(WHITE)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.bottom = y
        self.speedy = - 20
        posx += self.rect.centerx
        posy += self.rect.bottom

    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom < 0:
            self.kill()
        elif self.rect.left < -10:
            self.kill()
        elif self.rect.right > GAMEWIDTH:
            self.kill()

我认为,如果我修复 posx 和 posy 它最终会起作用,posx 应该将 x 和整数添加为 posy 与 y (给子弹一个新的位置,速度已经建立,问题是“从哪里开始出现枪而不是子弹本身”)

最佳答案

好吧,第一件事是针对不同的发射角度摆脱这些单独的类。一旦我们知道了不同镜头可能有所不同的所有内容(起始位置、速度、图像),我们就可以为所有这些镜头创建 1 个类,并将这些参数传递到 init() 函数中。我将其称为 Bullet(),所有 Main_fire 和 sub_fire 类都消失了,这应该更容易使用。

def fire(self):
    #this is lv2 and the base needs to be changed, it gains the subfire then another subfire set
    mfl = Bullet(self.rect.centerx, self.rect.top, "Alessa_MF.png",-20,0)
    all_sprites.add(mfl)
    bullets.add(mfl)
    sfl = Bullet(self.rect.centerx, self.rect.top, "Alessa_SF.png",-15,2)
    all_sprites.add(sfl)
    bullets.add(sfl)
    mfr = Bullet(self.rect.centerx, self.rect.top,"Alessa_SF.png",-15,-2)
    all_sprites.add(mfr)
    bullets.add(mfr)
    sfr = Bullet(self.rect.centerx, self.rect.top,"Alessa_SF.png",-10,-2)
    all_sprites.add(sfr)
    bullets.add(sfr)
    #fire_sound.play()

class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y,filename,vx,vy):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(os.path.join(img_folder, filename)).convert()
        self.image.set_colorkey(WHITE)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x-25
        self.speedy = vy
        self.speedx = vx

    def update(self):
        self.rect.y += self.vy
        self.rect.x += self.vx
        if self.rect.bottom < 0:
            self.kill()

关于python - 发射多颗子弹并通过按键改变它们的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56001971/

相关文章:

python - 如何使 html div 元素跟随垂直滚动(在 html/django 中)?

python - 导入错误 : No module named 'django.contrib.gis.django'

python - 平铺 block 在 pygame 平台游戏中渲染不可见

python - Python 中的边缘检测

python - 当尝试在 pygame 中 blit 背景时,我收到此错误消息

python - 如何将 'auto' 设置为上限,但使用 matplotlib.pyplot 保持固定的下限

python - 打开 .ipynb 文件时如何修复 "Distutils was imported before Setuptools"?

python - 使用 QLabel 在 PyQt GUI 中显示 gif

Pygame Sprite 碰撞随机移动

python - 如何使用 PyBox2d 检测碰撞并使用该信息