当前位置 博文首页 > 孤寒者的博客:Python常用模块 之 turtle模块——实现画太极,樱

    孤寒者的博客:Python常用模块 之 turtle模块——实现画太极,樱

    作者:[db:作者] 时间:2021-07-25 21:59

    1.太极

    import  turtle
    t=turtle.Turtle()
    t.penup()
    t.goto(0,-50)
    t.pendown()
    t.begin_fill()
    t.fillcolor('black')
    t.circle(150,extent=180)
    t.circle(75,extent=180)
    t.circle(-75,extent=180)
    t.end_fill()
    t.circle(-150,extent=180)
    t.penup()
    t.goto(0,160)
    t.pendown()
    t.begin_fill()
    t.fillcolor('white')
    t.circle(30,extent=360)
    t.end_fill()
    t.penup()
    t.goto(0,0)
    t.pendown()
    t.begin_fill()
    t.fillcolor('black')
    t.circle(30,extent=360)
    t.end_fill()
    turtle.done
    #画太极八卦
    import  turtle
    t=turtle.Turtle()
    t.penup()
    t.goto(0,-50)
    t.pendown()
    t.begin_fill()
    t.fillcolor('black')
    t.circle(150,extent=180)
    t.circle(75,extent=180)
    t.circle(-75,extent=180)
    t.end_fill()
    t.circle(-150,extent=180)
    t.penup()
    t.goto(0,160)
    t.pendown()
    t.begin_fill()
    t.fillcolor('white')
    t.circle(30,extent=360)
    t.end_fill()
    t.penup()
    t.goto(0,0)
    t.pendown()
    t.begin_fill()
    t.fillcolor('black')
    t.circle(30,extent=360)
    t.end_fill()
    turtle.done
    
    #画太极八卦
    import  turtle
    t=turtle.Turtle()
    t.penup()
    t.goto(0,-50)
    t.pendown()
    t.begin_fill()
    t.fillcolor('black')
    t.circle(150,extent=180)
    t.circle(75,extent=180)
    t.circle(-75,extent=180)
    t.end_fill()
    t.circle(-150,extent=180)
    t.penup()
    t.goto(0,160)
    t.pendown()
    t.begin_fill()
    t.fillcolor('white')
    t.circle(30,extent=360)
    t.end_fill()
    t.penup()
    t.goto(0,0)
    t.pendown()
    t.begin_fill()
    t.fillcolor('black')
    t.circle(30,extent=360)
    t.end_fill()
    turtle.done
    

    在这里插入图片描述

    2.樱花树

    import turtle as T
    import random
    import time
    
    # 画樱花的驱干(60,t)
    def Tree(branch,t):
        time.sleep(0.0005)
        if branch > 3:
            if 8 <= branch<=12:
                if random.randint(0,2)==0:
                    t.color("snow")#白
                else:
                    t.color("lightcoral")#淡珊湖色
                t.pensize(branch/3)
            elif branch<8:
                if random.randint(0,1)==0:
                    t.color("snow")
                else:
                    t.color("lightcoral")#淡珊湖色
                t.pensize(branch/2)
            else:
                t.color("sienna")#猪色
                t.pensize(branch/10)#6
            t.forward(branch)
            a=1.5*random.random()
            t.right(20*a)
            b=1.5*random.random()
            Tree(branch-10*b,t)
            t.left(40*a)
            Tree(branch - 10 * b, t)
            t.right(20*a)
            t.up()
            t.backward(branch)
            t.down()
    
            # 掉落的花瓣
    def Petal(m,t):
        for i in  range(m):
            a=200-400*random.random()
            b=10-20*random.random()
            t.up()
            t.forward(b)
            t.left(90)
            t.forward(a)
            t.down()
            t.color("lightcoral")#淡珊湖色
            t.circle(1)
            t.up()
            t.backward(a)
            t.right(90)
            t.backward(b)
    
    #绘图区域
    t=T.Turtle()
    #画布大小
    w=T.Screen()
    t.hideturtle()#隐藏画笔
    t.getscreen().tracer(5,0)
    w.screensize(bg="wheat")#   wheat小麦
    t.left(90)
    t.up()
    t.backward(150)
    t.down()
    t.color("sienna")
    
    #画樱花的驱干
    Tree(60,t)
    #掉落的花瓣
    Petal(200,t)
    w.exitonclick()
    

    在这里插入图片描述

    3.可爱的动漫少女

    import turtle as te
    import time
    WriteStep = 15  # 贝塞尔函数的取样次数
    Speed = 5
    Width = 600  # 界面宽度
    Height = 500  # 界面高度
    Xh = 0  # 记录前一个贝塞尔函数的手柄
    Yh = 0
    
    
    def Bezier(p1, p2, t):  # 一阶贝塞尔函数
        return p1 * (1 - t) + p2 * t
    
    
    def Bezier_2(x1, y1, x2, y2, x3, y3):  # 二阶贝塞尔函数
        te.goto(x1, y1)
        te.pendown()
        for t in range(0, WriteStep + 1):
            x = Bezier(Bezier(x1, x2, t / WriteStep),
                       Bezier(x2, x3, t / WriteStep), t / WriteStep)
            y = Bezier(Bezier(