用python画一棵樱花树
时间: 2023-06-01 09:07:18 浏览: 198
好的,我可以回答这个问题。以下是使用 Python 绘制樱花树的示例代码:
```python
import turtle
import random
# 设置画布大小
turtle.setup(width=800, height=600)
# 设置画笔大小和颜色
turtle.pensize(3)
turtle.pencolor("brown")
# 定义函数:绘制树干
def draw_trunk():
turtle.pendown()
turtle.right(90)
turtle.forward(100)
# 定义函数:绘制树枝
def draw_branch(branch_length):
if branch_length > 5:
turtle.forward(branch_length)
turtle.right(random.randint(15, 45))
draw_branch(branch_length - random.randint(10, 20))
turtle.left(random.randint(15, 45))
draw_branch(branch_length - random.randint(10, 20))
turtle.right(random.randint(15, 45))
turtle.backward(branch_length)
# 定义函数:绘制樱花
def draw_cherry_blossom():
turtle.pencolor("pink")
turtle.penup()
turtle.setpos(random.randint(-350, 350), random.randint(50, 250))
turtle.pendown()
turtle.begin_fill()
turtle.circle(random.randint(10, 30))
turtle.end_fill()
# 绘制樱花树
draw_trunk()
draw_branch(60)
draw_cherry_blossom()
# 隐藏画笔
turtle.hideturtle()
# 点击关闭窗口
turtle.exitonclick()
```
这是一段基于 Turtle 库的示例代码,可以绘制出一个随机的樱花树。希望能够帮到你。
阅读全文