Python:与椭圆形碰撞

时间:2016-02-05 23:36:57

标签: python collision-detection

我以前从未在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)

如何计算两个椭圆形,矿井和次椭圆之间的距离?如果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()

1 个答案:

答案 0 :(得分:0)

我制作了一个程序,可以感知python中的碰撞检测,这里是:

if oval 1 x < oval 2 x + oval 1 width and oval 1 x + oval 2 width > oval 2 x and oval 1 y < oval 2 y + oval 1 height and oval 2 height + oval 1 y > oval 2 y:
    #put the code that deletes the oval here

这可以通过将椭圆形放在一个假想的框中来实现。并检测第一个&#34;虚构框中的任何边缘是否为#34;正在接触第二个&#34;想象框&#34;的任何边缘。我希望这会有所帮助。