当海龟不在坐标上时拿起笔

Pick up pen when turtle is not at coordinate

我的程序绘制的图像已经附加了坐标。我希望我的乌龟在不在坐标处时能够拿起笔。现在乌龟在到达坐标之前继续写。

代码:

with open('output.txt', 'r') as f:
    data = ast.literal_eval(f.read())

tony = turtle.Turtle()

for z in data:
    position = tony.pos()
    tony.goto(z)

输出

1:

如您所见,乌龟甚至在到达坐标之前就继续绘制。

这里有一些我认为可能有用的东西,但我不确定如何实现它。

for z in data:
     position = tony.pos()
     while position in z == False:
         tony.penup()

for z in data:
     position = tony.pos()
     while position in z == True:
        tony.pendown()
        print("True")

也许尝试在移动之前拿起笔并在之后放下:

with open('output.txt', 'r') as f:
    data = ast.literal_eval(f.read())

tony = turtle.Turtle()

for z in data:
    tony.penup()
    tony.goto(z)
    tony.pendown()

我创建了一个函数来检测乌龟的位置是否在坐标列表中。然后使用 ontimer 函数每毫秒调用一次此函数。我还不得不放慢我的乌龟速度,以便程序在毫秒内检查位置

代码:

tony = turtle.Turtle()
tony.color("white", "cyan")
tony.speed(5.5)

def on_canvas():
    position = tony.pos()
    if position in data:
        tony.pendown()
        print("This is a coordinate")
    else:
        tony.penup()
        print("This is not a coordinate")


for z in data:
    playground.ontimer(on_canvas, 1)
    tony.goto(z)

turtle.done()