Pick up pen when turtle is not at coordinate

一笑奈何 提交于 2019-12-12 16:19:57

问题


My program draws images that already have coordinates attach to them. I want my turtle to be able to pick up the pen when not at the coordinate. Right now the turtle continues to write before getting to the coordinate.

code:

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)

output

1:

As you can see the turtle continues to draw even before getting to the coordinate.

Here's something I think may work but I'm not sure how to implement it.

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")

回答1:


I created a function that detected whether the position of the turtle was in the list of coordinates. This function was then called every millisecond using the ontimer function. I also had to slow down my turtle in order for the program to check the position within a millisecond

code:

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()




回答2:


Maybe try picking up the pen before you move and putting it down after:

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()


来源:https://stackoverflow.com/questions/56897234/pick-up-pen-when-turtle-is-not-at-coordinate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!