我想在移动并保持当前精灵的同时杀死我的最后一个精灵

i want to kill the my last sprite while moving and keeping my current sprtie

import pygame, sys
start = True
class Player(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y):
        super().__init__()
        self.attack_animation = False
        self.sprites = []
        self.sprites.append(pygame.image.load('crossHair.png'))
        self.sprites.append(pygame.image.load('crossHair_2.png'))
        self.sprites.append(pygame.image.load('crossHair_3.png'))
        self.sprites.append(pygame.image.load('crossHair_4.png'))
        self.current_sprite = 0
        self.image = self.sprites[self.current_sprite]
        self.image.set_colorkey('white')
        for items in self.sprites:
            items.set_colorkey('white')
        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x,pos_y]
        

    def attack(self):
        self.attack_animation = True
        self.image.set_colorkey('white')

    def update(self,speed):
        self.image.set_colorkey('white')
        
        self.current_sprite += speed
        if int(self.current_sprite) >= len(self.sprites):
            self.attack_animation = False
            self.current_sprite = 0
        self.image = self.sprites[int(self.current_sprite)]
# General setup
pygame.init()
clock = pygame.time.Clock()
# Game Screen
screen_width = 400
screen_height = 400
mouse = pygame.mouse.get_pos()
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("Sprite Animation")

# Creating the sprites and groups


moving_sprites = pygame.sprite.Group()
while True:
    globals()['mouse'] = pygame.mo[![now this is the problem][1]][1]use.get_pos()
    player = Player(mouse[0],mouse[1])
    moving_sprites.add(player)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            player.attack()
    # Drawing
    screen.fill((0,0,0))
    screen.set_colorkey('white')
    moving_sprites.draw(screen)
    moving_sprites.update(0.04)
    pygame.display.flip()
    clock.tick(120)

这是每 120 秒创建一次我的播放器/crossHair,它留下最后一个精灵

现在的问题是它留下了我的最后一个精灵,如果我把 moving_sprites = pygame.sprite.Group() 在 while 循环中然后它不会动画任何人都可以解决这个问题请回答我的问题...

这不是动画精灵的方法。每帧都创建一个新的精灵是一种不好的做法,并且会浪费性能,因为必须重新创建所有对象。您甚至可以在 Player class 的构造函数中加载图像。如果每一帧都这样做,那么每一帧图像都必须从卷中加载并解码。
在应用程序循环之前创建精灵一次,并在循环中改变精灵对象的属性。这将为您提供最佳性能:

class Player(pygame.sprite.Sprite):
    # [...]
   
    def set_pos(self, pos_x, pos_y):
        self.rect.topleft = (pos_x, pos_y)
moving_sprites = pygame.sprite.Group()
player = Player(0, 0)
moving_sprites.add(player)

while True:
    player.set_pos(*pygame.mouse.get_pos())

    # [...]