用乌龟绘制平行线

时间:2013-09-30 15:39:27

标签: python turtle-graphics

我需要编写一个在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

4 个答案:

答案 0 :(得分:1)

我建议改为:

def parallel():
    turtle.forward(length)
    turtle.rt(90)
    turtle.pu()
    turtle.forward(distanceyouwantbetweenthem)
    turtle.rt(90)
    turtle.forward(length)

答案 1 :(得分:0)

您已经回答了自己的问题:

  

画出第一行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的正确调用。

答案 2 :(得分:0)

到目前为止给出的答案都是不完整的,不正确的和/或破碎的。我下面有一个使用规定的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()

答案 3 :(得分:0)

绘制虚线的一种简单方法是通过改变范围值来增加长度

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