Skip to content

Instantly share code, notes, and snippets.

@RaiDeiNz
Forked from mohd-akram/matrix.c
Last active November 13, 2020 09:29
Show Gist options
  • Save RaiDeiNz/74622963340ae4f87e54d7bfbdffcd0b to your computer and use it in GitHub Desktop.
Save RaiDeiNz/74622963340ae4f87e54d7bfbdffcd0b to your computer and use it in GitHub Desktop.
Matrix digital rain in C.
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define WIDTH 80
#define HEIGHT 40
#define idx(x, y) ((x)+WIDTH*(y))
struct trail {
float start;
float end;
float speed;
} trails[WIDTH];
void init_trail(struct trail *trail)
{
trail->start = -(float)(rand() % HEIGHT);
trail->end = 0;
trail->speed = (rand() % 5) / 10.0f + 0.2f;
}
void update(CHAR_INFO consoleBuffer[])
{
CHAR_INFO space = {.Char.AsciiChar=' ', .Attributes=0};
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++)
consoleBuffer[idx(x, y)] = space;
int start = trails[x].start < 0 ? 0 : (int)trails[x].start;
int end = trails[x].end > HEIGHT ? HEIGHT : (int)trails[x].end;
for (int y = start; y < end; y++)
consoleBuffer[idx(x, y)] = (CHAR_INFO) {
.Char.AsciiChar = rand() % 256,
.Attributes = FOREGROUND_GREEN |
(y < (int)trails[x].end-1 ? 0 :
FOREGROUND_RED | FOREGROUND_BLUE)};
trails[x].start += trails[x].speed;
trails[x].end += trails[x].speed;
if (trails[x].start >= HEIGHT)
init_trail(&trails[x]);
}
}
int main(void)
{
srand((unsigned)time(NULL));
/* Console window */ // Need full size.
HANDLE wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT windowSize = {0, 0, WIDTH-1, HEIGHT-1};
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
COORD bufferSize = {WIDTH, HEIGHT};
SetConsoleScreenBufferSize(wHnd, bufferSize);
CHAR_INFO consoleBuffer[WIDTH * HEIGHT];
COORD charBufSize = {WIDTH, HEIGHT};
COORD characterPos = {0, 0};
SMALL_RECT writeArea = {0, 0, WIDTH-1, HEIGHT-1};
SetConsoleTitle(TEXT("NeO Way!"));
/* Trails */
for (int i = 0; i < WIDTH; i++)
init_trail(&trails[i]);
/* Matrix */
for (;;) {
update(consoleBuffer);
WriteConsoleOutputA(wHnd, consoleBuffer,
charBufSize, characterPos, &writeArea);
Sleep(11);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment