AttributeError in a pygame.sprite.Sprite subclass
<p>I am currently developing a 2D platformer game with pygame and I have discovered an issue. I usually handled sprite rendering with a single sprite group declared inside of the main function. Now that I need to have some specific sprites over others/under sprites, having a single group won't cut it, and having multiple groups just laying about is a mess. So I decided to add groups into my Entity class:</p>

<pre><code>class Entity(pygame.sprite.Sprite):
    entitiesTop = pygame.sprite.Group()
    entitiesMid = pygame.sprite.Group()
    entitiesBot = pygame.sprite.Group()
    entities = [entitiesBot, entitiesMid, entitiesTop]

    def __init__(self, force = None):
        pygame.sprite.Sprite.__init__(self)
        if force is None:
            if isinstance(self, Platform):
                Entity.entitiesTop.add(self)
            elif isinstance(self, (Bullet, Gun)):
                Entity.entitiesMid.add(self)
            else:
                Entity.entitiesBot.add(self)
        else:
            Entity.entities[force].add(self)
</code></pre>

<p>and I made all the other subclasses of Entity automatically get added to a group using its <code>__init__</code> method. I think it was working fine with the classes, since the error didn't show when I initialized the entities themselves, rather when I tried to run this code</p>

<pre><code>     for group in Entity.entities:
</code></pre>

<p>an <code>AttributeError</code> appeared</p>

<pre><code>AttributeError: type object 'Entity' has no attribute 'entities'
</code></pre>

<p>I am relatively new to python OOP, so I don't quite get what I am missing here. Does anyone know the solution to this?</p>

<p>furas at the comments has solved the mystery. I just forgot to remove the old definition of the class! Silly me. Its all working fine now. </p>

python class python-3.x oop pygame