欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python粒子煙花動(dòng)態(tài)效果實(shí)現(xiàn)

 更新時(shí)間:2023年01月03日 11:13:51   作者:輕松學(xué)Python  
這篇文章主要介紹了Python實(shí)現(xiàn)粒子煙花動(dòng)態(tài)效果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

跨年倒計(jì)時(shí)還有18天?我已經(jīng)開始整煙花了,雖然不是很好看吧,但是也能將就看看 ??

這個(gè)的背景圖,音樂,還有文字都是可以自己修改的哦

效果展示

依次導(dǎo)入本次需要使用到的模塊

import random
import pygame as py
import tkinter as tk
from time import time, sleep
from tkinter import filedialog
from PIL import Image, ImageTk
from math import sin, cos, radians
from random import choice, uniform, randint

實(shí)現(xiàn)代碼

生成隨機(jī)顏色

def randomcolor():
    #生成隨機(jī)顏色
    colArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
    color = ""
    for i in range(6):
        color += colArr[random.randint(0,14)]
    return "#"+color
GRAVITY = 0.06
#重力變量
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen','indigo', 'cornflowerblue', 'pink']
#顏色列表

屬性

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: 粒子的id
    - x, y: 粒子的坐標(biāo)
    - vx, vy: 粒子在對應(yīng)坐標(biāo)的變化速度
    - total:一顆煙花里的粒子總數(shù)
    - age: 粒子在畫布上停留的時(shí)間
    - color: 自我移植
    - cv: 畫布
    - lifespan: 粒子在畫布上停留的時(shí)間

粒子運(yùn)動(dòng)的速度

這個(gè)里面的新年快樂是可以自己更改的哦

class part:
#為每一個(gè)煙花綻放出來的粒子單獨(dú)構(gòu)建一個(gè)類的對象 ,每個(gè)粒子都會有一些重要的屬性,決定它的外觀(大小、顏色)、移動(dòng)速度等
    def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx = 0., vy = 0., size=3., color = 'red', lifespan = 2, **kwargs):
        self.id = idx
        #每個(gè)煙花的特定標(biāo)識符
        self.x = x
        #煙花綻放x軸
        self.y = y
        #煙花綻放y軸
        self.initial_speed = explosion_speed
        #粒子初始速度
        self.vx = vx
        #粒子運(yùn)動(dòng)x軸速度
        self.vy = vy
        #粒子運(yùn)動(dòng)y軸速度
        self.total = total
        #綻放粒子數(shù)
        self.age = 0
        #粒子已停留時(shí)間
        self.color = color
        #粒子顏色
        self.cv = cv
        #畫布
        self.cid = self.cv.create_oval(x - size, y - size, x + size,y + size, fill=self.color, outline='white',width=0.01)
        #指定一個(gè)限定矩形(Tkinter 會自動(dòng)在這個(gè)矩形內(nèi)繪制一個(gè)橢圓)
        self.lifespan = lifespan
        #粒子在畫布上停留的時(shí)間

xy軸移動(dòng)位移

如果粒子僅存活不擴(kuò)張(只是停留時(shí)間足夠,說明膨脹到最大了),則自由墜落

elif self.alive():
    columnFont = ('華文行楷',14)
    self.cv.create_text(250, 100, text='新',tag="write_tag", fill=choice(colors),font = columnFont) #字體
    self.cv.create_text(300, 100,  text='年',tag="write_tag", fill=choice(colors),font = columnFont)
    self.cv.create_text(350, 100, text='快',tag="write_tag", fill=choice(colors),font = columnFont)
    self.cv.create_text(400, 100,  text='樂',tag="write_tag", fill=choice(colors),font = columnFont)
    #刪除文字標(biāo)簽
    move_x = cos(radians(self.id*360/self.total))
    #x軸的移動(dòng)位移
    # we technically don't need to update x, y because move will do the job
    self.cv.move(self.cid, self.vx + move_x, self.vy+GRAVITY*dt)
    self.vy += GRAVITY*dt
    #更新y軸

膨脹效果時(shí)間幀

判斷膨脹時(shí)間是否小于1.2秒

def expand (self):
    #定義膨脹效果時(shí)間幀
    return self.age <= 1.2

判斷粒子是否仍在生命周期內(nèi)

判斷已停留時(shí)間是否小于應(yīng)該停留時(shí)間

def alive(self):
    #判斷粒子是否仍在生命周期內(nèi)
    return self.age <= self.lifespan

剩下代碼

不想一步步寫出來咯,有點(diǎn)麻煩哈哈

代碼后面都有注釋哈

def simulate(cv):
    t = time()
    #返回自1970年后經(jīng)過的浮點(diǎn)秒數(shù),精確到小數(shù)點(diǎn)后7位
    explode_points = []
    #爆炸點(diǎn)列表,煙花列表
    wait_time = randint(10,100)
    #等待時(shí)間為10到100之間整數(shù)
    numb_explode = randint(8,20)
    #爆炸煙花個(gè)數(shù)時(shí)6到10之間的隨機(jī)整數(shù)
    # create list of list of all particles in all simultaneous explosion
    for point in range(numb_explode):
        #為所有模擬煙花綻放的全部粒子創(chuàng)建一列列表
        if point<=4:
            objects = []
            #每個(gè)點(diǎn)的爆炸粒子列表粒子列表
            x_cordi = 250 + point*50
            #每個(gè)爆炸點(diǎn)的x軸
            y_cordi = 100
            #每個(gè)爆炸點(diǎn)的y軸
            speed = uniform (0.5, 2.5)
            #每個(gè)爆炸點(diǎn)的速度
            size = uniform (0.5,3)
            #每個(gè)爆炸點(diǎn)的大小
            color = choice(colors)
            #每個(gè)爆炸點(diǎn)的顏色
            explosion_speed = uniform(0.6, 3)
            #爆炸的綻放速度
            total_particles = randint(10,60)
            #煙花的總粒子數(shù)
            for i in range(1,total_particles):
            #同一個(gè)煙花爆炸出來的粒子大小、速度、坐標(biāo)都是相同的
                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))
                #把上述參數(shù)代入part函數(shù),但是每個(gè)粒子的生存時(shí)間是自己獨(dú)立的
                objects.append(r)
                #把r添加進(jìn)粒子列表
            explode_points.append(objects)
            #把粒子列表添加進(jìn)煙花列表
        else: 
            objects = []
            #每個(gè)點(diǎn)的爆炸粒子列表粒子列表
            x_cordi = randint(50,550)
            #每個(gè)爆炸點(diǎn)的x軸
            y_cordi = randint(50, 250)
            #每個(gè)爆炸點(diǎn)的y軸
            speed = uniform (0.5, 2.5)
            #每個(gè)爆炸點(diǎn)的速度
            size = uniform (0.5,3)
            #每個(gè)爆炸點(diǎn)的大小
            color = choice(colors)
            #每個(gè)爆炸點(diǎn)的顏色
            explosion_speed = uniform(0.3, 2)
            #爆炸的綻放速度
            total_particles = randint(10,50)
            #煙花的總粒子數(shù)
            for i in range(1,total_particles):
            #同一個(gè)煙花爆炸出來的粒子大小、速度、坐標(biāo)都是相同的
                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))
                #把上述參數(shù)代入part函數(shù),但是每個(gè)粒子的生存時(shí)間是自己獨(dú)立的
                objects.append(r)
                #把r添加進(jìn)粒子列表
            explode_points.append(objects)
            #把粒子列表添加進(jìn)煙花列表
    total_time = .0
    #初始化總時(shí)間
    # keeps undate within a timeframe of 1.8 second 
    while total_time < 2:
    #當(dāng)總時(shí)間小于1.8秒時(shí)運(yùn)行該循環(huán)
        sleep(0.03)
        #讓畫面暫停0.01秒
        tnew = time()
        #刷新時(shí)間
        t, dt = tnew, tnew - t
        #時(shí)間等于新時(shí)間,和上次時(shí)間間隔為tnew-t
        for point in explode_points:
        #遍歷煙花列表
            for item in point:
            #遍歷煙花里的粒子列表
                item.update(dt)
                #粒子更新時(shí)間
        cv.update()
        #刷新畫布
        total_time += dt
        #為while循環(huán)增加時(shí)間
    root.after(wait_time, simulate, cv)
    #將組件置于其他組件之后,放在最頂層,覆蓋下面的,遞歸調(diào)用自己,形成新一輪的爆炸
def close(*ignore):
    #打開模擬循環(huán)并關(guān)閉窗口
    """Stops simulation loop and closes the window."""
    global root
    root.quit()
if __name__ == '__main__': 
    root = tk.Tk() 
    root.title('新年快樂~~')  # 設(shè)置窗體的標(biāo)題欄
    cv = tk.Canvas(root, height=600, width=600)
    #繪制一個(gè)高600,寬600的畫布 
    bgpath = filedialog.askopenfilename(title='請選擇背景圖片')
    #選擇背景圖片
    image = Image.open(bgpath)
    #打開背景圖片
    image = image.resize((600,600), Image.ANTIALIAS)
    #把背景圖片調(diào)整成窗口大小
    photo = ImageTk.PhotoImage(image) 
    cv.create_image(0, 0, image=photo, anchor='nw')
    #在畫布上繪制加載的背景圖片 
    bgmusic = filedialog.askopenfilename(title='請選擇背景音樂')
    py.mixer.init()
    # 初始化
    py.mixer.music.load(bgmusic)
    # 文件加載
    py.mixer.music.play(-1, 0, fade_ms=50)
    # 播放  第一個(gè)是播放值 -1代表循環(huán)播放, 第二個(gè)參數(shù)代表開始播放的時(shí)間
    py.mixer.music.pause() 
    #暫停
    py.mixer.music.unpause()
    #取消暫停
    cv.pack()
    #把cv添加進(jìn)去
    root.protocol("WM_DELETE_WINDOW", close)
    root.after(200, simulate, cv)
    #在0.1秒后再調(diào)用stimulate函數(shù),生成一輪煙花綻放效果
    root.mainloop()
    #執(zhí)行root,生成窗口

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

相關(guān)文章

  • 淺析python中的迭代與迭代對象

    淺析python中的迭代與迭代對象

    在本文總小編給大家整理了關(guān)于python之中的迭代與迭代對象的相關(guān)基礎(chǔ)知識點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2018-10-10
  • 如何在sublime編輯器中安裝python

    如何在sublime編輯器中安裝python

    這篇文章主要介紹了如何在sublime編輯器中安裝python,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • python一行sql太長折成多行并且有多個(gè)參數(shù)的方法

    python一行sql太長折成多行并且有多個(gè)參數(shù)的方法

    今天小編就為大家分享一篇python一行sql太長折成多行并且有多個(gè)參數(shù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python中的pandas庫簡介及其使用教程

    Python中的pandas庫簡介及其使用教程

    pandas是用于數(shù)據(jù)挖掘的Python庫,Pandas中常見的數(shù)據(jù)結(jié)構(gòu)有Series和DateFrame兩種方式,今天通過本文給大家講解Python中的pandas庫簡介及其使用,感興趣你跟隨小編一起學(xué)習(xí)吧
    2022-11-11
  • Python使用Scapy實(shí)現(xiàn)構(gòu)造特殊數(shù)據(jù)包

    Python使用Scapy實(shí)現(xiàn)構(gòu)造特殊數(shù)據(jù)包

    Scapy是一款Python庫,可用于構(gòu)建、發(fā)送、接收和解析網(wǎng)絡(luò)數(shù)據(jù)包,這篇文章主要為大家詳細(xì)介紹了python如何使用Scapy構(gòu)造特殊數(shù)據(jù)包,有需要的可以參考下
    2023-11-11
  • 詳解Python如何實(shí)現(xiàn)查看WiFi密碼

    詳解Python如何實(shí)現(xiàn)查看WiFi密碼

    這篇文章主要為大家詳細(xì)介紹了如何使用python來試試看看能不能讀取到已連接過WIFI的密碼,文中的示例代碼講解詳細(xì),?感興趣的小伙伴可以了解下
    2023-11-11
  • 如何解決Selenium包安裝成功卻無法導(dǎo)入的問題

    如何解決Selenium包安裝成功卻無法導(dǎo)入的問題

    這篇文章主要介紹了如何解決Selenium包安裝成功卻無法導(dǎo)入的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python基于百度AI實(shí)現(xiàn)OCR文字識別

    Python基于百度AI實(shí)現(xiàn)OCR文字識別

    這篇文章主要介紹了Python基于百度AI實(shí)現(xiàn)OCR文字識別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • python list元素為tuple時(shí)的排序方法

    python list元素為tuple時(shí)的排序方法

    下面小編就為大家分享一篇python list元素為tuple時(shí)的排序方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Linux CentOS7下安裝python3 的方法

    Linux CentOS7下安裝python3 的方法

    在CentOS7下,默認(rèn)安裝的就是python2.7,下面通過本文給大家分享Linux CentOS7下安裝python3 的方法,需要的朋友參考下吧
    2018-01-01

最新評論