Fundamentals 4 min read

Creating a Matrix‑Style Code Rain Effect in the Terminal with Python

This article explains how to generate the iconic green falling‑character “code rain” effect from The Matrix in a terminal using Python, detailing the required script, ASCII character set, matrix update logic, and optional ANSI color enhancements.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Creating a Matrix‑Style Code Rain Effect in the Terminal with Python

The “code rain” visual effect popularized by the movie The Matrix can be recreated in a terminal using Python. This tutorial shows how to build a simple script that continuously prints falling green‑looking characters.

First, a script is created that generates a grid of characters using a selection of ASCII symbols that resemble Japanese katakana, mimicking the appearance of the original effect.

import os
import time
import random
import sys
# ASCII字符范围,这里我们选择一些看起来像日文的字符
ascii_chars = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン"

def code_rain(rows=20, columns=100):
    # 初始化一个二维数组,每一列都包含一个随机的字符序列
    matrix = [[' ' for _ in range(columns)] for _ in range(rows)]
    for col in range(columns):
        matrix[random.randint(0, rows-1)][col] = random.choice(ascii_chars)
    while True:
        os.system('cls' if os.name == 'nt' else 'clear')  # 清屏指令,'cls'用于Windows,'clear'用于Unix/Linux/macOS
        for row in matrix:
            print(''.join(row))
        # 更新矩阵,模拟字符下落
        for col in range(columns):
            for row in range(rows-1, 0, -1):
                if matrix[row][col] != ' ':
                    matrix[row-1][col] = matrix[row][col]
                    matrix[row][col] = ' '
            # 在底部添加新的字符
            if matrix[0][col] == ' ':
                matrix[0][col] = random.choice(ascii_chars)
        time.sleep(0.1)  # 控制刷新速率

if __name__ == "__main__":
    try:
        code_rain()
    except KeyboardInterrupt:
        print("\nExiting the Code Rain...")

This code creates a grid of the specified size, repeatedly clears the screen, prints the current matrix, and updates it so characters appear to fall from top to bottom. Adjust the rows and columns parameters to change the output dimensions.

Note that the script works in any terminal that supports ANSI escape codes for color; the current version prints plain characters. To display the characters in green, prepend the ANSI code \033[32m and reset with \033[0m .

For example, modify the print statement to: print('\033[32m' + ''.join(row) + '\033[0m') This will render all output characters in green, provided the terminal supports the color codes.

ASCIIANSI colorsCode RainMatrix EffectTerminal
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

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.