Python: 乌龟 goto() 命令

Python: turtle goto() command

我的作业要求我编写一些大小从 20-80 不等的嵌套方块的图形表示,如下所示:

创建第一个方块后,我需要将位置移动到下一个方块的开头。为此,我使用 goto() 命令。我的问题是 goto() 命令,因为我对水平和垂直输入使用了两个变量,但一次只有其中一个起作用——我需要它们都起作用。任何帮助将不胜感激。

#Draw a set of nested squares, increasing in size
from turtle import *

number_of_shapes = 4

for shape in range(1, number_of_shapes + 1):
    #Draw a square
    for sides in range(1,5):
        forward(20 + shape * 10)
        right(90)

#Move to position of next square
    penup()
    goto(shape * 10, shape * 10)
    pendown()

Python 的一大优点是可以很容易地在解释器中测试小段代码。让我们使用解释器来检查边的长度:

In [82]: number_of_shapes = 4

In [83]: for shape in range(1, number_of_shapes + 1):
   ....:     print(20 + shape * 10)
30
40
50
60

糟糕。现在我们可以清楚地看到 forward(20 + shape * 10) 不会使边长为 20、40、60 和 80。请注意 (20 + shape * 10) 使边长增加 10。我们想要增加 20 ,所以使用 (20 + shape * 20) 代替:

In [84]: for shape in range(1, number_of_shapes + 1):
   ....:     print(20 + shape * 20)
   ....: 
40
60
80
100

糟糕,我听错了。没问题,我们只需要将整个值减 20:

In [85]: for shape in range(1, number_of_shapes + 1):
   ....:     print(shape * 20)
   ....: 
20
40
60
80

啊,好多了。


所以现在代码如下所示:

import turtle

number_of_shapes = 4

for shape in range(1, number_of_shapes+1):
    #Draw a square
    for sides in range(4):
        turtle.forward(20 + shape * 20)
        turtle.right(90)

    #Move to position of next square
    turtle.penup()
    turtle.goto(shape * 10, shape * 10)
    turtle.pendown()
turtle.mainloop()

当你运行这段代码时,注意海龟从左上角开始每个方块 并开始向右绘图。


现在让我们考虑一下 goto(shape * 10, shape * 10) 语句。它要带我们去哪里?让我们用解释器找出来:

In [87]: for shape in range(1, number_of_shapes + 1):
   ....:     print(shape * 10, shape * 10)
   ....: 
10 10
20 20
30 30
40 40

将这些坐标与上面的结果进行比较,您可以看到乌龟正在向上和向右移动。每个正方形的左上角开始时稍微高一点,然后稍微偏右一点。相反,我们希望每个新方块的左上角开始更高一点,并且 向左 :

In [88]: for shape in range(1, number_of_shapes + 1):
   ....:     print(-shape * 10, shape * 10)
   ....: 
-10 10
-20 20
-30 30
-40 40

让我们看看现在会发生什么:

宾果。


顺便说一句,将 leftrightforward 等相对命令与 goto 这样的绝对命令混合需要……协调。数学很挑剔。 如果您坚持使用纯粹的相对命令,则不必费心思考如何使 goto 坐标公式正确:

turtle.penup()
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(180)
turtle.pendown()

使用纯相对命令的优点是现在您可以将海龟放置在任何您想要的位置,任何初始航向,并且仍然绘制嵌套方块:

import turtle

number_of_shapes = 4
turtle.setheading(45)
turtle.penup()
turtle.goto(20, 50)
turtle.pendown()
for shape in range(1, number_of_shapes+1):
    #Draw a square
    for sides in range(4):
        turtle.forward(20 + shape * 20)
        turtle.right(90)

    #Move to position of next square
    turtle.penup()
    turtle.left(90)
    turtle.forward(10)
    turtle.left(90)
    turtle.forward(10)
    turtle.left(180)
    turtle.pendown()
turtle.mainloop()

这个简单的几何形状是 冲压 如何 更简单 更快 的完美示例] 比 绘图:

from turtle import Screen, Turtle

NUMBER_OF_SHAPES = 4
CURSOR_SIZE = 20  # cursor sized relative to this starting size

screen = Screen()

turtle = Turtle('square', visible=False)
turtle.color('black', 'white')  # pencolor, fillcolor
turtle.penup()

for shape in range(NUMBER_OF_SHAPES - 1, -1, -1):  # from large to small
    turtle.shapesize((20 + shape * 20) / CURSOR_SIZE)
    turtle.stamp()

screen.mainloop()

就像绘图一样,我们可以旋转图像,只需添加:

turtle.setheading(45)

turtle.left(45)turtle.right(45):

但与 绘图 不同,我们还可以利用 turtle 未为 绘图 实现的其他 turtle 光标图形操作。例如,剪切:

turtle.shearfactor(0.5)

我确定您应该使用 绘图 来解决这个问题,但是当您开始自己的程序时,请保持 stamping 作为一些简单几何形状的更简单更快灵活解决方案。


import turtle
from turtle import *
speed()
a=50
x=-15
y=15
for i in range(7):
  for n in range (4):
    forward(a)
    right(90)
  penup()
  goto(x,y)
  pd()
  a+=10
  x-=5
  y+=5