这是copy“盛夏温暖流年” 这位作者的代码,我刚学python,觉得很有意思,顺便写篇解析小文章。

from turtle import *
from random import *
from math import *

def tree(n,l):
    pd()#下笔
    #阴影效果
    t = cos(radians(heading()+45))/8+0.25
    pencolor(t,t,t)
    pensize(n/3)
    forward(l)#画树枝

    if n>0:
        b = random()*15+10 #右分支偏转角度
        c = random()*15+10 #左分支偏转角度
        d = l*(random()*0.25+0.7) #下一个分支的长度
        #右转一定角度,画右分支
        right(b)
        tree(n-1,d)
        #左转一定角度,画左分支
        left(b+c)
        tree(n-1,d)
        #转回来
        right(c)
    else:
        #画叶子
        right(90)
        n=cos(radians(heading()-45))/4+0.5
        ran=random()
        #这里相比于原来随机添加了填充的圆圈,让樱花叶子看起来更多一点
        if(ran>0.7):
            begin_fill()
            circle(3)
            fillcolor('pink')
        #把原来随机生成的叶子换成了统一的粉色
        pencolor("pink")
        circle(3)
        if(ran>0.7):
            end_fill()
        left(90)
        #添加0.3倍的飘落叶子
        if(random()>0.7):
            pu()
            #飘落
            t = heading()
            an = -40 +random()*40
            setheading(an)
            dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
            forward(dis)
            setheading(t)
            #画叶子
            pd()
            right(90)
            n = cos(radians(heading()-45))/4+0.5
            pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)
            circle(2)
            left(90)
            pu()
            #返回
            t=heading()
            setheading(an)
            backward(dis)
            setheading(t)
    pu()
    backward(l)#退回
    
bgcolor(0.956,0.9255,0.9882)#设置背景色(把灰色换成淡紫色)
ht()#隐藏turtle
speed(0)#速度 1-10渐进,0 最快
tracer(0,0)
pu()#抬笔
backward(50)
left(90)#左转90度
pu()#抬笔
backward(300)#后退300
tree(12,100)#递归7层
done()

一.import语句

使用一个模块中的函数之前,必须用import语句导入该模块,使用import引用函数库有两种方式,第一种:import<库名>,使用库中函数的格式如下:<库名>.<函数名>(函数参数)。

第二种:from <库名> import*        //其中,*表示所有函数,使用格式如下:<函数名>(<函数参数>)

二.python中的库

turtle库:turtle库是一个直观有趣的图形绘制函数库。

1.turtle库语法元素分析

turtle.penup()函数

        别名:turtle.pu(),turtle.up()

        作用:抬起画笔,之后移动画笔不绘制形状。

        参数:无

turtle.pendown()函数

        别名:turtle.pd(),turtle.down()

        作用:落下画笔,之后移动画笔不绘制形状。

        参数:无

turtle.pensize()函数

        别名:turtle.width()

        作用:设置画笔宽度,当无参数输入时返回当前画笔宽度。

turtle.pencolor()函数

        作用:设置画笔颜色,当无参数输入时返回当前画笔颜色。

turtle.fd()函数

        别名:turtle.forward(distance)

        作用:向小海龟当前行进方向前进距离。

        参数distance:行进距离的像素值,当值为负数时,表示向相反方向前进。

turtle.right(degree)函数

        别名:turtle.rt(degree)

        作用:用于通过其参数值使 turtle 的头部向右移动。

        参数degree:度数

turtle.left(degree)函数

        别名:turtle.lf(degree)

        作用:用于通过其参数值使 turtle 的头部向左移动。

        参数degree:度数

begin_fill() 命令和end_fill() 命令解析如图:

turtle.circle()函数

         作用:根据半径radius绘制extent角度的弧形

        参数:

              radius :弧形半径

                             当radius值为正数时,圆心在当前位置/小海龟左侧。

                             当radius值为负数时,圆心在当前位置/小海龟右侧。

              extent :弧形角度。当无该参数或参数为None时,绘制整个圆形

                             当extent值为正数时,顺小海龟当前方向绘制。

                             当extent值为负数时,逆小海龟当前方向绘制。

turtle.setheading( to_angle)函数

        别名:turtle.seth()

        作用:设置海龟的朝向为 to_angle

        参数:to_angle 表示角度的数值 (整型或浮点型)。to_angle为正逆时针转向,顺时针旋转为to_angle为负顺时针转向。每次setheading(to_angle) 小海龟以正东(X轴正方向)为基准转向to_angle角度。
turtle.st()函数

        作用:显示小乌龟
turtle.ht()函数

        作用:隐藏小乌龟

turtle.speed()函数

        作用:通过其参数值更改 turtle 的速度。

        参数:turtle 的速度介于0到10之间,如果输入的数字大于10或小于0.5,则速度设置为0。

 

       

        

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐