我正在尝试找出创建具有多个状态的动画的最佳方法,但我似乎找不到任何以干净的方式实现我所追求的目标的示例。
我有一个角色的 Sprite 表,它有两种跳跃动画状态,有 6 个帧,其中角色动画基本上是从地面跳跃。
还有另外 6 帧,角色处于“跳跃循环”,因此角色已经离开地面,但 ARM 等轻微移动。
当我将所有图像放入列表并迭代它们时,跳跃的初始部分看起来很好,因为角色离开地面然后进入跳跃循环。但是,一旦帧的跳跃循环序列完成,动画就会返回到半空中的开始位置,因此看起来角色刚刚从某物上跳下来。
到目前为止我所拥有的函数的代码如下
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 帧,直到角色着陆。
最佳答案
我建议拆分动画 Spritesheet,这样您就可以在一行中包含用于跳跃的六个帧,然后将另一行用于其他六个下落的帧。
一旦你可以做到这一点,只需检查你的 Sprite 速度变量是正还是负,如下所示:
if self.vel > 0:
# Switch animation frames to jumping up
elif self.vel < 0:
# Switch animation frames to falling down
关于python - 具有多种状态的 Pygame 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55377426/