word办公技巧树图(Python笔下那些神奇的树)
- 办公技巧
- 2023-08-27 00:45:42
- 0
turtle是一只神奇的小海龟,可以画出大千世界。而turtle加入了python大家族,像是如龟得水,变得更加受欢迎。
古人有诗云“庭中有奇树,绿叶发华滋”,树之美,或婀娜、或繁茂、或苍劲、或青翠。python-turtle笔下的树更是别有一番风味。
小园新种红樱树,闲绕花行便当游。
#coding=gbkimportturtleasTimportrandomimporttime#画樱花的躯干(60,t)defTree(branch,t):time.sleep(0.0005)ifbranch>3:if8<=branch<=12:ifrandom.randint(0,2)==0:t.color('snow')#白else:t.color('lightcoral')#淡珊瑚色t.pensize(branch/3)elifbranch<8:ifrandom.randint(0,1)==0:t.color('snow')else:t.color('lightcoral')#淡珊瑚色t.pensize(branch/2)else:t.color('sienna')#赭(zhě)色t.pensize(branch/10)#6t.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()#掉落的花瓣defPetal(m,t):foriinrange(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)#wheat小麦w.screensize(bg='white')t.left(90)t.up()t.backward(150)t.down()t.color('sienna')#画樱花的躯干Tree(60,t)#掉落的花瓣Petal(200,t)w.exitonclick()
江南有丹橘,经冬犹绿林
#coding=gbkfromturtleimport*fromrandomimport*frommathimport*classTree:def__init__(self):setup(1000,500)bgcolor(1,1,1)#背景色#ht()#隐藏turtlespeed(10)#速度1-10渐进,0最快#tracer(1,100)#设置绘图屏幕刷新频率,参数1设置在正常刷新频次的第参数1次刷新,参数2设置每次刷新的时延tracer(0,0)pu()#抬笔backward(100)#保证笔触箭头方向始终不向下,此处使其左转90度,而不是右转left(90)#左转90度backward(300)#后退300deftree(self,n,l):pd()#下笔#阴影效果t=cos(radians(heading() 45))/8 0.25pencolor(t,t,t)pensize(n/1.2)forward(l)#画树枝ifn>0:b=random()*15 10#右分支偏转角度c=random()*15 10#左分支偏转角度d=l*(random()*0.25 0.7)#下一个分支的长度#右转一定角度,画右分支right(b)self.tree(n-1,d)#左转一定角度,画左分支left(b c)self.tree(n-1,d)#转回来right(c)else:#画叶子right(90)n=cos(radians(heading()-45))/4 0.5pencolor(n,n*0.8,n*0.8)fillcolor(n,n*0.8,n*0.8)begin_fill()circle(3)left(90)end_fill()#添加0.3倍的飘落叶子ifrandom()>0.7:pu()#飘落t=heading()an=-40 random()*40setheading(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.5pencolor(n*0.5 0.5,0.4 n*0.4,0.4 n*0.4)fillcolor(n,n*0.8,n*0.8)begin_fill()circle(2)left(90)end_fill()pu()#返回t=heading()setheading(an)backward(dis)setheading(t)#passpu()backward(l)#退回defmain():tree=Tree()tree.tree(12,100)#递归7层done()if__name__=='__main__':main()
落红不是无情物,化作春泥更护花
#coding=gbkfromturtleimport*fromrandomimport*#画树方法defdrawTree(n,l):pendown()pencolor('#5d3c3c')pensize(n/1.5)forward(l)ifn>0:dr=randint(30,40)dl=randint(30,40)move=l*(random()*0.4 0.5)right(dr)drawTree(n-1,move)left(dr dl)drawTree(n-1,move)right(dl)else:drawPetal(3)penup()backward(l)#花瓣位置生成defpetalPlace(m,x,y):penup()goto(x,y)pendown()setheading(0)tracer(False)foriinrange(m):ifi==0:drawPetal(5)else:penup()goto(x,y)a=randint(20,400)b=randint(-50,50)forward(a)left(90)forward(b)right(90)pendown()drawPetal(5)#花朵绘画方法defdrawPetal(n):colormode(255)r=randint(200,255)g=randint(8,158)b=randint(8,158)begin_fill()fillcolor(r,g,b)pencolor(r,g,b)circle(n)end_fill()#启动方法defrun():setup(1.0,1.0)penup()goto(-50,-150)left(90)pendown()hideturtle()tracer(False)drawTree(13,150)petalPlace(160,-100,-150)run()done()
川原秋色静,芦苇晚风鸣
importturtleimportrandomstack=[]defcreateWord(max_it,word,proc_rules,x,y,turn):turtle.up()turtle.home()turtle.goto(x,y)turtle.right(turn)turtle.down()t=0whilet
本文由 京廊文化根据互联网搜索查询后整理发布,旨在分享有价值的内容,本站为非营利性网站,不参与任何商业性质行为,文章如有侵权请联系删除,部分文章如未署名作者来源请联系我们及时备注,感谢您的支持。
本文链接: /bangong/24091.html