问题
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