发射的第一枪与第二枪叠加或在第二枪后消失等

First shot fired is superimposed by second shot or simply disappears after second shot and etc

我正在尝试解决如果我在短时间内发射第二颗子弹时触发的子弹消失的原因。我想 screen.fill(GRAY) 可能是一个原因,它应该在 Class Bullet() 内,或者我可能需要在其他地方使用 all_sprites_list.update()

这是我的 MWE 代码:

import pygame
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
GRAY = (128,128,128)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Cowboy(pygame.sprite.Sprite):
      def __init__(self):
          super().__init__()

          self.image = pygame.Surface([14,20])
          self.image.fill(BLUE)
          self.rect = self.image.get_rect()
class Bullet(pygame.sprite.Sprite):
      def __init__(self):
          super().__init__()
          self.image = pygame.image.load("bullet.png").convert()
          self.rect = self.image.get_rect()
      def update(self):
         self.rect.y += -3
def main():
    pygame.init()
    size = [SCREEN_WIDTH, SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size, pygame.RESIZABLE) 
    all_sprites_list = pygame.sprite.Group()
    bullet_list = pygame.sprite.Group() 
    enemies_list = pygame.sprite.Group()
    bullet = Bullet()
    enemy = Cowboy()
    enemies_list.add(enemy)
    all_sprites_list.add(enemy)
    enemy.rect.y = 50
    enemy.rect.x = 50
    done = False
    clock = pygame.time.Clock()
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                bullet_list.add(bullet)
                all_sprites_list.add(bullet)
                bullet.rect.x = 340
                bullet.rect.y = 200
        screen.fill(GRAY)
        all_sprites_list.draw(screen)
        clock.tick(60)
        pygame.display.flip()
    pygame.quit()
if __name__ == "__main__":
    main()
import pygame
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
GRAY = (128,128,128)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Cowboy(pygame.sprite.Sprite):
      def __init__(self):
          super().__init__()

          self.image = pygame.Surface([14,20])
          self.image.fill(BLUE)
          self.rect = self.image.get_rect()
class Bullet(pygame.sprite.Sprite):
      def __init__(self, x, y):
          pygame.sprite.Sprite.__init__(self)
          self.image = pygame.image.load("bullet.png").convert()
          self.rect = self.image.get_rect()
          self.rect.x = x
          self.rect.y = y
      def update(self):
         self.rect.y += -3
def main():
    pygame.init()
    size = [SCREEN_WIDTH, SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size, pygame.RESIZABLE) 
    all_sprites_list = pygame.sprite.Group()
    bullet_list = pygame.sprite.Group() 
    enemies_list = pygame.sprite.Group()
    bullet = Bullet(340, 240)
    enemy = Cowboy()
    enemies_list.add(enemy)
    all_sprites_list.add(enemy)
    enemy.rect.y = 50
    enemy.rect.x = 50
    done = False
    clock = pygame.time.Clock()
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                bullet = Bullet(340, 200)
                bullet_list.add(bullet)
                all_sprites_list.add(bullet)
                bullet.rect.x = 340
                bullet.rect.y = 200
        screen.fill(GRAY)
        all_sprites_list.update()
        all_sprites_list.draw(screen)
        clock.tick(60)
        pygame.display.flip()
    pygame.quit()
if __name__ == "__main__":
    main()

您必须创建一个新的 Bullet 实例,当按下鼠标按钮时,您必须调用 update 上的 update 子弹的 update 方法14=]:

def main():
    # [...]

    # bullet = Bullet() <--- DELETE

    # [...]
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.MOUSEBUTTONDOWN:

                # create new bullet and add it to the groups
                bullet = Bullet()
                bullet.rect.center = enemy.rect.center
                bullet_list.add(bullet)
                all_sprites_list.add(bullet)

        # update the bullets
        bullet_list.update()

        # [...]