[Python]-python画一朵花

import turtle
import math

def p_line(t, n, lenght, angle):
    for i in range(n):
        t.fd(lenght)
        t.lt(angle)

def polygon(t, n, length):
    angle = 360 / n
    p_line(t, n, length, angle)

def arc(t, r, angle):
    arc_lenght = 2 * math.pi * r * abs(angle) / 360
    n = int(arc_lenght / 4) + 1
    step_length = arc_lenght / n
    step_angle = float(angle) / n

    t.lt(step_angle / 2)
    p.line(t, n, step_angle,step_length)
    t.rt(step_angle / 2)

def petal(t, n, angle):
    for i in range(2):
        arc(t, r, angle)
        t.lt(180 - angle)


def flower(t, n, r, angle, p):
    for i in range(n):
        petal(t, n, angle)
        t.lt(p / n)


def leaf(t, r, angle, p):
    t.bengin_fill()
    t.down()
    flower(t, 1, r, angle, p)
    t.end_fill()


def main():
    window = turtle.Screen()
    window.bgcolor('white')
    window.title('Draw a flower')
    lucy = turtle.Turtle()
    lucy.shape('turtle')
    lucy.color('red')
    lucy.write(3)

    flower(lucy, 7, 60, 100, 360)

    lucy.color('brown')
    lucy.rt(90)
    lucy.fd(200)

    lucy.width(1)
    lucy.rt(270)
    lucy.color('green')
    leaf(lucy, 40, 80, 180)
    lucy.rt(140)
    lucy.color('black')
    lucy.fd(30)
    lucy.lt(180)
    lucy.fd(300)

    lucy.rt(120)
    lucy.color('green')
    leaf(lucy, 40, 80, 180)
    lucy.color('black')
    lucy.rt(140)
    lucy.fd(30)
    lucy.ht()
    window.exitonclick()

猜你喜欢

转载自www.cnblogs.com/chenwz/p/10456735.html