0

我在 Pygame 中有这张在屏幕上移动的图像。我想做的是每隔一段时间交替一次。所以,我希望它从 image1.png 开始,然后在页面上移动,在 human1.png 和 human2.png 之间切换,最后回到 image1.png。这可能吗?

我的代码是:

if (human1_position.left <= 555):
    human1_position = human1_position.move(2, 0)  # move first human
    pygame.display.update()
else:
    move = STOP
screen.blit(human1, human1_position)

谢谢

4

1 回答 1

1

这是一个可能的解决方案:

# Before main loop
human_files = ["human1.png", "human2.png"]
human_sprites = [pygame.image.load(filename).convert_alpha() for filename in human_files]
human1_index = 0

...

# During main loop
if (human1_position.left <= 555):
    human1_position = human1_position.move(2, 0)  # move first human
    human1_index = (human_index + 1) % len(human_sprites) # change sprite
else:
    move = STOP
    human1_index = 0
human1 = human_sprites[human1_index]
screen.blit(human1, human1_position)
pygame.display.update()

我移动了 update() 调用,它应该在所有绘制之后每帧只发生一次。

于 2012-10-24T15:33:51.287 回答