/* bounce2d 1.0 * bounce a character around the screen * defined by some parameters * * user input: s slow down x component * S slow down y component * f speed up x component * F speed up y component * Q quit * * blocks on read, but timer tick sends SIGALRM caught by ball_move * build: cc bounce2d.c -l curses -o bounce2d */ #include<curses.h> #include<signal.h> #include<sys/time.h> #include"bounce.h"
structppballthe_ball; structboardthe_board;
// the main loop voidset_up(); voidwrap_up();
intmain() { FILE *fp; fp = fopen("result", "w"); int c; set_up(); while (1) { c = getch(); if (c == 'Q') break;
int i; for (i = LEFT_EDGE-1; i <= RIGHT_EDGE+1; ++i) { mvaddch(TOP_ROW-1, i, X_BUND); //mvaddch(ERROR+1, i, XX_BUND); //mvaddch(i, TOP_ROW, X_BUND); //mvaddch(i, BOT_ROW, X_BUND); } for (i = TOP_ROW; i <= ERROR+1; ++i) { mvaddch(i, LEFT_EDGE-1, Y_BUND); mvaddch(i, RIGHT_EDGE+1, Y_BUND); //mvaddch(RIGHT_EDGE, i, Y_BUND); //mvaddch(LEFT_EDGE, i, Y_BUND); }
for (i = the_board.left; i < the_board.left + the_board.length; ++i) mvaddch(the_board.row, i, '_');
voidgame_over() { char * s = "GAME OVER"; int i, j; for (i = (LEFT_EDGE+RIGHT_EDGE)/2-4, j = 0; j <= sizeof(s)/sizeof(char); ++i, ++j) mvaddch((TOP_ROW+BOT_ROW)/2, i, s[j]); refresh(); //wrap_up(); signal(SIGALRM, SIG_DFL); sleep(2); endwin(); exit(-1); }
voidchange_board(int num) { int i; for (i = the_board.left; i < the_board.left + the_board.length; ++i) mvaddch(the_board.row, i, ' '); the_board.left += num; for (i = the_board.left; i < the_board.left + the_board.length; ++i) mvaddch(the_board.row, i, '_'); the_board.how = 0; //refresh(); }
/* [from set_ticker.c] * set_ticker(number_of_milliseconds) * arranges for interval timer to issue SIGALRMs at regular intervals * returns -1 on error, 0 for ok * arg in milliseconds, converted into whole seconds and mocroseconds * note: set_ticker(0) turns off ticker */ intset_ticker(int n_msecs) { structitimervalnew_timeset; long n_sec, n_usecs;
n_sec = n_msecs / 1000; // int part n_usecs = (n_msecs % 1000) * 1000L; // remainder
new_timeset.it_interval.tv_sec = n_sec; // set reload new_timeset.it_interval.tv_usec = n_usecs; // new ticker value
new_timeset.it_value.tv_sec = n_sec; // store this new_timeset.it_value.tv_usec = n_usecs;