C# Console Matrix
Written by: maffelu , 2011-01-21
static void RunMatrix(object obj) {
//Since we know that the object being sent in is a Random object,
//we just shift it...
Random random = obj as Random;
//Get starting coordinate values
int x = getRandomX(random);
int y = getRandomY(random);
//This is how fast the letters will move, in milliseconds
int speed = random.Next(60, 100);
//These are the colors to use on the screen. Green is Matrix style,
//but they can of course be changed
ConsoleColor lightColor = ConsoleColor.Green;
ConsoleColor darkColor = ConsoleColor.DarkGreen;
while (true) {
int digitToOutput = random.Next(0, 9);
//This writes the letter with a bright light color
lock (syncLock) {
Console.SetCursorPosition(x, y);
Console.ForegroundColor = lightColor;
Console.Write(digitToOutput);
}
Thread.Sleep(speed);
//This overrites the previous letter with the proper dark color
lock (syncLock) {
Console.SetCursorPosition(x, y);
Console.ForegroundColor = darkColor;
Console.Write(digitToOutput);
}
if (y >= Console.WindowHeight - 1) {
x = getRandomX(random);
y = getRandomY(random);
}
//Move down one step for every loop
y ;
}
}
static int getRandomX(Random r) {
return r.Next(0, Console.WindowWidth - 1);
}
static int getRandomY(Random r) {
return r.Next(0, Console.WindowHeight - 1);
}
static void Main(string[] args) {
Random r = new Random();
Thread[] threads = new Thread[15];
for (int i = 0; i < threads.Length; i ) {
threads[i] = new Thread(RunMatrix);
threads[i].Start(r);
}
Console.Read();
}
using System;
using System.Threading;
namespace MorkaMatrix {
class Program {
static object syncLock = new object();
static void Main(string[] args) {
Random r = new Random();
Thread[] threads = new Thread[15];
for (int i = 0; i < threads.Length; i ) {
threads[i] = new Thread(RunMatrix);
threads[i].Start(r);
}
Console.Read();
}
static void RunMatrix(object obj) {
//Since we know that the object being sent in is a Random object,
//we just shift it...
Random random = obj as Random;
//Get starting coordinate values
int x = getRandomX(random);
int y = getRandomY(random);
//This is how fast the letters will move, in milliseconds
int speed = random.Next(60, 100);
//These are the colors to use on the screen. Green is Matrix style,
//but they can of course be changed
ConsoleColor lightColor = ConsoleColor.Green;
ConsoleColor darkColor = ConsoleColor.DarkGreen;
while (true) {
int digitToOutput = random.Next(0, 9);
//This writes the letter with a bright light color
lock (syncLock) {
Console.SetCursorPosition(x, y);
Console.ForegroundColor = lightColor;
Console.Write(digitToOutput);
}
Thread.Sleep(speed);
//This overrites the previous letter with the proper dark color
lock (syncLock) {
Console.SetCursorPosition(x, y);
Console.ForegroundColor = darkColor;
Console.Write(digitToOutput);
}
if (y >= Console.WindowHeight - 1) {
x = getRandomX(random);
y = getRandomY(random);
}
//Move down one step for every loop
y ;
}
}
static int getRandomX(Random r) {
return r.Next(0, Console.WindowWidth - 1);
}
static int getRandomY(Random r) {
return r.Next(0, Console.WindowHeight - 1);
}
}
}
There are 10 comments on this article.
maffelu
2010-12-27 23:11:08
Doing this with Java would require little changes. I haven't worked too much with threading in Java, but I assume that it works in a similar way.
I'm pretty sure that if you copied this into a java project and changed the directly .NET-linked references (using Java´s Random instead of .NET's etc) it wouldn't take more than an hour to convert it.
Muppet
2011-01-19 19:56:18
I can confirm that the source is faulty, since it isnt copypastable, the webpage converts some of the code = makes it unusable
Muppet
2011-01-19 20:16:44
Okay, the correct code is,
Thread[] threads = new Thread[15];
for (int i = 0; i < threads.Length; i++) {
threads[i] = new Thread(RunMatrix);
threads[i].Start(r);
}
Maffelu
2011-01-21 11:50:10
Muppet: I just saw that, thanks for noticing, the code has been fixed!
Muppet
2011-01-22 22:57:22
No problem, really nice program, trying to convert it to XNA atm, to use as a screensaver thing,
Current stuff that i have functioning :
Mouse collision with the letters
full screen + shifting colors
should be a nice thing to try if you are boored ;)
aviation
2011-03-23 17:07:22
This code can not be ported to Java without major changes to the displaying code. Because Java is cross-platform, it doesn't know what it will be printing too. It could be the console for any of at least a half-dozen OSs, or the console to any of dozens of IDEs. As a result, you have very limited printing ability. The only commands that I know of are print() and println() (the difference being whether you go to a new line after printing or not). You cannot freely move the cursor, change the color, etc.
This is still a very cool idea.
Maffelu
2011-03-24 23:22:36
aviation: It would be quite easy to adapt the code to java. As you point out the major change is the output methods. The logic will work pretty much in the same way, so if you want to convert this to java, look at it as more of a guideline than a direct path.
Cheers!
lwusernamey52
2011-12-26 08:45:11
Modern Prestige is a well made, reliable, multi-purpose site. It supplies frequent, relevant news updates that are both concise and interesting. It is the first place I look for gaming news. The forums are interactive and helpful, and the support on the site is responsive, personal, and well informed. The site is well designed and easy to navigate.
We host games such as http://ModernPrestige.com - Modern warfare 3, http://ModernPrestige.com - MW3, http://ModernPrestige.com - Call of duty DLC, http://ModernPrestige.com - DLC leak, http://ModernPrestige.com - MW3 DLC, http://ModernPrestige.com - Morning Wood, http://ModernPrestige.com - ModernPrestige, http://ModernPrestige.com - MP, http://ModernPrestige.com - Battlefield 3
siusernamed75
2011-12-27 21:50:03
Hi, let me introduce you to our friendship based forum, <a href=http://cafe-des-ados.net>http://cafe-des-ados.net</a>.
Our forum is in french language by default ( friend in french = <a href=http://cafe-des-ados.net>ados</a> )
You are all welcome to join our community, meet new peoplle and discuss about love, family, hobbies and more!
Join usm we will be waiting for you!!
<a href=http://cafe-des-ados.net>www.cafe-des-ados.net/forum</a>
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.
Amy
2010-12-26 05:51:18
Nice post..
I wanna ask you, what if we create it using java console? I'm a lil bit confused to do that..
Need some help. Thanks.