matrix/src/main.c
2020-03-16 09:33:43 -04:00

147 lines
3.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <curses.h>
#define UP (1)
#define DOWN (2)
int main(int argc, char **argv) {
WINDOW term = *initscr();
start_color();
init_pair(1,COLOR_GREEN,COLOR_BLACK);
attron(COLOR_PAIR(1));
noecho();
if(nodelay(&term,TRUE)==ERR) {
puts("Error could not enter no-delay mode");
return ERR;
}
int time;
if(argc >= 3) {
time=atoi(argv[2]);
} else {
time=20;
}
timeout(time);
int maxX = COLS;
int maxY = LINES;
int initTailLen;
if(argc >= 2) {
initTailLen=atoi(argv[1]);
} else {
initTailLen=10;
}
int tailLen[maxX];
for(int i=0; i<maxX; i++) {
int num=rand()%2;
if(num>1) {
tailLen[i]=initTailLen+(rand()%(4%initTailLen));
} else {
if(num<=1) {
tailLen[i]=initTailLen-(rand()%(8%initTailLen));
}
}
}
int cascade[maxX];
int tail[maxX];
int jmpTail[maxX];
for(int i=0; i<maxX; i++)
cascade[i]=ERR;
int pause = 0;
int dir = DOWN;
int get = getch();
while(get!='q') {
if(get=='s') {
pause=1;
}
int xPos = rand()%maxX;//get random x position for the stream
if(cascade[xPos]==ERR) {//make sure the stream is set to active
cascade[xPos]=0;
}
for(int i=0; i<maxX; i++) {//move the streams down
if(cascade[i]!=ERR) {
tail[i] = cascade[i]-tailLen[i];
if(cascade[i]==maxY) {//check if the position is at the bottom of the screen
cascade[i]=ERR;//set the position to ERR if it is at the bottom
} else {
if(rand()%10==1) {//check if the stream will jump
if(!(jmpTail[i]<=maxY)) {
if(dir==DOWN) {
cascade[i]=cascade[i]+rand()%8;
jmpTail[i]=cascade[i]-tailLen[i];
} else {
cascade[i]=cascade[i]-rand()%8;
jmpTail[i]=cascade[i]+tailLen[i];
}
}
} else {
if(!pause) {
if(dir==DOWN) {
cascade[i]++;//increase the position
} else {
cascade[i]--;
}
}
}
}
}
if(tail[i]!=maxY) {//keep increasing the tail untill it is at the bottom
if(!pause) {
if(dir==DOWN) {
tail[i]++;
} else {
tail[i]--;
}
}
}
if(jmpTail[i]==maxY || jmpTail==0) {
jmpTail[i]=OK;
} else {
if(!pause) {
if(dir==DOWN) {
jmpTail[i]++;
} else {
jmpTail[i]--;
}
}
}
mvaddch(jmpTail[i],i,' ');
mvaddch(tail[i],i,' ');
mvaddch(cascade[i],i,(rand()%/*57*/57)+65);
}
refresh();
if(!pause) {
get=getch();
} else {
timeout(-1);
switch (getch()) {
case 's':
pause=!pause;
break;
case 'r':
pause=1;
if(dir=DOWN) {
dir=UP;
} else {
if(dir=UP) {
dir=DOWN;
}
}
break;
}
timeout(time);
}
}
endwin();
return 0;
}