python画静态烟花_python代码绘制动态烟花(代码简单只需二十几行)
时间: 2023-09-08 17:12:10 浏览: 62
以下是Python代码绘制静态烟花:
```python
import turtle
import random
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
turtle.speed(0)
turtle.hideturtle()
for i in range(50):
x = random.randint(-400, 400)
y = random.randint(-400, 400)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
color = random.choice(colors)
turtle.color(color)
size = random.randint(10, 40)
turtle.dot(size)
turtle.done()
```
以下是Python代码绘制动态烟花:
```python
import turtle
import random
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
turtle.speed(0)
turtle.hideturtle()
for i in range(50):
x = random.randint(-400, 400)
y = random.randint(-400, 400)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
color = random.choice(colors)
turtle.color(color)
size = random.randint(10, 40)
for i in range(10):
turtle.dot(size)
turtle.penup()
turtle.forward(size * 2)
turtle.pendown()
turtle.done()
```
以上代码中,我们使用turtle库来绘制烟花。在静态烟花中,我们随机生成50个位置,并使用dot方法来绘制烟花。在动态烟花中,我们在静态烟花的基础上,加入了循环,让烟花变得动态起来。