我目前正在开发一个二维平台游戏与pygame和我发现了一个问题。我通常在main函数中声明一个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)
我使用Entity的__init__
方法将Entity的所有其他子类自动添加到一个组中。我认为它可以很好地与类一起工作,因为在初始化实体本身时没有显示错误,而在尝试运行此代码时没有显示错误
for group in Entity.entities:
出现了AttributeError
AttributeError: type object 'Entity' has no attribute 'entities'
我对python-OOP还比较陌生,所以我不太了解这里缺少的东西。有人知道解决这个问题的办法吗?你知道吗
福拉斯在评论中解开了谜团。我只是忘了删除类的旧定义!我真傻。现在一切正常。你知道吗
相关问题 更多 >
编程相关推荐