python 乌龟与自己的图纸相撞

Turtle collision with its own drawings in python

我想知道乌龟是否和以前一样在同一个地方(python中的迷宫游戏)。 我试过了:

import turtle
c == 1
a = turtle.Turtle()
b = turtle.Turtle()
b.fd(100)
while c == 1:
  a.fd(10)
if a.is_collided_with(b):
  print("Collision")
break

问题是它说没有 .is_collided_with() 这样的东西(可能是因为我使用的是 python 3.9.1)。 有没有办法不用几只乌龟就可以做到这一点? 谢谢

忽略周围的代码,我们真正想要的是:

if a.distance(b) < 5:
    print("Collision")

当海龟在它们的浮点平面上游荡时,彼此正对的两只海龟可能不在相同的精确坐标上。 5 值可以设置为您认为碰撞的接近程度。

distance() 而言,它可以像您的示例中那样使用另一只乌龟,也可以使用已访问位置列表中的位置。

I want something that detects a collision with the turtle drawing.

这里有一个 方法是让墙本身变成乌龟(虽然它们看起来像线)并使用上面的距离函数来检测碰撞。