先新建文本文档
把后缀改为.py
输入以下代码。
from turtle import *
import random
# 设置色彩模式是RGB:
colormode(255)
lt(90)
lv = 14
l = 120
s = 45
width(lv)
# 初始化RGB颜色:
r = 1
g = 100
b = 234
pencolor(r, g, b)
penup()
bk(l)
pendown()
fd(l)
def draw_tree(l, level):
global r, g, b
# save the current pen width
w = width()
# narrow the pen width
width(w * 3.0 / 4.0)
# set color:
r = r + 1 * random.randint(0,10)
g = g + 2 * random.randint(0,10)
b = b + 3 * random.randint(0,10)
pencolor(r % 200, g % 200, b % 200)
l = 3.0 / 4.0 * l * random.uniform(0.9,1.1)
left(s)
forward(l)
if level < lv:
draw_tree(l, level + 1)
backward(l)
right(2 * s)
forward(l)
if level < lv:
draw_tree(l, level + 1)
backward(l)
left(s)
# restore the previous pen width
width(w)
speed(10000)
#speed("fastest")
draw_tree(l, 4)
done()
标签:画树,level,蝴蝶,random,tree,draw,lv,width
From: https://www.cnblogs.com/leo-jiayi/p/18183181