用python画一颗樱花树(不同品种) 实现代码:

 动态生成樱花
效果图(这个是动态的):

 

 

 实现代码:

 1 import turtle as T
 2 import random
 3 import time
 4 
 5 # 画樱花的躯干(60,t)
 6 def Tree(branch, t):
 7     time.sleep(0.0005)
 8     if branch > 3:
 9         if 8 <= branch <= 12:
10             if random.randint(0, 2) == 0:
11                 t.color('snow')  #
12             else:
13                 t.color('lightcoral')  # 淡珊瑚色
14             t.pensize(branch / 3)
15         elif branch < 8:
16             if random.randint(0, 1) == 0:
17                 t.color('snow')
18             else:
19                 t.color('lightcoral')  # 淡珊瑚色
20             t.pensize(branch / 2)
21         else:
22             t.color('sienna')  # 赭(zhě)色
23             t.pensize(branch / 10)  # 6
24         t.forward(branch)
25         a = 1.5 * random.random()
26         t.right(20 * a)
27         b = 1.5 * random.random()
28         Tree(branch - 10 * b, t)
29         t.left(40 * a)
30         Tree(branch - 10 * b, t)
31         t.right(20 * a)
32         t.up()
33         t.backward(branch)
34         t.down()
35 
36 # 掉落的花瓣
37 def Petal(m, t):
38     for i in range(m):
39         a = 200 - 400 * random.random()
40         b = 10 - 20 * random.random()
41         t.up()
42         t.forward(b)
43         t.left(90)
44         t.forward(a)
45         t.down()
46         t.color('lightcoral')  # 淡珊瑚色
47         t.circle(1)
48         t.up()
49         t.backward(a)
50         t.right(90)
51         t.backward(b)
52 
53 # 绘图区域
54 t = T.Turtle()
55 # 画布大小
56 w = T.Screen()
57 t.hideturtle()  # 隐藏画笔
58 t.getscreen().tracer(5, 0)
59 w.screensize(bg='wheat')  # wheat小麦
60 t.left(90)
61 t.up()
62 t.backward(150)
63 t.down()
64 t.color('sienna')
65 
66 # 画樱花的躯干
67 Tree(60, t)
68 # 掉落的花瓣
69 Petal(200, t)
70 w.exitonclick()

 飘落效果
效果图:

 

 

 实现代码:

 1 from turtle import *
 2 from random import *
 3 from math import *
 4 
 5 def tree(n,l):
 6     pd()#下笔
 7     #阴影效果
 8     t = cos(radians(heading()+45))/8+0.25
 9     pencolor(t,t,t)
10     pensize(n/3)
11     forward(l)#画树枝
12 
13     if n>0:
14         b = random()*15+10 #右分支偏转角度
15         c = random()*15+10 #左分支偏转角度
16         d = l*(random()*0.25+0.7) #下一个分支的长度
17         #右转一定角度,画右分支
18         right(b)
19         tree(n-1,d)
20         #左转一定角度,画左分支
21         left(b+c)
22         tree(n-1,d)
23         #转回来
24         right(c)
25     else:
26         #画叶子
27         right(90)
28         n=cos(radians(heading()-45))/4+0.5
29         pencolor(n,n*0.8,n*0.8)
30         circle(3)
31         left(90)
32         #添加0.3倍的飘落叶子
33         if(random()>0.7):
34             pu()
35             #飘落
36             t = heading()
37             an = -40 +random()*40
38             setheading(an)
39             dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
40             forward(dis)
41             setheading(t)
42             #画叶子
43             pd()
44             right(90)
45             n = cos(radians(heading()-45))/4+0.5
46             pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)
47             circle(2)
48             left(90)
49             pu()
50             #返回
51             t=heading()
52             setheading(an)
53             backward(dis)
54             setheading(t)
55     pu()
56     backward(l)#退回
57 
58 bgcolor(0.5,0.5,0.5)#背景色
59 ht()#隐藏turtle
60 speed(0)#速度 1-10渐进,0 最快
61 tracer(0,0)
62 pu()#抬笔
63 backward(100)
64 left(90)#左转90度
65 pu()#抬笔
66 backward(300)#后退300
67 tree(12,100)#递归7层
68 done()

 暗色效果
效果:

 

 

 实现代码:

 1 from turtle import *
 2 from random import *
 3 from math import *
 4 
 5 def tree(n, l):
 6     pd()
 7     t = cos(radians(heading() + 45)) / 8 + 0.25
 8     pencolor(t, t, t)
 9     pensize(n / 4)
10     forward(l)
11     if n > 0:
12         b = random() * 15 + 10
13         c = random() * 15 + 10
14         d = l * (random() * 0.35 + 0.6)
15         right(b)
16         tree(n - 1, d)
17         left(b + c)
18         tree(n - 1, d)
19         right(c)
20     else:
21         right(90)
22         n = cos(radians(heading() - 45)) / 4 + 0.5
23         pencolor(n, n, n)
24         circle(2)
25         left(90)
26     pu()
27     backward(l)
28 bgcolor(0.5, 0.5, 0.5)
29 ht()
30 speed(0)
31 tracer(0, 0)
32 left(90)
33 pu()
34 backward(300)
35 tree(13, 100)
36 done()

更多精彩文章文章关注微信公众号python社区营

学习python申请技术交流群:887934385 分享学习资料及视频

posted @ 2019-12-31 14:56  pypypypy  阅读(79310)  评论(0编辑  收藏  举报