不少用Python(大多是turtle库)绘制的树图,感觉很漂亮,我整理了一下,挑了一些我觉得不错的代码分享给大家(这些我都测试过,确实可以生成喔~)one 樱花树
另外注意:不管你是为了Python就业还是兴趣爱好,记住:项目开发经验永远是核心,如果你缺新项目练习或者没有python入门到高级实战精讲教程,可以关注我,后台私信我 ‘py’ 自动获取最新python教程资料!还有老司机解答哦!
- 动态生成樱花效果图(这个是动态的):
import turtle as Timport randomimport time# 画樱花的躯干(60,t)def Tree(branch, t): time.sleep(0.0005) if branch > 3: if 8 <&#61; branch <&#61; 12: if random.randint(0, 2) &#61;&#61; 0: t.color(&#39;snow&#39;) # 白 else: t.color(&#39;lightcoral&#39;) # 淡珊瑚色 t.pensize(branch / 3) elif branch <8: if random.randint(0, 1) &#61;&#61; 0: t.color(&#39;snow&#39;) else: t.color(&#39;lightcoral&#39;) # 淡珊瑚色 t.pensize(branch / 2) else: t.color(&#39;sienna&#39;) # 赭(zhě)色 t.pensize(branch / 10) # 6 t.forward(branch) a &#61; 1.5 * random.random() t.right(20 * a) b &#61; 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 &#61; 200 - 400 * random.random() b &#61; 10 - 20 * random.random() t.up() t.forward(b) t.left(90) t.forward(a) t.down() t.color(&#39;lightcoral&#39;) # 淡珊瑚色 t.circle(1) t.up() t.backward(a) t.right(90) t.backward(b)# 绘图区域t &#61; T.Turtle()# 画布大小w &#61; T.Screen()t.hideturtle() # 隐藏画笔t.getscreen().tracer(5, 0)w.screensize(bg&#61;&#39;wheat&#39;) # wheat小麦t.left(90)t.up()t.backward(150)t.down()t.color(&#39;sienna&#39;)# 画樱花的躯干Tree(60, t)# 掉落的花瓣Petal(200, t)w.exitonclick()
- 飘落效果效果图&#xff1a;代码&#xff1a;
from turtle import *from random import *from math import *def tree(n,l): pd()#下笔 #阴影效果 t &#61; cos(radians(heading()&#43;45))/8&#43;0.25 pencolor(t,t,t) pensize(n/3) forward(l)#画树枝 if n>0: b &#61; random()*15&#43;10 #右分支偏转角度 c &#61; random()*15&#43;10 #左分支偏转角度 d &#61; l*(random()*0.25&#43;0.7) #下一个分支的长度 #右转一定角度,画右分支 right(b) tree(n-1,d) #左转一定角度&#xff0c;画左分支 left(b&#43;c) tree(n-1,d) #转回来 right(c) else: #画叶子 right(90) n&#61;cos(radians(heading()-45))/4&#43;0.5 pencolor(n,n*0.8,n*0.8) circle(3) left(90) #添加0.3倍的飘落叶子 if(random()>0.7): pu() #飘落 t &#61; heading() an &#61; -40 &#43;random()*40 setheading(an) dis &#61; int(800*random()*0.5 &#43; 400*random()*0.3 &#43; 200*random()*0.2) forward(dis) setheading(t) #画叶子 pd() right(90) n &#61; cos(radians(heading()-45))/4&#43;0.5 pencolor(n*0.5&#43;0.5,0.4&#43;n*0.4,0.4&#43;n*0.4) circle(2) left(90) pu() #返回 t&#61;heading() setheading(an) backward(dis) setheading(t) pu() backward(l)#退回bgcolor(0.5,0.5,0.5)#背景色ht()#隐藏turtlespeed(0)#速度 1-10渐进&#xff0c;0 最快tracer(0,0)pu()#抬笔backward(100)left(90)#左转90度pu()#抬笔backward(300)#后退300tree(12,100)#递归7层done()
from turtle import *from random import *from math import *def tree(n, l): pd() t &#61; cos(radians(heading() &#43; 45)) / 8 &#43; 0.25 pencolor(t, t, t) pensize(n / 4) forward(l) if n > 0: b &#61; random() * 15 &#43; 10 c &#61; random() * 15 &#43; 10 d &#61; l * (random() * 0.35 &#43; 0.6) right(b) tree(n - 1, d) left(b &#43; c) tree(n - 1, d) right(c) else: right(90) n &#61; cos(radians(heading() - 45)) / 4 &#43; 0.5 pencolor(n, n, n) circle(2) left(90) pu() backward(l)bgcolor(0.5, 0.5, 0.5)ht()speed(0)tracer(0, 0)left(90)pu()backward(300)tree(13, 100)done()
two 玫瑰花效果(有绘制过程)
代码
from turtle import *import timesetup(1000,800,0,0)speed(0)penup()seth(90)fd(340)seth(0)pendown()speed(5)begin_fill()fillcolor(&#39;red&#39;)circle(50,30)for i in range(10): fd(1) left(10)circle(40,40)for i in range(6): fd(1) left(3)circle(80,40)for i in range(20): fd(0.5) left(5)circle(80,45)for i in range(10): fd(2) left(1)circle(80,25)for i in range(20): fd(1) left(4)circle(50,50)time.sleep(0.1)circle(120,55)speed(0)seth(-90)fd(70)right(150)fd(20)left(140)circle(140,90)left(30)circle(160,100)left(130)fd(25)penup()right(150)circle(40,80)pendown()left(115)fd(60)penup()left(180)fd(60)pendown()end_fill()right(120)circle(-50,50)circle(-20,90)speed(1)fd(75)speed(0)circle(90,110)penup()left(162)fd(185)left(170)pendown()circle(200,10)circle(100,40)circle(-52,115)left(20)circle(100,20)circle(300,20)speed(1)fd(250)penup()speed(0)left(180)fd(250)circle(-300,7)right(80)circle(200,5)pendown()left(60)begin_fill()fillcolor(&#39;green&#39;)circle(-80,100)right(90)fd(10)left(20)circle(-63,127)end_fill()penup()left(50)fd(20)left(180)pendown()circle(200,25)penup()right(150)fd(180)right(40)pendown()begin_fill()fillcolor(&#39;green&#39;)circle(-100,80)right(150)fd(10)left(60)circle(-80,98)end_fill()penup()left(60)fd(13)left(180)pendown()speed(1)circle(-200,23)exitonclick()
three 圣诞树
from turtle import *import randomimport timen &#61; 100.0speed("fastest")screensize(bg&#61;&#39;seashell&#39;)left(90)forward(3*n)color("orange", "yellow")begin_fill()left(126)for i in range(5): forward(n/5) right(144) forward(n/5) left(72)end_fill()right(126)color("dark green")backward(n*4.8)def tree(d, s): if d <&#61; 0: return forward(s) tree(d-1, s*.8) right(120) tree(d-3, s*.5) right(120) tree(d-3, s*.5) right(120) backward(s)tree(15, n)backward(n/2)for i in range(200): a &#61; 200 - 400 * random.random() b &#61; 10 - 20 * random.random() up() forward(b) left(90) forward(a) down() if random.randint(0, 1) &#61;&#61; 0: color(&#39;tomato&#39;) else: color(&#39;wheat&#39;) circle(2) up() backward(a) right(90) backward(b)time.sleep(60)
未完待续&#xff01;另外注意&#xff1a;不管你是为了Python就业还是兴趣爱好&#xff0c;记住&#xff1a;项目开发经验永远是核心&#xff0c;如果你缺新项目练习或者没有python入门到高级实战精讲教程&#xff0c;可以关注我&#xff0c;后台私信我 ‘py’ 自动获取最新python教程资料&#xff01;还有老司机解答哦&#xff01;
本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。