Python 利用 turtle画出樱花树

import turtle as T
import random
import time

def Tree(branch, tur):
    time.sleep(0.0001)
    if branch > 3:
        if 8 <= branch <= 12:
            rand = random.randint(0, 2)
            if rand == 0:
                tur.color(#fba3c6)
            if rand == 1:
                tur.color(#f94985)
            if rand == 2:
                tur.color(snow)
            tur.pensize(branch / 3)
        elif branch < 8:
            rand = random.randint(0, 1)
            if rand == 0:
                tur.color(#fba3c6)
            if rand == 1:
                tur.color(#f94985)
            tur.pensize(branch / 2)
        else:
            tur.color(#8a7081)  # 赭(zhě)色
            tur.pensize(branch / 10)  # 6
        tur.forward(branch)
        a = 1.6 * random.random()
        tur.right(20 * a)
        b = 1.6 * random.random()
        Tree(branch - 10 * b, tur)
        tur.left(40 * a)
        Tree(branch - 10 * b, tur)
        tur.right(20 * a)
        tur.up()
        tur.backward(branch)
        tur.down()

def Petal(m, tur):
    for i in range(m):
        a = 200 - 400 * random.random()
        b = 10 - 20 * random.random()
        tur.up()
        tur.forward(b)
        tur.left(90)
        tur.forward(a)
        tur.down()
        tur.color(#f94985)
        tur.circle(1)
        tur.up()
        tur.backward(a)
        tur.right(90)
        tur.backward(b)


# 绘图区域
tur = T.Turtle()
# 画布大小
w = T.Screen()
tur.hideturtle()  # 隐藏画笔
tur.getscreen().tracer(5, 0)
w.screensize(bg=#fffbfd)
tur.left(90)
tur.up()
tur.backward(200)
tur.down()
tur.color(#f94985)

# 画樱花的躯干
Tree(60, tur)
# 掉落的花瓣
Petal(100, tur)
w.exitonclick()
经验分享 程序员 微信小程序 职场和发展