Pygame .sprite. group()之后不能调用Pygame精灵对象



我最近开始使用pygame.sprite。Sprite Sprite . group ()类在pygame中,我遇到了一个问题,一旦我的精灵被分组,我就不能再初始化一个新的类实例,而是引发"TypeError: 'NPC' object is not callable"。下面是我的代码(为了简洁而删减);

class NPC(pygame.sprite.Sprite):
    def __init__(self, start_x, start_y, image):...
npc_pop=pygame.sprite.Group()
locations=[(100,200), (300,300), (150,200)]
def spawn_NPC(x, y):
    image_SS = ss.image_at(rando(), colorkey=(255, 0, 128)) #random image from sprite-sheet
    new_guy = NPC(x, y, image_SS)
    npc_pop.add(new_guy)
for c in locations:
    spawn_NPC(c[0],c[1])
while gameLoop == True:
    ....
    npc_pop.draw(screen)
    if len(npc_pop) < 2:
        spawn_NPC(100,100)

在不深入NPC类的细节的情况下,在游戏循环NPC表现为类并填充精灵容器npc_pop之前没有问题。然而,NPC类的下一个实例被作为一个函数调用,在最后一行引发一个错误,可以追溯到spawn_NPC()。为什么?

我读了这个线程得到一个错误,试图在Python中创建一个对象,并实现分组精灵改变类,但我仍然不完全理解逻辑。

我希望这段代码更清晰,我通过为我的NPC精灵类编写一个专用模块并构建一个类生成器来解决我的问题。

    import walk #NPC class now imported from a module
    @classmethod
    def spawnNPC(self, list):
    for c in list:
        image_SS = ss.image_at(rando(), colorkey=(255, 0, 128))
        new_guy = walk.NPC(c[0],c[1],image_SS)
        print type(new_guy), "googly"
        new_guy.add(npc_pop)
    #generate NPC instances at multiple locations before game start
    MiniSpawn = type("MiniSpawn", (object,), {"spawnNPC":spawnNPC})
    locations = [(333, 200), (300, 300), (200, 200), (50, 50)]
    MiniSpawn.spawnNPC(locations)
    #Generate new NPC instances in the game loop
    if started == 1 and len(npc_mov) < 2:
        MiniSpawn.spawnNPC([(200,300)])

最新更新