Click here to Skip to main content
16,020,628 members
Articles / Programming Languages / C#

Matrix Rain

Rate me:
Please Sign up or sign in to vote.
4.71/5 (23 votes)
5 Oct 2010CPOL4 min read 129.1K   5.3K   58   34
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:

C#
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:

C#
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.

C#
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.

C#
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():

C#
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)


Written By
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

 
QuestionHelp / Question to modify code. Pin
Member 114913293-Mar-15 0:12
Member 114913293-Mar-15 0:12 
AnswerRe: Help / Question to modify code. Pin
theAppo14-Sep-16 4:59
theAppo14-Sep-16 4:59 
QuestionDesktop background Pin
Member 1058305511-Mar-14 8:34
Member 1058305511-Mar-14 8:34 
GeneralMy vote of 5 Pin
sravani.v14-Jun-12 19:56
sravani.v14-Jun-12 19:56 
Generalcool Pin
Don Kackman6-Oct-10 12:25
Don Kackman6-Oct-10 12:25 
GeneralRe: cool Pin
NaveenSoftwares6-Oct-10 18:26
NaveenSoftwares6-Oct-10 18:26 
GeneralMy vote of 5 Pin
thatraja6-Oct-10 0:12
professionalthatraja6-Oct-10 0:12 
GeneralRe: My vote of 5 Pin
NaveenSoftwares6-Oct-10 0:44
NaveenSoftwares6-Oct-10 0:44 
GeneralReminds me of the "old days"... Pin
hoernchenmeister4-Oct-10 21:21
hoernchenmeister4-Oct-10 21:21 
GeneralRe: Reminds me of the "old days"... Pin
NaveenSoftwares5-Oct-10 0:47
NaveenSoftwares5-Oct-10 0:47 
GeneralSoothing Rain Pin
MikJr4-Oct-10 16:20
MikJr4-Oct-10 16:20 
GeneralRe: Soothing Rain Pin
NaveenSoftwares5-Oct-10 18:27
NaveenSoftwares5-Oct-10 18:27 
GeneralMy vote of 5 Pin
louislong30-Sep-10 10:20
louislong30-Sep-10 10:20 
GeneralRe: My vote of 5 Pin
NaveenSoftwares30-Sep-10 19:26
NaveenSoftwares30-Sep-10 19:26 
Generalnot bad Pin
BillW3328-Sep-10 8:50
professionalBillW3328-Sep-10 8:50 
GeneralRe: not bad Pin
NaveenSoftwares28-Sep-10 17:14
NaveenSoftwares28-Sep-10 17:14 
GeneralMy vote of 5 Pin
Kubajzz28-Sep-10 5:58
Kubajzz28-Sep-10 5:58 
GeneralRe: My vote of 5 Pin
NaveenSoftwares28-Sep-10 17:13
NaveenSoftwares28-Sep-10 17:13 
GeneralImage format Pin
Mi.Chal.28-Sep-10 4:09
Mi.Chal.28-Sep-10 4:09 
GeneralRe: Image format Pin
NaveenSoftwares28-Sep-10 4:25
NaveenSoftwares28-Sep-10 4:25 
GeneralFormatting required Pin
Praveen Nair (NinethSense)27-Sep-10 23:59
Praveen Nair (NinethSense)27-Sep-10 23:59 
Code and article formatting required.
PraVeeN
blog.ninethsense.com/

GeneralRe: Formatting required Pin
NaveenSoftwares28-Sep-10 0:12
NaveenSoftwares28-Sep-10 0:12 
GeneralRe: Formatting required Pin
Praveen Nair (NinethSense)28-Sep-10 0:52
Praveen Nair (NinethSense)28-Sep-10 0:52 
GeneralRe: Formatting required Pin
NaveenSoftwares28-Sep-10 2:38
NaveenSoftwares28-Sep-10 2:38 
GeneralRe: Formatting required Pin
Praveen Nair (NinethSense)28-Sep-10 2:52
Praveen Nair (NinethSense)28-Sep-10 2:52 

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

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