python新年烟花代码
时间: 2023-07-09 13:31:54 浏览: 108
以下是一个简单的 Python 新年烟花代码示例:
```python
import random
import turtle
import time
colors = ["red", "orange", "yellow", "green", "blue", "purple", "pink", "white"]
turtle.speed(0)
turtle.bgcolor("black")
while True:
# 画一个圆形作为烟花的爆炸点
turtle.color(random.choice(colors))
turtle.penup()
x = random.randint(-200, 200)
y = random.randint(-200, 200)
turtle.setpos(x, y)
turtle.pendown()
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
# 画出烟花的尾巴
turtle.penup()
turtle.setpos(x, y+10)
turtle.pendown()
for i in range(10):
turtle.color(random.choice(colors))
turtle.circle(1+i*2)
turtle.right(90)
# 清除烟花的尾巴和爆炸点
turtle.penup()
turtle.setpos(x, y+10)
turtle.pendown()
turtle.pencolor("black")
for i in range(10):
turtle.circle(1+i*2)
turtle.right(90)
turtle.penup()
turtle.setpos(x, y)
turtle.pendown()
turtle.pencolor("black")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
# 等待一段时间后清除屏幕
time.sleep(0.5)
turtle.clear()
```
该代码使用 `turtle` 模块绘制烟花效果,每次循环随机生成一个爆炸点,并在该点处绘制一个圆形。然后,使用 `turtle` 绘制圆形的尾巴,清除尾巴和爆炸点,等待一段时间后清除屏幕并开始下一次循环。
阅读全文