Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Matrix Rain

0.00/5 (No votes)
5 Oct 2010 3  
A simple C# Console program to display Matrix Movie Effect

Introduction

This article should explain how an animation can be presented by just using normal programming functions and this one shows the famous Matrix Rain from the Movie Matrix. There are many articles explaining the concept, but this one helps you to understand it better.

Need To Know

  • Basic C#

How It Works

The basis behind this application is just printing random characters at random heights but throughout all the positions along the width.

First, we need to do some initial settings at the startup, that is before the matrix action is performed we need to get random heights for all the X in the window. It is done by the following set:

height = Console.WindowHeight;
width = Console.WindowWidth - 1;
y = new int[width];
Console.Clear();
for (int x = 0; x < width; ++x)//Setting the cursor at random at program startup
{
    y[x] = rand.Next(height);
}

On the above code, the random heights are accessed through Rand.Next() function and is given to the widths throughout the screen.

Once this is done, what we have to do is to change the y position along the entire width one by one. This is the Matrix Rain.

But just assigning random height position to the Y from the initially generated random height will just make the screen look like (within seconds) ants moving in a perfect straight line. So all we have to do is to insert random spaces (dark characters indirectly). But this is also not quite enough, we need colouring effect too. Have a look at the following code:

for (x = 0; x < width; ++x)
{
    if (x % 10 == 1)//Randomly setting up the White Position
        Console.ForegroundColor = FancyColor;
    else
        Console.ForegroundColor = GlowColor;
    Console.SetCursorPosition(x, y[x]);
    Console.Write(AsciiCharacter);
    if (x % 10 == 9)
        Console.ForegroundColor = FancyColor;
    else
        Console.ForegroundColor = NormalColor;
    int temp = y[x] - 2;
    Console.SetCursorPosition(x, inScreenYPosition(temp, height));
    Console.Write(AsciiCharacter);
    int temp1 = y[x] - 20;
    Console.SetCursorPosition(x, inScreenYPosition(temp1, height));
    Console.Write(' ');
    y[x] = inScreenYPosition(y[x] + 1, height);
}

When looking at the above code, you might notice that before the loop gets incremented, three times some characters are printed at three different heights in the same X axis.

That is, first a bright green character is printed at a random height (The height for this particular X is already created in the initial section).

Then, a dark green character is printed two positions below it, then an empty space is printed twenty spaces below the first character. At this point, the screen might look like this:

First.JPG

Then the X gets incremented and this new X is paired with a Y which has random height location in it as the loops goes on. So the second step will look like this:

Second.JPG

I have given white colour at random for the originality.

So far the code will just display the matrix rain. But we need more. That is, we need to print a text at the center with fading Rain.

So to do that, first we need to fill the screen completely with characters (for few seconds), without any empty spaces.

 for (x = 0; x < width; ++x)
                {
                    Console.SetCursorPosition(x, y[x]);
                    if (x % 10 == 9)
                        Console.ForegroundColor = FancyColor;
                    else
                        Console.ForegroundColor = NormalColor;
                    Console.Write(AsciiCharacter);	//Printing the Character 
						//Always at Fixed position
                    y[x] = inScreenYPosition(y[x] + 1, height);
                }

Then, we need to slowly print the empty spaces rather that printing in it all of a sudden which may result in sudden disappearance of the rain. To do this, we need to print more empty spaces than the coloured characters for some time and then completely remove the rain.

for (x = 0; x < width; ++x)
{
 	Console.SetCursorPosition(x, y[x]);
 	Console.Write(' ');//Slowly blacking out the Screen
 	int temp1 = y[x] - 20;
 	Console.SetCursorPosition(x, inScreenYPosition(temp1, height));
 	Console.Write(' ');
	 if (Counter > FullFlow && Counter < Blacking)
	 {
  		if (x % 10 == 9)
  			 Console.ForegroundColor = FancyColor;
  		else
   			Console.ForegroundColor = NormalColor;
  		int temp = y[x] - 2;
 		 Console.SetCursorPosition(x, inScreenYPosition(temp, height));
 		 Console.Write(AsciiCharacter);//The Text is printed Always
 	}
	 Console.SetCursorPosition(width / 2, height / 2);
	 Console.Write(TextInput);
	 y[x] = inScreenYPosition(y[x] + 1, height);
}

The code found in the inner loop prevents the sudden disappearance of the rain for some time as I said earlier. But while doing this, we need to print the Text always. After some time, the screen becomes completely dark displaying the text alone.

The output will look like the one shown below in the middle of the program.

Output.JPG

This loop goes on continuously which is controlled by the WHILE loop at the Main():

while (true)
{
	Counter = Counter + 1;
	UpdateAllColumns(width, height, y);
	if (Counter > (3 * Interval))
		Counter = 0;
}

You Can Do This

The speed of the rain depends on the processor speed of your computer. So the speed of the rain differs from one computer to another. Also as I said, there are many articles available for this matrix rain, but almost all are done using threading concepts rather than FOR and WHILE loops. The main disadvantage of the threading concept is that the rain flows in normal speed only.

But you can try using threading concept with just replacing the FOR LOOP (That controls X) with multiple threads.

You Can Use It

When you have come to know about the working of this Matrix rain, this application may be unusable. So instead of just placing it in hard disk, I linked the executive file (will be generated at the BIN folder inside the application folder) with my Outlook account. So when every time a particular mail hits my inbox, this program will be executed for some interval of time (after some alteration in the code).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here