python turtle 画几株草

python turtle 画几株草

import turtle as tl
import random as rd
import math as mt


def write(n, t):
    print(n)
    tl.pensize(mt.sqrt(n+1)/10)
    tl.pencolor(0.3, 0.9, 0.3)
    tl.right(50 / (n+1))
    tl.forward(n/20)

    def pt(n):
        d = rd.randint(0, 1)
        if d:
            tl.left(15*n/t)
            tl.forward(10*n/t)
            tl.backward(10*n/t)
            tl.right(15*n/t)
        else:
            tl.right(15 * n / t)
            tl.forward(10 * n/t)
            tl.backward(10 * n/t)
            tl.left(15 * n / t)

    if n > 0:
       pt(n)
       write(n-1, t)


def move(x, y):
    tl.penup()
    tl.goto(x,y)
    tl.pd()


def m(n, t, Ysize, angle):
    Y = Ysize
    tl.right(50 / (n+1))
    tl.pensize(mt.sqrt(n)/5)
    tl.pencolor(0.1, 0.7, 0.1)
    tl.forward(25*n/t)
    if( n > 0 ):
        q1 = 45 * n / t
        q2 = 45 * n / t
        list = tl.position()
        x1 = list[0]
        y1 = list[1]
        angle = angle-50 / (n+1)
        tl.left(q1)
        write(Y*n, Y*n)
        move(x1, y1)
        tl.setheading(angle)
        tl.right(q2)
        write(Y*n, Y*n)
        move(x1, y1)
        tl.setheading(angle)
        #tl.right(180-45 * n / t)
        m(n-1, t, Ysize, angle)


def _main_():
    tl.screensize(canvwidth=1000, canvheight=1000)
    tl.speed(0)
    tl.bgcolor(1, 1, 1)
    tl.left(90)
    m(15, 15, 2.5, 90)
    move(-200, 0)
    tl.setheading(90)
    m(10, 10, 2, 90)
    move(200,0)
    tl.setheading(90)
    m(20, 20, 3, 90)
    tl.hideturtle()
    tl.mainloop()
    
    
_main_()

python turtle 画几株草_第1张图片

你可能感兴趣的:(python turtle 画几株草)