为了账号安全,请及时绑定邮箱和手机立即绑定

尝试在此游戏中切换子弹模式

尝试在此游戏中切换子弹模式

小唯快跑啊 2021-09-28 20:46:20
我正在尝试制作一个子弹地狱风格的游戏,我几乎已经可以使用它了,只是对手不会从射击一种子弹模式改变为另一种。应该是发射3发蓝色子弹8次,然后切换到发射2发紫色子弹8次。有一系列模式,但我只有两个。因此,每次当前模式拍摄一定次数时,它都应该遍历每个模式。当所有模式完成后,它应该完全停止拍摄。我见过有人尝试制作这些,但它始终是 Java,而我使用的是 Python。代码很长,但我不能再删减了。原始文件在多个文件中,但我已将其放入一个脚本中。简化几乎是不可能的。我的 GitHub 上的原始代码和资产:https : //github.com/E-Lee-Za/Eleeza-Crafter-The-Game
查看完整描述

1 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

这是您的代码的工作(和简化)版本。loop每次创建子弹时,当前法术的属性都会递减。当loop为 0 时,self.spellno增加并改变法术,否则如果spellno是>= len(self.sequence),则self.currentspell设置为None使其停止射击(只需添加if self.currentspell is not None到条件语句中)。


import pygame



class Spell:


    def __init__(self, bullet, pattern, speed, loop, tick_delay):

        self.bullet = bullet

        self.pattern = pattern

        self.speed = speed

        self.loop = loop

        self.tick_delay = tick_delay



class Opponent(pygame.sprite.Sprite):


    def __init__(self, sprite, sequence, *groups):

        super().__init__(*groups)

        self.image = sprite

        self.rect = self.image.get_rect(topleft=(425, 30))

        self.start_time = pygame.time.get_ticks()

        self.sequence = sequence

        self.spellno = 0

        self.currentspell = sequence[self.spellno]


    def update(self):

        time_gone = pygame.time.get_ticks() - self.start_time

        # Only create bullets if self.currentspell is not None.

        if self.currentspell is not None and time_gone > self.currentspell.tick_delay:

            self.start_time = pygame.time.get_ticks()

            for bullet in self.currentspell.pattern:

                if bullet[0] <= time_gone:

                    Bullet(self.rect.center, bullet[1], self.currentspell.bullet, sprites, bullets)


            # Decrement the loop attribute of the current spell and

            # switch to the next spell when it's <= 0. When all spells

            # are done, set self.currentspell to None to stop shooting.

            self.currentspell.loop -= 1

            if self.currentspell.loop <= 0:

                self.spellno += 1

                if self.spellno >= len(self.sequence):

                    self.currentspell = None

                else:

                    self.currentspell = self.sequence[self.spellno]



class Bullet(pygame.sprite.Sprite):


    def __init__(self, pos, direction, image, *groups):

        super().__init__(*groups)

        self.image = image

        self.rect = self.image.get_rect(topleft=pos)

        self.direction = direction

        self.pos = pygame.Vector2(self.rect.topleft)


    def update(self):

        self.pos += self.direction

        self.rect.topleft = (self.pos.x, self.pos.y)

        if not screen.get_rect().colliderect(self.rect):

            self.kill()



sprites = pygame.sprite.Group()

bullets = pygame.sprite.Group()


opponentgroup = pygame.sprite.Group()

img = pygame.Surface((30, 40))

img.fill((0, 100, 200))

mi1 = Spell(

    img,

    ((0, pygame.Vector2(-0.5, 1) * 4), (0, pygame.Vector2(0, 1) * 4),

     (0, pygame.Vector2(0.5, 1) * 4)),

    10, 8, 340

    )

img2 = pygame.Surface((30, 30))

img2.fill((110, 0, 220))

mi2 = Spell(

    img2,

    ((0, pygame.Vector2(1, 1) * 4), (0, pygame.Vector2(-1, 1) * 4)),

    4, 8, 340

    )


minty_spells = [mi1, mi2]

img3 = pygame.Surface((30, 50))

img3.fill((220, 0, 200))

Minty = Opponent(img3, minty_spells, opponentgroup)

sprites.add(Minty)


pygame.init()

SCREENWIDTH = 1000

SCREENHEIGHT = 650

screen = pygame.display.set_mode([SCREENWIDTH, SCREENHEIGHT])

screen.fill((255, 123, 67))

pygame.draw.rect(screen, (0, 255, 188), (50, 50, 900, 575), 0)

background = screen.copy()

clock = pygame.time.Clock()



def main():

    while True:

        for events in pygame.event.get():

            if events.type == pygame.QUIT:

                pygame.quit()

                return


        sprites.update()


        screen.blit(background, (0, 0))

        sprites.draw(screen)

        pygame.display.update()


        clock.tick(100)


if __name__ == '__main__':

    main()



查看完整回答
反对 回复 2021-09-28
  • 1 回答
  • 0 关注
  • 174 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号