【发布时间】:2015-02-12 23:54:31
【问题描述】:
我不是新手,也不是 python 经验,我发现自己陷入了一个严重的问题,即使在 Google 和 Bing 中使用了很多小时后,我也能找到答案。我的问题从这段代码开始:
class Rectangulo(object):
def __init__ (self, x, y, color, largo, alto, cambio_x, cambio_y):
self.alto = alto
self.largo = largo
self.color = color
self.cambio_x = cambio_x
self.cambio_y = cambio_y
self.x = x
self.y = y
def dibujar(self):
pygame.draw.rect(pantalla, self.color, (self.x, self.y, self.alto, self.largo))
def mover(self):
self.x += self.cambio_x
self.y += self.cambio_y
class Elipse(Rectangulo):
def __init__(self):
Rectangulo.__init__(self)
def dibujar (self):
pygame.draw.ellipse(pantalla, Rectangulo.color,(Rectangulo.x, Rectangulo.y, Rectangulo.alto, Rectangulo.largo))
这是目前最重要的一段代码。我发现问题存在于 Elipse 中,并尝试了多种方法使继承起作用,但控制台继续显示此消息。
Traceback (most recent call last):
File "/home/josh/Escritorio/Codigo python/Jueguito.py", line 63, in <module>
miEl = Elipse(x,y,VERDE,alto,largo,cam_x,cam_y)
TypeError: __init__() takes 1 positional argument but 8 were given
每次我尝试使用此代码调用类函数 dibujar():
for item in range(10):
x = random.randrange(685)
y = random.randrange(485)
alto = random.randrange(20, 71)
largo = random.randrange(20, 71)
cam_x = random.randrange(1, 2)
cam_y = random.randrange(-3, 3)
miObjeto = Rectangulo(x,y,VERDE,alto,largo,cam_x,cam_y)
miLista.append(miObjeto)
miEl = Elipse(x,y,VERDE,alto,largo,cam_x,cam_y)
miEl 变量曾经有自己的 for 循环,但我认为这样对我来说不会那么混乱。我仍然无法弄清楚发生了什么。我担心我需要一些帮助。
【问题讨论】:
-
这就是为什么大多数程序员使用英文作为变量名...
-
我是用西班牙语做的,因为教程是西班牙语的,所以我只是跟着节奏。但如果这对你来说真的很麻烦,我可以打断。
-
Elipse的构造函数是def __init__(self),但您传递了 7 个参数。您需要指定它们。 -
那么我应该放置像 def __init__(self, x, y, color, blah, blah) 这样的东西吗?我已经试过了,它不起作用
-
Traceback(最近一次调用最后一次):文件“/home/josh/Escritorio/Codigo python/Jueguito.py”,第 63 行,在
miEl = Elipse(x,y,VERDE, alto,largo,cam_x,cam_y) 文件“/home/josh/Escritorio/Codigo python/Jueguito.py”,第 22 行,在 init Rectangulo.__init__(self) TypeError: __init__() missing 7所需的位置参数:'x'、'y'、'color'、'largo'、'alto'、'cambio_x'和'cambio_y' >>>
标签: python list class inheritance pygame