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