Python pygame中的组管理

Python pygame中的组管理,python,pygame,Python,Pygame,我正在创建一个游戏,当你杀死一个暴徒,两个以上出现。我知道,当你杀死一个暴徒时,另外两个暴徒出现了,但只有一个保持可见,并且表现得像它应该的那样。另一个只是出现然后消失。我怎么才能让他们两个都按照他们应该的方式行事呢。以下是我到目前为止的情况: [暴民阶级] class MOB(pygame.sprite.Sprite): def __init__(self, location): self.pos = [0,0] self.image = ENEMY

我正在创建一个游戏,当你杀死一个暴徒,两个以上出现。我知道,当你杀死一个暴徒时,另外两个暴徒出现了,但只有一个保持可见,并且表现得像它应该的那样。另一个只是出现然后消失。我怎么才能让他们两个都按照他们应该的方式行事呢。以下是我到目前为止的情况:

[暴民阶级]

class MOB(pygame.sprite.Sprite):
    def __init__(self, location):
        self.pos = [0,0]
        self.image = ENEMY
        pygame.sprite.Sprite.__init__(self)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
        self.rect.right, self.rect.bottom = location
        self.rect.center = location
        self.speed = random
        self.hp = 4
    def update(self):
         if self.hp == 0:
            mobs.add(self)
            self.image = ENEMY
            self.pos = [0,0]
            self.hp = 4
    def moveH(self):
        if self.rect.centerx >= Player.rect.centerx:
            self.rect.left = self.rect.left - 4
        elif self.rect.centerx <= Player.rect.centerx:
            self.rect.left = self.rect.left + 4

        def moveV(self):
        if self.rect.centery <= Player.rect.centery:
            self.rect.top = self.rect.top + 4
        if self.rect.centery >= Player.rect.centery:
            self.rect.top = self.rect.top - 4
[在主循环中重新绘制和移动]

for Mob in mobs:
    if Mob.hp == 0:
        score = score + 1
        Mob.kill()
        new_mobs = MOB([50, 50]), MOB([60, 300])
        mobs.add(*new_mobs)
for Mob in mobs:
    Mob.moveV()
    Mob.moveH()
    screen.blit(Mob.image, Mob.rect)
尝试使用Mob.remove(Mob)代替Mob.kill 它可能会起作用:D

  • 您的代码正在删除矩形的宽度和高度。如果要使用中心坐标,请使用:

    self.rect = self.image.get_rect()
    self.rect.center = location
    
  • 什么是
    update()
    中的
    mobs.add()
    ?因为更新通常每帧调用一次,我猜暴徒是一个精灵团体

  • 您的更新:

    对于Mob中的Mob: Mob.moveV() Mob.moveH() 屏幕blit(Mob.image,Mob.rect)

  • 应该是

    mobs.update()
    mobs.draw(screen)
    

    看看这个家伙的例子

    Kill会导致那个人死亡,并确保它在任何spritegroups中都没有任何引用,请再多发布一点。当我根据你的回答改变它时,两个怪物在两个繁殖点保持原位,计分器(未列出)不断上升,说我杀了一群怪物。所以,我杀的一个精灵消失了,但根据游戏,他仍然在那里,但看不见。现在,两个出现了,我杀的那个永远消失了,但是现在出现的两个,只在一个看不见的窗口内移动