利用turtle库绘制樱花
时间: 2023-10-25 18:08:42 浏览: 91
使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
5星 · 资源好评率100%
利用turtle库绘制樱花的代码如下:
```python
import turtle as t
import random
# 绘制花瓣
def petal():
t.pencolor(random.uniform(0.5, 1), random.uniform(0, 0.5), random.uniform(0.5, 1))
t.fillcolor(random.uniform(0.5, 1), random.uniform(0, 0.5), random.uniform(0.5, 1))
t.begin_fill()
for i in range(6):
t.forward(30)
t.right(60)
t.forward(30)
t.right(120)
t.end_fill()
# 绘制花朵
def flower(pos):
t.penup()
t.goto(pos[0], pos[1])
t.pendown()
petal_num = random.randint(6, 12)
for i in range(petal_num):
t.right(360 / petal_num)
petal()
# 绘制樱花
def sakura():
t.bgcolor('#F781BE')
t.speed('fastest')
pos_list = []
for i in range(8):
x = random.uniform(-t.window_width() / 2, t.window_width() / 2)
y = random.uniform(-t.window_height() / 2, t.window_height() / 2)
pos_list.append((x, y))
for pos in pos_list:
flower(pos)
t.done()
# 绘制樱花
sakura()
```
阅读全文