3

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:enter image description here

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")
yemi.JUMP
  • 313
  • 2
  • 12

2 Answers2

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

enter image description here

Community
  • 1
  • 1
yemi.JUMP
  • 313
  • 2
  • 12
0

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()
Alex
  • 963
  • 9
  • 11
  • maybe try adding a signal to pickup and put down the pen or try calculating the distance and picking up the pen if it is within a threshold – Alex Jul 05 '19 at 06:16