Click here to Skip to main content
Click here to Skip to main content

Tagged as

Matrix Rain

, 5 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

NaveenSoftwares
Software Developer HCL Technologies, Chennai, India.
India India
Developer in HP Exstream(Dialogue),C#,PHP,C-Lite.
 
Profession link:
http://in.linkedin.com/pub/naveen-kumar-r/25/42a/2a3/

Comments and Discussions

 
QuestionDesktop background PinmemberMember 1058305511-Mar-14 8:34 
GeneralMy vote of 5 Pinmembersravani.v14-Jun-12 19:56 
Generalcool PinmemberDon Kackman6-Oct-10 12:25 
GeneralRe: cool PinmemberNaveenSoftwares6-Oct-10 18:26 
GeneralMy vote of 5 Pinmemberthatraja6-Oct-10 0:12 
GeneralRe: My vote of 5 PinmemberNaveenSoftwares6-Oct-10 0:44 
GeneralReminds me of the "old days"... Pinmemberhoernchenmeister4-Oct-10 21:21 
GeneralRe: Reminds me of the "old days"... PinmemberNaveenSoftwares5-Oct-10 0:47 
GeneralSoothing Rain PinmemberMikJr4-Oct-10 16:20 
GeneralRe: Soothing Rain PinmemberNaveenSoftwares5-Oct-10 18:27 
GeneralMy vote of 5 Pinmemberlouislong30-Sep-10 10:20 
GeneralRe: My vote of 5 PinmemberNaveenSoftwares30-Sep-10 19:26 
Generalnot bad PinmemberCIDev28-Sep-10 8:50 
GeneralRe: not bad PinmemberNaveenSoftwares28-Sep-10 17:14 
GeneralMy vote of 5 PinmemberKubajzz28-Sep-10 5:58 
GeneralRe: My vote of 5 PinmemberNaveenSoftwares28-Sep-10 17:13 
GeneralImage format PinmemberMi.Chal.28-Sep-10 4:09 
GeneralRe: Image format PinmemberNaveenSoftwares28-Sep-10 4:25 
GeneralFormatting required PinmemberNinethSense27-Sep-10 23:59 
GeneralRe: Formatting required PinmemberNaveenSoftwares28-Sep-10 0:12 
GeneralRe: Formatting required PinmemberNinethSense28-Sep-10 0:52 
GeneralRe: Formatting required PinmemberNaveenSoftwares28-Sep-10 2:38 
GeneralRe: Formatting required PinmemberNinethSense28-Sep-10 2:52 
GeneralRe: Formatting required PinmemberNaveenSoftwares28-Sep-10 3:03 
GeneralRe: Formatting required PinmemberNaveenSoftwares28-Sep-10 4:16 
GeneralRe: Formatting required PinmvpMycroft Holmes28-Sep-10 0:53 
GeneralRe: Formatting required PinmemberNaveenSoftwares28-Sep-10 2:52 
GeneralRe: Formatting required PinmemberNinethSense28-Sep-10 2:55 
GeneralRe: Formatting required [modified] PinmemberNaveenSoftwares28-Sep-10 3:05 
QuestionRe: Formatting required PinmemberNaveenSoftwares27-Oct-10 3:15 
AnswerRe: Formatting required PinmemberCésar de Souza27-Oct-10 13:22 
GeneralRe: Formatting required PinmemberNaveenSoftwares27-Oct-10 18:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 6 Oct 2010
Article Copyright 2010 by NaveenSoftwares
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid