如何使用John Zelle的graphics.py
模块检查两个框之间的冲突
编辑:
我已经找到了一种方法来找到任意两个类之间的碰撞,它们有x,y,width,height。虽然有点凌乱,但它确实起到了作用:
def collided(self, collider):
if self.x < collider.x + collider.width and\
self.x + self.width > collider.x and\
self.y < collider.y + collider.height and\
self.y + self.height > collider.y:
return True
return False
如果有人有更好的方法,我们将不胜感激
这里有一个小的演示程序,演示了一种相当简洁的方法——它包含一个名为
intersect()
的函数,用于检查graphics
模块的Rectangle
类的两个实例是否相交这样做有点棘手,因为用于定义
Rectangle
的两个Point
不需要是它的右下角和左上角,intersect()
函数中的逻辑需要这两个角。为了处理这个问题,使用一个名为canonical_rect()
的helper函数使它传递的每个参数都是这样相关问题 更多 >
编程相关推荐