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

我正在尝试制作一个子弹地狱风格的游戏,我几乎已经可以使用它了,只是对手不会从射击一种子弹模式改变为另一种。

应该是发射3发蓝色子弹8次,然后切换到发射2发紫色子弹8次。有一系列模式,但我只有两个。

因此,每次当前模式拍摄一定次数时,它都应该遍历每个模式。当所有模式完成后,它应该完全停止拍摄。

我见过有人尝试制作这些,但它始终是 Java,而我使用的是 Python。

代码很长,但我不能再删减了。原始文件在多个文件中,但我已将其放入一个脚本中。简化几乎是不可能的。

我的 GitHub 上的原始代码和资产:https : //github.com/E-Lee-Za/Eleeza-Crafter-The-Game


小唯快跑啊
浏览 175回答 1
1回答

白衣非少年

这是您的代码的工作(和简化)版本。loop每次创建子弹时,当前法术的属性都会递减。当loop为 0 时,self.spellno增加并改变法术,否则如果spellno是>= len(self.sequence),则self.currentspell设置为None使其停止射击(只需添加if self.currentspell is not None到条件语句中)。import pygameclass Spell:&nbsp; &nbsp; def __init__(self, bullet, pattern, speed, loop, tick_delay):&nbsp; &nbsp; &nbsp; &nbsp; self.bullet = bullet&nbsp; &nbsp; &nbsp; &nbsp; self.pattern = pattern&nbsp; &nbsp; &nbsp; &nbsp; self.speed = speed&nbsp; &nbsp; &nbsp; &nbsp; self.loop = loop&nbsp; &nbsp; &nbsp; &nbsp; self.tick_delay = tick_delayclass Opponent(pygame.sprite.Sprite):&nbsp; &nbsp; def __init__(self, sprite, sequence, *groups):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(*groups)&nbsp; &nbsp; &nbsp; &nbsp; self.image = sprite&nbsp; &nbsp; &nbsp; &nbsp; self.rect = self.image.get_rect(topleft=(425, 30))&nbsp; &nbsp; &nbsp; &nbsp; self.start_time = pygame.time.get_ticks()&nbsp; &nbsp; &nbsp; &nbsp; self.sequence = sequence&nbsp; &nbsp; &nbsp; &nbsp; self.spellno = 0&nbsp; &nbsp; &nbsp; &nbsp; self.currentspell = sequence[self.spellno]&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; time_gone = pygame.time.get_ticks() - self.start_time&nbsp; &nbsp; &nbsp; &nbsp; # Only create bullets if self.currentspell is not None.&nbsp; &nbsp; &nbsp; &nbsp; if self.currentspell is not None and time_gone > self.currentspell.tick_delay:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.start_time = pygame.time.get_ticks()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for bullet in self.currentspell.pattern:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if bullet[0] <= time_gone:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bullet(self.rect.center, bullet[1], self.currentspell.bullet, sprites, bullets)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Decrement the loop attribute of the current spell and&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # switch to the next spell when it's <= 0. When all spells&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # are done, set self.currentspell to None to stop shooting.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.currentspell.loop -= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.currentspell.loop <= 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.spellno += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.spellno >= len(self.sequence):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.currentspell = None&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.currentspell = self.sequence[self.spellno]class Bullet(pygame.sprite.Sprite):&nbsp; &nbsp; def __init__(self, pos, direction, image, *groups):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(*groups)&nbsp; &nbsp; &nbsp; &nbsp; self.image = image&nbsp; &nbsp; &nbsp; &nbsp; self.rect = self.image.get_rect(topleft=pos)&nbsp; &nbsp; &nbsp; &nbsp; self.direction = direction&nbsp; &nbsp; &nbsp; &nbsp; self.pos = pygame.Vector2(self.rect.topleft)&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; self.pos += self.direction&nbsp; &nbsp; &nbsp; &nbsp; self.rect.topleft = (self.pos.x, self.pos.y)&nbsp; &nbsp; &nbsp; &nbsp; if not screen.get_rect().colliderect(self.rect):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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(&nbsp; &nbsp; img,&nbsp; &nbsp; ((0, pygame.Vector2(-0.5, 1) * 4), (0, pygame.Vector2(0, 1) * 4),&nbsp; &nbsp; &nbsp;(0, pygame.Vector2(0.5, 1) * 4)),&nbsp; &nbsp; 10, 8, 340&nbsp; &nbsp; )img2 = pygame.Surface((30, 30))img2.fill((110, 0, 220))mi2 = Spell(&nbsp; &nbsp; img2,&nbsp; &nbsp; ((0, pygame.Vector2(1, 1) * 4), (0, pygame.Vector2(-1, 1) * 4)),&nbsp; &nbsp; 4, 8, 340&nbsp; &nbsp; )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 = 1000SCREENHEIGHT = 650screen = 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():&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; for events in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if events.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.quit()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; sprites.update()&nbsp; &nbsp; &nbsp; &nbsp; screen.blit(background, (0, 0))&nbsp; &nbsp; &nbsp; &nbsp; sprites.draw(screen)&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; &nbsp; &nbsp; clock.tick(100)if __name__ == '__main__':&nbsp; &nbsp; main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python