移动时如何使敌人的精灵碰撞而不是啮合在一起

时间:2015-10-22 00:58:53

标签: python pygame sprite collision

编辑:请停止向我推荐与岩石游戏碰撞的鸟,我的问题不在他的附近。我让玩家能够在我的游戏中碰撞并且不会穿过墙壁,但代码不会转移到僵尸。僵尸不会相互碰撞和摩擦,它们只是滑入彼此并成为一个街区。请阅读下面的解释< 3

所以我正在玩pygame游戏,当玩家向下射击时僵​​尸移向玩家(我知道,原创,对吗?)但是我必须检测僵尸相互碰撞的代码呢不能以任何实际的方式工作,如果没有它,僵尸只会移动到玩家并一起啮合成一个僵尸块。我可以在僵尸代码中改变什么来让它们在靠近吃掉我的大脑时不会相互移动?

class Enemy(pygame.sprite.Sprite):
    def __init__(self, color):
        super().__init__()
        self.image = pygame.Surface([20, 20])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.pos_x = self.rect.x = random.randrange(35, screen_width - 35)
        self.pos_y = self.rect.y = random.randrange(35, screen_height - 135)

        # How Zombies move towards player

    def update(self):
        zombie_vec_x = self.rect.x - player.rect.x
        zombie_vec_y = self.rect.y - player.rect.y
        vec_length = math.sqrt(zombie_vec_x ** 2 + zombie_vec_y ** 2)
        zombie_vec_x = (zombie_vec_x / vec_length) * 1 # These numbers determine
        zombie_vec_y = (zombie_vec_y / vec_length) * 1 # zombie movement speed

        self.pos_x -= zombie_vec_x
        self.pos_y -= zombie_vec_y

        self.rect.x = self.pos_x
        self.rect.y = self.pos_y


        block_hit_list = pygame.sprite.spritecollide(self, sprites_list, False)
        for block in block_hit_list:
            if self.rect.x > 0:
                self.rect.right = block.rect.left
            elif self.rect.x < 0:
                self.rect.left = block.rect.right
            elif self.rect.y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

这是整个僵尸代码,当前的碰撞代码位于底部,从block_hit_list =开始。任何有关如何纠正这一点或解释碰撞编码如何工作的见解将非常感激!

0 个答案:

没有答案
相关问题