print green digital rain in python like in matrix in python

Here's an example code to print green digital rain:

main.py
import random
import time

# define the ASCII characters used for the digital rain
characters = ['0', '1']
# define the width and height of the terminal
terminal_width, terminal_height = 80, 24

# initialize the matrix with random characters
matrix = [[random.choice(characters) for y in range(terminal_height)] for x in range(terminal_width)]

# infinite loop to animate the matrix effect
while True:
    # clear the terminal for each frame
    print("\033[2J\033[1;1H")

    # loop through the matrix
    for y in range(terminal_height):
        for x in range(terminal_width):
            # print the current character and add a space
            if random.random() > 0.9:  # occasional green characters
                print('\033[1;32m' + characters[random.randint(0, len(characters)-1)] + '\033[0m', end=' ')
            else:
                print(matrix[x][y], end=' ')
        # move to the next line
        print()

    # shift the matrix down by one row
    matrix.pop(0)
    matrix.append([random.choice(characters) for y in range(terminal_height)])

    # wait for a short time before displaying the next frame
    time.sleep(0.05)
1146 chars
34 lines

This code uses the random and time modules to animate the matrix effect in the terminal. The ASCII characters used for the digital rain are defined in the characters list. The width and height of the terminal are defined in terminal_width and terminal_height respectively. The matrix variable is initialized with random characters, and is shifted down by one row on each frame to create the animation effect.

The occasional green characters are added using ANSI escape sequences for Green color (\033[1;32m and \033[0m). The program clears the terminal and prints the current state of the matrix for each frame. To wait for a short time before displaying the next frame, the time.sleep function is called with a delay of 0.05 seconds.

gistlibby LogSnag