3

I want to know how to make Turtle go to an (x, y) coordinate without having it move across the screen. I tried the .goto() and .setposition() methods, but they both make the turtle move across the screen. How do I do this?

1
  • Have you tried using setx and sety?
    – Frito
    Commented Nov 13, 2017 at 4:41

3 Answers 3

1

You can use pen up, then set the turtle invisible, then use goto to move the turtle to your position.

turtle.up()
turtle.ht()
turtle.goto(x, y)

# reshow the turtle
turtle.st()
turtle.down()
2
  • Just because i could find it in the manpage by searching for the string: ht = hideturtle(self), and st = showturtle(self). same goes for up = penup and down = pendown.
    – mazunki
    Commented Dec 13, 2022 at 16:12
  • This doesn't change the speed.
    – ggorlen
    Commented Mar 18 at 18:11
0

You can disable movement animations by setting turtle.speed(0). This will make the Turtle move instantaneously across the screen.

import turtle

turtle.setup(400, 300)  # set up canvas

my_object = turtle.Turtle()  # create object
my_object.speed(0)  # configure object to move without animations
my_object.goto(-150, 0)  # move object

turtle.done()

A thin black line originating from the center of the window towards an arrow on the left

Sources:

0

.speed(0) still takes time to move the turtle, and hiding/showing the turtle doesn't change the overall delay.

The only way to move the turtle somewhere instantaneously (in a single frame) is to disable tracer, which is turtle's internal update loop, and use .update() to trigger a redraw after repositioning the turtle. You can re-enable tracer if you want after the move.

Here's an example showing 100 gotos performed in a single frame:

from random import randint
from turtle import Screen, Turtle


screen = Screen()
screen.tracer(0)  # disable turtle's update loop
w, h = screen.screensize()
t = Turtle()

for _ in range(100):
    t.goto(randint(-w, w), randint(-h, h))

screen.update()  # draw the frame

screen.tracer(1)  # optionally re-enable the internal update loop
t.goto(randint(-w, w), randint(-h, h))

screen.exitonclick()

You can use t.penup() to avoid the lines, and you can use t.hideturtle() if you don't want to see the turtle.

If you're trying to make a real-time game, animation or app where precise control and user input is necessary, see this more involved demo.

Not the answer you're looking for? Browse other questions tagged or ask your own question.