如何在Pygame中制作动画

3

我试图制作一个动画,当我的玩家被射击时,让他看起来像是倒地。

我尝试了下面的代码,但似乎并不起作用。它会减慢帧速率并且只显示我的动画的最后一张图片。在pygame中有更简单的动画方式吗?

    if player2_hit_sequence == True:
        stage1 = True
        if stage1 == True:
            game_display.blit(dying1_p2, (player2X, player2Y))
            time.sleep(0.2)
            stage1 = False
            stage2 = True
        if stage2 == True:
            game_display.blit(dying2_p2, (player2X, player2Y))
            time.sleep(0.2)
            stage2 = False
            stage3 = True
        if stage3 == True:
            game_display.blit(dying3_p2, (player2X, player2Y))
            time.sleep(0.2)

有没有制作图像序列或类似内容的函数?
1个回答

2

好的,对于动画,您需要一堆图像和一个计时器。

我将展示一些基于pygame sprite的代码片段。也许这并不完全符合问题,但与手动绘制/复制图像相比,它似乎是更好的解决方案。

首先,代码从一个sprite类开始:

class AlienSprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.base_image = pygame.image.load('alien.png').convert_alpha()
        self.image = self.base_image
        self.rect = self.image.get_rect()
        self.rect.center = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
        # Load warp animation
        self.warp_at_time = 0
        self.warp_images = []
        for filename in [ "warp1.png", "warp2.png", "warp3.png" ]:
            self.warp_images.append( pygame.image.load(filename).convert_alpha() )

所以,Alien Sprite的想法是有一个“正常”的图像,但当它“扭曲”(瞬间移动)时,会播放一个动画。实现的方法是有一个动画图像列表。当动画开始时,精灵的imagebase_image更改为warp_images []中的第一个。随着时间的流逝,精灵的图像会更改为下一帧,然后是下一帧,最后恢复到基本图像。通过将所有这些嵌入到精灵的update()函数中,精灵的正常更新机制处理外星人精灵的当前“状态”,即正常或“warp”。一旦触发“warp”状态,它就会在没有pygame主循环的任何额外参与的情况下运行。
def update(self):
    # Get the current time in milliseconds (normally I keep this in a global)
    NOW_MS = int(time.time() * 1000.0)
    # Did the alien warp? (and at what time)
    if (self.warp_at_time > 0):
        # 3 Frames of warp animation, show each for 200m
        ms_since_warp_start = NOW_MS - self.warp_at_time
        if ( ms_since_warp > 600 ):
            # Warp complete
            self.warp_at_time = 0
            self.image = self.base_image  # return to original bitmap
            # Move to random location
            self.rect.center = ( random.randrange( 0, WINDOW_WIDTH ), random.randrange( 0,  WINDOW_HEIGHT ) )
        else:
            image_number = ms_since_warp // 200  # select the frame for this 200ms period
            self.image = self.warp_images[image_number] # show that image

def startWarp(self):
    # Get the current time in milliseconds (normally I keep this in a global)
    NOW_MS = int(time.time() * 1000.0)
    # if not warping already ...
    if (self.warp_at_time == 0):
        self.warp_at_time = NOW_MS

首先需要注意的是,update() 函数使用时钟来计算动画从开始到现在经过了多少毫秒。为了跟踪时间,我通常在游戏循环中设置一个全局变量 NOW_MS

在精灵中,我们有三帧动画,每帧之间间隔200毫秒。要开始播放精灵动画,只需调用 startWarp() 函数,它会启动定时器。

SPRITES = pygame.sprite.Group()
alien_sprite = AlienSprite()
SPRITES.add(alien_sprite)

...

# Game Loop
done = False
while not done:
    SPRITES.update()

    # redraw window
    screen.fill(BLACK)
    SPRITES.draw(screen)
    pygame.display.update()
    pygame.display.flip()

    if (<some condition>):
        alien_sprite.startWarp()  # do it

显然,所有这些帧计时和其他东西都应该是精灵类的成员变量,但为了使示例简单,我没有这样做。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接