is there any way to delete pygame sprite image using a function?

Viewed 39

As you can probabely tell by "self.image", it is meant to represent player's health. I wonder if there's any way to use "healthDeleted(self)" in order to remove(or change) self.image when a certain condition is met.

class Player_sprite_Health(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load("images/HUD/hud_heartFull.png")
        self.rect = self.rect = self.image.get_rect(topleft = (35,10))

    def healthDeleted(self):
        ''' Upon collision with an enemy object
            this function will be called
            It's purpose is to remove health
         '''

1 Answers

If you want to delete a pygame.sprite.Sprite object, you must remove it from all pygame.sprite.Groups. A Sprite can be removed from all Groups with kill:

class Player_sprite_Health(pygame.sprite.Sprite):
    # [...]

    def healthDeleted(self):
        self.kill()
Related