Skip to content

Instantly share code, notes, and snippets.

@celsojr
Created January 22, 2018 03:45
Show Gist options
  • Select an option

  • Save celsojr/b799ff8a25abfd3c825aed06a4a50e7a to your computer and use it in GitHub Desktop.

Select an option

Save celsojr/b799ff8a25abfd3c825aed06a4a50e7a to your computer and use it in GitHub Desktop.
/*--------------------------------------------------------------------------------------------
* Copyright (c) 2017 Celso Junior, celsojrfull@gmail.com. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* Inspired by Matrix Rain: https://www.codeproject.com/Articles/113227/Matrix-Rain
* --------------------------------------------------------------------------------------------*/
namespace Matrix
{
using System;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using static System.Console;
using static CtrlHandlerCode;
using static System.ConsoleKey;
using static System.ConsoleColor;
class Program
{
[DllImport("Kernel32")]
static extern bool SetConsoleCtrlHandler(Handler handler, bool add);
delegate bool Handler(CtrlHandlerCode ctrlHandlerCode);
static bool thistime = false;
static Random rnd = new Random();
static ConsoleColor restoredColor;
static async Task Main()
{
SetConsoleCtrlHandler(new Handler(ConsoleEventHandler), true);
restoredColor = ForegroundColor;
WindowLeft = WindowTop = 0;
BufferHeight = LargestWindowHeight;
BufferWidth = LargestWindowWidth;
CursorVisible = false;
Init(out int width, out int height, out int[] y, out int[] l);
for (;;)
{
await Task.WhenAll(Task.Run(() => Step(width, height, y, l)), Task.Delay(40));
if (KeyAvailable)
{
if (ReadKey().Key == F5)
{
await Task.Run(() => Init(out width, out height, out y, out l));
}
}
}
}
static void Step(int width, int height, int[] y, int[] l)
{
thistime = !thistime;
for (var x = 0; x < width; ++x)
{
if (NotWhite())
{
continue;
}
else if (x % 11 != 10)
{
ForegroundColor = DarkGreen;
SetCursorPosition(x, YPos(y[x] - 2 - (l[x] / 40 * 2), height));
Write(Ascii);
ForegroundColor = Green;
}
SetCursorPosition(x, y[x]);
Write(Ascii);
y[x] = YPos(y[x] + 1, height);
SetCursorPosition(x, YPos(y[x] - l[x], height));
Write(' ');
int YPos(int n, int _height) => (n %= _height) < 0 ? n + _height : n;
bool NotWhite()
{
if (x % 11 == 10 && !thistime)
{
return true;
}
ForegroundColor = White;
return false;
}
}
}
static void Init(out int width, out int height, out int[] y, out int[] x)
{
var h1 = default(int);
var h2 = (h1 = (height = WindowHeight) / 2) / 2;
width = WindowWidth - 1;
y = new int[width];
x = new int[width];
Clear();
for (var i = 0; i < width; ++i)
{
y[i] = rnd.Next(height);
x[i] = rnd.Next(h2 * Multiplying(i), h1 * Multiplying(i));
int Multiplying(int j) => ((j % 11 != 10) ? 2 : 1);
}
}
static char Ascii
{
get
{
switch (rnd.Next(10))
{
case int s when s <= 2: return (char)('0' + rnd.Next(10));
case int s when s <= 4: return (char)('a' + rnd.Next(27));
case int s when s <= 6: return (char)('A' + rnd.Next(27));
default: return (char)(rnd.Next(32, 255));
}
}
}
static bool ConsoleEventHandler(CtrlHandlerCode eventCode)
{
switch (eventCode)
{
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
ForegroundColor = restoredColor;
break;
}
return (false);
}
}
enum CtrlHandlerCode : uint
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment