0

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

[暴民类]

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)
4

2 回答 2

0
  1. Your code is erasing the Rect's width and height. If you want to use coordinates of the center, use:

    self.rect = self.image.get_rect()
    self.rect.center = location
    
  2. What is mobs.add() in update()? Because update is normally called every frame, and I'm guessing mobs is a sprite group.

  3. Your update:

    for Mob in mobs: Mob.moveV() Mob.moveH() screen.blit(Mob.image, Mob.rect)

should be

mobs.update()
mobs.draw(screen)

Check out this guys example https://stackoverflow.com/a/10418347/341744

于 2013-07-09T16:16:25.560 回答
0

尝试使用 mob.remove(Mob) 而不是 Mob.kill 它可能会起作用:D

于 2013-07-09T15:01:00.543 回答