我目前正在开发一个2D平台游戏与游戏,我已经发现了一个问题。我通常使用在主函数内部声明的一个sprite组来处理sprite渲染。既然我需要有一些特定的精灵来对付其他人/在间谍之下,有一个单独的组是不会削减它的,而多个组只是躺在一起就会变得一团糟。因此,我决定将组添加到实体类中:
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)我让实体的所有其他子类使用它的__init__方法自动添加到一个组中。我认为它在类中运行得很好,因为当我初始化实体本身时没有显示错误,而是当我试图运行这段代码时。
for group in Entity.entities:一个AttributeError出现了
AttributeError: type object 'Entity' has no attribute 'entities'我对python还比较陌生,所以我不太明白我在这里所缺少的东西。有人知道解决这个问题的办法吗?
发布于 2016-10-16 17:19:33
弗拉斯在评论中解开了谜团。我只是忘了删除类的旧定义!我真傻。现在一切都很好。
https://stackoverflow.com/questions/40072913
复制相似问题