用Python编写一个烟花代码
时间: 2023-05-19 18:07:16 浏览: 104
以下是一个简单的Python烟花代码:
```python
import random
import turtle
turtle.speed(0)
turtle.hideturtle()
turtle.bgcolor("black")
colors = ["red", "orange", "yellow", "green", "blue", "purple", "white"]
def explode(x, y):
for i in range(50):
color = random.choice(colors)
size = random.randint(10, 40)
draw_firework(x, y, color, size)
def draw_firework(x, y, color, size):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
turtle.penup()
turtle.goto(x, y + 5)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
turtle.circle(size)
turtle.end_fill()
turtle.onclick(explode)
turtle.mainloop()
```
当你点击屏幕时,会出现一个随机颜色和大小的烟花。
阅读全文