python 如何修复Pygame中移动动画的TypeError?

rt4zxlrg  于 2024-01-05  发布在  Python
关注(0)|答案(1)|浏览(144)

这是我第一次编写游戏,所以这个问题的答案可能很简单,但我一直在努力与它的年龄。我试图使运动的游戏,但我一直收到这个错误:

TypeError:Animation.update_animation()接受1个位置参数,但给出了6个

这是我尝试过的
下面是Animation类的代码段:

  1. def update_animation(self, x_movement, y_movement, right, left, up, down):
  2. # Updates the animation frame if the cooldown time has passed
  3. current_time = pygame.time.get_ticks()
  4. if current_time - self.last_update >= self.animation_cooldown:
  5. self.current_frame = (self.current_frame + 1) % self.animation_frames
  6. self.last_update = current_time
  7. # What frames are outputted depending on player movement
  8. if x_movement: #animations for moving along the x-axis
  9. if left:
  10. self.current_animation_frames = self.move_left
  11. elif right:
  12. self.current_animation_frames = self.move_right
  13. elif y_movement: #moving along the y axis
  14. if up:
  15. self.current_animation_frames = self.move_up
  16. elif down:
  17. self.current_animation_frames = self.move_down
  18. else:
  19. self.current_animation_frames = self.idle

字符串
下面是Character类中的代码,我在其中调用了update_animation方法:

  1. # changes animation frame
  2. self.player_animation.update_animation(x_movement, y_movement, left, right, up, down)
  3. # draws the player sprite with the current animation frame
  4. screen.blit(self.player_animation.get_current_frame(), (self.rect.x, self.rect.y))


我很困,因为我不知道我错过了什么,任何帮助将不胜感激。谢谢!

yduiuuwa

yduiuuwa1#

你能给我们看一下初始化调用类方法的对象的代码和调用类方法的行吗?如果不看,很难进一步说什么是错误的。
如果异常告诉你给出了太多的位置参数,它可能期望传递的变量在 Package 器或某种可迭代类型中。通常情况下,这可以通过在包含方法的所有位置参数的tuple/list/iterable前面加上 * 来调用类方法来解决。
Ie.

  1. player = Player(0, 0, spriteImg, **kwargs)
  2. player.player_animation.update_animation(*(False, True, False, False, True, False))
  3. screen.blit(player.player_animation.get_current_frame(), (player.rect.x, player.rect.y))

字符串
您还应该注意到,在类中调用animation_update()方法不会产生任何更改,除非您动态地将kb/mouse输入传递给类示例。(如果从Sprite继承),以获取必要的动画参数并将其传递给player_animation.update_animation()的调用然后使用结果对象运行screen.blit。

  1. def update(**kwargs):
  2. aniupdate = self.player_animation.update_animation(**kwargs)
  3. screen.blit(aniupdate, (self.coords))

展开查看全部

相关问题