启动代码时如何设置 startpos(海龟方块的左上角)?
我并不是说它从中间开始然后到那个位置。
我希望乌龟从那里开始。
Thomas Antony 的
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()
海龟第一次出现应该在窗口的左上角。
您可以将世界坐标设置为其他值。例如,要从左下角附近开始,请执行以下操作:
turtle.setworldcoordinates(-1, -1, 20, 20)
这将使整个窗口成为 21x21“单位”,并将原点放置在距底部和左边缘 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
只需将您的坐标系转换为海龟的坐标系即可。假设您想从正方形的左上角开始 - 为了论证起见,我们将其称为 (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()
但这并不那么有趣:(
需要考虑的几个可能的陷阱:
Turtle()
创建一只新海龟就可以让它瞬间出现。up
-用笔不一定能隐藏乌龟本身。防止这些陷阱的解决方案:
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
为此,您需要根据您的屏幕尺寸设置海龟坐标,例如您的屏幕尺寸是 (400, 400) 那么在这种情况下您需要将坐标设置为turtle.goto(190,190) 和 190 而不是 200 否则您的海龟将躲在边界里。
但是动画不会出现,即它从中间开始,然后转到该位置。您必须将跟踪器设置为 0,如“screen.tracer(0)”,然后您需要每次更新屏幕才能查看程序的内容。为此,您将需要一个 while 循环。将变量设置为 true 并像这样使用:
应用我建议的更改后,您的代码应该像这样:
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
您必须使用
penup
功能。然后,您可以将海龟放在上面,这样它就不会绘制,并将其放在左上角或任何您想要的位置,然后使用 pendown
函数。看起来像这样:
turtle.penup()
turtle.left(180)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.pendown()
在我的例子中,寡妇的高度是 675,窗口的宽度是 720,我希望它位于左下角 由于海龟从中间开始,左下角将位于 (-) 窗口宽度/2 (-360) 和 (-) 窗口高度/2 (-337.5) 为了一点点偏移,我将 x 设置为 -320,将高度设置为 -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()
我认为这里没有说过,但turtle.teleport()对我有用。
您可以设置它的x和y。所以对于我的代码,我做了 tim.teleport(0, 100)。这使我的乌龟保持在中心,但将其向上移动了 100 像素。
在代码的开头,定义乌龟后,你可以这样做:
tom.penup()
tom.goto(x coordinate that you want, y coordinate that you want)
tom.pendown()
这将使海龟隐形。
然后转到 x 和 y 给定的位置,这使得海龟的踪迹再次可见。