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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值