reference: New Year ~ arrange a fireworks show in Python (qq.com) The source code is attached at the end
Principle introduction:
Before introducing the code, first introduce the basic principle of Pygame drawing fireworks. Fireworks are divided into three stages from launch to bloom:
1. Launch stage: in this stage, the shape of fireworks is linear upward. Set a group of points with different sizes and colors to simulate the motion of "upward launch". In the process of motion, five points are given accelerations of different sizes. With the passage of time, the later points will catch up with the previous points, and finally all points will gather together and be in the stage of bloom preparation;
2. Fireworks bloom: at this stage of fireworks bloom, one point is dispersed and multiple points diverge in different directions, and the moving track of each point can be recorded in order to track the whole bloom track.
3. Fireworks annihilation. This stage is responsible for describing the effect of fireworks after blooming. The falling speed and brightness (also known as transparency in the code) of fireworks at each time point are different. Therefore, in the code, after fireworks bloom, each point is given two attributes: gravity vector and life cycle to simulate the different display effects of fireworks at different times,
code:
The code part encapsulates fireworks into three classes:
Firework: fireworks as a whole;
Particle: fireworks particles (including tracks)
Trail: the trail of fireworks is essentially a point.
The relationship between the three classes is: a Firework is composed of multiple particles, while a Particle is composed of multiple trails
First, set global variables, such as gravity vector, window size, the color list of Trail (mostly gray or white) and the interval between Trail in different states
vector = pygame.math.Vector2 gravity = vector(0, 0.3) # Gravity setting # Adjust the length and width of the display box according to the size of the picture you use DISPLAY_WIDTH = 1100 DISPLAY_HEIGHT = 700 trail_colours = [(45, 45, 45), (60, 60, 60), (75, 75, 75), (125, 125, 125), (150, 150, 150)] dynamic_offset = 1 static_offset = 3
Create a Trail class and define the show method to draw tracks and get_pos real time acquisition of trajectory coordinates
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)
Then write the relevant codes of Firework and Particle. Finally, write a main method to initialize the pygame environment, such as background pictures and text, set the page refresh interval, refresh every 60ms set in the program, and then add background music.
pygame.display.set_caption("Happy New Year") # Set title background = pygame.image.load("fire/1.PNG") # Set background myfont = pygame.font.Font("fire/abg.ttf",80) # Get font object through ttf font file testsurface = myfont.render("happy new year!!!",False,(237, 90, 101)) pygame.mixer.music.load("fire/SNH.mp3") # Load background music file pygame.mixer.music.play(-1,8.0) # Play background music, - 1 indicates infinite cycles. The second parameter is the starting point of playing (unit: s) # pygame.image.load("") # Picture display win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT)) # win.blit(background) clock = pygame.time.Clock() fireworks = [Firework() for i in range(2)] # create the first fireworks running = True # Program main loop while running: clock.tick(60) # Refresh every 60ms win.fill((20, 20, 30)) # draw background win.blit(background,(0,0)) win.blit(testsurface,(200,30)) if randint(0, 20) == 1: # create new firework fireworks.append(Firework()) update(win, fireworks)
Relevant details
Nice color RGB: Chinese color - traditional Chinese color Font download website: http://www.ddooo.com/zt/tffzt.htm
Finally, I wish you a happy New Year! Safe and healthy, everything goes well.
Attach the source code: Collection: some gadgets written in Python - gitee com