Reputation: 63
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
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
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
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:
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