4
votes

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.

5
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

5 Answers

8
votes

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.

7
votes

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

4
votes

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

2
votes

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
votes

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.