python - TypeError:缺少一个必需的位置参数
问题描述
我正在开发一个游戏作为一个有趣的副项目,我遇到了这个错误,我真的不知道为什么会这样......
这是代码:
class players:
def __init__(self, location, image_file, direction):
self.location = location
self.image_file = image_file
self.direction = direction
self.rect = self.image_file.get_rect()
def turn(self, direction, playerImages):
keys = pygame.key.get_pressed()
if keys[pygame.K_a] == True:
self.direction -= 1
if self.direction < -3:
self.direction = 3
if keys[pygame.K_d] == True:
self.direction = 1
if self.direction > 3:
self.direction = 3
if self.direction == -3:
self.image_file = playerImages[0]
if self.direction == -2:
self.image_file = playerImages[1]
if self.direction == -1:
self.image_file = playerImages[2]
if self.direction == 0:
self.image_file = playerImages[3]
if self.direction == 1:
self.image_file = playerImages[4]
if self.direction == 2:
self.image_file = playerImages[5]
if self.direction == 3:
self.image_file = playerImages[6]
return self.direction, self.image_file
我这样称呼它:
skierDirection, playerImage = players.turn(skierDirection, playerImages)
我得到的错误是:
Traceback (most recent call last):
File "C:\Users\Owen\Desktop\coding compile file\SkiFreeX\SkiFreeX.py", line 129, in <module>
main()
File "C:\Users\Owen\Desktop\coding compile file\SkiFreeX\SkiFreeX.py", line 122, in main
skierDirection, playerImage = players.turn(skierDirection, playerImages)
TypeError: turn() missing 1 required positional argument: 'playerImages'
[Finished in 0.385s]
有任何想法吗?
解决方案
您不应该直接调用类方法,而是创建该类的实例:
p1 = players(your, values, here)
skierDirection, playerImage = p1.turn(skierDirection, playerImages)
要详细说明您遇到的错误:
TypeError:turn()缺少1个必需的位置参数:'playerImages'
这是因为需要作为第一个参数 ( )turn
的实例。类方法总是将实例作为第一个参数传递,因此将实际传递 3 个参数给.players
self
p1.turn(skierDirection, playerImages)
players.turn
推荐阅读
- json - 如何使用 PL/SQL 循环遍历 json 响应?
- xamarin - 无法加载文件路径中包含特殊字符 (!@#) 的 pdf 文件。Webview Xamarin 表单
- qt - QT 套接字等待用户输入
- java - 如何使用 Java 在 SQL Server 中存储具有精确小数的 DateTime
- python - 处理 .csv 文件:错误:只能将整数标量数组转换为标量索引
- javascript - 错误 [ERR_PACKAGE_PATH_NOT_EXPORTED]:在@babel/helper-compilation-targets/package.json 中没有解决“导出”主要问题
- nsis - 如何调试 nsis 脚本?3.05-2 给出错误:Error: unterminated string parsing line at macro:_IncludeStrFunction
- java - INSTALL_FAILED_NO_MATCHING_ABIS
- netsuite - 如何在 NetSuite 中的“消息”(crmmessage)和“文件”记录类型上创建自定义字段
- reactjs - 如何使用 usestate 和 setstate 而不是 this.setState 使用 react 和 typescript?