Don't display first frame in Pygame (sprites)

Viewed 26

I am making a hit animation for a game made in pygame using sprites. I made it and it works, but my problem is, that after the animation ends, the first frame stays on the screen. Here is my code for the animations:

class HitAnimation(pygame.sprite.Sprite):

        def __init__(self,pos_x,pos_y):
            super().__init__()
            self.sprites = []
            self.is_animating = False
            self.sprites.append(pygame.image.load("hit0.gif").convert_alpha())
            self.sprites.append(pygame.image.load("hit1.gif").convert_alpha())
            self.sprites.append(pygame.image.load("hit2.gif").convert_alpha())
            self.sprites.append(pygame.image.load("hit3.gif").convert_alpha())
            self.sprites.append(pygame.image.load("hit4.gif").convert_alpha())
            self.current_sprite = 0
            self.image = self.sprites[self.current_sprite]
            self.rect = self.image.get_rect()
            self.rect.topleft = [pos_x,pos_y]

        def animate(self):
            self.is_animating = True
        
        def update(self):
            if self.is_animating == True:
                self.current_sprite += 0.3

                if self.current_sprite >= len(self.sprites):
                    self.current_sprite = 0
                    self.is_animating = False

                self.image = self.sprites[int(self.current_sprite)]

moving_hitsprites = pygame.sprite.Group()

Than later in the main game function:

while True:
   #basic stuff, the game itself
   #than here I am updating and drawing the hit animation
   moving_hitsprites.update()
   moving_hitsprites.draw(WIN)

   if event.type==PLAYER1_HIT:
       location = HitAnimation(playerrect.x-playerrect.width//2,playerrect.y-playerrect.height//2)
       moving_hitsprites.add(location)
       location.animate() # here I am starting the animation of the hit effect

The animation appears, but after it goes through all the frames, and after its done it will continue to display the first frame forever. How can I fix this?

1 Answers

You have to kill the animation sprite when the animation ends:

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

    def update(self):
        if self.is_animating == True:
            self.current_sprite += 0.3

            if self.current_sprite >= len(self.sprites):
                self.current_sprite = 0
                self.is_animating = False
 
                self.kill()                           # <---

            self.image = self.sprites[int(self.current_sprite)]

kill removes the Sprite from all Groups, so the Sprite will be removed from moving_hitsprites when the animation ends.

Actually, you don't need the is_animating attribute at all:

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

    def update(self):
        self.current_sprite += 0.3
        if self.current_sprite >= len(self.sprites):
            self.current_sprite = 0
            self.kill()
        self.image = self.sprites[int(self.current_sprite)]
Related