網站首頁 編程語言 正文
python煙花代碼
如下
# -*- coding: utf-8 -*-
import math, random,time
import threading
import tkinter as tk
import re
#import uuid
Fireworks=[]
maxFireworks=8
height,width=600,600
class firework(object):
def __init__(self,color,speed,width,height):
#uid=uuid.uuid1()
self.radius=random.randint(2,4) #粒子半徑為2~4像素
self.color=color #粒子顏色
self.speed=speed #speed是1.5-3.5秒
self.status=0 #在煙花未爆炸的情況下,status=0;爆炸后,status>=1;當status>100時,煙花的生命期終止
self.nParticle=random.randint(20,30) #粒子數量
self.center=[random.randint(0,width-1),random.randint(0,height-1)] #煙花隨機中心坐標
self.oneParticle=[] #原始粒子坐標(100%狀態時)
self.rotTheta=random.uniform(0,2*math.pi) #橢圓平面旋轉角
#橢圓參數方程:x=a*cos(theta),y=b*sin(theta)
#ellipsePara=[a,b]
self.ellipsePara=[random.randint(30,40),random.randint(20,30)]
theta=2*math.pi/self.nParticle
for i in range(self.nParticle):
t=random.uniform(-1.0/16,1.0/16) #產生一個 [-1/16,1/16) 的隨機數
x,y=self.ellipsePara[0]*math.cos(theta*i+t), self.ellipsePara[1]*math.sin(theta*i+t) #橢圓參數方程
xx,yy=x*math.cos(self.rotTheta)-y*math.sin(self.rotTheta), y*math.cos(self.rotTheta)+x*math.sin(self.rotTheta) #平面旋轉方程
self.oneParticle.append([xx,yy])
self.curParticle=self.oneParticle[0:] #當前粒子坐標
self.thread=threading.Thread(target=self.extend) #建立線程對象
def extend(self): #粒子群狀態變化函數線程
for i in range(100):
self.status+=1 #更新狀態標識
self.curParticle=[[one[0]*self.status/100, one[1]*self.status/100] for one in self.oneParticle] #更新粒子群坐標
time.sleep(self.speed/50)
def explode(self):
self.thread.setDaemon(True) #把現程設為守護線程
self.thread.start() #啟動線程
def __repr__(self):
return ('color:{color}
'
'speed:{speed}
'
'number of particle: {np}
'
'center:[{cx} , {cy}]
'
'ellipse:a={ea} , b={eb}
'
'particle:
{p}
'
).format(color=self.color,speed=self.speed,np=self.nParticle,cx=self.center[0],cy=self.center[1],p=str(self.oneParticle),ea=self.ellipsePara[0],eb=self.ellipsePara[1])
def colorChange(fire):
rgb=re.findall(r'(.{2})',fire.color[1:])
cs=fire.status
f=lambda x,c: hex(int(int(x,16)*(100-c)/30))[2:] #當粒子壽命到70%時,顏色開始線性衰減
if cs>70:
ccr,ccg,ccb=f(rgb[0],cs),f(rgb[1],cs),f(rgb[2],cs)
else:
ccr,ccg,ccb=rgb[0],rgb[1],rgb[2]
return '#{0:0>2}{1:0>2}{2:0>2}'.format(ccr,ccg,ccb)
def appendFirework(n=1): #遞歸生成煙花對象
if n>maxFireworks or len(Fireworks)>maxFireworks:
pass
elif n==1:
cl='#{0:0>6}'.format(hex(int(random.randint(0,16777215)))[2:]) # 產生一個0~16777215(0xFFFFFF)的隨機數,作為隨機顏色
a=firework(cl,random.uniform(1.5,3.5),width,height)
Fireworks.append( {'particle':a,'points':[]} ) #建立粒子顯示列表,‘particle'為一個煙花對象,‘points'為每一個粒子顯示時的對象變量集
a.explode()
else:
appendFirework()
appendFirework(n-1)
def show(c):
for p in Fireworks: #每次刷新顯示,先把已有的所以粒子全部刪除
for pp in p['points']:
c.delete(pp)
for p in Fireworks: #根據每個煙花對象,計算其中每個粒子的顯示對象
oneP=p['particle']
if oneP.status==100: #狀態標識為100,說明煙花壽命結束
Fireworks.remove(p) #移出當前煙花
appendFirework() #新增一個煙花
continue
else:
li=[[int(cp[0]*2)+oneP.center[0],int(cp[1]*2)+oneP.center[1]] for cp in oneP.curParticle] #把中心為原點的橢圓平移到隨機圓心坐標上
color=colorChange(oneP) #根據煙花當前狀態計算當前顏色
for pp in li:
p['points'].append(c.create_oval(pp[0]-oneP.radius, pp[1]-oneP.radius, pp[0]+oneP.radius, pp[1]+oneP.radius, fill=color)) #繪制煙花每個粒子
root.after(50, show,c) #回調,每50ms刷新一次
if __name__=='__main__':
appendFirework(maxFireworks)
root = tk.Tk()
cv = tk.Canvas(root, height=height, width=width)
cv.create_rectangle(0, 0, width, height, fill="black")
cv.pack()
root.after(50, show,cv)
root.mainloop()
圖片展示
原文鏈接:https://blog.csdn.net/m0_54850467/article/details/125241220
相關推薦
- 2023-05-15 使用Bash讀取和處理CSV文件的方法_linux shell
- 2022-05-13 ByteTrack的卡爾曼濾波使用什么樣的邊框坐標信息
- 2022-04-12 Redis?Server啟動過程的詳細步驟_Redis
- 2023-05-31 python升級pip及失敗處理方式_python
- 2022-07-12 Linux下Redis6集群安裝部署指南
- 2022-06-14 C語言?auto和register關鍵字_C 語言
- 2022-11-17 Python中的優先隊列(priority?queue)和堆(heap)_python
- 2023-01-19 Android?任務棧機制詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支