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
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)
{
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)
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:
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;
else
Console.ForegroundColor = NormalColor;
Console.Write(AsciiCharacter);
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(' ');
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);
}
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.
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).
Developer in HP Exstream(Dialogue),C#,PHP,C-Lite.
Profession link:
http://in.linkedin.com/pub/naveen-kumar-r/25/42a/2a3/