How do i change the position of a turtle in python?

How do I set the startpos (topleft of my turtle square) when starting my code?

And I don't mean that it starts from the middle and then goes to that position.

I want the turtle to start there.

How do i change the position of a turtle in python?

asked Feb 5, 2013 at 17:11

1

Thomas Antony's setworldcoordinates() solution is viable (+1) but that's a tricky function to work with (easy to mess up your aspect ratio.) The other folks who suggested something like:

penup()
goto(...)
pendown()

are simply wrong and/or didn't read your question as your user will see the turtle move into position. Unfortunately, your question isn't clear when you say, "my turtle square" as it's not clear if you mean the window, a square you've drawn, or a square you're about to draw.

I'll give you my solution for starting at the top left of the window and you can adjust it to your needs:

from turtle import Turtle, Screen

TURTLE_SIZE = 20

screen = Screen()

yertle = Turtle(shape="turtle", visible=False)
yertle.penup()
yertle.goto(TURTLE_SIZE/2 - screen.window_width()/2, screen.window_height()/2 - TURTLE_SIZE/2)
yertle.pendown()
yertle.showturtle()

screen.mainloop()

The turtle's first appearance should be at the top left of the window.

answered Feb 9, 2017 at 6:27

How do i change the position of a turtle in python?

Python turtle, change start position:

import turtle
a = turtle.Turtle()      #instantiate a new turtle object called 'a'
a.hideturtle()           #make the turtle invisible
a.penup()                #don't draw when turtle moves
a.goto(-200, -200)       #move the turtle to a location
a.showturtle()           #make the turtle visible
a.pendown()              #draw when the turtle moves
a.goto(50, 50)           #move the turtle to a new location

The turtle becomes visible and starts drawing at position -200, -200 and goes to 50, 50.

Here's the documentation on how you can change the turtle's state: https://docs.python.org/2/library/turtle.html#turtle-state

answered Jul 3, 2015 at 13:58

How do i change the position of a turtle in python?

Eric LeschinskiEric Leschinski

139k91 gold badges405 silver badges327 bronze badges

0

You can set the world coordinates to something else. For example, to start near the lower left corner, do:

turtle.setworldcoordinates(-1, -1, 20, 20)

This will make the entire window be 21x21 "units" and place the origin one unit from the bottom and left edges. Whatever position you command will also be in terms of these units (rather than pixels).

answered Feb 9, 2017 at 3:16

Thomas AntonyThomas Antony

5041 gold badge6 silver badges16 bronze badges

Just translate your co-ordinate system to that of the turtle. Say you want to start in the top left of the square - lets call that (0, 10) for arguments sake.

Now, whenever you need to specify a co-ordinate for the turtle, just translate it!

my_start = (0, 10)

If you want to move to (10, 10) - the top right corner, just provide the new co-ordinates:

>>> new_position = (10 - my_start[0], 10 - my_start[1])
>>> new_position
(10, 0)

(10, 0) is to the East of the turtle - in the turtle's co-ordinate system, but for you it's (10, 10) the top right! Everyone wins!

Edit

You could just do

turtle.penup()
turtle.setx(my_start[0])
turtle.sety(my_start[1])
turtle.pendown()

but that's not nearly as fun :(

answered Feb 5, 2013 at 17:25

danodonovandanodonovan

18.8k8 gold badges70 silver badges76 bronze badges

1

Several possible pitfalls to take into account:

  • Creating a new Turtle with just Turtle() can make it appear for a split second.
  • Just up-ing the pen doesn't necessarily hide the turtle itself.
  • Similarly, just hiding the turtle can leave its pen stroke visible.
  • Even if everything is invisible, the turtle usually still takes time to get to its position.

Solution that prevents these pitfalls:

t = turtle.Turtle(visible=False)  # make invisible turtle
initial_speed = t.speed()         # store its initial speed
t.speed(0)          # set its movement speed to instant 
t.up()              # stop drawing

t.setpos(x_of_your_starting_position, 
         y_of_your_starting_position)  # figure those out first... 

t.speed(initial_speed)  # set turtle's speed to initial value
t.showturtle()          # make turtle appear in desired position
t.down()                # resume drawing

answered Oct 4, 2021 at 14:51

RautermannRautermann

1952 silver badges9 bronze badges

For that you need to set the turtle coordinates depending upon your screen size for example your screen size is (400, 400) then in this case you need to set coordinates as turtle.goto(190,190) and 190 not 200 otherwise ur turtle will hide in the border.

But for the animation to not appear i.e it starts from the middle and then goes to that position. You have to set tracer as 0 like this "screen.tracer(0)" then you need to update your screen every time to see the contents of your program. For that You will need a while loop. Set a variable as true and use like this:

Ur code should like this after applying the changes I suggested:

from turtle import Screen, Turtle

turtle = Turtle()    #Turtle creation
screen = Screen()    #Screen object creation
screen.tracer(0)     #Tracer to stop "it starts from the middle and then goes to that position."


game_is_on = True
while game_is_on:
    screen.update()  #update to see contents of your screen

answered Aug 2 at 11:39

How do i change the position of a turtle in python?

At the beginning of the code, after defining the turtle, you can do:

tom.penup()
tom.goto(x coordinate that you want, y coordinate that you want)
tom.pendown()

This will make the turtle invisible.
Then go to the position given by x and y, it makes the turtles trail visible again.

סטנלי גרונן

2,84923 gold badges46 silver badges65 bronze badges

answered Feb 23, 2020 at 8:30

How do i change the position of a turtle in python?

ArshamArsham

1411 silver badge9 bronze badges

How do you move the turtle pen in Python?

up() The turtle. up() method is used to pull the pen up from the screen. It gives no drawing on moving to another position or direction.

How do you determine the position of a turtle?

This method is used to find the turtle's current location (x, y), as a Vec2D-vector. This method has the Aliases: pos | position. This function does not require any argument and returns the current position of the turtle in the format (x,y) where x and y represent the 2D vector. The default value is (0.0, 0.0).

How do you align a turtle?

This function is used to write text at the current turtle position. ... turtle. write().