|
| 1 | +import turtle |
| 2 | +import random |
| 3 | + |
| 4 | +def cherryTree(branch, t): |
| 5 | + if branch > 4: |
| 6 | + if 7 <= branch <= 13: |
| 7 | + # 随机数生成 |
| 8 | + if random.randint(0, 3) == 0: |
| 9 | + t.color('snow') # 花瓣心的颜色 |
| 10 | + else: |
| 11 | + t.color('pink') #花瓣颜色 |
| 12 | + # 填充的花瓣大小 |
| 13 | + t.pensize( branch / 6) |
| 14 | + elif branch < 8: |
| 15 | + if random.randint(0, 2) == 0: |
| 16 | + t.color('snow') |
| 17 | + else: |
| 18 | + # 设置樱花树叶颜色 |
| 19 | + t.color('green') # 樱花树颜色 |
| 20 | + t.pensize(branch / 5) |
| 21 | + else: |
| 22 | + t.color('Peru') # 树干颜色 |
| 23 | + t.pensize(branch / 11) #调整树干的粗细 |
| 24 | + t.forward(branch) |
| 25 | + |
| 26 | + a = 1 * random.random() |
| 27 | + t.right(20 * a) |
| 28 | + b = 1 * random.random() |
| 29 | + cherryTree(branch - 10 * b, t) |
| 30 | + t.left(60 * a) |
| 31 | + cherryTree(branch - 10 * b, t) |
| 32 | + t.right(40 * a) |
| 33 | + t.up() |
| 34 | + t.backward(branch) |
| 35 | + t.down() |
| 36 | + |
| 37 | +# 掉落的花瓣 参数是画板和笔 |
| 38 | +def petal(m, t): |
| 39 | + for i in range(m): |
| 40 | + a = 200 - 400 * random.random() |
| 41 | + b = 10 - 20 * random.random() |
| 42 | + t.up() |
| 43 | + t.forward(b) |
| 44 | + # 向左移动 |
| 45 | + t.left(75) |
| 46 | + # 向前移动 |
| 47 | + t.forward(a) |
| 48 | + # 放下画笔 |
| 49 | + t.down() |
| 50 | + # 设置花瓣颜色 |
| 51 | + t.color('pink') # 粉红色 |
| 52 | + # 画个小圆当作花瓣 |
| 53 | + t.circle(1) |
| 54 | + # 提起画笔 |
| 55 | + t.up() |
| 56 | + # 画笔向后退 |
| 57 | + t.backward(a) |
| 58 | + # 画笔向前行 |
| 59 | + t.right(70) |
| 60 | + t.backward(b) |
| 61 | + |
| 62 | +# 描述樱花的句子 |
| 63 | +def des_word(): |
| 64 | + t.color('LightCoral') # 字体颜色设置 |
| 65 | + t.hideturtle() |
| 66 | + t.goto(-50,-130) |
| 67 | + t.pu() |
| 68 | + # 昨日雪如花,今日花如雪,山樱如美人,红颜易消歇。 |
| 69 | + t.write('昨日雪如花,',move=False, align='center', font=('Arial', 20, 'normal')) |
| 70 | + t.pd() |
| 71 | + |
| 72 | + t.pu() |
| 73 | + t.goto(90,-130) |
| 74 | + t.write('今日花如雪', move=False, align='center', font=('Arial', 20, 'normal')) |
| 75 | + t.pd() |
| 76 | + |
| 77 | + |
| 78 | +# 绘图区域 |
| 79 | +t = turtle.Turtle() |
| 80 | +# 画布大小 获取到屏幕 |
| 81 | +w = turtle.Screen() |
| 82 | +t.hideturtle() # 隐藏画笔 |
| 83 | +t.getscreen().tracer(8, 0) # 获取屏幕大小 |
| 84 | +w.screensize(bg='LightCyan') # 设置屏幕背景颜色 |
| 85 | +t.left(80) |
| 86 | +t.up() |
| 87 | +t.backward(140) |
| 88 | +t.down() |
| 89 | +t.color('sienna') |
| 90 | +cherryTree(50, t) |
| 91 | +petal(300, t) |
| 92 | +des_word() |
| 93 | +# 点击屏幕任意地方退出 |
| 94 | +w.exitonclick() |
| 95 | + |
0 commit comments