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

python代碼實現(xiàn)煙花實例

 更新時間:2022年01月25日 09:47:19   作者:/^Mike^/  
這篇文章主要給大家分享了python煙花詳細的代碼,文章主要以python煙花的代碼展開全文,所以解說會比較少,代碼較多。喜歡的小伙伴可以參考一下,希望對你有所幫助

實現(xiàn)代碼如下:

# -*- 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)  #粒子數(shù)量
        self.center=[random.randint(0,width-1),random.randint(0,height-1)]   #煙花隨機中心坐標
        self.oneParticle=[]    #原始粒子坐標(100%狀態(tài)時)
        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)生一個 [-1/16,1/16) 的隨機數(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:]     #當前粒子坐標
        self.thread=threading.Thread(target=self.extend)   #建立線程對象
        

    def extend(self):         #粒子群狀態(tài)變化函數(shù)線程
        for i in range(100):
            self.status+=1    #更新狀態(tài)標識
            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)    #把現(xiàn)程設(shè)為守護線程
        self.thread.start()          #啟動線程
            

    def __repr__(self):
        return ('color:{color}\n'  
                'speed:{speed}\n'
                'number of particle: {np}\n'
                'center:[{cx} , {cy}]\n'
                'ellipse:a={ea} , b={eb}\n'
                'particle:\n{p}\n'
                ).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:])   # 產(chǎn)生一個0~16777215(0xFFFFFF)的隨機數(shù),作為隨機顏色
        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:                #根據(jù)每個煙花對象,計算其中每個粒子的顯示對象
        oneP=p['particle']
        if oneP.status==100:        #狀態(tài)標識為100,說明煙花壽命結(jié)束
            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)   #根據(jù)煙花當前狀態(tài)計算當前顏色
            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)  #回調(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煙花詳細代碼的文章就介紹到這了,更多相關(guān)python煙花代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺析Python中的多條件排序?qū)崿F(xiàn)

    淺析Python中的多條件排序?qū)崿F(xiàn)

    Python中使用sort方法和sorted函數(shù)排序時關(guān)鍵就在于key參數(shù)值的編寫技巧,這里我們來舉兩個實例淺析Python中的多條件排序?qū)崿F(xiàn):
    2016-06-06
  • Python 語句的表達式和縮進

    Python 語句的表達式和縮進

    本篇文章將會使大家了解Python 語句、表達式以及它們之間的區(qū)別。還包含幾個示例來更清楚地解釋這個概念。接下來,我們將解釋如何在 Python 編程中使用多行語句和縮進,需要的朋友可以參考一下
    2021-09-09
  • python中requests庫安裝與使用詳解

    python中requests庫安裝與使用詳解

    requests是一個很實用的Python HTTP客戶端庫,爬蟲和測試服務器響應數(shù)據(jù)時經(jīng)常會用到,下面這篇文章主要給大家介紹了關(guān)于python中requests庫安裝與使用的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • python中的項目目錄結(jié)構(gòu)

    python中的項目目錄結(jié)構(gòu)

    這篇文章主要介紹了python中的項目目錄結(jié)構(gòu),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python實現(xiàn)抖音熱搜定時爬取功能

    Python實現(xiàn)抖音熱搜定時爬取功能

    這篇文章主要為大家介紹了利用Python制作的一個新摸魚神器,可以實現(xiàn)抖音熱搜定時爬取。文中的實現(xiàn)步驟講解詳細,感興趣的可以試一試
    2022-03-03
  • python爬蟲之教你如何爬取地理數(shù)據(jù)

    python爬蟲之教你如何爬取地理數(shù)據(jù)

    這篇文章主要介紹了python爬蟲之教你如何爬取地理數(shù)據(jù),文中有非常詳細的代碼示例,對正在學習python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04
  • Python列表推導式,元組推導式,字典推導式,集合推導式

    Python列表推導式,元組推導式,字典推導式,集合推導式

    這篇文章主要介紹了Python列表推導式,元組推導式,字典推導式,集合推導式,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-09-09
  • python ftplib模塊使用代碼實例

    python ftplib模塊使用代碼實例

    這篇文章主要介紹了python ftplib模塊使用代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Phantomjs抓取渲染JS后的網(wǎng)頁(Python代碼)

    Phantomjs抓取渲染JS后的網(wǎng)頁(Python代碼)

    phantomjs:我的理解就是它是一個無顯示的瀏覽器,也就是說除了不能顯示頁面內(nèi)容以外,瀏覽器能干的活兒它基本上都能干。下面我們就來利用他做點有趣的事情
    2016-05-05
  • python公司內(nèi)項目對接釘釘審批流程的實現(xiàn)

    python公司內(nèi)項目對接釘釘審批流程的實現(xiàn)

    最近把組內(nèi)的一個項目對接釘釘審批接口,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評論

汽车| 临高县| 临泉县| 定州市| 三都| 靖远县| 石台县| 女性| 乐东| 沅江市| 吴川市| 边坝县| 张家川| 云和县| 三穗县| 什邡市| 千阳县| 安溪县| 循化| 汝南县| 罗源县| 来宾市| 汤阴县| 皮山县| 贵定县| 大同县| 安岳县| 西和县| 乌鲁木齐县| 永康市| 曲阜市| 孝昌县| 奉节县| 黄浦区| 蛟河市| 慈利县| 忻州市| 府谷县| 射洪县| 顺平县| 樟树市|