Python tkinter實(shí)現(xiàn)春節(jié)煙花效果demo
Python實(shí)現(xiàn)春節(jié)煙花效果
就快要過(guò)年了!??!很多小伙伴已經(jīng)和朋友約好一起放煙花了,但是現(xiàn)在很多地方都禁止煙花燃放,過(guò)年沒(méi)有煙花總感覺(jué)缺少一點(diǎn)味道~為了實(shí)現(xiàn)在家和朋友看煙花的愿望~這篇文章給大家?guī)?lái)2024過(guò)年煙花代碼,讓大家都能在家和朋友一起看自己編寫(xiě)的煙花,快快收藏起來(lái)吧!
用tkinter寫(xiě)的python煙花效果
今天給你們帶來(lái)了用tkinter寫(xiě)的python煙花效果,點(diǎn)擊鼠標(biāo)即可觸發(fā)。
完整實(shí)例
import tkinter as tk import random # 定義隨機(jī)顏色函數(shù) def random_color(): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) return f'#{r:02x}{g:02x}{b:02x}' # 定義煙花類(lèi) class Firework: def __init__(self, canvas, x, y): self.canvas = canvas self.x = x self.y = y self.color = random_color() self.radius = 2 self.particles = [] self.create_particles() self.lifespan = 100 # 設(shè)置煙花消失的時(shí)間 # 創(chuàng)建粒子效果 def create_particles(self): for i in range(120): vx = random.uniform(-1, 1) * 8 vy = random.uniform(-1, 1) * 8 particle = self.canvas.create_oval(self.x, self.y, self.x+self.radius, self.y+self.radius, fill=self.color) self.particles.append((particle, vx, vy)) # 更新煙花效果 def update(self): self.radius += 0.5 self.lifespan -= 1 for particle in self.particles: self.canvas.move(particle[0], particle[1], particle[2]) particle_position = self.canvas.coords(particle[0]) # 刪除飛出屏幕的粒子 if particle_position[0] < 0 or particle_position[1] < 0 or particle_position[2] > 800 or particle_position[3] > 600: self.canvas.delete(particle[0]) self.particles.remove(particle) # 刪除所有粒子后刪除煙花 if not self.particles or self.lifespan <= 0: self.canvas.delete(self.firework_id) # 啟動(dòng)煙花效果 def fire(self): self.firework_id = self.canvas.create_oval(self.x, self.y, self.x+self.radius, self.y+self.radius, fill=self.color) self.canvas.after(10, self.launch) # 發(fā)射煙花 def launch(self): self.update() if self.particles: self.canvas.after(10, self.launch) # 創(chuàng)建Tkinter窗口 root = tk.Tk() root.geometry('800x600') # 創(chuàng)建畫(huà)布 canvas = tk.Canvas(root, bg='black') canvas.pack(fill=tk.BOTH, expand=1) # 點(diǎn)擊事件處理函數(shù) def on_click(event): x = event.x y = event.y firework = Firework(canvas, x, y) firework.fire() # 綁定點(diǎn)擊事件 canvas.bind('<Button-1>', on_click) # 啟動(dòng)Tkinter事件循環(huán) root.mainloop()
3D旋轉(zhuǎn)煙花
給大家?guī)?lái)了3D旋轉(zhuǎn)煙花,這是一段Python代碼,大家直接使用瀏覽器打開(kāi)即可觀看煙花,非常方便,并且效果也非常炫酷
代碼實(shí)例:
import pygame from random import randint, uniform, choice import math vector = pygame.math.Vector2 gravity = vector(0, 0.3) DISPLAY_WIDTH = DISPLAY_HEIGHT = 800 trail_colours = [(45, 45, 45), (60, 60, 60), (75, 75, 75), (125, 125, 125), (150, 150, 150)] dynamic_offset = 1 static_offset = 5 class Firework: def __init__(self): self.colour = (randint(0, 255), randint(0, 255), randint(0, 255)) self.colours = ( (randint(0, 255), randint(0, 255), randint(0, 255) ), (randint(0, 255), randint(0, 255), randint(0, 255)), (randint(0, 255), randint(0, 255), randint(0, 255))) self.firework = Particle(randint(0, DISPLAY_WIDTH), DISPLAY_HEIGHT, True, self.colour) # Creates the firework particle self.exploded = False self.particles = [] self.min_max_particles = vector(100, 225) def update(self, win): # called every frame if not self.exploded: self.firework.apply_force(gravity) self.firework.move() for tf in self.firework.trails: tf.show(win) self.show(win) if self.firework.vel.y >= 0: self.exploded = True self.explode() else: for particle in self.particles: particle.apply_force( vector(gravity.x + uniform(-1, 1) / 20, gravity.y / 2 + (randint(1, 8) / 100))) particle.move() for t in particle.trails: t.show(win) particle.show(win) def explode(self): amount = randint(self.min_max_particles.x, self.min_max_particles.y) for i in range(amount): self.particles.append( Particle(self.firework.pos.x, self.firework.pos.y, False, self.colours)) def show(self, win): pygame.draw.circle(win, self.colour, (int(self.firework.pos.x), int( self.firework.pos.y)), self.firework.size) def remove(self): if self.exploded: for p in self.particles: if p.remove is True: self.particles.remove(p) if len(self.particles) == 0: return True else: return False class Particle: def __init__(self, x, y, firework, colour): self.firework = firework self.pos = vector(x, y) self.origin = vector(x, y) self.radius = 20 self.remove = False self.explosion_radius = randint(5, 18) self.life = 0 self.acc = vector(0, 0) # trail variables self.trails = [] # stores the particles trail objects self.prev_posx = [-10] * 10 # stores the 10 last positions self.prev_posy = [-10] * 10 # stores the 10 last positions if self.firework: self.vel = vector(0, -randint(17, 20)) self.size = 5 self.colour = colour for i in range(5): self.trails.append(Trail(i, self.size, True)) else: self.vel = vector(uniform(-1, 1), uniform(-1, 1)) self.vel.x *= randint(7, self.explosion_radius + 2) self.vel.y *= randint(7, self.explosion_radius + 2) self.size = randint(2, 4) self.colour = choice(colour) for i in range(5): self.trails.append(Trail(i, self.size, False)) def apply_force(self, force): self.acc += force def move(self): if not self.firework: self.vel.x *= 0.8 self.vel.y *= 0.8 self.vel += self.acc self.pos += self.vel self.acc *= 0 if self.life == 0 and not self.firework: # check if particle is outside explosion radius distance = math.sqrt((self.pos.x - self.origin.x) ** 2 + (self.pos.y - self.origin.y) ** 2) if distance > self.explosion_radius: self.remove = True self.decay() self.trail_update() self.life += 1 def show(self, win): pygame.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)), self.size) def decay(self): # random decay of the particles if 50 > self.life > 10: # early stage their is a small chance of decay ran = randint(0, 30) if ran == 0: self.remove = True elif self.life > 50: ran = randint(0, 5) if ran == 0: self.remove = True def trail_update(self): self.prev_posx.pop() self.prev_posx.insert(0, int(self.pos.x)) self.prev_posy.pop() self.prev_posy.insert(0, int(self.pos.y)) for n, t in enumerate(self.trails): if t.dynamic: t.get_pos(self.prev_posx[n + dynamic_offset], self.prev_posy[n + dynamic_offset]) else: t.get_pos(self.prev_posx[n + static_offset], self.prev_posy[n + static_offset]) class Trail: def __init__(self, n, size, dynamic): self.pos_in_line = n self.pos = vector(-10, -10) self.dynamic = dynamic if self.dynamic: self.colour = trail_colours[n] self.size = int(size - n / 2) else: self.colour = (255, 255, 200) self.size = size - 2 if self.size < 0: self.size = 0 def get_pos(self, x, y): self.pos = vector(x, y) def show(self, win): pygame.draw.circle(win, self.colour, (int( self.pos.x), int(self.pos.y)), self.size) def update(win, fireworks): for fw in fireworks: fw.update(win) if fw.remove(): fireworks.remove(fw) pygame.display.update() def main(): pygame.init() pygame.display.set_caption("Fireworks in Pygame") win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT)) clock = pygame.time.Clock() fireworks = [Firework() for i in range(2)] # create the first fireworks running = True while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: # Change game speed with number keys if event.key == pygame.K_1: fireworks.append(Firework()) if event.key == pygame.K_2: for i in range(10): fireworks.append(Firework()) win.fill((20, 20, 30)) # draw background if randint(0, 20) == 1: # create new firework fireworks.append(Firework()) update(win, fireworks) # stats for fun # total_particles = 0 # for f in fireworks: # total_particles += len(f.particles) # print(f"Fireworks: {len(fireworks)}\nParticles: {total_particles}\n\n") pygame.quit() quit() main()
效果如下:
以上就是Python tkinter實(shí)現(xiàn)春節(jié)煙花效果demo的詳細(xì)內(nèi)容,更多關(guān)于Python tkinter煙花效果的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
CentOS下使用yum安裝python-pip失敗的完美解決方法
這篇文章主要介紹了CentOS下使用yum安裝python-pip失敗的完美解決方法,需要的朋友可以參考下2017-08-08python實(shí)現(xiàn)將字符串中的數(shù)字提取出來(lái)然后求和
這篇文章主要介紹了python實(shí)現(xiàn)將字符串中的數(shù)字提取出來(lái)然后求和,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python字符串和常用數(shù)據(jù)結(jié)構(gòu)知識(shí)總結(jié)
在本文中我們系統(tǒng)的給大家整理了關(guān)于python字符串和常用數(shù)據(jù)結(jié)構(gòu)的相關(guān)知識(shí)點(diǎn)以及實(shí)例代碼,需要的朋友們學(xué)習(xí)下。2019-05-05Pygame實(shí)戰(zhàn)練習(xí)之紙牌21點(diǎn)游戲
21點(diǎn)想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂(lè)的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何利用python寫(xiě)一個(gè)簡(jiǎn)單的21點(diǎn)小游戲的相關(guān)資料,需要的朋友可以參考下2021-09-09淺談Pycharm最有必要改的幾個(gè)默認(rèn)設(shè)置項(xiàng)
今天小編就為大家分享一篇淺談Pycharm最有必要改的幾個(gè)默認(rèn)設(shè)置項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02用python生成與調(diào)用cntk模型代碼演示方法
今天小編就為大家分享一篇用python生成與調(diào)用cntk模型代碼演示方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08