Python龟集开始位置

时间:2013-02-05 17:11:45

标签: python turtle-graphics

启动代码时,如何设置startpos(我的乌龟方形)?

我并不是说它从中间开始然后就到那个位置。

我希望乌龟从那里开始。

5 个答案:

答案 0 :(得分:3)

您可以将世界坐标设置为其他内容。例如,要从左下角附近开始,请执行以下操作:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1>ToDo</h1>
<input type="text" placeholder="Please Enter a Task" id="input">
<input type="submit" value="Add" id="addButton" hidden="hidden">

这将使整个窗口为21x21“单位”,并将原点从左下边缘放置一个单位。你所掌握的任何位置也将是这些单位(而不是像素)。

答案 1 :(得分:2)

将您的坐标系统翻译成乌龟的坐标系统。假设你想从广场的左上角开始 - 为了论证,我们可以调用(0,10)。

现在,每当你需要为乌龟指定一个坐标时,只需翻译它!

my_start = (0, 10)

如果你想转到(10, 10) - 右上角,只需提供新的坐标:

>>> new_position = (10 - my_start[0], 10 - my_start[1])
>>> new_position
(10, 0)

(10, 0)位于龟的东边 - 在乌龟的坐标系中,但对你来说,它是(10, 10)的右上角!每个人都赢了!

修改

你可以做到

turtle.penup()
turtle.setx(my_start[0])
turtle.sety(my_start[1])
turtle.pendown()

但这并不是那么有趣:(

答案 2 :(得分:2)

托马斯·安东尼的setworldcoordinates()解决方案是可行的(+1),但这是一个棘手的功能(容易弄乱你的宽高比。)其他人建议如下:

penup()
goto(...)
pendown()

完全错误和/或没有阅读您的问题,因为您的用户将看到乌龟移动到位。不幸的是,当你说“我的乌龟广场”时,你的问题并不清楚,因为你不清楚你是指窗户,你绘制的正方形,还是你要绘制的正方形。

我会给你解决方案,从窗口的左上角开始,你可以根据自己的需要进行调整:

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

乌龟的第一次出现应该在窗口的左上角。

答案 3 :(得分:1)

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

乌龟变得可见,并开始在-200,-200的位置绘制,并转到50,50。

以下是关于如何改变海龟状态的文档: https://docs.python.org/2/library/turtle.html#turtle-state

答案 4 :(得分:-1)

在代码的开头,在定义了乌龟之后,您可以执行以下操作:

tom.penup()
tom.goto(x coordinate that you want, y coordinate that you want)
tom.pendown()

这将使乌龟不可见。
然后转到x和y给出的位置,这使得海龟的踪迹再次可见。