Python乌龟设置起始位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14713037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python turtle set start position
提问by Wouter Vandenputte
How do I set the startpos (topleft of my turtle square) when starting my code?
启动代码时如何设置 startpos(乌龟方块的左上角)?
And I don't mean that it starts from the middle and then goes to that position.
我并不是说它从中间开始,然后到那个位置。
I want the turtle to start there.
我希望乌龟从那里开始。
回答by danodonovan
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.
只需将您的坐标系转换为海龟的坐标系即可。假设你想从正方形的左上角开始 - 为了参数的缘故,让我们称之为 (0, 10) 。
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:
如果你想移动到(10, 10)- 右上角,只需提供新的坐标:
>>> 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!
(10, 0)在乌龟的东边——在乌龟的坐标系中,但对你来说它是(10, 10)右上角!每个人都赢了!
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 :(
但这并没有那么有趣:(
回答by Eric Leschinski
Python turtle, change start position:
Python乌龟,改变起始位置:
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.
海龟变得可见并从位置 -200、-200 开始绘制,然后转到 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: //docs.python.org/2/library/turtle.html#turtle-state
回答by Thomas Antony
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).
这将使整个窗口为 21x21“单位”,并将原点放置在距底部和左侧边缘一个单位的位置。您命令的任何位置也将使用这些单位(而不是像素)。
回答by cdlane
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:
托马斯安东尼的setworldcoordinates()解决方案是可行的(+1),但这是一个棘手的功能(容易弄乱你的纵横比。)其他人提出了类似的建议:
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.
海龟的第一次出现应该在窗口的左上角。
回答by Arsham
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.
这将使乌龟不可见。
然后转到 x 和 y 给定的位置,它使海龟的踪迹再次可见。

