如何减少这段用 python.turtle 编写的代码的长度?

问题描述 投票:0回答:1

我需要帮助减少用 python.turtle 编写的这段代码的总长度或行数。我需要有相同的输出,如果可能的话,使用其他函数来减少行的长度!这是代码:

import turtle
t=turtle.Turtle()
t.speed(5)
#external form
t.pen(pensize=2, pencolor="blue")
t.forward(400)
t.right(120)
t.forward(200)
t.pen(pensize=2, pencolor="purple")
t.right(120)
t.forward(200)
t.right(60)
t.forward(200)
t.left(120)
t.forward(200)
t.right(60)
t.forward(200)
t.left(120)
t.forward(200)
t.right(60)
t.forward(200)
t.left(120)
t.forward(200)
t.right(60)
t.forward(200)
t.left(120)
t.forward(200)
t.right(60)
t.forward(200)
t.left(120)
t.forward(200)
t.right(60)
t.forward(200)
t.penup()
t.goto(0,0)
t.pendown()
t.pen(pensize=2, pencolor="blue")
t.left(180)
t.forward(400)
t.right(120)
t.forward(200)
t.pen(pensize=2, pencolor="purple")
t.right(60)
t.forward(200)
t.pen(pensize=2, pencolor="blue")
t.right(60)
t.backward(200)
t.forward(800)
t.right(120)
t.forward(200)
t.backward(200)
t.left(120)
t.backward(800)
t.left(60)
t.forward(200)
t.penup()
t.goto(0,0)
t.pendown()
t.left(60)
t.forward(400)
t.right(120)
t.forward(200)
t.backward(200)
t.right(60)
t.forward(800)
t.right(120)
t.forward(200)
#inner figure
t.penup()
t.goto(-45, -230)
t.pendown()
t.pen(pensize=4, pencolor="yellow")
t.right(120)
t.forward(90)
t.circle(5,90)
t.forward(90)
t.left(90)
t.forward(105)
t.back(110)
t.left(-90)
t.forward(15)
t.left(90)
t.forward(110)
t.left(90)
t.forward(15)
t.left(90)
t.forward(5)
t.right(90)
t.forward(90)
t.circle(5,90)
t.penup()
t.goto(-25,-118)
t.pendown()
t.left(90)
t.forward(8)
t.circle(-10,90)
t.forward(30)
t.circle(-10,90)
t.forward(8)
t.penup()
t.goto(-25,-215)
t.pendown()
t.left(180)
t.forward(65)
t.penup()
t.goto(0,-215)
t.pendown()
t.forward(65)
t.penup()
t.goto(25,-215)
t.pendown()
t.forward(65)

我已经尝试了很多但无法获得相同的输出。我试图通过在范围内使用 for in range 和其他函数来减少代码,但就像我说的那样无法得到相同的结果。顺便说一句,我是初学者!

python forms reduce figure python-turtle
1个回答
0
投票

宝贝是这样的:

import turtle

def draw_line(t, length, angle, color, pensize):
    t.pensize(pensize)
    t.pencolor(color)
    t.forward(length)
    t.right(angle)

def draw_polyline(t, lines):
    for line in lines:
        draw_line(t, *line)

def draw_figure(t, color, lines):
    t.penup()
    t.goto(*lines[0][0])
    t.pendown()
    draw_polyline(t, lines)
    t.penup()

t = turtle.Turtle()
t.speed(5)

# external form
external_lines = [
    [(0, 0), (400, 0), (-120, 200)],
    [(0, 0), (-200, 346.4), (-200, -173.2)],
    [(0, 0), (200, 346.4), (-200, 346.4)],
    [(0, 0), (-400, 0), (120, -200)],
    [(0, 0), (200, -346.4), (-200, -346.4)],
    [(0, 0), (-200, -346.4), (200, -173.2)]
]
draw_figure(t, "blue", external_lines)

# inner figure
inner_lines = [
    [(-45, -230), (90, 0), (5, 90), (90, 0), (-90, 105), (-110, 0), (90, -90), (110, 0),
     (-90, -90), (15, -90), (-110, 0), (-15, -90), (-5, -90), (90, 90), (5, 90)],
    [(-25, -118), (8, -90), (-10, 90), (30, -90), (-10, 90), (8, -90)],
    [(-25, -215), (65, -180)],
    [(0, -215), (65, 0)],
    [(25, -215), (65, 0)]
]
draw_figure(t, "yellow", inner_lines)
© www.soinside.com 2019 - 2024. All rights reserved.