Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python turtle set start position

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.

like image 713
Wouter Vandenputte Avatar asked Feb 05 '13 17:02

Wouter Vandenputte


1 Answers

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.

like image 92
cdlane Avatar answered Sep 19 '22 16:09

cdlane