Matrix-Style Falling Characters in Python

by tanish satpal in Teachers > 6

147 Views, 3 Favorites, 0 Comments

Matrix-Style Falling Characters in Python

Rain.png

The Python code creates a Matrix-style animation using falling characters, with each character in random colors. This is the first time I am using the 'matplotlib' package in python. It isn't perfect due to matplot's limitations, but is still pretty amazing to see in a novice project

Supplies

Image 15-09-24 at 4.02 PM.jpeg

You will need to install a program that can run Python, mine being Pycharm.

Inside the program, you will need to go into its terminal and type the following:

pip install matplotlib

-----

These are packages that can be used to make Python code easier for plotting.

Import Libraries

Image 15-09-24 at 4.22 PM.jpeg

• This code first brings in tools (libraries) to help it draw the animation and handle time delays:

• matplotlib.pyplot is used to draw and display the animation.

• numpy helps with creating random numbers and performing calculations.

• FuncAnimation from matplotlib.animation makes it easy to create animations.

• delay from pygame.time adds a small pause to the animation to make it smoother.

Setting Up the Screen

Image 15-09-24 at 4.23 PM.jpeg

• The fig and ax represent the drawing area where the animation will happen.

• The screen is set to a size of 10x10 units.

• ax.set_xlim(0, 100) and ax.set_ylim(0, 100) create a 100x100 space for the falling characters to move in.

• The background color is set to black with fig.set_facecolor('black').

• ax.axis('off') turns off the axis so you don’t see any borders or numbers, just the animation.

Choosing the Characters

Image 15-09-24 at 4.24 PM.jpeg

The matrix_chars list contains random characters (letters, numbers, and symbols) that will be used for the falling text.

Setting Up Columns for the Characters

Image 15-09-24 at 4.26 PM.jpeg

• We want 200 columns of falling text, so we set num_columns = 200. This makes it more realistic

• Positions and Velocities create random starting positions and speeds for each column:

• Positions stores where each column starts vertically.

• Velocities stores how fast each column will move down the screen.

Random Color Generator

Image 15-09-24 at 4.28 PM.jpeg

• The function random_color() creates a new random color for each character.

• It uses np.random.rand(3, ) to generate three random numbers (RGB) that combine to make a colour.

• There is a delay of 1 millisecond to make the animation more smooth and reduce lag

Placing the Characters

Image 15-09-24 at 4.32 PM.jpeg

• We will create loop creates 200 pieces of text (one for each column):

• Each character starts at a random location (x and y coordinates) within the 100x100 space.

• A random character from the matrix_chars list is chosen for each piece of text.

• The random_color() function is called to give each piece of text a different color.

• The text objects are then stored in the text_objects list for future updates.

Animating the Characters

Image 15-09-24 at 4.34 PM.jpeg

• The update(frame) function is called to move the text down the screen.

• For each character:

1. The y-position is updated, making the text move down.

2. If the text reaches the bottom (y < 0), it jumps back to the top (y = 100).

3. When it jumps to the top, the character is changed to a new random one.

• This is done repeatedly for each character in every frame of the animation.

Running the Animation

Image 15-09-24 at 4.35 PM.jpeg

• FuncAnimation controls the entire animation:

• It updates the figure (fig) using the update() function.

• frames=np.arange(0, 1000) specifies 1,000 frames.

• interval=50 sets a small delay between frames to make the animation smoother.

• repeat=True makes the animation loop forever.

Displaying the Animation

Image 15-09-24 at 4.36 PM.jpeg

• Finally, plt.show() is called to display the window with the animation. This runs the animation on your screen.

Summary

Image 15-09-24 at 3.50 PM.jpeg

• This code creates a visual effect where random characters (letters, numbers, symbols) fall from the top of the screen to the bottom.

• Each character falls at a different speed, and when it reaches the bottom, it reappears at the top with a new random character.

• The characters change color randomly and continuously, making the animation more interesting.

• This is done using a mix of text placement, movement, and colour changes inside a loop that runs over and over to create a smooth animation.


This truly was a new experience for me, as I experienced the use of packages and libraries in this python project. The python project will be found on my Google Colab link, "The Basics of Python". Don't run the code on Colab, instead copy it down onto Pycharm or any other programming software, and import matplotlib using the terminal.

The link: https://colab.research.google.com/drive/1JRapgvN5cfsyea6Zu8M3TeRaNvxIVjO5?usp=sharing

Hope to se you once again!