【问题标题】:How to get position of turtles ? (turtle module python)如何获得海龟的位置? (海龟模块python)
【发布时间】:2019-10-16 11:59:12
【问题描述】:

我正在使用海龟模块,我想获取海龟的位置。我查看了文档,但也许我只是没有看到它。

我想知道两只海龟是否在同一个位置,所以这样的代码:

from turtle import *

turtle1 = Turtle()
turtle2 = Turtle()

pos1 = ... #Should be turtle1 position
pos2 = ... #SHould be turtle2 position
if pos1 == pos2:
    #do stuff

【问题讨论】:

标签: python python-3.x turtle-graphics


【解决方案1】:

由于海龟在浮点平面上爬行,从长远来看,==(相等)比较不会很好地工作。您想获得两个海龟之间的距离,并决定您的应用程序的距离表示相同

from turtle import Screen, Turtle

turtle1 = Turtle()
turtle2 = Turtle()

if turtle1.distance(turtle2) < 5:  # pick distance that works for your app
    # do stuff

【讨论】:

    最近更新 更多