GameCube
GameCube

Reputation: 43

How do I detect a collision of a player with multiple objects with the same name?

I am trying to create an endless (for a long while) runner so what I did to spawn enemies is :

numbers = [-3, -1,0, 1, 3]
for i in range(0, 10000, 10):
    enemy1 = Sprite("assets/blue.png", scale=(6, 12), position=(Vec3(random.choice(numbers), random.randint(-3, 3)+i, -3)), collider = "box")
    enemy2 = Sprite("assets/red.png", scale=(6, 12), position=(Vec3(random.choice(numbers), random.randint(-3, 3)+i, -3)), collider = "box")
    enemy3 = Sprite("assets/green.png", scale=(6, 12), position=(Vec3(random.choice(numbers), random.randint(-3, 3)+i, -3)), collider = "box")

def update():
    if player.intersects(enemy1).hit == True:
            print("collided with enemy1")
    if player.intersects(enemy2).hit == True:
            print("collided with enemy2")
    if player.intersects(enemy3).hit == True:
            print("collided with enemy3")

the upper part of the code is where the enemies and their colliders are declared and spawned while the if statements are there to detect the enemy collision. For some reason the enemy collision doesnt work though. For some reason it does work when I do pretty much the same thing with the walls:

wall1 = Entity(model="quad", scale=(1, 10000), position=(Vec3(5, -4, -3)), collider = "box")
wall2 = Entity(model="quad", scale=(1, 10000), position=(Vec3(-5, 4, -3)), collider = "box")

def update():
    if player.intersects(wall1).hit == True:
            print("collided with wall1")
    if player.intersects(wall2).hit == True:
            print("collided with wall2")

Same code structure here. The upper part of the code is there to spawn the walls and its colliders while the if statements are there to detect collision. As I said it works for the walls but not for the enemies. This is how I spawn the player if its useful:

player = Animation("assets/car", scale=(0.75, 1.75), position=Vec3(0, 0, -3))
player.collider = BoxCollider(player, center=Vec3(player.x, player.y), size=Vec3(1.5, 1.5)) 

I tried to place the if statements to detect collision somewhere else for example inside the for loop where the enemies are declared and spawned:

numbers = [-3, -1,0, 1, 3]
for i in range(0, 10000, 10):
    enemy1 = Sprite("assets/blue.png", scale=(6, 12), position=(Vec3(random.choice(numbers), random.randint(-3, 3)+i, -3)), collider = "box")
    enemy2 = Sprite("assets/red.png", scale=(6, 12), position=(Vec3(random.choice(numbers), random.randint(-3, 3)+i, -3)), collider = "box")
    enemy3 = Sprite("assets/green.png", scale=(6, 12), position=(Vec3(random.choice(numbers), random.randint(-3, 3)+i, -3)), collider = "box")

    if player.intersects(enemy1).hit == True:
            print("collided with enemy1")
    if player.intersects(enemy2).hit == True:
            print("collided with enemy2")
    if player.intersects(enemy3).hit == True:
            print("collided with enemy3")

It still doesnt work. I was expecting it to detect the collision and print "collided with enemyX". It only prints that when I collide with the wall. I have also changed the render mode to see if the colliders actually exist and they do. Please look at the image, the smaller rectangle is the player and the bigger rectangles all around him are the enemies aka the trucks the player needs to avoid. Image to showcase that the colliders exist

Upvotes: 0

Views: 43

Answers (0)

Related Questions