当我开始编写代码时,如何设置海龟方块的起始位置(左上角)?
我的意思不是它从中间开始然后移动到那个位置。
我想让海龟从那个位置开始。
当我开始编写代码时,如何设置海龟方块的起始位置(左上角)?
我的意思不是它从中间开始然后移动到那个位置。
我想让海龟从那个位置开始。
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"单位",并将原点定位在底部和左侧各一单位的位置。你所指定的任何位置也将以这些单位为基准(而不是像素)。
Python turtle, 更改起始位置:
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()
goto()
,首先是x位置的移动,然后是y位置的移动!原帖作者不希望他的用户看到任何一个移动。 - cdlane为此,您需要根据屏幕大小设置海龟坐标。例如,如果您的屏幕大小为(400,400),则在这种情况下,您需要将坐标设置为turtle.goto(190,190),而不是200,否则您的海龟会隐藏在边框中。
但是为了使动画看起来不出现任何问题,也就是说,它从中间开始并到达那个位置时看起来自然,您需要将tracer设置为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
while
循环会让 CPU 负荷过重。最好使用 ontimer
来控制帧率,使其保持合理的稳定性,而不是“尽可能快地运行此计算机”。 - ggorlen需要注意的几个潜在问题:
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
你需要使用 penup
函数。这样你可以把海龟抬起来,然后它就不会画出任何东西,你可以把它放在左上角或者其他你想让它停留的位置,然后再使用 pendown
函数。代码如下:
turtle.penup()
turtle.left(180)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.pendown()
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()
tom.penup()
tom.goto(x coordinate that you want, y coordinate that you want)
tom.pendown()
这将使海龟不可见。
然后前往给定的x和y位置,它会再次使海龟的轨迹可见。