Pygame如何使用步行动画

use*_*041 4 python pygame

当我向上,向下,向左或向右走时,我想要在我向那个方向走的时候播放3张图像.

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()
Run Code Online (Sandbox Code Playgroud)

(这是我的代码btw的一部分)所以,有什么方法可以让我这样做,当我走下去时,它就像一个走下来的图像的动画?

Cri*_*ujo 7

您可以使用名称创建循环缓冲区.

images = ['data/down1.png','data/down2.png','data/down3.png']
Run Code Online (Sandbox Code Playgroud)

然后换一个柜台

...

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
...
Run Code Online (Sandbox Code Playgroud)

按下按钮时会改变图像.