python樱花树动态源代码
时间: 2023-11-08 20:58:15 浏览: 325
Python画樱花树代码
5星 · 资源好评率100%
以下是Python樱花树动态源代码:
```python
import turtle
import random
# 设置画布大小
turtle.setup(800, 600)
# 设置画笔速度
turtle.speed(10)
# 设置画笔颜色
turtle.pencolor("white")
# 设置画布背景色
turtle.bgcolor("black")
# 定义花瓣
def petal():
turtle.fillcolor(random.uniform(0.5, 1), random.uniform(0, 0.5), random.uniform(0.5, 1))
turtle.begin_fill()
turtle.circle(10, 90)
turtle.left(90)
turtle.circle(10, 90)
turtle.end_fill()
# 定义花枝
def tree(branch_len):
if branch_len > 3:
if branch_len > 10:
turtle.pensize(branch_len / 10)
else:
turtle.pensize(2)
turtle.forward(branch_len)
turtle.right(20)
petal_num = random.randint(3, 5)
for i in range(petal_num):
petal()
turtle.right(360 / petal_num)
tree(branch_len - random.randint(10, 20))
turtle.left(40)
petal_num = random.randint(3, 5)
for i in range(petal_num):
petal()
turtle.right(360 / petal_num)
tree(branch_len - random.randint(10, 20))
turtle.right(20)
turtle.pensize(branch_len / 10)
turtle.backward(branch_len)
# 定义主函数
def main():
turtle.left(90)
turtle.penup()
turtle.backward(200)
turtle.pendown()
tree(100)
turtle.exitonclick()
# 运行主函数
if __name__ == '__main__':
main()
```
运行以上代码,将会在画布上绘制出一棵樱花树,效果如下:
![樱花树动态效果](https://cdn.luogu.com.cn/upload/image_hosting/7brl5piv.png)
阅读全文