我需要编写一个在turtle中制作平行线并采用以下四个参数的函数:
- 一只乌龟
- 长度,即每行的长度
- reps,即要绘制的行数
- 间距,即平行线之间的距离
到目前为止,我有这个:
import turtle as t
def parallelLines(length, reps, separation):
t.fd(length)
t.penup()
t.goto(0, separation)
for i in reps:
return i
我需要编写一个在turtle中制作平行线并采用以下四个参数的函数:
到目前为止,我有这个:
import turtle as t
def parallelLines(length, reps, separation):
t.fd(length)
t.penup()
t.goto(0, separation)
for i in reps:
return i
到目前为止给出的答案都是不完整的、不正确的和/或损坏的。我在下面有一个使用规定的 API 并绘制平行线。
OP 没有明确线应该出现在相对于海龟的位置的位置,所以我选择了海龟在两个维度的中心点:
import turtle
STAMP_SIZE = 20
def parallelLines(my_turtle, length, reps, separation):
separation += 1 # consider how separation 1 & 0 differ
my_stamp = my_turtle.clone()
my_stamp.shape('square')
my_stamp.shapesize(1 / STAMP_SIZE, length / STAMP_SIZE, 0)
my_stamp.tilt(-90)
my_stamp.penup()
my_stamp.left(90)
my_stamp.backward((reps - 1) * separation / 2)
for _ in range(reps):
my_stamp.stamp()
my_stamp.forward(separation)
my_stamp.hideturtle()
turtle.pencolor('navy')
parallelLines(turtle.getturtle(), 250, 15, 25)
turtle.hideturtle()
turtle.exitonclick()
绘制虚线的一种简单方法是通过更改范围值来增加长度
from turtle import Turtle, Screen
t = Turtle()
for i in range(15):
t.forward(10)
t.penup()
t.forward(10)
t.pendown()
screen = Screen()
screen.exitonclick()
我会建议:
def parallel():
turtle.forward(length)
turtle.rt(90)
turtle.pu()
turtle.forward(distanceyouwantbetweenthem)
turtle.rt(90)
turtle.forward(length)
您已经回答了自己的问题:
绘制第一行 X 长度,然后从第一行 Y 长度的开头向下移动并重复,直到我需要多少次重复
这翻译成代码如下所示:
goto start position
for _ in reps:
pen down
move length to the right
pen up
move length to the left
move separation to the bottom
现在您只需要填写对您的turtle-API 的正确调用。