zoukankan      html  css  js  c++  java
  • 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()
    
    
  • 相关阅读:
    10 Iterable之遍历Map、Set、Array
    9 Map和Set
    8 循环
    5 字符串
    6 数组
    4 数据类型
    2 变量
    实现简单的邮件收发器(十二)
    10.19 初识django
    10.18 数据库之索引优化方案
  • 原文地址:https://www.cnblogs.com/PushyTao/p/15101048.html
Copyright © 2011-2022 走看看