user3777041
user3777041

Reputation: 63

Pygame How to use walking animations

When I walk Up, Down, Left, or Right I want 3 images to play while I'm walking in that direction.

player = pygame.image.load('data/down1.png')

playerX = 610
playerY = 350

while 1:
    clock.tick(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            return
        if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
                player = pygame.image.load('data/down1.png')
                player = pygame.image.load('data/down2.png')
                player = pygame.image.load('data/down3.png')
                playerY = playerY + 5

        screen.fill((0, 0, 0))
        screen.blit(player, (playerX, playerY))

        pygame.display.flip()

(This is part of my code btw) So is there a way I can make it so when I'm walking down it plays like an animation of the walking down images?

Upvotes: 5

Views: 11530

Answers (3)

furas
furas

Reputation: 142641

It is more complicated then you think.

You have to load images in list (not in one variable player).
You need variable walking = True. If walking == True then display next image from list and playerY = playerY + 1.
On next loop do the same till player moved to expected position. Then walking = False.

I can check it but it will look more like this:

player_images = []
player_images.append( pygame.image.load('data/down1.png') )
player_images.append( pygame.image.load('data/down2.png') )
player_images.append( pygame.image.load('data/down3.png') )
player_current = 0
player = player_images[ player_current ]

playerX = 610
playerY = 350

walking = False
walking_steps = 0

while True:

    # --- FPS ---

    clock.tick(30)

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            return
        if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_ESCAPE:
               return
           elif event.key == pygame.K_DOWN:
               walking = True
               walking_steps = 5

    # --- moves ---    

    if walking == True:
        # here you need to check some counter 
        # if it is time for next step to walk slower
        # but don't use `time.sleep()`
        if walking_steps > 0:
            player_current = (player_current + 1) % len(player_images)
            player = player_images[ player_current ]
            playerY = playerY + 1
            walking_steps -= 1
        else:
            walking = False

    # --- draws ---

    screen.fill((0, 0, 0))
    screen.blit(player, (playerX, playerY))

    pygame.display.flip()

Upvotes: 1

Cristiano Araujo
Cristiano Araujo

Reputation: 1962

You can create a circular buffer with the names.

images = ['data/down1.png','data/down2.png','data/down3.png']

and then change a counter

...

if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
    player = pygame.image.load(images[counter])
    counter = (counter + 1) % len(images)
    playerY = playerY + 5
...

It will change the image while the button is pressed.

Upvotes: 10

The Maniac
The Maniac

Reputation: 2626

TL;DR: You've gotta do the animation yourself

All you're doing in that code is loading 3 images, with the 3rd one being shown since it was the last to overwrite the player variable. You'll need to set up an animation loop, which is quite complex and out of the scope of a StackExchange reply, unfortunately.

Here's the general idea though, hopefully you can find a more complete tutorial on this:

  1. Create sets of sprites for each of the movement directions. This should be done outside of the main loop.
  2. Your event handler should just handle sprite position and state (i.e. "moving up" or "moving down").
  3. Inside your main loop, but outside of the event handler if-statement, check for the player's movement state. This determines the set of sprites you'll be using.
  4. Finally, determine the "next" sprite that should be shown and display it. Note: take into account your FPS, and figure how how many frames each sprite should be shown. In your case each frame is 1/30th of a second, so maybe 5-10 frames per sprite?

This is a vast oversimplification so be ready to figure out more as you go. This is probably a very inefficient way to do it as well, but you shouldn't worry about optimization until you need to (i.e. you're game is lagging). Good luck!

Upvotes: 3

Related Questions