樱花树动态代码python
时间: 2023-11-16 13:55:16 浏览: 174
Python画樱花树代码
5星 · 资源好评率100%
以下是绘制樱花树的 Python 代码:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('pink')
# 定义画樱花的函数
def draw_sakura(petal_num, petal_size, x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for i in range(petal_num):
turtle.begin_fill()
turtle.circle(random.randint(petal_size-10, petal_size+10))
turtle.end_fill()
turtle.right(random.randint(0, 360))
turtle.forward(random.randint(petal_size-10, petal_size+10))
# 绘制樱花树
for i in range(50):
x = random.randint(-400, 400)
y = random.randint(-200, 200)
draw_sakura(random.randint(5, 10), random.randint(20, 40), x, y)
# 隐藏画笔
turtle.hideturtle()
# 点击关闭窗口
turtle.exitonclick()
```
阅读全文