Skip to content
Advertisement

Pygame collision checking lists of hit boxes against each other only checks the last box in the list

I am coding a game in pygame and my entity objects have attribute lists of hit boxes.

All the hit boxes are pygame.rect objects and when i have them drawn on the screen all of them show up but for some reason the objects only act like they collide if the last hit box in their list collides with the last hit box in the other objects list.

Here is the method I use for the check:

JavaScript

Anyone have an idea what I’m doing wrong here or if this is even possible using only pygame.rect objects and pygame.rect.collide_rect?

Advertisement

Answer

I can’t test it but problem can be because break can exit only last/inner for-loop – so code still runs outer for-loop which can set again is_collide = False (because it runs is_collide = box.colliderect(obj_box)). And finally it gets is_collide = True only if last boxes collides.

You should check collision without assigning to variable and assign variable only inside if`

JavaScript

instead of

JavaScript

Ot put both for-loops in separated function and use return True instead of break

JavaScript

EDIT:

You could use return directly in original function

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement