在 pygame 中无限动画精灵

问题描述

我正在制作飞机射击游戏。我制作了不同的飞机精灵,我正在寻找一种方法来为它们设置动画,这样我的飞机看起来就像它的螺旋桨无限转动。 myPlane (in position 1)

pos1 = pygame.image.load(r'C:\Users\me\Documents\MyGame\planee.png')
pos2 = pygame.image.load(r'C:\Users\me\Documents\MyGame\planee2.png')
pos3 = pygame.image.load(r'C:\Users\me\Documents\MyGame\planee3.png')
pos4 = pygame.image.load(r'C:\Users\me\Documents\MyGame\planee4.png')
pos5 = pygame.image.load(r'C:\Users\me\Documents\MyGame\planee5.png')

所以请记住,从游戏开始到结束,飞机似乎应该无限转动其螺旋桨,这意味着这与按下或键或任何事件无关。 我怎么能这样做?

解决方法

制作动画图像列表:

image_list = [pos1,pos2,pos3,pos4,pos5]

添加将存储图像列表索引的变量:

current_image = 0

在应用循环中增加索引,如果等于列表的长度则重置索引:

run = True
while run:
    # [...]

    current_image += 1
    if current_image == len(image_list):
        current_image = 0

    # [...]

通过订阅获取当前图像:

screen.blit(image_list[current_image],x,y)