Game Development 6 min read

Creating a Code Rain Effect with Python and Pygame

This tutorial demonstrates how to build a classic "code rain" screen using Python's pygame library, guiding readers through imports, window setup, character rendering, and a concise main loop that produces the animated effect in roughly thirty lines of code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating a Code Rain Effect with Python and Pygame

Many beginners encounter the pygame package when learning Python because it combines fun visual output with fundamental programming concepts. This article shows how to recreate the iconic "code rain" effect seen in hacker movies using only a small amount of code.

First, import the required modules: import pygame</code><code>import random Next, initialize the display and related parameters. Define the screen size, a semi‑transparent black background, and set up pygame, a font, and a surface that will be repeatedly blitted:

# parameters
SCREENSIZE = (600, 600)
BLACK = (0, 0, 0, 13)

# initialization
pygame.init()
font = pygame.font.SysFont('宋体', 20)
screen = pygame.display.set_mode(SCREENSIZE)
surface = pygame.Surface(SCREENSIZE, flags=pygame.SRCALPHA)
pygame.Surface.convert(surface)
surface.fill(BLACK)
screen.fill(BLACK)

Prepare the characters that will fall down the screen. The list combines digits (0‑9) and lowercase letters (a‑z), renders each character in green, and creates a column index list for positioning:

# content
lib = [chr(i) for i in range(48, 48 + 10)] + [chr(i) for i in range(97, 97 + 26)]  # [0‑9 a‑z]
texts = [font.render(l, True, (0, 255, 0)) for l in lib]
cols = list(range(40))  # each column corresponds to a 15‑pixel step in a 600‑pixel window

The main loop handles quit events, updates the display at a modest frame rate, selects a random character for each column, draws it, and updates the column's vertical position. When a column reaches the bottom or a random chance occurs, it resets to the top:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    pygame.time.delay(33)
    screen.blit(surface, (0, 0))
    for i in range(n := len(cols)):
        text = random.choice(texts)
        screen.blit(text, (i * 15, cols[i] * 15))
        cols[i] = 0 if cols[i] > 80 or random.random() > 0.95 else cols[i] + 1
    pygame.display.flip()

Putting all parts together yields the complete, runnable script shown below:

import pygame
import random
# parameters
SCREENSIZE = (600, 600)
BLACK = (0, 0, 0, 13)
# initialization
pygame.init()
font = pygame.font.SysFont('宋体', 20)
screen = pygame.display.set_mode(SCREENSIZE)
surface = pygame.Surface(SCREENSIZE, flags=pygame.SRCALPHA)
pygame.Surface.convert(surface)
surface.fill(BLACK)
screen.fill(BLACK)
# content
lib = [chr(i) for i in range(48, 48 + 10)] + [chr(i) for i in range(97, 97 + 26)]  # [0‑9 a‑z]
texts = [font.render(l, True, (0, 255, 0)) for l in lib]
cols = list(range(40))  # font size 15, window 600
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    pygame.time.delay(33)
    screen.blit(surface, (0, 0))
    for i in range(n := len(cols)):
        text = random.choice(texts)
        screen.blit(text, (i * 15, cols[i] * 15))
        cols[i] = 0 if cols[i] > 80 or random.random() > 0.95 else cols[i] + 1
    pygame.display.flip()

Running this script opens a 600×600 window where green alphanumeric characters cascade down the screen, recreating the nostalgic "code rain" visual.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GraphicsPythonGame DevelopmentCode RainPygame
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

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.