烟花由中国古代人民较早发明,常用于盛大的典礼或表演中,也在除夕夜及元宵节中燃放用来烘托节日氛围。近年来随着环境污染的加剧,一些地区已经禁止燃放烟花了,那我们就用 Python 实现一场无污染的烟花秀。
环境
- 操作系统:Windows
- Python 版本:3.6
- 涉及模块:tkinter、PIL、time、random、math
实现
导入库:
import tkinter as tk from PIL import Image, ImageTk
from time import time, sleep
from random import choice, uniform, randint
from math import sin, cos, radians
烟花颜色:
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']
定义烟花类:
class fireworks:
def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx=0., vy=0., size=2., color='red', lifespan=2, **kwargs):
self.id = idx
# 烟花绽放 x 轴
self.x = x
# 烟花绽放 x 轴
self.y = y
self.initial_speed = explosion_speed
# 外放 x 轴速度
self.vx = vx
# 外放 y 轴速度