当它在Pygame中移动时替换图像

时间:2012-10-24 15:20:58

标签: python pygame

我在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)

由于

1 个答案:

答案 0 :(得分: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()调用,在绘制完所有内容后,每帧只发生一次。