具有多个状态的Pygame动画

问题描述 投票:1回答:2

我试图找出创建具有多个状态的动画的最佳方法,但我似乎无法在干净的方式上找到任何示例来实现我所追求的目标。

我有一个用于具有两种跳跃动画状态的角色的精灵表,有6个帧,其中角色动画基本上是从地面跳下来的。

还有另外6帧,其中角色处于“跳跃循环”中,因此角色已经离开地面但是手臂等稍微移动。

当我将所有图像放入列表并迭代它们时,跳转的初始部分看起来很好,因为角色离开地面然后进入跳跃循环。但是一旦帧的跳跃循环序列完成,动画就会回到半空中的开始,所以看起来这个角色只是跳了一些东西。

到目前为止,我的功能代码如下

def animate(self):            
    now = pg.time.get_ticks()

    # Jumping
    if self.jumping:
        if now - self.last_update > 18:
            self.last_update = now
            if self.left:
                self.current_frame = (self.current_frame + 1) % len(self.jump_l)
                bottom = self.rect.midbottom
                self.image = self.jump_l[self.current_frame]
                self.rect = self.image.get_rect()
            elif self.right:
                self.current_frame = (self.current_frame + 1) % len(self.jump_r)
                bottom = self.rect.midbottom
                self.image = self.jump_r[self.current_frame]
                self.rect = self.image.get_rect()
            self.rect.midbottom = bottom

动画可以工作,但基本上我想做的只是前6帧如何循环,然后在最后6帧循环,直到角色落地。

Current animation

python animation pygame sprite-sheet
2个回答
1
投票

我建议拆分动画Spritesheet,这样你就有一行有六个帧用于跳跃,然后另一行用于其他六个下降帧。

一旦你能做到这一点,只需检查你的精灵速度变量是正面还是负面,如下所示:

if self.vel > 0:
    # Switch animation frames to jumping up

elif self.vel < 0:
    # Switch animation frames to falling down

0
投票

谢谢大家,我确实将动画分成了两个(不是spritesheet),我让它使用了速度(self.vel.x> 0),但经过一些考虑,这些额外的6帧并没有真正为动画添加太多所以我把它拿出来并使用跳跃的循环部分作为动画。

仔细观察它只有3帧,这是当角色在完全跳跃之前稍微抬起他的腿并且它不是真正引人注目的因此并没有真正添加任何东西所以我废弃了这个想法。

© www.soinside.com 2019 - 2024. All rights reserved.