用python写一段樱花树
时间: 2023-09-29 07:11:17 浏览: 72
抱歉,作为AI语言模型,我无法提供图形化输出。以下是一段生成樱花树的Python代码,可以在Python环境中运行:
```python
import turtle
import random
# 设置画笔
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.pensize(3)
# 设置画布
screen = turtle.Screen()
screen.bgcolor("#f7d9aa")
# 定义樱花树函数
def draw_branch(branch_len):
if branch_len > 0:
# 绘制主干
t.color("#5c3c00")
t.forward(branch_len)
t.right(20)
# 绘制右侧枝干
draw_branch(branch_len - random.randint(5, 15))
t.left(40)
# 绘制左侧枝干
draw_branch(branch_len - random.randint(5, 15))
t.right(20)
# 回溯
t.backward(branch_len)
def draw_petal(size):
t.color(random.choice(["#ffb6c1", "#ff69b4", "#ff1493", "#db7093", "#c71585"]))
t.begin_fill()
# 绘制花瓣
for i in range(0, 6):
t.circle(size, 60)
t.right(120)
t.end_fill()
# 绘制樱花树
t.left(90)
t.penup()
t.backward(200)
t.pendown()
draw_branch(80)
# 绘制花朵
for i in range(0, 50):
t.penup()
x = random.randint(-300, 300)
y = random.randint(0, 300)
t.goto(x, y)
t.pendown()
draw_petal(random.randint(10, 30))
# 隐藏画笔
t.hideturtle()
# 显示画布
turtle.done()
```
阅读全文