有没有一种方法可以使用颜色在PyGame中使形状之间发生冲突



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

是的,这里有一个来自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

最新更新