Click here to Skip to main content
14,391,319 members

Matrix Rain

Rate this:
4.71 (23 votes)
Please Sign up or sign in to vote.
4.71 (23 votes)
5 Oct 2010CPOL
A simple C# Console program to display Matrix Movie Effect


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];
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;
        Console.ForegroundColor = GlowColor;
    Console.SetCursorPosition(x, y[x]);
    if (x % 10 == 9)
        Console.ForegroundColor = FancyColor;
        Console.ForegroundColor = NormalColor;
    int temp = y[x] - 2;
    Console.SetCursorPosition(x, inScreenYPosition(temp, height));
    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:


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:


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;
                       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;
   			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);
	 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.


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).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer HCL Technologies, Chennai, India.
India India
Developer in HP Exstream(Dialogue),C#,PHP,C-Lite.

Profession link:

Comments and Discussions

QuestionHelp / Question to modify code. Pin
Member 114913293-Mar-15 1:12
MemberMember 114913293-Mar-15 1:12 
Hey, i am doing a project at my internship.

I want to add only 3 specific numbers in the Matrix rain (1, 3, 9) etc, and only highlight those numbers with white color. Maybe randomly around in the matrix rain.

Is this something you can help, or do for me?

My boss wants it the be cryptic code number you have to look after, to unlock a secret password. He is doing events and stuff like that.
AnswerRe: Help / Question to modify code. Pin
theAppo14-Sep-16 5:59
MembertheAppo14-Sep-16 5:59 
QuestionDesktop background Pin
Member 1058305511-Mar-14 9:34
MemberMember 1058305511-Mar-14 9:34 
GeneralMy vote of 5 Pin
sravani.v14-Jun-12 20:56
Membersravani.v14-Jun-12 20:56 
Generalcool Pin
Don Kackman6-Oct-10 13:25
MemberDon Kackman6-Oct-10 13:25 
GeneralRe: cool Pin
NaveenSoftwares6-Oct-10 19:26
MemberNaveenSoftwares6-Oct-10 19:26 
GeneralMy vote of 5 Pin
thatraja6-Oct-10 1:12
professionalthatraja6-Oct-10 1:12 
GeneralRe: My vote of 5 Pin
NaveenSoftwares6-Oct-10 1:44
MemberNaveenSoftwares6-Oct-10 1:44 
GeneralReminds me of the "old days"... Pin
hoernchenmeister4-Oct-10 22:21
Memberhoernchenmeister4-Oct-10 22:21 
GeneralRe: Reminds me of the "old days"... Pin
NaveenSoftwares5-Oct-10 1:47
MemberNaveenSoftwares5-Oct-10 1:47 
GeneralSoothing Rain Pin
MikJr4-Oct-10 17:20
MemberMikJr4-Oct-10 17:20 
GeneralRe: Soothing Rain Pin
NaveenSoftwares5-Oct-10 19:27
MemberNaveenSoftwares5-Oct-10 19:27 
GeneralMy vote of 5 Pin
louislong30-Sep-10 11:20
Memberlouislong30-Sep-10 11:20 
GeneralRe: My vote of 5 Pin
NaveenSoftwares30-Sep-10 20:26
MemberNaveenSoftwares30-Sep-10 20:26 
Generalnot bad Pin
BillW3328-Sep-10 9:50
professionalBillW3328-Sep-10 9:50 
GeneralRe: not bad Pin
NaveenSoftwares28-Sep-10 18:14
MemberNaveenSoftwares28-Sep-10 18:14 
GeneralMy vote of 5 Pin
Kubajzz28-Sep-10 6:58
MemberKubajzz28-Sep-10 6:58 
GeneralRe: My vote of 5 Pin
NaveenSoftwares28-Sep-10 18:13
MemberNaveenSoftwares28-Sep-10 18:13 
GeneralImage format Pin
Mi.Chal.28-Sep-10 5:09
MemberMi.Chal.28-Sep-10 5:09 
GeneralRe: Image format Pin
NaveenSoftwares28-Sep-10 5:25
MemberNaveenSoftwares28-Sep-10 5:25 
GeneralFormatting required Pin
Praveen Nair (NinethSense)28-Sep-10 0:59
MemberPraveen Nair (NinethSense)28-Sep-10 0:59 
GeneralRe: Formatting required Pin
NaveenSoftwares28-Sep-10 1:12
MemberNaveenSoftwares28-Sep-10 1:12 
GeneralRe: Formatting required Pin
Praveen Nair (NinethSense)28-Sep-10 1:52
MemberPraveen Nair (NinethSense)28-Sep-10 1:52 
GeneralRe: Formatting required Pin
NaveenSoftwares28-Sep-10 3:38
MemberNaveenSoftwares28-Sep-10 3:38 
GeneralRe: Formatting required Pin
Praveen Nair (NinethSense)28-Sep-10 3:52
MemberPraveen Nair (NinethSense)28-Sep-10 3: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.

Posted 28 Sep 2010

Tagged as


56 bookmarked