【问题标题】:Pygame, inheritance issuePygame,继承问题
【发布时间】: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


【解决方案1】:

您的Eclipse 实例未能初始化,因为当它调用其基类Rectangulo 的初始化时,那个 类需要 7 个参数(x、y 颜色等)而你没有提供任何东西。

所以你有很多选择,但两种最常见的方法是:

  • 将相同的7个参数传递给Eclipse__init__方法,然后将这些相同的参数传递给Rectangulo.__init__(self, ...)的调用
  • 决定在Elipse 中用于Rectangulo 的参数,例如Rectangulo.__init__(self, 1, 2, "red", ...)

一般来说,您可能需要第一个选项。例如

class Elipse(Rectangulo):
    def __init__(self, x, y, color, largo, alto, cambio_x, cambio_y):
        Rectangulo.__init__(self, x, y, color, largo, alto, cambio_x, cambio_y)

如果您的 Elipse 类本身不需要额外的参数,那么您可以通过以下方式稍微简化上述内容:

class Elipse(Rectangulo):
    def __init__(self, *args, **kwargs):
        Rectangulo.__init__(self, *args, **kwargs)

这基本上会将给 Elipse 实例化的所有参数传递给 Elipse.__init__ 调用。

【讨论】:

  • 谢谢,它就像一个咒语。多亏了您,该程序再次运行。
  • 太好了,如果您能接受将不胜感激的答案!
  • 我为我的小白道歉,但我该怎么做呢?
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2021-07-24
  • 2012-05-11
相关资源
最近更新 更多