【问题标题】:Maximum recursion depth exceeded when creating subclass of pygame.sprite.Sprite创建 pygame.sprite.Sprite 的子类时超出最大递归深度
【发布时间】:2018-02-23 13:14:50
【问题描述】:

我正在尝试实例化我创建的对象,女巫是 pygame.sprite.Sprite 的子类。这是我用来实例化它的代码:

    import pygame as pg
    from GameObject import GameObject    

    player = GameObject("Sprites/Player", 6)

这是对象本身的代码:

import pygame as pg
class GameObject(pg.sprite.Sprite):
    def __ini__(self, image_path, number_of_images):
        for i in range(number_of_images):
            self.images.append(pg.image.load("{}/sprite{}.png".format(image_path, i)))

当我尝试运行它时,我得到了这个:

Traceback (most recent call last):
  File "main.py", line 11, in <module>
    player = GameObject("Sprites/Player", 6)
  File "/usr/lib/python3.6/site-packages/pygame/sprite.py", line 124, in __init__
    self.add(*groups)
  File "/usr/lib/python3.6/site-packages/pygame/sprite.py", line 142, in add
    self.add(*group)
  File "/usr/lib/python3.6/site-packages/pygame/sprite.py", line 142, in add
    self.add(*group)
  File "/usr/lib/python3.6/site-packages/pygame/sprite.py", line 142, in add
    self.add(*group)
  [Previous line repeated 327 more times]
RecursionError: maximum recursion depth exceeded

我查看了一些用于创建此类的子类的示例代码,但我看不出我的代码与其他代码之间的区别。我错过了什么?

【问题讨论】:

  • 这是你的全部代码吗?根据该堆栈跟踪,您的 ini 函数似乎正在调用自己,但我没有看到您的代码中发生这种情况
  • 这是所有代码,是的。你的意思是__init__?好像是自动调用的,因为它是超类的构造函数。
  • 如果将__ini__ 更改为__init__,问题会消失吗?
  • 哦,我的..这很尴尬..我会尝试
  • 修复了!谢谢和抱歉...

标签: python recursion pygame sprite subclass


【解决方案1】:

我遇到了同样的问题,这是由__init__(你写了__ini__)函数中的拼写错误引起的(当我写_init_而不是__init__时发生了这种情况)。 修正错字使其工作

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 2013-12-01
    • 2017-03-24
    • 2011-12-31
    • 2017-08-09
    • 2011-03-31
    • 1970-01-01
    相关资源
    最近更新 更多