Created
November 9, 2012 00:42
-
-
Save jquast/4042957 to your computer and use it in GitHub Desktop.
some silly C ansimation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
/* modify speed if it runs too fast */ | |
#define SPEED 20 | |
#define FLAKES 128 | |
#define FLIES 15 | |
#define COLUMNS 80 | |
#define ROWS 25 | |
#define CENTER_X 40 | |
#define CENTER_Y 12 | |
// ANSI colors | |
#define BLACK 30 | |
#define RED 31 | |
#define GREEN 32 | |
#define BROWN 33 | |
#define BLUE 34 | |
#define PURPLE 35 | |
#define CYAN 36 | |
#define GREY 37 | |
// ANSI attributes | |
#define NORMAL 0 | |
#define BRIGHT 1 | |
#define DIM 2 | |
#define UNDERLINE 4 | |
#define BLINK 5 | |
// ANSI functions | |
void color (unsigned short int, unsigned short int); | |
void ansipos (short int, short int); | |
void cls (); | |
void cursor_off (); | |
void cursor_on (); | |
int rnd (int, int); | |
void draw_main (void); | |
void demo1 (void); | |
void demo2 (void); | |
void demo3 (void); | |
void typewriter (char **); | |
void goto_dos (void); | |
void color (unsigned short int color, unsigned short int attr) { | |
/* display ANSI sequence for defined color attributes */ | |
printf ("\33[%d;%dm", attr, color); | |
} | |
int rnd (int min, int max) { | |
/* return a random number between min and max */ | |
return (rand() % (max - min + 1)) + min; | |
} | |
void ansipos (short int x, short int y) { | |
/* move the location of the cursor position x, y */ | |
if(x < 1 || x > COLUMNS || y < 1 || y > ROWS) return; //saftey net | |
printf ("\33[%d;%dH", y, x); | |
} | |
void cls () { | |
/* clear the screen */ | |
printf ("\33[H\33[J"); | |
} | |
void cursor_off () { | |
/* make cursor visable */ | |
printf ("\33[?25h"); | |
} | |
void cursor_on () { | |
/* hide cursor (sometimes) */ | |
printf ("\33[?25l"); | |
} | |
int main (int argc, char **argv) { | |
int quit = 0; | |
char ch; | |
srand (time (NULL)); | |
cls (); | |
cursor_on (); | |
while (!quit) { | |
draw_main (); | |
ansipos (1, ROWS-1); | |
ch = getc (stdin); | |
switch (ch) { | |
case '0': demo1 (); break; | |
case '1': demo2 (); break; | |
case '2': demo3 (); break; | |
case '3': quit++; break; | |
default: break; | |
} | |
} | |
cls (); | |
goto_dos (); | |
cursor_off (); color (GREY, NORMAL); | |
return 0; | |
} | |
void draw_main () { | |
/* weak... */ | |
int x, y; | |
char *title = "Dingo's ansi-rama"; | |
int center = (80 / 2) - (strlen (title) / 2); | |
char *choices[] = { | |
"cold winter in ANSIland", | |
"nethack on LSD", | |
"sit on the porch and swat at flies", | |
"exit to DOS" }; | |
register unsigned short int sw = 0; | |
register int count, n; | |
ansipos (center, 10); | |
color (GREEN, BRIGHT); | |
printf ("%s", title); | |
// earthquake menu | |
for (count=0; count < .8 * SPEED; count++) { | |
for (n=0; n < 4; n++) { | |
ansipos(15 + rnd(0,8), 14 + n); | |
sw ? color(GREEN, NORMAL) : color(GREEN, BRIGHT); | |
printf (" %i. %s ", n, choices[n]); | |
for (y=12; y < 20; y +=7 ) { | |
for (x=25; x < 55; x +=2) { | |
ansipos (x, y); | |
color (CYAN, sw ? BRIGHT : NORMAL ); printf("-"); | |
color (CYAN, sw ? NORMAL : BRIGHT ); printf("="); | |
usleep (.1*SPEED); printf("\n"); | |
sw ? sw -- : sw ++; | |
} | |
} | |
sw ? sw-- : sw ++; | |
usleep(.1*SPEED); printf("\n"); | |
} | |
} | |
ansipos(25, 20); | |
printf("Enter choice and press enter\n"); | |
return; | |
} | |
void demo1 () { | |
/* Every snowflake is unique */ | |
char *mesg[] = { | |
" bits hath been shaken,", | |
" the programmer dare hacketh", | |
" black magic of ANSI C", "\0"}; | |
char ch_flakes[] = { | |
'.', ',', '-', '\'', | |
'^', ':', ';', '*', | |
'-', '^', '%', '^', | |
'.', ',', '.' }; | |
struct { | |
int ch; | |
float ypos; | |
float xpos; | |
float xslp; | |
float yslp; | |
} sf[FLAKES]; | |
register int c, n; | |
int wind_xslp = 1; // sidestrength | |
int wind_yslp = 1; // downforce | |
// create snowflakes | |
for(c=0; c < FLAKES; c++) { | |
if (rnd(0,2)==1) { | |
sf[c].ypos = rnd (3, 6); | |
sf[c].xpos = rnd (8, 14); | |
sf[c].ch = rnd(0,7); | |
} else { | |
sf[c].ypos = rnd (16, 20); | |
sf[c].xpos = rnd (68, 73); | |
sf[c].ch = rnd(6,14); | |
} | |
sf[c].xslp = rnd (-10, 10)*.03; | |
sf[c].yslp = rnd (-10, 10)*.02; | |
} | |
cls (); | |
if (rnd (0,4) == 2) typewriter(mesg); | |
/* Mother Nature BEGIN */ | |
for (n=0; n < 100 * SPEED; n++) { | |
/* wind blows */ | |
if (n%4 == 1) { | |
wind_xslp = rnd (-9,9); | |
wind_yslp = rnd (-4,4); | |
} | |
/* draw flakes */ | |
for (c=0; c<FLAKES; c++) { | |
// add a little color variaty as the loop progresses | |
if (n < 100 * SPEED / 2) { | |
if (c%12 == 1) color (GREY, BRIGHT); | |
else if (c%7 == 1) color (BLACK, BRIGHT); | |
else color (GREY, NORMAL); | |
} else if (n<100*SPEED-(100*SPEED/3)) { | |
if (c%12 == 1) color (GREY, BRIGHT); | |
else if (c%7 == 1) color (BLUE, BRIGHT); | |
else color (CYAN, NORMAL); | |
} else { | |
color (rnd(BLACK, GREY), rnd(NORMAL, BRIGHT)); | |
} | |
ansipos ((int) sf[c].xpos, (int) sf[c].ypos); | |
putchar (ch_flakes[sf[c].ch]); | |
ansipos (1,1); putchar('\n'); | |
} | |
usleep(.1 * SPEED); | |
/* snowflake calculations */ | |
for (c=0; c < FLAKES; c++) | |
{ | |
// evolve | |
sf[c].ch == 14 ? sf[c].ch = 0 : sf[c].ch++; | |
// breeze | |
if (rnd (0,10) < 8) { | |
sf[c].yslp += wind_yslp*.02; | |
sf[c].xslp += wind_xslp*.03; | |
} | |
// calm the furious flakes | |
if (sf[c].xslp > 3 || sf[c].xslp < -3) sf[c].xslp *= (rnd(3,9)*.1); | |
if (sf[c].yslp > 2 || sf[c].yslp < -2) sf[c].yslp *= (rnd(3,9)*.1); | |
} | |
/* erase old flakes */ | |
for (c=0; c < FLAKES; c++) { | |
ansipos ((int) sf[c].xpos, (int) sf[c].ypos); | |
if (!n<100*SPEED-(100*SPEED/3)) | |
putchar(' '); | |
} | |
/* update flake position */ | |
for (c=0; c < FLAKES; c++) { | |
sf[c].xpos += sf[c].xslp; | |
sf[c].ypos += sf[c].yslp; | |
// astroids! teleport | |
if (sf[c].xpos < 1) sf[c].xpos = COLUMNS-1; | |
else if (sf[c].xpos > COLUMNS) sf[c].xpos = 1; | |
if (sf[c].ypos < 1) sf[c].ypos = ROWS-1; | |
else if (sf[c].ypos > ROWS) sf[c].ypos = 1; | |
} | |
} | |
/* END Mother Nature */ | |
} | |
void demo2 () { | |
/* a nethack nightmare */ | |
float scale = 1.0; | |
float angle = 0.2; | |
int lp = 0; | |
int mod = 0; | |
int y, x; | |
char ch; | |
cls(); | |
while (lp < 8*SPEED) { | |
while (scale > 0) { | |
y = (int) (CENTER_Y + ((CENTER_Y-1) * sinf(angle) * scale)); | |
x = (int) (CENTER_X + ((CENTER_X-1) * cosf(angle) * scale)); | |
ansipos (x, y+1); | |
if (lp%4) { | |
ch = rnd('A','z'); | |
color (rnd(30, 37), (rnd(0,1))); | |
} else { | |
ch = ' '; | |
color (BLACK, NORMAL); | |
} | |
printf ("%c\n",ch); | |
angle += .001+mod; | |
scale -= .0007; | |
} | |
mod+=rnd(1,1000)*.01; | |
if (angle > 100) | |
angle += (rnd(-100,100)); | |
else | |
angle += rnd(0,5); | |
lp +=1; | |
scale = 1.0; | |
} | |
return; | |
} | |
void demo3() { | |
/* dont ask... */ | |
struct { | |
char ch; | |
float ypos; | |
float xpos; | |
float yslp; | |
float xslp; | |
} fly[FLIES]; | |
int n; | |
int c; | |
int zap; | |
const float speed = .1; | |
char wings[] = { '&', '*', '#' }; | |
for (n=0;n<FLIES;n++) { | |
if (rnd(0,2)==1) { | |
fly[n].xpos = rnd(0,10); | |
fly[n].ypos = rnd(0,ROWS); | |
} else { | |
fly[n].xpos = rnd(0,COLUMNS); | |
fly[n].ypos = rnd(0,4); | |
} | |
fly[n].ch = wings[rnd(0,2)]; | |
fly[n].xslp = fly[n].yslp = 0.0; | |
} | |
/* takeoff */ | |
for(c=0;c<40*SPEED;c++) { | |
// erase flies | |
color (BLACK, NORMAL); | |
for (n=0;n<FLIES;n++) { | |
ansipos ((int) fly[n].xpos, (int)fly[n].ypos); | |
putchar (' '); | |
} | |
// fly flies | |
for (n=0;n<FLIES;n++) { | |
if (fly[n].xpos > CENTER_X) fly[n].xslp -= speed; | |
else fly[n].xslp += speed; | |
if (fly[n].ypos > CENTER_Y) fly[n].yslp -= speed; | |
else fly[n].yslp += speed; | |
fly[n].xpos += fly[n].xslp; | |
fly[n].ypos += fly[n].yslp; | |
} | |
// zap a fly | |
if (c%64==1) { | |
zap = rnd(0,FLIES); | |
ansipos ((int) fly[zap].xpos, fly[zap].ypos); | |
color (RED, BRIGHT); | |
putchar ('*'); | |
fly[zap].xpos = rnd(0,COLUMNS); | |
fly[zap].ypos = rnd(0,ROWS-1); | |
fly[zap].xslp = fly[zap].yslp = 0; | |
} | |
// draw flies | |
color (GREY, NORMAL); | |
for(n=0;n<FLIES;n++) { | |
ansipos ((int) fly[n].xpos, (int)fly[n].ypos); | |
n%2 ? color(GREY, NORMAL) : color(BLACK, BRIGHT); | |
putchar (wings[rnd(0,2)]); | |
} | |
usleep(4000*SPEED); putchar('\n'); | |
} | |
return; | |
} | |
void typewriter(char *mesg[]) { | |
int x, y, xc, yc; | |
int n, ch, f; | |
int cl[] = {BLACK, GREY, GREY, GREY}; | |
int ca[] = {BRIGHT, NORMAL, BRIGHT, NORMAL}; | |
int c=0; | |
cls (); | |
color (GREY, NORMAL); | |
while (mesg[c] != "\0") c++; | |
n = 100; | |
yc = CENTER_Y-(c/2); | |
for (n=0;n<c;n++) { | |
xc = (COLUMNS/2) - (strlen(mesg[n])/2); | |
for(ch=0;ch<strlen(mesg[n])+4;ch++) { | |
if (ch > 3) { | |
color (BLACK, BRIGHT); | |
ansipos (xc+ch-4, yc+n); | |
printf ("%c\n", mesg[n][ch-4]); | |
usleep(1); | |
} | |
if (ch < strlen(mesg[n])) { | |
for (f=0;f<4;f++) { | |
color (cl[f], ca[f]); | |
ansipos (xc+ch, yc+n); | |
printf ("%c\n", mesg[n][ch]); | |
usleep(1); | |
} | |
} | |
} | |
} | |
return; | |
} | |
void goto_dos() { | |
color (GREY, NORMAL); | |
printf("C:\\WINDOWS> ps ax\n"); | |
printf(" PID TT STAT TIME COMMAND\n"); | |
printf(" 1 ?? Is 0:00.05 C:\\COMMAND.COM\n"); | |
printf(" 9129 ?? CRASHED 0:00.04 system logger\n"); | |
printf(" 5310 ?? I 4:23.79 C:\\WINDOWS\\SYSTEM32\\MYDOOM.COM\n"); | |
printf("31635 ?? UNKNOWN 0:00.01 C:\\WINDOWS\\EXPLORER.EXE\n"); | |
printf("26953 ?? I 0:39.30 C:\\WINDOWS\\N0TEPAD.EXE\n"); | |
printf("28254 ?? Is 0:00.01 wheres my pants?\n"); | |
printf("19498 ?? I 0:00.15 \053\032\043\45\053\034\045\143\234\n"); | |
printf("24190 ?? Is 0:00.01 C:\\PROGRA~1\\SERV-U\\SERVU.EXE /S i.b33n.hax0red.net /F D:\\MY PICTU~1\\\n"); | |
printf("22795 ?? ?!? 0:00.01 /usr/sbin/sshd\n"); | |
printf("16080 ?? Is 0:00.04 cron\n"); | |
printf("25765 C0- I 0:00.01 C:\\DIEBOLD\\VOTE.BAS /WINNER BUSH\n"); | |
printf("31641 C0 R+ 0:00.00 ps ax\n"); | |
printf("C:\\WINDOWS> whoami\n"); | |
printf(" ..have you seen my stack??? shit...\n"); | |
printf("C:\\WI\341\124\242Z\\<\n"); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment