python,递归函数画树,海龟作图,turtle

 

 

import turtle
import time
def tree(t,trunk_length, pensize):
    if trunk_length > 5:
        t.pensize(pensize)
        t.forward(trunk_length)
        t.right(20)
        tree(t,trunk_length-5,pensize-1)
        t.left(40)
        tree(t,trunk_length-5,pensize-1)
        if trunk_length - 5 <= 5:
            t.pencolor('green')
            t.circle(2)
            t.pencolor('purple')
        t.right(20)
        t.backward(trunk_length)

t = turtle.Turtle()
trunk_length = 40
pensize = 8
t.pencolor('purple')

angle = 20
t.left(90)
tree(t, trunk_length, pensize)
time.sleep(40)

posted @ 2020-04-07 10:18  Iceberg_710815  阅读(1381)  评论(0编辑  收藏  举报