简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM