# 项目实操——用python上演一场浪漫的烟花秀

2年前 阅读 796 点赞 2

### 基础知识：用Python和Tkinter设计烟花

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


Tkinter应用的基本设置如下：

root = tk.Tk()


w = tk.Label(root, text="Hello Tkinter!")


w.pack()
root.mainloop()


### 将烟花绽放转译成代码

'''
particles 类

- id: 粒子的id
- x, y: 粒子的坐标
- vx, vy: 在坐标的变化速度
- total: 总数
- age: 粒子存在的时长
- color: 颜色
- cv: 画布
- lifespan: 最高存在时长

'''
class part: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
self.x = x
self.y = y
self.initial_speed = explosion_speed
self.vx = vx
self.vy = vy
self.total = total
self.age = 0self.color = color
self.cv = cv
self.cid = self.cv.create_oval(
x - size, y - size, x + size,
y + size, fill=self.color)
self.lifespan = lifespan


def update(self, dt):
# 粒子膨胀if self.alive() and self.expand():
self.vx = move_x/(float(dt)*1000)
self.vy = move_y/(float(dt)*1000)
self.cv.move(self.cid, move_x, move_y)

# 以自由落体坠落
elif self.alive():
# we technically don't need to update x, y because move will do the jobself.cv.move(self.cid, self.vx + move_x, self.vy+GRAVITY*dt)self.vy += GRAVITY*dt

# 如果粒子的生命周期已过，就将其移除
elif self.cid is not None:
cv.delete(self.cid)
self.cid = None


# 定义膨胀效果的时间帧
def expand (self):
return self.age <= 1.2

# 检查粒子是否仍在生命周期内
def alive(self):
return self.age <= self.lifespan


### 使用Tkinter模拟

numb_explode = randint(6,10)
# 为所有模拟烟花绽放的全部粒子创建一列列表
for point in range(numb_explode):
objects = []
x_cordi = randint(50,550)
y_cordi = randint(50, 150)
size = uniform (0.5,3)
color = choice(colors)
explosion_speed = uniform(0.2, 1)
total_particles = randint(10,50)
for i in range(1,total_particles):
r = part(cv, idx = i, total = total_particles, explosion_speed = explosion_speed, x = x_cordi, y = y_cordi,
color=color, size = size, lifespan = uniform(0.6,1.75))
objects.append(r)
explode_points.append(objects)


total_time = .0
# 在1.8秒时间帧内保持更新
while total_time < 1.8:
sleep(0.01)
tnew = time()
t, dt = tnew, tnew - t
for point in explode_points:
for part in point:
part.update(dt)
cv.update()
total_time += dt


if __name__ == '__main__':
root = tk.Tk()
cv = tk.Canvas(root, height=600, width=600)
# 绘制一个黑色背景
cv.create_rectangle(0, 0, 600, 600, fill="black")
cv.pack()

root.protocol("WM_DELETE_WINDOW", close)
# 在1秒后才开始调用stimulate()
root.after(100, simulate, cv)
root.mainloop()


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

# 模拟重力
GRAVITY = 0.05
# 颜色选项（随机或者按顺序）
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']

'''
particles 类

- id: 粒子的id
- x, y: 粒子的坐标
- vx, vy: 在坐标的变化速度
- total: 总数
- age: 粒子存在的时长
- color: 颜色
- cv: 画布
- lifespan: 最高存在时长

'''

class Particle: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
self.x = x
self.y = y
self.initial_speed = explosion_speed
self.vx = vx
self.vy = vy
self.total = total
self.age = 0self.color = color
self.cv = cv
self.cid = self.cv.create_oval(
x - size, y - size, x + size,
y + size, fill=self.color)
self.lifespan = lifespan

def update(self, dt):
self.age += dt

# 粒子范围扩大if self.alive() and self.expand():
move_x = cos(radians(self.id * 360 / self.total)) * self.initial_speed
move_y = sin(radians(self.id * 360 / self.total)) * self.initial_speed
self.cv.move(self.cid, move_x, move_y)
self.vx = move_x / (float(dt) * 1000)

# 以自由落体坠落elif self.alive():
move_x = cos(radians(self.id * 360 / self.total))
# we technically don't need to update x, y because move will do the jobself.cv.move(self.cid, self.vx + move_x, self.vy + GRAVITY * dt)
self.vy += GRAVITY * dt

# 移除超过最高时长的粒子elif self.cid is not None:
cv.delete(self.cid)
self.cid = None

# 扩大的时间def expand(self):return self.age <= 1.2

# 粒子是否在最高存在时长内def alive(self):return self.age <= self.lifespan

'''

'''

def simulate(cv):
t = time()
explode_points = []
wait_time = randint(10, 100)
numb_explode = randint(6, 10)
# 创建一个所有粒子同时扩大的二维列表for point in range(numb_explode):
objects = []
x_cordi = randint(50, 550)
y_cordi = randint(50, 150)
speed = uniform(0.5, 1.5)
size = uniform(0.5, 3)
color = choice(colors)
explosion_speed = uniform(0.2, 1)
total_particles = randint(10, 50)
for i in range(1, total_particles):
r = Particle(cv, idx=i, total=total_particles, explosion_speed=explosion_speed, x=x_cordi, y=y_cordi,
vx=speed, vy=speed, color=color, size=size, lifespan=uniform(0.6, 1.75))
objects.append(r)
explode_points.append(objects)

total_time = .0# 1.8s内一直扩大while total_time < 1.8:
sleep(0.01)
tnew = time()
t, dt = tnew, tnew - t
for point in explode_points:for item in point:
item.update(dt)
cv.update()
total_time += dt
# 循环调用
root.after(wait_time, simulate, cv)

def close(*ignore):"""退出程序、关闭窗口"""global root
root.quit()

if __name__ == '__main__':
root = tk.Tk()
cv = tk.Canvas(root, height=400, width=600)
# 选一个好看的背景会让效果更惊艳！
image = Image.open("./image.jpg")
photo = ImageTk.PhotoImage(image)

cv.create_image(0, 0, image=photo, anchor='nw')
cv.pack()

root.protocol("WM_DELETE_WINDOW", close)
root.after(100, simulate, cv)
root.mainloop()