TypeError:缺少一个必需的位置参数

时间:2018-05-29 23:40:29

标签: python object 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]

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您不应该直接调用类方法,而是创建该类的实例:

p1 = players(your, values, here)
skierDirection, playerImage = p1.turn(skierDirection, playerImages)

详细说明你得到的错误:

  

TypeError:turn()缺少1个必需的位置参数:'playerImages'

这是因为turn需要players的实例作为第一个参数(self)。类方法总是作为第一个参数传递给实例,因此p1.turn(skierDirection, playerImages)会将3个参数实际传递给players.turn

答案 1 :(得分:0)

您必须使用()作为类名

按照下面的代码

http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=8776
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1662
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=3255
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=1524
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=6463
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=4511
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=3273
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=7471
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=4672
http://11.111.11.11:0000/Menu_EXAMPLE.jsp?NUMBER=2828