python画烟花炸开
时间: 2023-06-29 07:14:07 浏览: 90
python实现烟花小程序
以下是一个简单的 Python 代码,可以画出烟花炸开的效果:
```python
import turtle
import random
# 设置画布
turtle.bgcolor("black")
turtle.setup(width=600, height=400)
# 定义画烟花的函数
def draw_firework(x, y):
# 设置画笔
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.pensize(3)
pen.penup()
pen.goto(x, y)
pen.pendown()
# 设置烟花颜色
colors = ["red", "orange", "yellow", "green", "blue", "purple", "white"]
color = random.choice(colors)
pen.color(color)
# 画出烟花
for i in range(30):
pen.fd(20)
pen.rt(150)
# 隐藏画笔
pen.hideturtle()
# 点击屏幕画出烟花
turtle.onscreenclick(draw_firework)
# 运行画布
turtle.mainloop()
```
运行代码后,点击画布即可画出烟花炸开的效果。每次点击都会在鼠标位置画出一个随机颜色的烟花。
阅读全文