我们用到的 Python 模块包括:tkinter、PIL、time、random、math,如果第三方模块没有装的话,pip install 一下即可,下面看一下代码实现。
1.导库
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
2.烟花颜色
colors = [‘red’, ‘blue’, ‘yellow’, ‘white’, ‘green’, ‘orange’, ‘purple’, ‘seagreen’, ‘indigo’, ‘cornflowerblue’]
3.定义烟花类
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 轴速度
self.vy = vy
# 绽放的粒子数
self.total = total
# 已停留时间
self.age = 0
# 颜色
self.color = color
# 画布
self.cv = cv
self.cid = self.cv.create_oval(x - size, y - size,