You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Pygame开发触发update()参数个数不匹配TypeError报错求解

Pygame TypeError: update() takes 1 positional argument but 2 were given 问题解决方案

问题根因

报错的核心原因是self.tiles精灵组中混入了不符合参数要求的精灵对象:

  • 你自定义的Tile类update方法确实支持接收x_shift偏移参数
  • 但在level.py的setup_level方法中,你误将Player实例添加到了self.tiles组,而Player类的update方法仅支持1个self参数,当调用self.tiles.update(self.world_shift)时,所有组内精灵都会被传入world_shift参数,Player实例的update方法接收到多余参数就触发了类型错误。

解决步骤

  • 修正setup_level方法的精灵分组逻辑,Player实例不要加入self.tiles组,改为加入专属的self.player分组即可:
# level.py setup_level方法修改片段
if cell == 'P':
    player_sprite = Player((x,y))
    # 删掉原有的self.tiles.add(player_sprite),改为加入player组
    self.player.add(player_sprite)
  • (可选兼容方案)如果你的游戏逻辑需要玩家也跟随地图全局偏移,可以修改Player类的update方法,添加可选的偏移参数:
# player.py Player类修改片段
def update(self, x_shift=0):
    # 原有玩家移动、碰撞等逻辑保留,新增偏移处理
    self.rect.x += x_shift

修改完成后重新运行代码即可解决报错。


内容的提问来源于stack exchange,提问作者SecretSix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 05:36:04