3

I'm trying to find a way to have enemies track the player in my 2d game (pygame) but not clump up

Currently, when I shoot at them, the bullet collides into and damages all of the enemies that are clumped. I would like it to be a hoard but spread out just enough to where I can't hit every single enemy at once

It looks like this

Here's a gif of them clumping

I'm not sure how I would get the individual values of the enemies' positions so I can move them when they collide or how I should move them

This is what I currently have for the enemies to track the player:

     for aliveEnemies in enemy:
        if playerObj.rect.x - aliveEnemies.rect.x != 0:
            if playerObj.rect.x > aliveEnemies.rect.x:
                aliveEnemies.rect.x += 1
            if playerObj.rect.x < aliveEnemies.rect.x:
                aliveEnemies.rect.x -= 1
        if playerObj.rect.y - aliveEnemies.rect.y != 0:
            if playerObj.rect.y > aliveEnemies.rect.y:
                aliveEnemies.rect.y += 1
            if playerObj.rect.y < aliveEnemies.rect.y:
                aliveEnemies.rect.y -= 1"

Any help or points in the right direction would be greatly appreciated

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ryan
  • 47
  • 1
  • 6

2 Answers2

1

You can do collision detection between the enemies, to determine which ones are too close. You'll also need to change their behavior, to decide what to do when they actually get too close.

If you know you'll never get too many enemies, you can try comparing every enemy with every other enemy. This will take O(N^2) work, but that is probably OK if N is limited.

If you are comparing every enemy to every other anyway, you have a wider variety of options than just "collision detection": like the Boids algorithm (which does collision avoidance instead).

comingstorm
  • 25,557
  • 3
  • 43
  • 67
  • Thank you that's a great answer. So I have a list of all the enemies' Xs and the enemies Ys which are removed when they die and added when they spawn. I already have collisions implemented with the bullets and enemies. I decided to go the route you suggested but got stuck at comparing them. How would I compare ALL X's and Y's to see of any match, since there also has to be a range in which those Xs and Ys collide which is larger than 1 because they're 20 pixels wide? – Ryan Jun 21 '20 at 01:03
  • 1
    You can use two nested for loops, each over all enemies. The inner loop can just skip over the enemy being handled by the outer for loop. – comingstorm Jun 22 '20 at 05:26
  • That's so much easier than what I was trying to do haha. Thank you so much for your help – Ryan Jun 22 '20 at 18:55
1

Pygame rect objects have a function called "colliderect" which tests whether two rect objects are overlapping: https://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect You can use this to test each enemy whether they're overlapping any other enemy before moving them.

Pzet
  • 470
  • 2
  • 4
  • 11
  • Thank you a lot, I went to the docs and read a bunch. Since I'm thinking of having like 100 enemies how would I check every enemy effectively with that function since there are many combinations? – Ryan Jun 21 '20 at 01:07