【问题标题】:Why pygame.sprite causes "maximum recursion depth" in my class?为什么 pygame.sprite 在我的课程中导致“最大递归深度”?
【发布时间】:2021-06-21 15:47:12
【问题描述】:

我做了这个简单的类,由于某种原因它使 python 崩溃了,我不知道为什么

这是代码

import pygame
pygame.init()
class Entity(pygame.sprite.Sprite()):
    def __init__(self, pos, size):
        super().__init__()

这是我得到的错误

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "entity.py", line 4, in <module>
    class Entity(pygame.sprite.Sprite()):
  File "C:\Users\playe\AppData\Local\Programs\Python\Python38\lib\site-packages\pygame\sprite.py", line 124, in __init__
    self.add(*groups)
  File "C:\Users\playe\AppData\Local\Programs\Python\Python38\lib\site-packages\pygame\sprite.py", line 142, in add
    self.add(*group)
  File "C:\Users\playe\AppData\Local\Programs\Python\Python38\lib\site-packages\pygame\sprite.py", line 142, in add
    self.add(*group)
  File "C:\Users\playe\AppData\Local\Programs\Python\Python38\lib\site-packages\pygame\sprite.py", line 142, in add
    self.add(*group)
  [Previous line repeated 992 more times]
  File "C:\Users\playe\AppData\Local\Programs\Python\Python38\lib\site-packages\pygame\sprite.py", line 137, in add
    if hasattr(group, '_spritegroup'):
RecursionError: maximum recursion depth exceeded while calling a Python object

我该如何纠正这个错误?

【问题讨论】:

    标签: python python-3.x pygame pygame-surface


    【解决方案1】:

    pygame.sprite.Sprite() 创建一个对象。对于基类,只需要指定类名即可。见Inheritance

    pygame.sprite.Sprite() 更改为pygame.sprite.Sprite

    class Entity(pygame.sprite.Sprite()):

    class Entity(pygame.sprite.Sprite):
    

    【讨论】:

    • 哇,你真快,我在发布这个问题后就看到了。感谢您的帮助:3
    猜你喜欢
    • 1970-01-01
    • 2020-07-24
    • 2015-01-08
    • 1970-01-01
    • 2020-12-12
    • 2022-08-09
    • 1970-01-01
    • 2018-01-14
    • 2022-12-19
    相关资源
    最近更新 更多