Python海龟绘图设置起始位置

12

当我开始编写代码时,如何设置海龟方块的起始位置(左上角)?

我的意思不是它从中间开始然后移动到那个位置。

我想让海龟从那个位置开始。


1
我认为人们投票反对你的问题,是因为你以“学校作业”开头。没有人想替你做功课。但是我会给你一个提示。你不能从左上角开始,你必须移动海龟到那里。不过你可以作弊,将笔的大小设置为0或与背景颜色相同,然后将其移动到你想要的起始点,并将笔的大小或颜色重新设置为你想要的。 - Davos
9个回答

14

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

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


10
您可以将世界坐标设置为其他值。例如,要从靠近左下角开始,请执行以下操作:
turtle.setworldcoordinates(-1, -1, 20, 20)

这将使整个窗口的大小为21x21"单位",并将原点定位在底部和左侧各一单位的位置。你所指定的任何位置也将以这些单位为基准(而不是像素)。


9

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


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

但这样做没有那么有趣 :(

1
原帖作者说:“我的意思不是从中间开始,然后到达那个位置。” 我不明白你们两种方法如何避免乌龟首先出现在屏幕中央,然后再移动到实际的起始位置。你的第二种方法是更糟糕的,因为你可能会看到两次移动,因为你没有简单地使用goto(),首先是x位置的移动,然后是y位置的移动!原帖作者不希望他的用户看到任何一个移动。 - cdlane

0

为此,您需要根据屏幕大小设置海龟坐标。例如,如果您的屏幕大小为(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

0

需要注意的几个潜在问题:

  • 只使用 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

0

你需要使用 penup 函数。这样你可以把海龟抬起来,然后它就不会画出任何东西,你可以把它放在左上角或者其他你想让它停留的位置,然后再使用 pendown 函数。代码如下:

turtle.penup()

turtle.left(180)

turtle.forward(100)

turtle.right(90)

turtle.forward(100)

turtle.pendown()

0
使用turtle包提供的函数获取窗口的高度和宽度 打印出来以了解它们 使用goto函数(提供x和y坐标)来指定你希望乌龟所在的位置
在我的情况下,窗口的高度是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()

-1
在代码开头,在定义海龟之后,你可以这样做:
tom.penup()
tom.goto(x coordinate that you want, y coordinate that you want)
tom.pendown()

这将使海龟不可见。
然后前往给定的x和y位置,它会再次使海龟的轨迹可见。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接