用turtle画樱花树
时间: 2023-08-20 20:06:09 浏览: 112
好的,以下是用Python的turtle库来画樱花树的代码:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor("#F8F8FF")
# 设置画笔宽度和颜色
turtle.penup()
turtle.pensize(3)
turtle.color("#8B0000")
# 定义函数来画樱花
def draw_flower():
turtle.right(random.randint(0, 360))
turtle.forward(random.randint(10, 30))
turtle.right(random.randint(0, 360))
turtle.pendown()
turtle.color("#FF69B4")
turtle.begin_fill()
turtle.circle(random.randint(5, 10))
turtle.end_fill()
turtle.penup()
turtle.color("#8B0000")
turtle.right(random.randint(0, 360))
turtle.backward(random.randint(10, 30))
# 画樱花树
turtle.goto(0, -250)
turtle.pendown()
turtle.left(90)
turtle.forward(150)
turtle.right(135)
turtle.forward(200)
turtle.left(135)
turtle.forward(200)
turtle.right(135)
turtle.forward(200)
turtle.left(135)
turtle.forward(200)
turtle.right(135)
turtle.forward(150)
# 画樱花
turtle.goto(0, 0)
for i in range(100):
draw_flower()
# 隐藏画笔
turtle.hideturtle()
# 点击关闭窗口
turtle.exitonclick()
```
运行上述代码,即可在turtle窗口中看到画出的樱花树。效果如下图所示:
![樱花树](https://img-blog.csdnimg.cn/20210520162328989.png)
阅读全文