我想杀死我的精灵。如何杀死它?

i want to kill my sprite. how to kill it?

    import pygame as pygame , sys,time
    
    pygame.init()
    size = (700,500)
    window_game = pygame.display.set_mode(size)
    
    _run_ = True
    
        
    
    white = (255,255,255) 
    mouse_pos = pygame.mouse.get_pos()
      
    
    
    class mySprite(pygame.sprite.Sprite):
        def __init__ (self,cord_x,cord_y,picture,colorkey):
            super().__init__()
            self.image = pygame.Surface([0,0])
            self.image = pygame.image.load(picture)
            self.image.set_colorkey(colorkey)
            self.rect = self.image.get_rect()
            self.rect.center = [cord_x,cord_y] 
            self.kill()
            
    
    placeSP_group = pygame.sprite.Group()
    Clock = pygame.time.Clock()
    FPS = 500
    while _run_:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.QUIT
                sys.exit()
        mouse_pos = pygame.mouse.get_pos()
        placeSP = [mySprite(mouse_pos[0],mouse_pos[1],'sp_1.png',white)]
        pygame.display.flip()
        placeSP_group.draw(window_game)
        placeSP_group.add(placeSP[:])
        Clock.tick(FPS)

现在我希望我的精灵被杀死并获得新的鼠标位置 和 window_game.fill('black') 不工作你能做些什么来解决它请告诉我...... 我不想每次移动鼠标时都不想杀死最后一个精灵并使用当前精灵位置创建另一个精灵。

您在错误的时间使用了 kill()

kill()Sprite 位于 Group 内时有效,但您首先创建 Sprite 并尝试使用 kill() 然后将此精灵添加到 Group.

您应该首先创建 Sprite,然后添加到 Group,然后 运行 kill() 用于 Group[= 中的第一个 Sprite 26=]

placeSP_group.sprites()[0].kill()

但正常 Group 不必保持 sprite 有序并更好地使用

placeSP_group = pygame.sprite.OrderedUpdates()

完整的工作代码。它显示 5 个跟随鼠标的精灵。

import pygame
import sys

# --- constants ---  # PEP8: `UPPER_CASE_NAMES` for constants

WHITE = (255, 255, 255)   # PE8: space after `,`
SIZE = (700, 500)
FPS = 50  # there is no need to use `500` because Python can't run so fast,
           # and monitors runs with 60Hz (eventually 120Hz) which can display 60 FPS (120 FPS)

# --- classes ---  # PEP8: `CamelCaseNames` for classes

class MySprite(pygame.sprite.Sprite):
    
    def __init__(self, x, y, picture, colorkey):
        super().__init__()
        
        # you need one of them
        
        # load image
        #self.image = pygame.image.load(picture)
        
        # OR

        # create surface        
        self.image = pygame.Surface((10, 10))
        self.image.fill((255, 0, 0))
        
        # ----
        
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)

        self.image.set_colorkey(colorkey)

# --- main ---

pygame.init()
window_game = pygame.display.set_mode(SIZE)

#placeSP_group = pygame.sprite.Group()
placeSP_group = pygame.sprite.OrderedUpdates()  # Group which keep order

sprite1 = MySprite(0, 0, 'sp_1.png', WHITE)
sprite2 = MySprite(0, 0, 'sp_1.png', WHITE)
sprite3 = MySprite(0, 0, 'sp_1.png', WHITE)
sprite4 = MySprite(0, 0, 'sp_1.png', WHITE)
sprite5 = MySprite(0, 0, 'sp_1.png', WHITE)
placeSP_group.add([sprite1, sprite2, sprite3, sprite4, sprite5])

clock = pygame.time.Clock()   # PEP8: `lower_case_names` for variables

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            #running = False
            pygame.quit()
            exit()
            
    # create new Sprite
    x, y = pygame.mouse.get_pos()
    new_sprite = MySprite(x, y, 'sp_1.png', WHITE)
    
    # add new Sprite at the end of OrderedUpdates()
    placeSP_group.add([new_sprite])
    
    # remove Sprite at the beginning of OrderedUpdates()
    placeSP_group.sprites()[0].kill()  

    # ---

    pygame.display.flip()
    
    window_game.fill('black') 
    placeSP_group.draw(window_game)
    
    clock.tick(FPS)