void ball_move(int signum) { int y_cur, x_cur, moved; signal( SIGALRM , SIG_IGN ); /* dont get caught now */ y_cur = the_ball.y_pos ; /* old spot */ x_cur = the_ball.x_pos ; moved = 0 ; if ( the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1 ){ the_ball.y_pos += the_ball.y_dir ; /* move */ the_ball.y_ttg = the_ball.y_ttm ; /* reset*/ moved = 1; } if ( the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1 ){ the_ball.x_pos += the_ball.x_dir ; /* move */ the_ball.x_ttg = the_ball.x_ttm ; /* reset*/ moved = 1; } if ( moved ){ mvaddch( y_cur, x_cur, BLANK ); mvaddch( y_cur, x_cur, BLANK ); mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol ); bounce_or_lose( &the_ball ); move(LINES-1,COLS-1); refresh(); } signal( SIGALRM, ball_move); /* for unreliable systems */ }
void ball_move( int signum ) { int y_cur, x_cur, moved; signal( SIGALRM, SIG_IGN ); y_cur = the_ball.y_pos; x_cur = the_ball.x_pos; moved = 0; if( the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1 ) { the_ball.y_pos += the_ball.y_dir; the_ball.y_ttg = the_ball.y_ttm; moved = 1; } if( the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1 ) { the_ball.x_pos += the_ball.x_dir; the_ball.x_ttg = the_ball.x_ttm; moved = 1; } if( moved ) { mvaddch( y_cur, x_cur, BLANK ); mvaddch( y_cur, x_cur, BLANK ); mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol ); bounce_or_lose( &the_ball ); move( LINES - 1, COLS - 1 ); refresh(); } signal( SIGALRM, ball_move ); }
/* * ball move handler * First checks if ball has moved earlier so that we call * bounce_or_loose only if ball moved. * if moved checks if ball bounced or lost. * bounce_or_loose is checked before moving the ball to ensure * that correct state of paddle and ball has been compared. * Otherwise if we move ball first and at the same time paddle * is moved by pressing keyboard key by user we may loose paddle * movement while ball_move is running because during ball_move * SIGIO is blocked. * moves ball when x/y ticker reaches 0. * if moved resets x/y ticker otherwise decrements it. * lost redraws ball at a new position * notifies if ball lost. * Return : void */ void ball_move(void) { int y_cur, x_cur;//x and y current position //check bounce_or_lose() first before moving the ball if( the_ball.m_moved && bounce_or_lose() == -1){//check if lost or bounced ballLost();//notifies if ball lost return;//return if ball lost } y_cur = the_ball.y_pos ; // old position x_cur = the_ball.x_pos ; the_ball.m_moved = 0 ; if ( the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1 ){//y movement the_ball.y_pos += the_ball.y_dir ; // move the_ball.y_ttg = the_ball.y_ttm ; // reset the_ball.m_moved = 1; } if ( the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1 ){//y movement the_ball.x_pos += the_ball.x_dir ; // move the_ball.x_ttg = the_ball.x_ttm ; // reset the_ball.m_moved = 1; } if ( the_ball.m_moved ){//if ball moved mvaddch( y_cur, x_cur, BLANK );//draw blank mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );//redraw move(LINES-1,COLS-1);//park cursor refresh();//refresh screen } }
extern void b_move(int signum) { signal(SIGALRM, SIG_IGN); int x_org_pos = the_ball.x_pos, y_org_pos = the_ball.y_pos; bool ball_moved = false; if (the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1) { the_ball.x_pos += the_ball.x_dir; the_ball.x_ttg = the_ball.x_ttm; ball_moved = true; } if (the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1) { the_ball.y_pos += the_ball.y_dir; the_ball.y_ttg = the_ball.y_ttm; ball_moved = true; } if (ball_moved == true) { mvaddch(y_org_pos, x_org_pos, BLANK); mvaddch(the_ball.y_pos, the_ball.x_pos, the_ball.symbol); bounce_or_lose(&the_ball); move(LINES - 1, COLS - 1); } if (the_bar.dir == BAR_UP && the_bar.top_y != TOP_EDGE) { mvaddch(the_bar.top_y + the_bar.dir, RIGHT_EDGE, BAR_SYMBOL); mvaddch(the_bar.bottom_y, RIGHT_EDGE, BLANK); the_bar.top_y += the_bar.dir; the_bar.bottom_y += the_bar.dir; } if (the_bar.dir == BAR_DOWN && the_bar.bottom_y != BOTTOM_EDGE) { mvaddch(the_bar.top_y, RIGHT_EDGE, BLANK); mvaddch(the_bar.bottom_y + the_bar.dir, RIGHT_EDGE, BAR_SYMBOL); the_bar.top_y += the_bar.dir; the_bar.bottom_y += the_bar.dir; } if (ball_moved || the_bar.dir != BAR_STOP) { refresh(); the_bar.dir = BAR_STOP; } signal(SIGALRM, b_move); }