C# Console Matrix
Written by: maffelu , 2010-01-10 14:39:16
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[italic] = new Thread(RunMatrix);
threads[italic].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[italic] = new Thread(RunMatrix);
threads[italic].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 no comments on this article.
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.