Game Development 7 min read

Creating a Colorful Fireworks Display with Python and Pygame

This tutorial demonstrates how to use Python's pygame library to build a dynamic fireworks simulation, covering module imports, window setup, color configuration, class definition for fireworks particles, and the main loop that renders the animated display.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating a Colorful Fireworks Display with Python and Pygame

In recent years many places have banned real fireworks; this article shows how to recreate a fireworks show using Python and the pygame library.

Step 1: Import required modules

import pygame, math, time, random, os
from sys import exit

Step 2: Define window dimensions

WIN_W = 2200
WIN_H = 1300

Step 3: Set timing, display frequency, and color list

t1 = 0.18  # time flow speed
show_n = 0
show_frequency = 0.0015  # higher value means higher fireworks frequency
color_list = [
    [255, 50, 50],
    [50, 255, 50],
    [50, 50, 255],
    [255, 255, 50],
    [255, 50, 255],
    [50, 255, 255],
    [255, 255, 255]
]

Step 4: Initialize pygame and the music mixer

pygame.init()
pygame.mixer.init()

Step 5: Create a resizable window and set its title

screen = pygame.display.set_mode((WIN_W, WIN_H), pygame.RESIZABLE, 32)
pygame.display.set_caption("五彩烟花大放送")

Step 6: Load background music (optional)

sound_wav = pygame.mixer.music.load("123.mp3")
pygame.mixer.music.play()

Step 7: Define the Fireworks class

class Fireworks():
    is_show = False
    x, y = 0, 0
    vy = 0
    p_list = []
    color = [0, 0, 0]
    v = 0
    def __init__(self, x, y, vy, n=300, color=[0, 255, 0], v=10):
        self.x = x
        self.y = y
        self.vy = vy
        self.color = color
        self.v = v
        for i in range(n):
            self.p_list.append([random.random() * 2 * math.pi, 0, v * math.pow(random.random(), 1/3)])
    def again(self):
        self.is_show = True
        self.x = random.randint(WIN_W // 2 - 350, WIN_W // 2 + 350)
        self.y = random.randint(int(WIN_H/2), int(WIN_H*3/5))
        self.vy = -40 * (random.random() * 0.4 + 0.8) - self.vy * 0.2
        self.color = color_list[random.randint(0, len(color_list)-1)].copy()
        n = len(self.p_list)
        self.p_list = []
        for i in range(n):
            self.p_list.append([random.random() * 2 * math.pi, 0, self.v * math.pow(random.random(), 1/3)])
    def run(self):
        global show_n
        for p in self.p_list:
            p[1] = p[1] + (random.random() * 0.6 + 0.7) * p[2]
            p[2] = p[2] * 0.97
            if p[2] < 1.2:
                self.color[0] *= 0.9999
                self.color[1] *= 0.9999
                self.color[2] *= 0.9999
            if max(self.color) < 10 or self.y > WIN_H + p[1]:
                show_n -= 1
                self.is_show = False
                break
        self.vy += 10 * t1
        self.y += self.vy * t1

Step 8: Create a list of fireworks instances

fk_list = []
fk_list.append(Fireworks(300, 300, -20, n=100, color=[0,255,0], v=10))
fk_list.append(Fireworks(300, 300, -20, n=200, color=[0,0,255], v=11))
# (additional instances omitted for brevity)
clock = pygame.time.Clock()

Step 9: Main loop that updates and renders the fireworks

# Game main loop
while True:
    if not pygame.mixer.music.get_busy():
        pygame.mixer.music.play()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    screen.fill((0, 0, 0))
    for i, fk in enumerate(fk_list):
        if not fk.is_show:
            if random.random() < show_frequency * (len(fk_list) - show_n):
                show_n += 1
                fk.again()
            continue
        fk.run()
        for p in fk.p_list:
            x, y = fk.x + p[1] * math.cos(p[0]), fk.y + p[1] * math.sin(p[0])
            if random.random() < 0.055:
                screen.set_at((int(x), int(y)), (255,255,255))
            else:
                screen.set_at((int(x), int(y)), (int(fk.color[0]), int(fk.color[1]), int(fk.color[2])))
    pygame.display.update()
    time_passed = clock.tick(50)

The script produces a colorful, animated fireworks display that can be run on any system with Python and pygame installed.

graphicsgame developmentTutorialPygamefireworks
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.