用Python写出烟花
时间: 2023-09-25 13:15:43 浏览: 69
用python写的烟花代码
很高兴回答你的问题,以下是一个简单的Python代码实现烟花效果:
```python
import turtle
import random
def explosion(t,x,y,color):
t.speed(0)
t.pensize(4)
for i in range(40):
t.pencolor(color[random.randint(0,len(color)-1)])
t.penup()
t.goto(x,y)
t.pendown()
t.setheading(random.randint(0,360))
distance = random.randint(50,100)
t.forward(distance)
t.backward(distance)
t.penup()
t.goto(0,0)
def main():
t = turtle.Turtle()
turtle.bgcolor("black")
colors = ["red","orange","yellow","green","blue","purple"]
t.hideturtle()
for i in range(5):
x = random.randint(-200,200)
y = random.randint(-200,200)
color = random.randint(0,5)
explosion(t,x,y,colors)
if __name__ == "__main__":
main()
```
希望这能帮到你,如果还有其他问题请随时问我哦~
阅读全文