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?
-
Have you tried using setx and sety?– FritoCommented Nov 13, 2017 at 4:41
3 Answers
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()
-
Just because i could find it in the manpage by searching for the string:
ht = hideturtle(self)
, andst = showturtle(self)
. same goes forup = penup
anddown = pendown
.– mazunkiCommented Dec 13, 2022 at 16:12 -
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()
Sources:
.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.