帮我生成一个烟花动画的代码
时间: 2023-04-10 12:02:00 浏览: 124
以下是一个简单的烟花动画的代码,你可以根据需要进行修改和优化:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor("black")
# 定义烟花函数
def firework():
# 随机生成烟花的颜色和大小
colors = ["red", "orange", "yellow", "green", "blue", "purple", "white"]
size = random.randint(10, 50)
color = random.choice(colors)
# 绘制烟花
turtle.pencolor(color)
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(size)
turtle.end_fill()
# 绘制烟花爆炸的效果
for i in range(30):
turtle.pencolor(random.choice(colors))
turtle.pensize(random.randint(1, 5))
turtle.fd(30)
turtle.right(30)
turtle.backward(30)
turtle.left(30)
# 隐藏画笔
turtle.hideturtle()
# 循环生成烟花
while True:
x = random.randint(-300, 300)
y = random.randint(-200, 200)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
firework()
```
希望这个代码能够帮到你!