Pygame 具有多个状态的动画
Pygame animation with multiple states
我正在尝试找出创建具有多个状态的动画的最佳方法,但我似乎无法找到任何示例来以干净的方式实现我所追求的目标。
我有一个角色的 sprite sheet,它有两种跳跃动画状态,有 6 帧角色动画基本上是从地面上跳下来的。
还有另外 6 帧角色处于 "jump loop" 中,因此角色已经离开地面但手臂等轻微移动。
当我将所有图像放入一个列表并遍历它们时,跳跃的初始部分看起来很好,因为角色离开地面然后进入跳跃循环。但是一旦帧的跳跃循环序列完成,动画就会回到半空中的开始,所以看起来好像角色只是从什么东西上跳下来。
目前我的函数代码如下
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 分开,这样你就有一行包含六个用于跳跃的帧,然后另一行包含其他六个下降帧。
一旦你可以做到这一点,只需检查你的精灵速度变量是正数还是负数,如下所示:
if self.vel > 0:
# Switch animation frames to jumping up
elif self.vel < 0:
# Switch animation frames to falling down
谢谢大家,我确实将动画分成两部分(不是 spritesheet)并且我让它与速度一起工作(self.vel.x > 0)但是经过一些考虑那些额外的 6 帧并没有真正给动画添加了很多,所以我就把它拿出来,用跳跃的循环部分作为动画。
仔细观察,只有 3 帧,当角色在完全跳跃之前稍微抬起他的腿时,它并不是很明显,因此没有真正添加任何东西,所以我报废了这个想法。
我正在尝试找出创建具有多个状态的动画的最佳方法,但我似乎无法找到任何示例来以干净的方式实现我所追求的目标。
我有一个角色的 sprite sheet,它有两种跳跃动画状态,有 6 帧角色动画基本上是从地面上跳下来的。
还有另外 6 帧角色处于 "jump loop" 中,因此角色已经离开地面但手臂等轻微移动。
当我将所有图像放入一个列表并遍历它们时,跳跃的初始部分看起来很好,因为角色离开地面然后进入跳跃循环。但是一旦帧的跳跃循环序列完成,动画就会回到半空中的开始,所以看起来好像角色只是从什么东西上跳下来。
目前我的函数代码如下
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 分开,这样你就有一行包含六个用于跳跃的帧,然后另一行包含其他六个下降帧。
一旦你可以做到这一点,只需检查你的精灵速度变量是正数还是负数,如下所示:
if self.vel > 0:
# Switch animation frames to jumping up
elif self.vel < 0:
# Switch animation frames to falling down
谢谢大家,我确实将动画分成两部分(不是 spritesheet)并且我让它与速度一起工作(self.vel.x > 0)但是经过一些考虑那些额外的 6 帧并没有真正给动画添加了很多,所以我就把它拿出来,用跳跃的循环部分作为动画。
仔细观察,只有 3 帧,当角色在完全跳跃之前稍微抬起他的腿时,它并不是很明显,因此没有真正添加任何东西,所以我报废了这个想法。