最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Python實(shí)現(xiàn)煙花效果的示例代碼

 更新時(shí)間:2022年06月13日 09:37:40   作者:m0_54850467  
這篇文章主要為大家詳細(xì)介紹了如何利用Python制作出煙花的效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下

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;當(dāng)status>100時(shí),煙花的生命期終止
        self.nParticle=random.randint(20,30)  #粒子數(shù)量
        self.center=[random.randint(0,width-1),random.randint(0,height-1)]   #煙花隨機(jī)中心坐標(biāo)
        self.oneParticle=[]    #原始粒子坐標(biāo)(100%狀態(tài)時(shí))
        self.rotTheta=random.uniform(0,2*math.pi)  #橢圓平面旋轉(zhuǎn)角

        #橢圓參數(shù)方程: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)  #產(chǎn)生一個(gè) [-1/16,1/16) 的隨機(jī)數(shù)
            x,y=self.ellipsePara[0]*math.cos(theta*i+t), self.ellipsePara[1]*math.sin(theta*i+t)    #橢圓參數(shù)方程
            xx,yy=x*math.cos(self.rotTheta)-y*math.sin(self.rotTheta),  y*math.cos(self.rotTheta)+x*math.sin(self.rotTheta)     #平面旋轉(zhuǎn)方程
            self.oneParticle.append([xx,yy])
        
        self.curParticle=self.oneParticle[0:]     #當(dāng)前粒子坐標(biāo)
        self.thread=threading.Thread(target=self.extend)   #建立線程對(duì)象
        

    def extend(self):         #粒子群狀態(tài)變化函數(shù)線程
        for i in range(100):
            self.status+=1    #更新狀態(tài)標(biāo)識(shí)
            self.curParticle=[[one[0]*self.status/100, one[1]*self.status/100] for one in self.oneParticle]   #更新粒子群坐標(biāo)
            time.sleep(self.speed/50)
    
    def explode(self):
        self.thread.setDaemon(True)    #把現(xiàn)程設(shè)為守護(hù)線程
        self.thread.start()          #啟動(dòng)線程
            

    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:]    #當(dāng)粒子壽命到70%時(shí),顏色開始線性衰減
    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):   #遞歸生成煙花對(duì)象
    if n>maxFireworks or len(Fireworks)>maxFireworks:
        pass
    elif n==1:
        cl='#{0:0>6}'.format(hex(int(random.randint(0,16777215)))[2:])   # 產(chǎn)生一個(gè)0~16777215(0xFFFFFF)的隨機(jī)數(shù),作為隨機(jī)顏色
        a=firework(cl,random.uniform(1.5,3.5),width,height)
        Fireworks.append( {'particle':a,'points':[]} )   #建立粒子顯示列表,‘particle'為一個(gè)煙花對(duì)象,‘points'為每一個(gè)粒子顯示時(shí)的對(duì)象變量集
        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:                #根據(jù)每個(gè)煙花對(duì)象,計(jì)算其中每個(gè)粒子的顯示對(duì)象
        oneP=p['particle']
        if oneP.status==100:        #狀態(tài)標(biāo)識(shí)為100,說明煙花壽命結(jié)束
            Fireworks.remove(p)     #移出當(dāng)前煙花
            appendFirework()           #新增一個(gè)煙花
            continue
        else:
            li=[[int(cp[0]*2)+oneP.center[0],int(cp[1]*2)+oneP.center[1]] for cp in oneP.curParticle]       #把中心為原點(diǎn)的橢圓平移到隨機(jī)圓心坐標(biāo)上
            color=colorChange(oneP)   #根據(jù)煙花當(dāng)前狀態(tài)計(jì)算當(dāng)前顏色
            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))  #繪制煙花每個(gè)粒子

    root.after(50, show,c)  #回調(diào),每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()

圖片展示

到此這篇關(guān)于基于Python實(shí)現(xiàn)煙花效果的示例代碼的文章就介紹到這了,更多相關(guān)Python煙花內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 簡(jiǎn)單實(shí)例帶你了解Python的編譯和執(zhí)行全過程

    簡(jiǎn)單實(shí)例帶你了解Python的編譯和執(zhí)行全過程

    python 是一種解釋型的編程語言,所以不像編譯型語言那樣需要顯式的編譯過程。然而,在 Python 代碼執(zhí)行之前,它需要被解釋器轉(zhuǎn)換成字節(jié)碼,這個(gè)過程就是 Python 的編譯過程,還不知道的朋友快來看看吧
    2023-04-04
  • python的函數(shù)形參和返回值你了解嗎

    python的函數(shù)形參和返回值你了解嗎

    這篇文章主要為大家詳細(xì)介紹了python的函數(shù)形參和返回值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • python pyinstaller打包exe報(bào)錯(cuò)的解決方法

    python pyinstaller打包exe報(bào)錯(cuò)的解決方法

    這篇文章主要給大家介紹了關(guān)于python pyinstaller打包exe報(bào)錯(cuò)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Python數(shù)據(jù)結(jié)構(gòu)之單鏈表詳解

    Python數(shù)據(jù)結(jié)構(gòu)之單鏈表詳解

    這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)結(jié)構(gòu)之單鏈表的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 深入探討Python中的RegEx模式匹配

    深入探討Python中的RegEx模式匹配

    正則表達(dá)式通??s寫為?regex,是處理文本的有效工具,這篇文章主要來和大家深入探討一下Python中的RegEx模式匹配,感興趣的可以了解一下
    2023-07-07
  • Python入門教程(三十六)Python的文件寫入

    Python入門教程(三十六)Python的文件寫入

    這篇文章主要介紹了Python入門教程(三十六)Python的文件寫入,open()函數(shù)可以打開一個(gè)文件供讀取或?qū)懭?,如果這個(gè)函數(shù)執(zhí)行成功,會(huì)回傳文件對(duì)象,需要的朋友可以參考下
    2023-05-05
  • Python中的urllib庫高級(jí)用法教程

    Python中的urllib庫高級(jí)用法教程

    這篇文章主要介紹了Python中的urllib庫高級(jí)用法教程,想要請(qǐng)求需要設(shè)置一些請(qǐng)求頭,如果要在請(qǐng)求的時(shí)候增加一些請(qǐng)求頭,那么就必須使用request.Request類來實(shí)現(xiàn)了,比如要增加一個(gè) User-Agent ,增加一個(gè) Referer 頭信息等,需要的朋友可以參考下
    2023-10-10
  • python中有函數(shù)重載嗎

    python中有函數(shù)重載嗎

    在本篇內(nèi)容里下邊給大家整理的是關(guān)于python函數(shù)重載的知識(shí)點(diǎn)總結(jié),有需要的朋友們可以學(xué)習(xí)下。
    2020-05-05
  • Pytorch 多維數(shù)組運(yùn)算過程的索引處理方式

    Pytorch 多維數(shù)組運(yùn)算過程的索引處理方式

    今天小編就為大家分享一篇Pytorch 多維數(shù)組運(yùn)算過程的索引處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python 類屬性與實(shí)例屬性,類對(duì)象與實(shí)例對(duì)象用法分析

    Python 類屬性與實(shí)例屬性,類對(duì)象與實(shí)例對(duì)象用法分析

    這篇文章主要介紹了Python 類屬性與實(shí)例屬性,類對(duì)象與實(shí)例對(duì)象用法,結(jié)合實(shí)例形式分析了java類相關(guān)的屬性、實(shí)例化、對(duì)象等相關(guān)概念與操作技巧,需要的朋友可以參考下
    2019-09-09

最新評(píng)論

垫江县| 米脂县| 石棉县| 临城县| 彰化县| 会理县| 南木林县| 望都县| 长宁区| 绥滨县| 台前县| 宜阳县| 慈溪市| 黎城县| 石河子市| 离岛区| 囊谦县| 新丰县| 鱼台县| 凤阳县| 政和县| 尼勒克县| 城口县| 灌南县| 吴忠市| 高州市| 惠安县| 康马县| 六盘水市| 南陵县| 安西县| 福安市| 德令哈市| 桑植县| 会宁县| 西充县| 伊金霍洛旗| 剑阁县| 中超| 来安县| 商洛市|