我刚刚开始进入课堂,理解了基本概念,但是我不明白为什么它会给我带来这个错误。
我的代码:
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
class shape():
def __init__(self, place, colour, x, y):
self.place = place
self.colour = colour
self.x = x
self.y = y
class rectangle(shape):
def __init__(self, place, colour, x, y, length, width):
super().__init__(self, place, colour, x, y)
self.length = length
self.width = width
pygame.draw.rect(screen, colour, pygame.Rect(x, y, lenght, width))
Rectangle = rectangle(screen, (0, 128, 255), 30, 30, 60, 60)
pygame.display.flip()
我收到的错误:
Traceback (most recent call last):
File "Pygame.py", line 34, in <module>
Rectangle = rectangle(screen, (0, 128, 255), 30, 30, 60, 60)
File "Pygame.py", line 23, in __init__
super().__init__(self, place, colour, x, y)
TypeError: __init__() takes 5 positional arguments but 6 were given
我不知道为什么它会出现错误,因为我正在创建一个“矩形”对象。我找到了一些例子,它们似乎和我的一样。
发布于 2019-11-21 11:28:57
问题出现在super().__init__(self, place, colour, x, y)
上。
您正在尝试调用您扩展的类的初始化程序,但是您的rectangle
类没有扩展任何类,这意味着super()
正在寻找一些您无法控制的内置类。
既然你说你从一个例子中学到了这一点,我猜你错过了扩展一个类的机会。
要使脚本正常工作,可以删除对super().__init__...
的调用
发布于 2019-11-21 11:46:12
我也面临着同样的问题,iam使用python2.7.x。我直接用类名解决了这个问题。这是Python扩展-使用超级Python 3和Python 2的解释
尝尝这个
shape.__init_(self,place, colour, x, y)
https://stackoverflow.com/questions/58973743
复制相似问题