Pygame碰撞器提供了比Pygame模块标准的Rect矩形碰撞检测更复杂的功能。
虽然名字是Pygame碰撞器,但它并不和Pygame模块绑定,即不依赖于Pygame模块,
也和Pygame模块没啥关系。你尽管应用它于tkinter模块或其它地方。
安装这个模块的方法是在命令提示符下输入 pip install pygame-colliders 进行安装。
这个库提供了两种碰撞器,一个是convex即凸边形碰撞器,另一种是concave凹边形碰撞器。
下面是一个例子
collider_a_points = [(13, 10), (13, 3), (6, 3), (6, 10)] collider_a = create_collider(collider_points) # 创建碰撞器 collider_b_points = [(3, 3), (5, 3), (5, 4), (4, 4), (4, 5), (5, 5), (5, 6), (3, 6)] collider_b = create_collider(collider_points) if collider_a.collide(collider_b): print("Collision detected")
上面的程序没有指定是使用哪种碰撞器。下面的代码指定了凸碰撞器。
collider_points = [(13, 10), (13, 3), (6, 3), (6, 10)] collider = ConvexCollider(collider_points)
凹边形是一个或者多个内角大于180度的多边形。它可以分解为多个凸边形。
如果实例化凹碰撞器,最终也是调用凸边形的碰撞检测。下面是创建一个凹碰撞器。
collider_points = [(3, 3), (5, 3), (5, 4), (4, 4), (4, 5), (5, 5), (5, 6), (3, 6)] collider = ConcaveCollider(collider_points)
上面的代码会创建一像C形状的碰撞器。
下面是test程序:
from pygame_colliders import ConcaveCollider, ConvexCollider def test_no_collision(): poly_a_points = [(13, 10), (13, 3), (6, 3), (6, 10)] poly_b_points = [(14, 18), (15, 11), (10, 13)] poly_a = ConvexCollider(poly_a_points) poly_b = ConvexCollider(poly_b_points) assert poly_a.collide(poly_b) is False def test_collision_2(): collider_a_points = [(13, 20), (13, 13), (6, 13), (6, 20)] collider_b_points = [(13, 13), (8, 9), (7, 15)] collider_a = ConvexCollider(collider_a_points) collider_b = ConvexCollider(collider_b_points) assert collider_a.collide(collider_b) is True def test_collision(): poly_a = [(11, 10), (11, 3), (4, 3), (4, 10)] poly_b = [(13, 13), (8, 9), (7, 15)] p_a = ConvexCollider(poly_a) p_b = ConvexCollider(poly_b) assert p_a.collide(p_b) is True def test_clockwise(): poly = [(13, 13), (8, 9), (7, 15)] p = ConvexCollider(poly) assert p.is_clockwise is True def test_counter_clockwise(): poly = [(7, 15), (8, 9), (13, 13)] p = ConvexCollider(poly) assert p.is_clockwise is False def test_point_collide(): poly_points = [(7, 15), (8, 9), (13, 13)] point = (10, 12) poly = ConvexCollider(poly_points) assert poly.point_collide(point) is True def test_point_not_collide(): poly_points = [(7, 15), (8, 9), (13, 13)] point = (10, 7) poly = ConvexCollider(poly_points) assert poly.point_collide(point) is False def test_concave_convex_collision(): poly_a_points = [(3, 3), (5, 3), (5, 4), (4, 4), (4, 5), (5, 5), (5, 6), (3, 6)] poly_b_points = [(4.5, 3.5), (6, 2), (6, 4)] poly_a = ConcaveCollider(poly_a_points) poly_b = ConvexCollider(poly_b_points) assert poly_a.collide(poly_b) is True def test_convex_concave_collision(): poly_a_points = [(3, 3), (5, 3), (5, 4), (4, 4), (4, 5), (5, 5), (5, 6), (3, 6)] poly_b_points = [(4.5, 3.5), (6, 2), (6, 4)] poly_a = ConcaveCollider(poly_a_points) poly_b = ConvexCollider(poly_b_points) assert poly_b.collide(poly_a) is True def test_concave_no_collision(): poly_a_points = [(3, 3), (5, 3), (5, 4), (4, 4), (4, 5), (5, 5), (5, 6), (3, 6)] poly_b_points = [(6.5, 3.5), (8, 2), (8, 4)] poly_a = ConcaveCollider(poly_a_points) poly_b = ConvexCollider(poly_b_points) assert poly_a.collide(poly_b) is False def test_concave_concave_collision(): poly_a_points = [(3, 3), (5, 3), (5, 4), (4, 4), (4, 5), (5, 5), (5, 6), (3, 6)] poly_b_points = [(6.5, 5.5), (4.5, 5.5), (4.5, 6.5), (5.5, 6.5), (5.5, 7.5), (4.5, 7.5), (4.5, 8.5), (6.5, 8.5)] poly_a = ConcaveCollider(poly_a_points) poly_b = ConcaveCollider(poly_b_points) assert poly_a.collide(poly_b) is True
免费下载网址:
链接:https://pan.baidu.com/s/1Q5lek0W3Qqn7ehYVFcjbQw
提取码:px7m
发表评论