/* Copyright 2005 alexeyt at freeshell dot org * released under GPL version 2.0 or later * A simplistic ASCII-art aquarium. Build with: * cc -o aquarium aquarium.c -lcurses * You can give the number of fish as an argument. Have fun. */ #include #include #include #include #include struct fish { int x; int y; int color; int right; }; void finish ( int sig ) { endwin(); exit ( sig ); } void spawn ( struct fish *f ) { f->y = random() % LINES; f->color = random() % 7 + 1; if ( random() % 2 ) { f->right = 1; f->x = -1; } else { f->right = 0; f->x = COLS; } } void swim ( struct fish *f ) { int x; attrset ( COLOR_PAIR ( f->color ) | A_BOLD ); if ( f->right ) { if ( ++f->x > COLS ) { move ( f->y, COLS - 1 ); addch ( ' ' ); spawn ( f ); } x = ( f->x - 2 ); if ( x >= 0 ) { move ( f->y, x ); addch ( ' ' ); } x++; if ( x >= 0 ) { if ( !x ) move ( f->y, 0 ); addch ( '>' ); } x++; if ( ( x >= 0 ) && ( x < COLS ) ) { if ( !x ) move ( f->y, 0 ); addch ( '<' ); } x++; if ( x < COLS ) { if ( !x ) move ( f->y, 0 ); addch ( '>' ); } } else { if ( f->x-- < 0 ) { move ( f->y, 0 ); addch ( ' ' ); spawn ( f ); } x = ( f->x - 1 ); if ( x >= 0 ) { move ( f->y, x ); addch ( '<' ); } x++; if ( ( x >= 0 ) && ( x < COLS ) ) { if ( !x ) move ( f->y, 0 ); addch ( '>' ); } x++; if ( x < COLS ) { if ( !x ) move ( f->y, 0 ); addch ( '<' ); } x++; if ( x < COLS ) addch ( ' ' ); } move ( 0, 0 ); refresh(); } void curses_init() { initscr(); noecho(); nonl(); cbreak(); intrflush ( stdscr, 0 ); keypad ( stdscr, 0 ); start_color(); if ( has_colors() && ( COLOR_PAIRS > 7 ) ) { init_pair ( 1, COLOR_GREEN, COLOR_BLACK ); init_pair ( 2, COLOR_RED, COLOR_BLACK ); init_pair ( 3, COLOR_YELLOW, COLOR_BLACK ); init_pair ( 4, COLOR_BLUE, COLOR_BLACK ); init_pair ( 5, COLOR_MAGENTA, COLOR_BLACK ); init_pair ( 6, COLOR_CYAN, COLOR_BLACK ); init_pair ( 7, COLOR_WHITE, COLOR_BLACK ); bkgd ( COLOR_PAIR(7) ); } else { endwin(); exit ( 1 ); } } int main ( int argc, char **argv ) { int nfish, i; struct fish *school; if ( ( argc < 2 ) || ( ( nfish = atoi ( argv[1] ) ) <= 0 ) ) nfish = 10; if ( ! ( school = calloc ( nfish, sizeof ( struct fish ) ) ) ) exit ( 1 ); srandom ( time ( 0 ) ); signal ( SIGINT, finish ); curses_init(); for ( i = 0; i < nfish; i++ ) { spawn ( &( school[i] ) ); school[i].x = random() % COLS; } while ( 1 ) { for ( i = 0; i < nfish; i++ ) swim ( &( school[i] ) ); usleep ( 100000 ); } endwin(); exit ( 0 ); }