-1

Edit: These suggestions do not answer my question and I've already tried to find an answer to my question. I'm not asking how to detect when 2 things collide in pygame, I'm asking how to detect several of the same thing colliding. I have a list created and all the asteroids get added to the list when spawned into the game, I need to check if one of the asteroids in that list collide with another asteroid in that list, which has proven to be more difficult then I'd imagined. Please keep this post open

I'm creating a game where your a spaceship in space avoiding asteroids, however there is no logic telling the game not to spawn asteroids on top of each other and instead space them out. I honestly have no idea where or how to start

I create asteroids with this method:

def asteroid_spawner():
    asteroids_to_spawn = random.randint(asteroid_min, asteroid_max)
    for _ in range(asteroids_to_spawn):
        randx = random.randint(0, 980)

        path_number = random.randint(1, 3)
        if path_number == 1:
            image_path = "Sprites/Enemies/Asteroid_Small.png"
        elif path_number == 2:
            image_path = "Sprites/Enemies/Asteroid_Medium.png"
        elif path_number == 3:
            image_path = "Sprites/Enemies/Asteroid_Large.png"

        new_asteroid = Asteroid(randx, -40, image_path)
        asteroids.append(new_asteroid)

I spawn the asteroids with this method:

if asteroid_spawn >= max(500, asteroid_spawn_rate):
            asteroid_spawn_rate -= asteroid_spawn_rate_decrease
            asteroid_spawn = 0
            asteroid_spawner()

Variables:

asteroid_spawn_rate = 2000 # How often to spawn an asteroid (in milliseconds)
asteroid_spawn = 1000 # Updates every tick and spawns more asteroids if it's greater than asteroid_spawn_rate
asteroid_spawn_rate_decrease = 50 # Decreases asteroid_spawn_rate every time more asteroids spawn 
asteroid_min, asteroid_max = 1, 3
asteroids = []
Trevn Jones
  • 341
  • 2
  • 4
  • 11
  • `spritecollide`, as suggested in one of the duplicates, will do exactly what you want, no? Inside of `asteroid_spawner`, after creating, but before adding, check if the new sprite collides with anything in the existing asteroid group. – MegaIng Aug 29 '23 at 14:21

0 Answers0