Game Development 8 min read

Creating a Colorful Fireworks Effect with Python and Pygame

This tutorial walks through building a dazzling fireworks animation in Python using the Pygame library, covering environment setup, installing dependencies via domestic mirrors, defining particle and firework classes, and implementing the main game loop with full source code examples.

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

We demonstrate how to create an eye‑catching fireworks display using Python's pygame library. The guide starts with preparing the development environment, including installing required modules (e.g., requests , lxml , csv ) and configuring a domestic PyPI mirror such as Tsinghua or Aliyun.

Next, we define a Particle class that stores position, color, radius, angle, speed, gravity, and lifespan, and provides update and draw methods to animate each particle.

We then create a Firework class that initializes a random launch position and color, manages a list of particles, and implements explode , update , and draw methods to handle the ascent, explosion, and particle fading.

The main game loop handles quitting events, clears the screen, spawns new fireworks when the list is short, updates and draws each firework and its particles, removes finished fireworks and dead particles, and finally updates the display at 60 FPS.

Below is the complete source code for the fireworks program:

import pygame
import math
from random import randint, choice

# Initialize Pygame
pygame.init()

# Window settings
DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600
win = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption("烟花")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.radius = 3
        self.angle = randint(0, 360)
        self.speed = randint(1, 5)
        self.gravity = 0.1
        self.life = randint(20, 25)
    def update(self):
        if self.life > 0:
            radian = math.radians(self.angle)
            self.x += self.speed * math.cos(radian)
            self.y -= self.speed * math.sin(radian)
            self.speed -= self.gravity
            self.life -= 1
    def draw(self):
        pygame.draw.circle(win, self.color, (int(self.x), int(self.y)), self.radius)

class Firework:
    def __init__(self):
        self.x = randint(100, DISPLAY_WIDTH - 100)
        self.y = DISPLAY_HEIGHT
        self.color = (randint(0, 255), randint(0, 255), randint(0, 255))
        self.particles = []
        self.exploded = False
    def explode(self):
        for _ in range(100):
            self.particles.append(Particle(self.x, self.y, self.color))
    def update(self):
        if not self.exploded:
            self.y -= 3
            if self.y <= randint(200, 400):
                self.explode()
                self.exploded = True
        else:
            for particle in self.particles:
                particle.update()
    def draw(self):
        pygame.draw.circle(win, self.color, (int(self.x), int(self.y)), 5)

fireworks = []
running = True
clock = pygame.time.Clock()
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    win.fill(BLACK)
    if len(fireworks) < 10 and randint(0, 100) < 2:
        fireworks.append(Firework())
    for firework in fireworks:
        firework.update()
        firework.draw()
        for particle in firework.particles:
            particle.draw()
    fireworks = [f for f in fireworks if not f.exploded or len(f.particles) > 0]
    for f in fireworks:
        f.particles = [p for p in f.particles if p.life > 0]
    pygame.display.update()
pygame.quit()

By following these steps, readers learn how to use Pygame for graphical effects, understand particle systems, and acquire a complete, runnable example of a fireworks animation.

graphicsPythonGame developmentTutorialParticle SystemPygamefireworks
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.