用python画一个动态烟花代码
时间: 2023-09-08 12:12:00 浏览: 131
好的,以下是用 Python 绘制动态烟花的代码:
```python
import turtle
import random
# 创建画布
canvas = turtle.Screen()
canvas.bgcolor("black")
canvas.title("Dynamic Fireworks")
# 创建绘图笔
pen = turtle.Turtle()
pen.speed(0)
pen.penup()
pen.hideturtle()
# 定义烟花爆炸的函数
def explode(x, y):
# 绘制烟花芯
pen.goto(x, y)
pen.pendown()
pen.dot(10, "white")
pen.penup()
# 绘制烟花花瓣
for i in range(50):
pen.goto(x, y)
pen.setheading(random.randint(0, 360))
distance = random.randint(50, 200)
pen.pendown()
pen.color(random.choice(["red", "orange", "yellow", "green", "blue", "purple", "pink"]))
pen.pensize(random.randint(1, 4))
pen.forward(distance)
pen.penup()
# 注册点击事件
canvas.onclick(explode)
# 保持窗口不关闭
turtle.mainloop()
```
运行代码后,你可以在窗口上单击鼠标左键,就可以看到烟花的爆炸效果了。
阅读全文