'Pygame Collision detection `colliderect()`
While going through the docs of pygame, there was a method of pygame called colliderect()
that is used to test if two rect
objects have overlapped.
colliderect() test if two rectangles overlap
colliderect(Rect) -> bool
Returns true if any portion of either rectangle overlap (except the top+bottom or left+right edges).
In the last line , it said except the top+bottom or left+right
What does this statement mean?
Solution 1:[1]
It means that the rectangles r1
and r2
:
r1 = pygame.Rect(0, 0, 10, 10)
r2 = pygame.Rect(10, 0, 10, 10)
are not colliding. r1.colliderect(r2)
returns False
, even though r1.left + r1.width
is equal r2.right
.
That is because the r1
covers the (x) region from 0 to 9 (inclusive) and r2
covers the (x) region from 10 to 19 (inclusive).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rabbid76 |