stopping pygame animation on the last frame, using temporary variable

70 Views Asked by At

I have this function which triggers players death animation:

def draw_player_death_animation(self):
    self.is_player_image = False
    death_duration = 400
    animation_time = pg.time.get_ticks() % (3 \* death_duration)
    frame_index = animation_time // death_duration


    if frame_index == 0:
        self.last_moved = self.player_image_death_1
        self.movement_speed = 0
    elif frame_index == 1:
        self.last_moved = self.player_image_death_2
        self.movement_speed = 0
    else:
        self.last_moved = self.player_image_death_final
        self.movement_speed = 0

However I have a problem, animation does not stop it goes in circle, and I need it to freeze at self.player_image_death_final. How to do it?

This function is in my player class:

class Player(pg.sprite.Sprite):
    def __init__(self...)
        self.last_moved = ''

"self.last_moved stores" the last sprite that should be drawn on screen.

So how do I make it freeze on the last sprite?

I tried adding the flag, but it did not work:

    def draw_player_death_animation(self):
            self.is_player_image = False
            last_image = False
            death_duration = 400
            animation_time = pg.time.get_ticks() % (3 * death_duration)
            frame_index = animation_time // death_duration

            if frame_index == 0:
                self.last_moved = self.player_image_death_1
                self.movement_speed = 0
            elif frame_index == 1:
                self.last_moved = self.player_image_death_2
                self.movement_speed = 0
                last_image = True
            elif last_image == True:
                self.last_moved = self.player_image_death_final
               self.movement_speed = 0
1

There are 1 best solutions below

0
On

frame_index and last image must be an attribute of the class. Do not change the frame_index anymore if last_image is set:

class Player(pg.sprite.Sprite):
    def __init__(self...)
        self.last_moved = ''
        self.last_image = False
        self.frame_index = 0
        # [...]

    def draw_player_death_animation(self):
        self.is_player_image = False
        death_duration = 400
        animation_time = pg.time.get_ticks() % (3 * death_duration)

        if self.last_image:
            self.last_moved = self.player_image_death_final

        else:
            self.frame_index = animation_time // death_duration
            if frame_index == 0:
                self.last_moved = self.player_image_death_1
            
            elif frame_index == 1:
                self.last_moved = self.player_image_death_2
           
            else:   
                self.last_image = True