Python-turtle库画樱花树

Posted PushyTao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-turtle库画樱花树相关的知识,希望对你有一定的参考价值。

本文代码来自下面链接:

https://blog.csdn.net/July__July/article/details/99543992

但是因为代码不能够运行,出现了比较多的问题,所以重新进行了修改

下面是主要的代码和效果图:
在这里插入图片描述

import turtle as t
from random import *
from math import *


def draw(n, l):
    t.setup(1000, 800)
    t.pd() # 阴影效果
    tt = cos(radians(t.heading() + 45)) / 8 + 0.25
    t.pencolor(tt, tt, tt)
    t.pensize(n / 3)
    t.forward(l)
    if n > 0:
        b = random() * 15 + 10
        c = random() * 15 + 10
        d = l * (random() * 0.25 + 0.7)
        # 右转一定角度,画右分支
        t.right(b)
        draw(n - 1, d) # 左转一定角度,画左分支
        t.left(b + c)
        draw(n - 1, d)
        t.right(c)
    else: # 画叶子
        t.right(90)
        n = cos(radians(t.heading() - 45)) / 4 + 0.5
        t.pencolor(n, n * 0.8, n * 0.8)
        t.circle(3)
        t.left(90)# 添加0.3倍的飘落叶子
        if random() > 0.7:
            t.pu()# 飘落
            tt = t.heading()
            an = -40 + random() * 40
            t.setheading(an)
            dis = int(800 * random() * 0.5 + 400 * random() * 0.3 + 200 * random() * 0.2)
            t.forward(dis)
            t.setheading(tt)# 画叶子
            t.pd()
            t.right(90)
            n = cos(radians(t.heading() - 45)) / 4 + 0.5
            t.pencolor(n * 0.5 + 0.5, 0.4 + n * 0.4, 0.4 + n * 0.4)
            t.circle(2)
            t.left(90)
            t.pu() # 返回
            tt = t.heading()
            t.setheading(an)
            t.backward(dis)
            t.setheading(tt)
    t.pu()
    t.backward(l)


# t.bgcolor(0.5, 0.5, 0.5)
t.bgcolor('pink') #255.0, 192.0, 203.0
t.ht()
t.speed(0)
t.tracer(0, 0)
t.pu()
t.backward(100)
t.left(90)
t.pu()
t.backward(300)
draw(12, 100)
t.done()

以上是关于Python-turtle库画樱花树的主要内容,如果未能解决你的问题,请参考以下文章

python-turtle绘制雪容融,已打包成exe可直接运行

python-turtle库,心得随心画~~

python-turtle-画雪花-2种方法及效果的详解

今天用matplotlib库画数学图啦

Python-turtle库知识小结(python绘图工具)

为啥用python turtle库画蟒蛇会出现这个错误?