只需一点可视化技巧,100余行Python代码和程序库Tkinter,最后我们就能达到下面这个效果 通过让画面上一个粒子分裂为X数量的粒子来模拟爆炸效果。粒子会发生“膨胀”,意思是它们会以恒速移动且相互之间的角度相等。这样就能让我们以一个向外膨胀的圆圈形式模拟出烟花绽放的画面。经过一定时间后,粒子会进入“自由落体”阶段,也就是由于重力因素它们开始坠落到地面,仿若绽放后熄灭的烟花。
Tkinter,它是Python的标准 GUI 库,广泛应用于各种各样的项目和程序开发,在Python中使用 Tkinter 可以快速的创建 GUI 应用程序。
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之外,为了能让界面有漂亮的背景,我们也导入PIL用于图像处理,以及导入其它一些包,比如time,random和math。它们能让我们更容易的控制烟花粒子的运动轨迹。。class part:
将烟花绽放转译成代码
现在我们设计一个对象,表示烟花事件中的每个粒子。每个粒子都会有一些重要的属性,支配了它的外观和移动状况:大小,颜色,位置,速度等等。
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
= 0
self
.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
如果我们回过头想想最开始的想法,就会意识到必须确保每个烟花绽放的所有粒子必须经过3个不同的阶段,即“膨胀”“坠落”和“消失”。 所以我们向粒子类中再添加一些运动函数,如下所示:
def update(self
, 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
.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
():
move_x
= cos
(radians
(self
.id*360/self
.total
))
self
.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模拟
现在我们将粒子的移动概念化,不过很明显,一个烟花不能只有一个粒子,一场烟花秀也不能只有一个烟花。我们下一步就是让Python和Tkinter以我们可控的方式向天上连续“发射”粒子。
到了这里,我们需要从操作一个粒子升级为在屏幕上展现多个烟花及每个烟花中的多个粒子。
我们的解决思路如下:创建一列列表,每个子列表是一个烟花,其包含一列粒子。每个列表中的例子有相同的x,y坐标、大小、颜色、初始速度。
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
)
我们下一步就是确保定期更新粒子的属性。这里我们设置让粒子每0.01秒更新它们的状态,在1.8秒之后停止更新(这意味着每个粒子的存在时间为1.6秒,其中1.2秒为“绽放”状态,0.4秒为“坠落”状态,0.2秒处于Tkinter将其完全移除前的边缘状态)。
total_time
= .0
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
现在,我们只需将最后两个gist合并为一个能被Tkinter调用的函数,就叫它simulate()吧。该函数会展示所有的数据项,并根据我们设置的时间更新每个数据项的属性。在我们的主代码中,我们会用一个alarm处理模块after()调用此函数,after()会等待一定的时间,然后再调用函数。
我们这里设置让Tkinter等待100个单位(1秒钟)再调取simulate。
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
)
root
.after
(100, simulate
, cv
)
root
.mainloop
()
完整代码:
'''
FIREWORKS SIMULATION WITH TKINTER
*self-containing code
*to run: simply type python simple.py in your console
*compatible with both Python 2 and Python 3
*Dependencies: tkinter, Pillow (only for background image)
*The design is based on high school physics, with some small twists only for aesthetics purpose
'''
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']
'''
Generic class for particles
particles are emitted almost randomly on the sky, forming a round of circle (a star) before falling and getting removed
from canvas
Attributes:
- id: identifier of a particular particle in a star
- x, y: x,y-coordinate of a star (point of explosion)
- vx, vy: speed of particle in x, y coordinate
- total: total number of particle in a star
- age: how long has the particle last on canvas
- color: self-explantory
- cv: canvas
- lifespan: how long a particle will last on canvas
'''
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
= 0
self
.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
))
self
.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
'''
Firework simulation loop:
Recursively call to repeatedly emit new fireworks on canvas
a list of list (list of stars, each of which is a list of particles)
is created and drawn on canvas at every call,
via update protocol inside each 'part' object
'''
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
= part
(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
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
):
"""Stops simulation loop and closes the window."""
global root
root
.quit
()
if __name__
== '__main__':
root
= tk
.Tk
()
cv
= tk
.Canvas
(root
, height
=600, 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
()
来源:https://jizhi.im/blog/post/py_make_fireworks