Wou*_*tte 1 python turtle-graphics
启动代码时如何设置startpos(乌龟方块的左上角)?
我的意思不是说它从中间开始然后到那个位置。
我要乌龟从那里开始。
托马斯安东尼的setworldcoordinates()解决方案是可行的(+1),但这是一个棘手的功能(容易弄乱你的纵横比。)其他人提出了类似的建议:
penup()
goto(...)
pendown()
Run Code Online (Sandbox Code Playgroud)
完全错误和/或没有阅读您的问题,因为您的用户会看到海龟移动到位。不幸的是,当您说“我的乌龟方块”时,您的问题并不清楚,因为不清楚您是指窗户、您绘制的方块还是您将要绘制的方块。
我将在窗口左上角为您提供我的解决方案,您可以根据需要对其进行调整:
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()
Run Code Online (Sandbox Code Playgroud)
海龟的第一次出现应该在窗口的左上角。
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
Run Code Online (Sandbox Code Playgroud)
海龟变得可见并从位置 -200、-200 开始绘制,然后转到 50、50。
这是有关如何更改海龟状态的文档:https : //docs.python.org/2/library/turtle.html#turtle-state
您可以将世界坐标设置为其他坐标。例如,要从左下角开始,请执行以下操作:
turtle.setworldcoordinates(-1, -1, 20, 20)
Run Code Online (Sandbox Code Playgroud)
这将使整个窗口为21x21“单位”,并将原点从底部和左侧边缘放置一个单位。您命令的任何位置也将以这些单位(而不是像素)为单位。
只需将您的坐标系转换为海龟的坐标系即可。假设您想从正方形的左上角开始 - 为了论证起见,我们将其称为 (0, 10)。
现在,每当您需要为海龟指定坐标时,只需翻译它即可!
my_start = (0, 10)
Run Code Online (Sandbox Code Playgroud)
如果你想移动到(10, 10)- 右上角,只需提供新的坐标:
>>> new_position = (10 - my_start[0], 10 - my_start[1])
>>> new_position
(10, 0)
Run Code Online (Sandbox Code Playgroud)
(10, 0)位于海龟的东边——在海龟的坐标系中,但对你来说它是(10, 10)右上角!每个人都赢了!
你可以这样做
turtle.penup()
turtle.setx(my_start[0])
turtle.sety(my_start[1])
turtle.pendown()
Run Code Online (Sandbox Code Playgroud)
但这并不那么有趣:(
| 归档时间: |
|
| 查看次数: |
46830 次 |
| 最近记录: |