Python turtle set start position

风流意气都作罢 提交于 2019-12-19 11:33: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:


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




回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/14713037/python-turtle-set-start-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!