用 Python 實現一場環保無汙染的烟花秀

2021-10-01 08:08:05 字數 4379 閱讀 4595

烟花由中國古代人民較早發明,常用於盛大的典禮或表演中,也在除夕夜及元宵節中燃放用來烘托節日氛圍。近年來隨著環境汙染的加劇,一些地區已經禁止燃放煙花了,那我們就用 python 實現一場無汙染的烟花秀。

導入庫

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 軸速度

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

# 更新資料

defupdate

(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 +

0.5* dt)

self.vy +=

0.5* dt

# 過期移除

elif self.cid is

notnone

: cv.delete(self.cid)

self.cid =

none

# 定義膨脹效果的時間幀

defexpand

(self)

:return self.age <=

1.5# 檢查粒子是否仍在生命週期內

defalive

(self)

:return self.age <= self.lifespan

燃放煙花
def

ignite

(cv)

: t = time(

)# 烟花列表

explode_points =

wait_time = randint(10,

100)

# **的個數

numb_explode = randint(6,

10)for point in

range

(numb_explode)

:# **粒子列表

objects =

# ** x 軸

x_cordi = randint(50,

550)

# ** y 軸

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 = fireworks(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))

# 新增進粒子列表裡

# 把粒子列表新增到烟花列表

total_time =

.0# 在 1.8 秒時間幀內保持更新

while total_time <

1.8:

# 讓畫面暫停 0.01s

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, ignite, cv)

啟動
if __name__ ==

"__main__"

: root = tk.tk(

)# 繪製乙個畫布

cv = tk.canvas(root, height=

400, width=

600)

# 背景圖

# 在畫板上繪製一張

cv.create_image(0,

0, image=photo, anchor=

'nw'

) cv.pack(

) root.protocol(close)

root.after(

100, ignite, cv)

# 生成視窗

root.mainloop(

)

最終效果,如圖所示:

快到春節了,用 Python 實現一場烟花秀

春節的腳步越來越近了,每逢春節很多地方都會燃放煙花來增添節日的氣氛,然而因環境汙染的加劇,近年來不少地方已經禁止燃放煙花了,為了彌補這個遺憾,本文我們來看一下如何使用 python 來實現一場烟花秀。功能實現用到的 python 庫包括 tkinter pil time random math,如果...

高甜時刻 用Python畫出一場櫻花祭

先上效果圖 在執行 前首先要裝好turtle庫 在random庫的操作下,每次畫出的櫻花樹都是隨機的 import turtle import random from turtle import from time import sleep 畫櫻花的軀幹 60 t def tree branchle...

用python編寫乙個無重複子串

給定乙個字串,請你找出其中不含有重複字元的 最長子串 的長度。示例 1 輸入 abcabcbb 輸出 3 解釋 因為無重複字元的最長子串是 abc 所以其長度為 3。示例 2 輸入 bbbbb 輸出 1 解釋 因為無重複字元的最長子串是 b 所以其長度為 1。示例 3 輸入 pwwkew 輸出 3 ...