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

利用Python寫一場(chǎng)新年煙花秀

 更新時(shí)間:2022年01月04日 09:35:08   作者:?Python小二?  
這篇文章主要介紹了利用Python寫一場(chǎng)新年煙花秀,可以送給朋友還可以當(dāng)作練習(xí),Python 模塊包括:tkinter、PIL、time、random、math,下面來看一下代碼實(shí)現(xiàn),需要的朋友可以參考一下

我們用到的 Python 模塊包括:tkinter、PILtime、randommath,如果第三方模塊沒有裝的話,pip install 一下即可,下面看一下代碼實(shí)現(xiàn)。

1.導(dǎo)庫(kù)

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

2.煙花顏色

colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']

3.定義煙花類

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
? ? ? ? # 綻放的粒子數(shù)
? ? ? ? self.total = total
? ? ? ? # 已停留時(shí)間
? ? ? ? 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

? ? # 更新數(shù)據(jù)
? ? def update(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 not None:
? ? ? ? ? ? cv.delete(self.cid)
? ? ? ? ? ? self.cid = None

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

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

4.燃放煙花

def ignite(cv):
? ? t = time()
? ? # 煙花列表
? ? explode_points = []
? ? wait_time = randint(10, 100)
? ? # 爆炸的個(gè)數(shù)
? ? 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)
? ? ? ? # 爆炸的粒子數(shù)半徑
? ? ? ? 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))
? ? ? ? ? ? # 添加進(jìn)粒子列表里
? ? ? ? ? ? objects.append(r)
? ? ? ? # 把粒子列表添加到煙花列表
? ? ? ? explode_points.append(objects)
? ? total_time = .0
? ? # 在 1.8 秒時(shí)間幀內(nèi)保持更新
? ? while total_time < 1.8:
? ? ? ? # 讓畫面暫停 0.01s
? ? ? ? sleep(0.01)
? ? ? ? # 刷新時(shí)間
? ? ? ? tnew = time()
? ? ? ? t, dt = tnew, tnew - t
? ? ? ? # 遍歷煙花列表
? ? ? ? for point in explode_points:
? ? ? ? ? ? # 遍歷煙花里的粒子列表
? ? ? ? ? ? for item in point:
? ? ? ? ? ? ? ? # 更新時(shí)間
? ? ? ? ? ? ? ? item.update(dt)
? ? ? ? # 刷新頁(yè)面
? ? ? ? cv.update()
? ? ? ? total_time += dt
? ? root.after(wait_time, ignite, cv)

5.啟動(dòng)

if __name__ == "__main__":
? ? root = tk.Tk()
? ? # 繪制一個(gè)畫布
? ? cv = tk.Canvas(root, height=400, width=600)
? ? # 背景圖
? ? image = Image.open("bg.jpg")
? ? photo = ImageTk.PhotoImage(image)
? ? # 在畫板上繪制一張圖片
? ? cv.create_image(0, 0, image=photo, anchor='nw')
? ? cv.pack()
? ? root.protocol(close)
? ? root.after(100, ignite, cv)
? ? # 生成窗口
? ? root.mainloop()

看一下效果:

相關(guān)文章

  • 使用python批量修改XML文件中圖像的depth值

    使用python批量修改XML文件中圖像的depth值

    這篇文章主要介紹了使用python批量修改XML文件中圖像的depth值,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 深入了解Python中描述器的使用

    深入了解Python中描述器的使用

    Python描述器是Python編程語(yǔ)言中的一個(gè)重要特性,它提供了一種靈活且強(qiáng)大的機(jī)制來控制屬性訪問行為。在本文中,我們將詳細(xì)介紹Python描述器的概念、實(shí)現(xiàn)方式以及如何使用Python描述器來增強(qiáng)我們的Python程序
    2023-03-03
  • python中判斷數(shù)字是否為質(zhì)數(shù)的實(shí)例講解

    python中判斷數(shù)字是否為質(zhì)數(shù)的實(shí)例講解

    在本篇文章里小編給大家分享了關(guān)于python中判斷數(shù)字是否為質(zhì)數(shù)的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-12-12
  • OpenCV基于ORB算法實(shí)現(xiàn)角點(diǎn)檢測(cè)

    OpenCV基于ORB算法實(shí)現(xiàn)角點(diǎn)檢測(cè)

    這篇文章主要為大家詳細(xì)介紹了OpenCV基于ORB算法實(shí)現(xiàn)角點(diǎn)檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 解鎖Python中神器vars內(nèi)置函數(shù)的使用

    解鎖Python中神器vars內(nèi)置函數(shù)的使用

    vars()函數(shù)是一個(gè)內(nèi)置函數(shù),用于返回對(duì)象的__字典__,其中包含對(duì)象的__屬性__,本文主要為大家詳細(xì)介紹了vars()函數(shù)的具體使用,需要的小伙伴可以了解下
    2023-11-11
  • 使用Python處理PDF文件的實(shí)踐分享

    使用Python處理PDF文件的實(shí)踐分享

    在現(xiàn)代數(shù)字化時(shí)代,PDF(Portable?Document?Format)文件已經(jīng)成為廣泛使用的電子文檔格式,這篇文章主要為分享了Python處理PDF文件的簡(jiǎn)介與實(shí)踐,需要的可以參考下
    2023-06-06
  • python異?;怋aseException詳解

    python異?;怋aseException詳解

    這篇文章主要為大家介紹了python異常基類BaseException詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 詳解重置Django migration的常見方式

    詳解重置Django migration的常見方式

    這篇文章主要介紹了詳解重置Django migration的常見方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • 利用Python對(duì)文件夾下圖片數(shù)據(jù)進(jìn)行批量改名的代碼實(shí)例

    利用Python對(duì)文件夾下圖片數(shù)據(jù)進(jìn)行批量改名的代碼實(shí)例

    今天小編就為大家分享一篇關(guān)于利用Python對(duì)文件夾下圖片數(shù)據(jù)進(jìn)行批量改名的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Python線程池的正確使用方法

    Python線程池的正確使用方法

    這篇文章主要介紹了Python線程池的正確使用方法,Python的線程池與Java線程池基本原理和概念是共通的。最大的區(qū)別大概就是語(yǔ)言的區(qū)別吧,感興趣的朋友可以參考下面內(nèi)容
    2021-09-09

最新評(píng)論