【发布时间】:2021-12-17 12:02:32
【问题描述】:
我目前正在尝试为我的角色设置碰撞箱,但我似乎无法摆脱这个错误。
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
self.x = x
self.y = y
pygame.sprite.Sprite.__init__(self)
self.image = IronMan
self.rect = self.image.get_rect()
self.rect.y = 475
self.direction = 1
self.hitbox = (self.x + 20, self.y + 11, 28, 60)
def draw(self, win):
self.hitbox = (self.x + 20, self.y + 11, 28, 60)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
当代码运行时,我面临着
TypeError: __init__() missing 1 required positional argument: 'y'
【问题讨论】:
-
full 错误信息是什么?您如何尝试实例化您的
Player? -
您只包含您的定义。我们需要查看您调用
__init__()的那一行,它应该类似于variable_name = Player(x_value,y_value) -
您可能没有将“y”参数传递给 __init__()。找到你实例化这个类的行并为 y 写一个值