python烟花跨年
时间: 2024-01-01 07:23:41 浏览: 79
以下是一个使用turtle库在Python中实现烟花跨年的例子:
```python
import turtle
import random
# 创建烟花
def create_firework():
fireworks = []
shapes = ['circle', 'classic', 'square', 'triangle', 'arrow', 'turtle']
for i in range(25):
new_firework = turtle.Turtle()
new_firework.speed('fastest')
new_firework.up()
new_firework.ht()
new_firework.shape(random.choice(shapes))
new_firework.shapesize(0.5, 2.5)
fireworks.append(new_firework)
return fireworks
# 爆炸效果
def explode(fireworks):
for firework in fireworks:
firework.goto(random.randint(-200, 200), random.randint(-200, 200))
firework.showturtle()
# 主函数
def main():
turtle.setup(800, 600)
turtle.bgcolor('black')
turtle.title('2022烟花跨年')
fireworks = create_firework()
while True:
explode(fireworks)
turtle.update()
turtle.clear()
turtle.done()
# 运行主函数
if __name__ == '__main__':
main()
```
这段代码使用turtle库创建了25个烟花,每个烟花的形状随机选择。然后,烟花会在屏幕上随机位置爆炸,形成跨年的烟花效果。
阅读全文