I am new to pygame and trying to make space invaders. So when I spawn enemies the enemies usually overlap each other which I don't want, I tried to fix it for hours but nothing worked and ended up here. I want the enemies not to intersect each other (here's a picture of what's happening. I don't want the results like the enemies in blue. I want the results like in red.)
code
class Enemy(pygame.sprite.Sprite):
hp = 100
X_change = 3
Y_change = 40
def __init__(self):
super().__init__()
self.image = pygame.image.load("images/enemy.png")
self.X = random.randint(0, 800)
self.Y = random.randint(33,130)
self.rect = self.image.get_rect(center = (self.X, self.Y))
def update(self):
# movement
self.rect.x += self.X_change
if self.rect.x <= 0:
self.X_change = 3
self.rect.y += self.Y_change
elif self.rect.x>= 736:
self.X_change = -3
self.rect.y += self.Y_change