【发布时间】:2016-08-20 05:09:33
【问题描述】:
我以前从未用 python 编写过,但我正在尝试进行碰撞检测,因为当两个椭圆发生碰撞时,其中一个椭圆(气泡/我的)将被删除。
def delete_bubble(k):
bubble_id[k].remove
bubble_r[k].remove
def get_dist(mine,sub):
x = c.coords(mine)
a = c.coords(sub)
#compare coordinates and if same, return 0
def collide():
for k in range(len(bubble_id)):
x = get_dist(bubble_id[k],ship_c)
if x == 0:
delete_bubble(k)
我如何计算两个椭圆之间的距离,我的和 sub?如果 x == a 则返回 0?或者我需要写一个距离公式来计算,还是我需要找到每个椭圆的中心并进行比较?我也有每个椭圆的半径,但我对如何写这个感到困惑。 由于这是交互式游戏的一部分,我需要不断检查碰撞,我将如何在 main 中实现它:
#main game loop
for x in range(10):
create_mines(c)
window.after(40, move_mines, c)
window.after(10, collide) #does this work?
window.mainloop()
【问题讨论】:
-
你需要距离:毕达哥拉斯
a^2 + b^2 = c^2,其中c是距离,a = x1-x2,b = y1-y2。然后您可以比较c <= r1+r2或c^2 <= (r1+r2)^2而不必使用square root -
它应该在 move_mines 中,因为您希望每次移动时都检查一下。
标签: python collision-detection