/**********************************************************\ * Rocks : a bit-mapped arcade game for the AT&T UNIX PC. * * * * By : Hans Jespersen * * * \**********************************************************/ #include "rocks.h" void collission_check(); int explode; playgame() { int key; int i; int yp, xp, bp, rp; flushinp(); for(explode = FALSE; quitflag != TRUE ;) { key = wgetc( wn ); switch( key ) { case Mouse: wreadmouse( wn, &xp, &yp, &bp, &rp ); switch( bp ){ case MBUTL: rotate( LEFT ); break; case MBUTL|MBUTR: break; case MBUTM: thrust(); break; case MBUTM|MBUTL: thrust(); rotate( LEFT ); break; case MBUTM|MBUTR: thrust(); rotate( RIGHT ); break; case MBUTALL: break; case MBUTR: rotate( RIGHT ); break; } break; case BEEP: togglebeep(); break; case LEFTKEY: rotate( LEFT ); break; case RIGHTKEY: rotate( RIGHT ); break; case SHOOT: shoot(); break; case THRUST: thrust(); break; case HYPERSPACE: hyperspace(); break; case QUIT: cleanup(); } moverocks(); moveshots(); moveship(); printscreen(); collission_check(); } if( explode ){ debrislist[0].x = myship.x; debrislist[0].y = myship.y; debrislist[0].xdelta = -1; debrislist[0].ydelta = 0; debrislist[1].x = myship.x; debrislist[1].y = myship.y; debrislist[1].xdelta = 0; debrislist[1].ydelta = 1; debrislist[2].x = myship.x; debrislist[2].y = myship.y; debrislist[2].xdelta = 1; debrislist[2].ydelta = -1; } for(i=0; i < RANGE + 5 ; i++){ moverocks(); moveshots(); if( explode ) movedebris(); else moveship(); printscreen(); } clear(); } rotate( which_way ) int which_way; { wrastop(wn,ship[direction],2,screen,WIDTH,0,0,myship.x,myship.y,SHIP_WIDTH,SHIP_HEIGHT,SRCSRC,DSTCAM,0); direction += which_way; if ( direction == 8 ) direction = 0; else if ( direction == -1 ) direction = 7; wrastop(wn,ship[direction],2,screen,WIDTH,0,0,myship.x,myship.y,SHIP_WIDTH,SHIP_HEIGHT,SRCSRC,DSTOR,0); } togglebeep() { beepflag = 1 - beepflag; } void collission_check() { int i; for (i = 0; i < nrocks;i++){ if ( collide( myship, SHIP_WIDTH, SHIP_HEIGHT, rocklist[i], rockwidth[ rocklist[i].size ], rockheight[ rocklist[i].size ] ) ){ quitflag = TRUE; explode = TRUE; men -= 1; level -= 1; } } } /* * General collision detection routine based on overlapping rectangles. * Wraparound is considered only for second (ie. b ) rectangle. */ int collide( apos, awidth, aheight, bpos, bwidth, bheight ) position apos; int awidth; int aheight; position bpos; int bwidth; int bheight; { /* check for regular y-axis overlap */ if( (bpos.y >= apos.y - bheight) && (bpos.y <= apos.y + aheight) ) { /* check for regular x-axis overlap */ if( (bpos.x >= apos.x - bwidth) && (bpos.x <= apos.x + awidth) ) return( TRUE ); /* check for wraparound x-axis overlap */ if( (bpos.x - SCREEN_WIDTH >= apos.x - bwidth) && (bpos.x -SCREEN_WIDTH <= apos.x + awidth) ) return( TRUE ); else return( FALSE ); } /* check for wraparound y-axis overlap */ else if( (bpos.y - SCREEN_HEIGHT >= apos.y - bheight) && (bpos.y - SCREEN_HEIGHT <= apos.y + aheight) ) { /* check for regular x-axis overlap */ if( (bpos.x >= apos.x - bwidth) && (bpos.x <= apos.x + awidth) ) return( TRUE ); /* check for wraparound x-axis overlap */ if( (bpos.x - SCREEN_WIDTH >= apos.x - bwidth) && (bpos.x - SCREEN_WIDTH <= apos.x + awidth) ) return( TRUE ); else return( FALSE ); } /* no overlap */ else return( FALSE ); } printscore() { char scorestr[7]; char outstr[20]; int digit; int tmpscore; int index; tmpscore = score; strcpy( outstr , "score : "); for( index = 0; index <= 5; index++ ) scorestr[index] = ' '; scorestr[6] = '\0'; if( tmpscore == 0 ) scorestr[5] = '0'; else{ for( index = 5; tmpscore > 0; index-- ) { digit = tmpscore % 10; tmpscore = (tmpscore - digit)/10; scorestr[index] = digit + '0'; } } strcat( outstr, scorestr ); wprompt(wn,outstr); } int randdir() { int direction; if ( rand() % 2 == 0 ) direction = -1; else direction = 1; return( direction ); }