如何获得乌龟的位置?

How to get the position of the turtle?

如何找到乌龟在python中的坐标?

例如,如果海龟位于 (200, 300),我将如何检索该位置?

turtle documentation says that turtle.pos() gets the position as a Vec2D 向量。

I wanted to know if turtle1 and turtle2 were on the same coordinate I would do if turtle1.pos() == turtle2.pos()

是的,但是由于乌龟在浮点平面上玩耍,因此当它们靠近但不是完全重叠时,这可能会给您带来麻烦。你可能会更好地进行这样的测试:

turtle1.distance(turtle2) < 0.1

即是中心之间的距离小于您确定的某个模糊因子。如果您想知道乌龟的任何部分是否重叠(例如腿接触),那么您的模糊因子可能高达 10.0

如 Python 文档所示,turtle.pos() returns 乌龟的当前位置作为 (x,y) 的二维向量。

那么 turtle.pos() 方法 returns 一个元组

A Vector is derived from tuple, so a vector is a tuple!

因此您可以使用以下方法检查乌龟是否在特定坐标处:

if turtle.pos() == (5.0, 0.0):
    print(“at coordinate”)