51CTO 2022-05-14 15:52:45 阅读数:324
" Slow taste of human fireworks color It's a long time to watch the world "
——— To oneself
???? When the forest is deep, see the deer , See a whale when the sea is blue , I see you when I wake up ????.
Starlight, moon and night fireworks belong to you , I belong to you, too . About Fireworks ???? How much do you know ? How many confessions take place in smoke
Under flower , Must be Xiaobai Needless to say, everyone knows that ba ~
Today's short passage is about The story of fireworks ! Are you ready to enter the world of fireworks with me ?
“ Every sentence is copied , There's a story , When you walk in , Please listen to ”
Get ready :Python3、Pycharm、Tkinter、Pygame、Pillow And some built-in modules .
The installation command is unified :
pip install -i https://pypi.douban.com/simple/ + Module name
2.2 Picture material ( Can replace the person you like )
3) Officially tap the code ????
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
3.2 Interface window settings
if __name__ == '__main__':
root = tk. Tk()
root. title( ' Fireworks all over the sky —— Wish you all — Lovers get married ') # Set the title bar of the form
cv = tk. Canvas( root, height = 600, width = 600)
# Draw a high 600, wide 600 Canvas of
bgpath = filedialog. askopenfilename( title = ' Please select a background picture ')
# Select the background image
image = Image. open( bgpath)
# Open the background picture
image = image. resize(( 600, 600), Image. ANTIALIAS)
# Resize background image to window size
photo = ImageTk. PhotoImage( image)
cv. create_image( 0, 0, image = photo, anchor = 'nw')
# Draw the loaded background picture on the canvas
bgmusic = filedialog. askopenfilename( title = ' Please select background music ')
py. mixer. init()
# initialization
py. mixer. music. load( bgmusic)
# File loading
py. mixer. music. play( - 1, 0, fade_ms = 50)
# Play The first is the playback value -1 For loop play , The second parameter represents the start time
py. mixer. music. pause()
# Pause
py. mixer. music. unpause()
# Cancel the suspension
cv. pack()
# hold cv Add in
root. protocol( "WM_DELETE_WINDOW", close)
root. after( 200, simulate, cv)
# stay 0.1 Call in seconds stimulate function , Generate a round of fireworks blooming effect
root. mainloop()
# perform root, Generate window
def randomcolor():
# Generate random colors
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
# Gravity variable
colors = [ 'red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue', 'pink']
# Color list
class part:
# A class object is built separately for each particle of fireworks , Every particle has some important attributes , Determine its appearance ( size 、 Color )、 Moving speed, etc
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
# Specific identifier of each fireworks
self. x = x
# Fireworks bloom x Axis
self. y = y
# Fireworks bloom y Axis
self. initial_speed = explosion_speed
# Initial particle velocity
self. vx = vx
# Particle motion x Shaft speed
self. vy = vy
# Particle motion y Shaft speed
self. total = total
# Number of blooming particles
self. age = 0
# The particle has residence time
self. color = color
# Particle color
self. cv = cv
# canvas
self. cid = self. cv. create_oval( x - size, y - size, x + size, y + size, fill = self. color, outline = 'white', width = 0.01)
# Specify a bounding rectangle (Tkinter Will automatically draw an ellipse inside this rectangle )
self. lifespan = lifespan
# The time the particle stays on the canvas
def update( self, dt):
self. age += dt
# Update particle residence time
if self. alive() and self. expand():
# If the particle is both alive and expanding
move_x = cos( radians( self. id * 360 / self. total)) * self. initial_speed
# The particle x The shaft continues to expand
move_y = sin( radians( self. id * 360 / self. total)) * self. initial_speed
# The particle y The shaft continues to expand
self. cv. move( self. cid, move_x, move_y)
# according to id Move the particles on the canvas x and y Distance
self. vx = move_x /( float( dt) * 1000)
# The particle x The speed of the shaft
elif self. alive():
columnFont = ( ' Chinese Xingkai ', 14)
# If particles only survive and do not expand ( Just stay long enough , It means that it has expanded to the maximum ), Then fall freely
self. cv. create_text( 250, 100, text = ' xi ', tag = "write_tag", fill = choice( colors), font = columnFont) # typeface
self. cv. create_text( 300, 100, text = ' huan ', tag = "write_tag", fill = choice( colors), font = columnFont)
self. cv. create_text( 350, 100, text = ' you ', tag = "write_tag", fill = choice( colors), font = columnFont)
self. cv. create_text( 400, 100, text = ' Acridine ', tag = "write_tag", fill = choice( colors), font = columnFont)
# Delete text labels
move_x = cos( radians( self. id * 360 / self. total))
#x The movement displacement of the shaft
# 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
# to update y Axis
elif self. cid is not None:
# If the particle life cycle has passed , Then remove it
cv. delete( self. cid)
# Remove the particle object on the canvas
self. cv. delete( "write_tag")
# Also remove the font
self. cid = None
def expand ( self):
# Defines the time frame for the inflation effect
return self. age <= 1.2
# Determine whether the expansion time is less than 1.2 second
def alive( self):
# Determine whether the particle is still in its life cycle
return self. age <= self. lifespan
# Judge whether the dwell time is less than the expected dwell time
'''
Firework simulation loop:
Recursively call to repeatedly emit new fireworks on canvas
a list of list (list of stars, each of which is a list of particles)
is created and drawn on canvas at every call,
via update protocol inside each 'part' object
'''
def simulate( cv):
t = time()
# Return from 1970 Floating point seconds elapsed after years , Accurate to the decimal point 7 position
explode_points = []
# List of explosion points , Fireworks list
wait_time = randint( 10, 100)
# The waiting time is 10 To 100 An integer between
numb_explode = randint( 8, 20)
# When the number of fireworks exploded 6 To 10 Random integer between
# create list of list of all particles in all simultaneous explosion
for point in range( numb_explode):
# Create a list of all particles that simulate a fireworks bloom
if point <= 4:
objects = []
# Explosive particle list for each point
x_cordi = 250 + point * 50
# Of each explosion point x Axis
y_cordi = 100
# Of each explosion point y Axis
speed = uniform ( 0.5, 1.5)
# The speed of each explosion point
size = uniform ( 0.5, 3)
# The size of each explosion point
color = choice( colors)
# The color of each explosion point
explosion_speed = uniform( 0.6, 3)
# The speed of the explosion
total_particles = randint( 10, 60)
# The total number of particles in fireworks
for i in range( 1, total_particles):
# The particle size of the same fireworks explosion 、 Speed 、 The coordinates are the same
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))
# Substitute the above parameters into part function , But the survival time of each particle is independent
objects. append( r)
# hold r Add to particle list
explode_points. append( objects)
# Add the particle list to the fireworks list
else:
objects = []
# Explosive particle list for each point
x_cordi = randint( 50, 550)
# Of each explosion point x Axis
y_cordi = randint( 50, 150)
# Of each explosion point y Axis
speed = uniform ( 0.5, 1.5)
# The speed of each explosion point
size = uniform ( 0.5, 3)
# The size of each explosion point
color = choice( colors)
# The color of each explosion point
explosion_speed = uniform( 0.3, 2)
# The speed of the explosion
total_particles = randint( 10, 50)
# The total number of particles in fireworks
for i in range( 1, total_particles):
# The particle size of the same fireworks explosion 、 Speed 、 The coordinates are the same
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))
# Substitute the above parameters into part function , But the survival time of each particle is independent
objects. append( r)
# hold r Add to particle list
explode_points. append( objects)
# Add the particle list to the fireworks list
total_time = .0
# Total initialization time
# keeps undate within a timeframe of 1.8 second
while total_time < 2:
# When the total time is less than 1.8 Run the loop at seconds
sleep( 0.03)
# Pause the screen 0.01 second
tnew = time()
# Refresh time
t, dt = tnew, tnew - t
# Time equals new time , And last time interval is tnew-t
for point in explode_points:
# Traverse the fireworks list
for item in point:
# Traverse the list of particles in the fireworks
item. update( dt)
# Particle update time
cv. update()
# Refresh the canvas
total_time += dt
# by while Cycle increase time
root. after( wait_time, simulate, cv)
# Put components after other components , Put it on the top floor , Cover the following , Recursively call itself , Form a new round of explosion
def close( * ignore):
# Open the simulation loop and close the window
"""Stops simulation loop and closes the window."""
global root
root. quit()
Show four groups of fireworks effects —— Try the rest with your own code ~???? In fact, they are dynamic fireworks drops ~
1.0 Random works display effect
Not enough stars Then let me show you fireworks
Fireworks are beautiful Your smile is more beautiful when you look at the fireworks
You showed me a fireworks Your smile is all in my dream
If time is intoxicating fireworks , Can you see the bright wind with me
Sweetheart has his fireworks Sweetheart, your eyes are burning
I will luve thee still, my dear.
While the sands o' life shall run.
A fairy stick can burn 9 second Instant release 180 There are more flames than there are stars in the Milky way So I called you out to set off fireworks to give you the stars
Watch the fireworks bloom alone , Keep the fireworks cool
Fireworks are fleeting , The ashes are eternal
I use three fireworks , And you'll be lost forever
you , Once such beautiful fireworks were in full bloom in my sky , When the fireworks are gone , I live in the ruins , Only one place was found fragmented
I will luve thee still, my dear.
While the sands o' life shall run.
Since then, fireworks , More than a moment , As my blessing , Will not fall with the lonely moment
Your appearance Like fireworks in the middle of the night Although only for a moment But it lit up my whole life
Shili Hantan road , Half the fireworks wake up
Fireworks bloom , Look up slowly , Lonely and sleepless , Siqing is heartbroken
Long street , There are many fireworks , You pick up the light and look back , Short Pavilion, short , Red dust rolling , I sighed Xiao again
I will luve thee still, my dear.
While the sands o' life shall run.
Fireworks are forever , Because it engraves eternal beauty in my heart . Fireworks are happy , Because it affirms its beauty for the moment it leaves behind
When I like you , My heart is full of small fireworks . that , When fireworks are in full bloom this year , Just let me jump into your arms, okay ?
I must watch a fireworks conference with you , Feel a romantic and exciting
Fireworks are in full bloom , A moment is a lifetime . The heart of protection is firm , The millennium is just around the corner
In a flash , Like quicksand at the fingertips . Brilliant fireworks , I can't cut it after all
I will luve thee still, my dear.
While the sands o' life shall run.
Confession 3. Fireworks available ( Source code ).rar- Web page making document resources
版权声明:本文为[51CTO]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205141525034028.html