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 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.
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.
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).
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
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!
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 :(
Several possible pitfalls to take into account:
Turtle()
can make it appear for a split second.up
-ing the pen doesn't necessarily hide the turtle itself.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
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
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()
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()
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.