pythonpygamecollision-detectioncollision

Know which rect has been collided in a collidelist


I am trying to find a specific rect in a collidelist when my bullet touches the rect. I have 92 rects, and each of them are given a type : "Opaque", "Transparent". When I create the rects at the launch of the game, I place them in a list, to after test the collidelist. And an other list which contains all the types in order. Like

collide_wall_list = [rect,rect,rect,rect]
#Each rect is a pygame.draw.rect

wall_type_list = ["Opaque","Opaque","Opaque","Transparent"]

They are created in order, so the 2nd rect is the 2nd type.

if new_bullet_list[n].collidelist(collide_wall_list) != -1:

This is to test if any of my bullet is colliding with the list of rects. Now, the problem is that with the collidelist, we can't know WHICH of the rects are being collided. Do you have any idea how to know which of the rects are being collided ? And then for the type it will be easy, because the rects and the types are in the same position.


Solution

  • collidelist returns the index of the first collision, or -1 if no collision occurs:

    "Test whether the rectangle collides with any in a sequence of rectangles. The index of the first collision found is returned. If no collisions are found an index of -1 is returned."

    http://www.pygame.org/docs/ref/rect.html