所以我正在玩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 =
开始。任何有关如何纠正这一点或解释碰撞编码如何工作的见解将非常感激!