from turtle import *
import random


def drawTree(length):
    if length > 1:
        # 缩小树干
        if 14 < length < 30:
            pensize(4)
        # 长度这个范围内为绿叶
        elif 5 < length < 15:
            color('#04B486')
            pensize(3)
        # 长度这个范围内为红花
        elif 1 < length < 5:
            color('#FE2E9A')
            pensize(2)
        # 其它范围为树干
        else:
            color('#5E5E5E')
            pensize(5)
        # 随机角度和长度
        randangle = 2 * random.random()
        randlen = 2 * random.random()

        # 绘制线段,向右调整角度
        fd(length)
        right(20 * randangle)
        drawTree(length - 10 * randlen)

        # 向左调整角度
        left(40 * randangle)
        drawTree(length - 10 * randlen)

        # 为什么需要再向右转20度,那是因为一共向左赚了40度,使用 backword 后退,必须是相同
        right(20 * randangle)

        up()
        backward(length)
        down()


def fallingFlowers(m):
    x, y = -1000, -750
    for i in range(30):
        up()
        goto(x, y)
        x += 80
        down()
        yval = 50
        for z in range(m):
            a = 100 * random.random()
            b = 2 * random.random()
            if a > 59:
                color('#FE2E9A')
            else:
                color('#04B486')
            circle(2)
            up()
            goto(x, y + (yval * b))
            fd(a)
            yval += 50
            down()


setworldcoordinates(-1000, -750, 1000, 750)
tracer(200, 1)

# 绘制落叶
fallingFlowers(10)
bgcolor('#F5F6CE')
color('#5E5E5E')
pensize(5)


up()
# 跳到绘制起始点
goto(0, -700)

down()


left(80)
fd(140)
drawTree(120)
input()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值