我正在使用海龟模块,我想获取海龟的位置。我查看了文档,但也许只是看不到。
我想知道两只海龟是否在同一位置,所以这样的代码:
from turtle import *
turtle1 = Turtle()
turtle2 = Turtle()
pos1 = ... #Should be turtle1 position
pos2 = ... #SHould be turtle2 position
if pos1 == pos2:
#do stuff
答案 0 :(得分:0)
由于海龟爬浮点平面,所以从长远来看,==
(相等)的比较将无法正常工作。您想要获得两只海龟之间的距离,并确定在您的应用程序中,多近距离代表相同:
from turtle import Screen, Turtle
turtle1 = Turtle()
turtle2 = Turtle()
if turtle1.distance(turtle2) < 5: # pick distance that works for your app
# do stuff