0
votes

I've a little problem. I'm writting a program in Python with Pygame, and I have a player moving with key of the keyboard (Right, left, jump ...). But, I would like to create an animation when my player "walks", because I have 6 image (PNG) to do this, but I can't do it. This is my program :

self.image = pygame.image.load(image_perso_droite).convert_alpha()

frame_1 = pygame.image.load("images/GIF/course_6.png").convert_alpha()

frame_2 = pygame.image.load("images/GIF/course_5.png").convert_alpha()

frame_3 = pygame.image.load("images/GIF/course_4.png").convert_alpha()

frame_4 = pygame.image.load("images/GIF/course_3.png").convert_alpha()

frame_5 = pygame.image.load("images/GIF/course_2.png").convert_alpha()

frame_6 = pygame.image.load("images/GIF/course_1.png").convert_alpha()
self.frame_set = [frame_1, frame_2, frame_3, frame_4, frame_5, frame_6]
self.current_frame = 0
self.timer = time.clock()

self.image = self.frame_set[self.current_frame]

if droite:

    self.xvel = 8
    if time.clock() >= self.timer + .05 and self.onGround:
        self.current_frame = 1
        self.image = self.frame_set[self.current_frame]

I try also with pyganim, without success. Thank you.

1
Please provide a self-contained compilable example of your problem. - jb.

1 Answers

0
votes

Assign values of clock.tick() for the player to alternate between the walking images.

e.g. if you have two "walk images", you can use the function:

def changeIMG(img):
    if img = IMG1:
        img = IMG2
    else:
        img = IMG1

...

clock.tick(20)
i += 1

if i % 5 == 0:
    changeIMG(currentIMG)