【问题标题】:Is there a way to make collision between shapes in PyGame using colors?有没有办法使用颜色在 PyGame 中的形状之间进行碰撞?
【发布时间】:2020-07-26 12:50:15
【问题描述】:

当形状接触特定颜色时,有没有办法在 pygame 中进行碰撞。比如当一个形状以某种方式被控制时(例如:一个弹力球、一个移动的形状等),是否有办法检测到它是否接触了特定类型的颜色?

【问题讨论】:

  • 不知道你的推理,很难提出建议。如果对象具有静态颜色(例如:总是绿色),您可以将它们拆分为单独的列表并仅对您感兴趣的颜色进行碰撞测试。对于动态颜色,我会使用常规颜色几何碰撞,然后检查当前颜色,看看是否仍然算作“命中”。

标签: python pygame collision-detection


【解决方案1】:

是的,你可以,这里是 sentdex 的一个例子 链接:https://pythonprogramming.net/detecting-collisions-intermediate-python-tutorial/

def is_touching(b1,b2):
    return np.linalg.norm(np.array([b1.x,b1.y])-np.array([b2.x,b2.y]))< (b1.size + b2.size)


def handle_collisions(blob_list):
    blues, reds, greens = blob_list
    for blue_id, blue_blob in blues.copy().items():
        for other_blobs in blues, reds, greens:
            for other_blob_id, other_blob in other_blobs.copy().items():
                if blue_blob == other_blob:
                    pass
                else:
                    if is_touching(blue_blob, other_blob):
                        blue_blob + other_blob
                        if other_blob.size <= 0:
                            del other_blobs[other_blob_id]
                        if blue_blob.size <= 0:
                            del blues[blue_id]
                            
    return blues, reds, greens

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    相关资源
    最近更新 更多