Ошибка типа: отсутствует один обязательный позиционный аргумент - PullRequest
0 голосов
/ 30 мая 2018

Я работаю над игрой как побочный проект для удовольствия, и я столкнулся с этой ошибкой, и я действительно не знаю, почему это происходит ...

Вот код:

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 голосов
/ 21 февраля 2019

Вам необходимо использовать () с именем класса

. Следуйте приведенному ниже коду

skierDirection, playerImage = players().turn(skierDirection, playerImages)
0 голосов
/ 30 мая 2018

Вы не должны вызывать метод класса напрямую, вместо этого создайте экземпляр этого класса:

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

Для уточнения получаемой ошибки:

Ошибка типа: turn () отсутствует 1 обязательный позиционный аргумент: 'playerImages'

Это потому, что turn нужен экземпляр players в качестве первого аргумента (self).Метод класса всегда получает экземпляр в качестве первого аргумента, поэтому p1.turn(skierDirection, playerImages) фактически передаст 3 параметра в players.turn.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...