Python confession Code: "starlight, moonlit night, fireworks belong to you, and I belong to you"

51CTO 2022-05-14 15:52:45 阅读数:324

pythonconfessioncodestarlightmoonlit


Introduction

" Slow taste of human fireworks color It's a long time to watch the world "

                                                    ——— To oneself Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_python

 Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_ Back end _02

 ???? 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 Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_python_03 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 ?

Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_ load _04

Text

“ Every sentence is copied , There's a story , When you walk in , Please listen to ”

Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_ Back end _05

1) Environmental installation ????

Get ready :​​Python​​3、Pycharm、Tkinter、Pygame、Pillow And some built-in modules .

The installation command is unified :

pip install -i https://pypi.douban.com/simple/ + Module name
  • 1.

2) Material display ????


2.1 Music background ( Optional )

Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_ development language _06

 2.2 Picture material ( Can replace the person you like )

     Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_python_07    

3) Officially tap the code ???? 

3.1 Import library

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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

3.3 Color settings are random

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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

3.4 Main program fireworks

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()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.

4) Effect display ????

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

Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_ Back end _08

1.1  Fireworks copywriting —— The sky is full of stars ????

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

Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_ Back end _09

I will luve thee still, my dear.

While the sands o' life shall run.


1.2 Fireworks copywriting —— Three fireworks ????

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

Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_ Back end _10

I will luve thee still, my dear.

While the sands o' life shall run.


1.3  Fireworks copywriting —— Ten mile long street ????

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

Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_ Background image _11

I will luve thee still, my dear.

While the sands o' life shall run.


1.4  Fireworks copywriting —— Ten thousand years at a glance ????

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


Python Confession code :“ Starlight, moon and night fireworks belong to you , I belong to you, too ”_python_12

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