12

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.

1
  • 1
    I think people voted your question down because you started with "school assignment". Nobody wants to do your homework for you. But I will give a hint about this. You can't start in the top left corner, you have to move the turtle there. You can cheat though, and set the pen to either size 0 or same color as background, then move it to the starting point you want and set the pen size or color back to what you want.
    – Davos
    Commented Mar 25, 2015 at 5:46

10 Answers 10

14

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.

11

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

9

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

0
2

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

1
  • 1
    The OP said, "I don't mean that it starts from the middle and then goes to that position". I don't see how either of your approaches avoids the turtle first appearing in middle of the screen and then visibly moving to the actual starting position. Your second approach is doubly bad as you potentially see two moves, since you didn't simply use goto(), first the x position shift and then the y position shift! OP doesn't want his user to see either.
    – cdlane
    Commented Feb 9, 2017 at 6:35
0

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
0

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
1
  • Your while loop just slams the CPU as hard as possible. Better to throttle the framerate with ontimer so it's reasonably regular, not just "as fast as this computer can run".
    – ggorlen
    Commented Nov 7, 2022 at 2:15
0

I don't think it has been said here but turtle.teleport() was what worked for me.

you can set its x and y. So for my code I did tim.teleport(0, 100). This kept my turtle in the center but moved it up 100 pixels.

-1

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.

-1

You have to use the penup function. You would then put the turtle up so that it does not draw and put it in the top left or wherever else you want it and then use the pendown function. It looks like this:

turtle.penup()

turtle.left(180)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.pendown()
-1
  1. Get your window's height and width with the functions provided by the turtle package
  2. print them out in order to know them
  3. Use the goto function (providing x and y coordinates) to specify the location you want your turtle to be at

In My case the widow's height was 675 and the window's width was 720 and i wanted it to be at the bottom left corner As the turtle starts in the middle to the left bottom corner would be at (-) window's width/2 (-360) and (-) window's height/2 (-337.5) for a little offset I put up the x as -320 and the height as -280

from turtle import Turtle, Screen

tim = Turtle()

screen = Screen()
print(f"Screen Width: {screen.window_width()}")
print(f"Screen Width: {screen.window_height()}")

tim.penup()
tim.goto(-320,-280)
tim.pendown()

screen.exitonclick()

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