简体   繁体   English

Pygame 碰撞检测`colliderect()`

[英]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.在浏览pygame的文档时,有一个名为colliderect()的 pygame 方法用于测试两个rect对象是否重叠。

colliderect() test if two rectangles overlap colliderect() 测试两个矩形是否重叠

colliderect(Rect) -> bool碰撞(矩形)-> 布尔

Returns true if any portion of either rectangle overlap (except the top+bottom or left+right edges).如果任一矩形的任何部分重叠(顶部+底部或左+右边缘除外),则返回 true。

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 :这意味着矩形r1r2

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 . r1.colliderect(r2)返回False ,即使r1.left + r1.width等于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).这是因为r1覆盖了从 0 到 9(含)的 (x) 区域,而r2覆盖了从 10 到 19(含)的 (x) 区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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