void death(double astr[][5], double shots[][5], double *x, double *y, int score[]) { int i; gfx_clear(); gfx_color(255, 0, 0); for(i=0; i<10; i++){ gfx_flush(); gfx_color(255, 0, 0); drawship(350, 350, 0, 0); //redraws ship and pauses to allow user time to recover usleep(100000); gfx_color(255, 255, 255); drawship(350, 350, 0, 0); usleep(100000); } gfx_flush(); *x=350; *y=350; score[1]=score[1]-1; //subtracts a life initialize(astr); //resets astroids and shots initialize(shots); }
void flyship(void) { double x, y; double theta; char c; int i, go, x1ship, x2ship, y1ship, y2ship; int score[2]; score[0]=0; score[1]=3; char str[100]; go=0; //will tell drawship function if forward button is held down so flames can display int iterate=0; //counts iterations of loop to determine when to move asteroids x=350; y=350; //initial conditions theta=0; //the 2d array roids will contain 32 elements, one for each potential asteroid //and each element will have four numbers for the asteroids' properties double astr[32][5]; double shots[32][5]; //2d array for the shots out of the ship and its properties initialize(astr); initialize(shots); drawship(x, y, theta, go); double speed=0; while(1){ if(gfx_event_waiting()) { c=gfx_wait(); if (score[1]==0 || c=='q') { //this will run when lives are 0 and display the score gfx_clear(); sprintf(str, "Score: %i Press q to exit game", score[0], score[1]); gfx_text(250, 350, str); c=gfx_wait(); if(c=='q'){ break; } } if(c=='d'){ theta=theta-PI/20; //this will rotate the ship } if(c=='a'){ theta=theta+PI/20; } if(c=='w'){ speed=speed+1; //moves ship forward if (speed>5){ speed=5; } go=1; //will tell drawship function that ship is moving } if(c=='o') { shoot(shots, theta, x, y); } if(x>700){ x=1; } else if(x<0){ x=699; } else if(y>700){ //this keeps the ship from flying off the screen y=1; } else if(y<0){ y=699; } } gfx_clear(); usleep(10000); x1ship= x+20*cos(theta+4*PI/3); //these variables create a sort of box around the ship, which is later used to check the ship's coordinates for collisions y1ship= y; x2ship= x+20*cos(theta+5*PI/3); y2ship= y-20*sin(theta); move_shots(shots); destroy(astr, shots, score); //checks whether an asteroid should be destroyed move_asteroids(astr, shots, x1ship, y1ship, x2ship, y2ship, &x, &y, &speed, &theta, score); x=x+speed*cos(theta+PI/2); //iterates coordinates of ship based on changes to theta and speed y=y-speed*sin(theta+PI/2); drawship(x, y, theta, go); if(go>=1){ go=go-1; //keeps go at 1 so it stops when stop pressing forward button } if(speed>.025){ speed=speed-.025; } iterate++; sprintf(str, "Score: %d Lives: %d", score[0], score[1]); gfx_text(20, 680, str); } }
int main() { int x; char gravcount; struct terrain *terrain; terrain = loadterrain("TERRAIN.DAT"); if (terrain == NULL) return (1); if (loadship("SHIP.DAT") != 0) return (1); initgraph(); px = 160 * 16; py = 9 * 16; velx = 1; vely = 0; while (exitflag == 0) { /* physics */ px += velx; py += vely; posx = px / 16; posy = py / 16; gravcount++; /* gravity */ if (gravcount > 100) { gravcount = 0; vely++; } /* input */ scankb(); /* wrap around sides of screen */ if (posx < minx + 9) px = (maxx - 9) * 16; if (posx > maxx - 9) px = (minx + 9) * 16; /* prevent leaving the top of the screen */ if (posy < miny + 9) { py = (miny + 9) * 16; vely = 0; } /* terrain collision checking */ if (posy > maxy - terrain->data[posx] - 7) crash(); if (posy > maxy - terrain->data[posx] - 6) { for (x = -8; x <= 8; x++) if (posy != maxy - terrain->data[posx + x] - 9) if (crashflag == 0) land(); } /* swap frame buffer */ wtsync(); swap(virt, screen); cleardevice(virt); /* graphics */ drawship(posx, posy); drawterrain(terrain); } setmode(0x0003); return (0); }
/* calculates and draws both the ships on the water grid. */ void drawAllShips(void) { drawship(mesh_1, &translateShip_1, &rotateShip_1, &scaleShip_1, colour.red); drawship(mesh_2, &translateShip_2, &rotateShip_2, &scaleShip_2, colour.green); }