ELEC2645 (2015/16) / Mbed 2 deprecated frdm_Asteroids

Dependencies:   N5110 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "main.h"
00002 
00003 ///enumerator stores the 9 possible directions the joystick could be positioned in
00004 enum DirectionName {
00005     Up,
00006     Upright,
00007     Right,
00008     Downright,
00009     Down,
00010     Downleft,
00011     Left,
00012     Upleft,
00013     Center,
00014 };
00015 
00016 typedef struct JoyStick Joystick;
00017 struct JoyStick {
00018     float x;
00019     float x0;
00020     float y;
00021     float y0;
00022     int button;
00023     DirectionName direction ;
00024 };
00025 Joystick joystick;
00026 int main()
00027 {
00028 
00029     lcd.init(); //the screen is initialised to allow interaction
00030 
00031     ticker.attach(&timer_isr,0.1);
00032     calibrateJoystick();
00033     b1.fall(&b1_isr);
00034     b2.fall(&b2_isr);
00035 
00036     b1.mode(PullDown);      //sets b1 as a pull down
00037     b2.mode(PullDown);      //sets b2 as a pull down
00038 
00039     //A Welcome message is displayed
00040 
00041     lcd.printString("Welcome to",2,1);
00042     lcd.printString("Asteroids" ,2,2);
00043     lcd.refresh();              //the screen is refreshed to display the message
00044     wait(2);                    //a small wait so the player can read the message
00045     lcd.clear();                //clers the message
00046     g_b1_flag  =0;               //sets the flags to 0
00047     g_b2_flag  = 0;
00048     while(1) {
00049         lcd.printString("Menu",1,1);
00050         lcd.printString("Play",2,2);
00051         lcd.printString("Settings",2,3);                //prints the two options on the menu
00052         lcd.refresh();                                  //refresh required to display on screen
00053         if (g_b2_flag ) {
00054             g_b2_flag  =0;                               //reset the flag
00055             break;                                      //breaks out the while loop and heads into the main game
00056         }
00057         if(g_b1_flag ) {
00058             g_b1_flag  = 0;                              //the flag is cleared
00059             settings  =1;                                //settings variable is increased to 1to allow the settings menu to be accessed
00060             lcd.clear();
00061         }
00062 
00063         while(settings  == 1) {
00064             lcd.printString("Normal Mode",2,2);
00065             lcd.printString("Inverse mode",2,3);        //prints the options from the settings menu
00066             lcd.refresh();
00067 
00068             if (g_b2_flag ) {
00069                 g_b2_flag  = 0;                          //resets the flag
00070                 lcd.normalMode();                       //normal mode is activated
00071                 settings  = 0;                           //the settings is decreased to 0 to exit the loop
00072                 lcd.clear();                            //the screen is cleared
00073             }
00074             if (g_b1_flag ) {
00075                 g_b1_flag  = 0;                          //resets the flag
00076                 lcd.inverseMode();                      //inverse mode is activated
00077                 settings  = 0;                           //settings is decreased to 0 to exit the loop
00078                 lcd.clear();                            //the screen is cleared
00079             }
00080         }
00081     }
00082     while(1) {
00083         while(game_over  == 0) {     //so long as game_over is 0 the game will loop
00084             if (g_timer_flag ) {     //the code will periodically run based on the timer
00085                 g_timer_flag  = 0;   //resets the timer flag
00086                 updateJoystick();   // the joystick is read
00087                 lcd.clear();        //the previous screen is cleared
00088                 game();             //the game function
00089                 score  ++;
00090                 //the score is incremented with time
00091                 lcd.refresh();      //the screen is refreshed to display the updated settings
00092             }
00093             sleep();                //the mbed is put to sleep to try and save power
00094         }
00095 
00096         lcd.clear();                            //the screen is cleared
00097         char buffer[14];
00098         sprintf(buffer,"%d",score );
00099 
00100         lcd.printString("GAME OVER",2,2);       //the message game over is printed
00101         lcd.printString(buffer,3,3);            //the players score is also printed
00102 
00103 
00104         lcd.refresh(); // refreshes the screen for players to see they lost and their score
00105         wait(2);        // a small wait so players can see the score
00106         score  = 0; //resets the score
00107         game_over  = 0;    //reset the game over so players can play again
00108     }
00109 
00110 
00111 }
00112 
00113 
00114 
00115 /// time-triggered interrupt
00116 void timer_isr()
00117 {
00118     g_timer_flag  = 1;   // set flag in ISR
00119 }
00120 
00121 /// button event-triggered interrupt
00122 void b1_isr()
00123 {
00124     g_b1_flag  = 1;   // set flag in ISR
00125 }
00126 
00127 /// button evet-triggered interrupt
00128 void b2_isr()
00129 {
00130     g_b2_flag  = 1;   // set flag in ISR
00131 }
00132 
00133 ///calibrates Joystick to a default position
00134 void calibrateJoystick()
00135 {
00136     joystick.x0 = xPot;     //the current value of the x potentiometer is set as the center position
00137     joystick.y0 = yPot;     //the current value of the y potentiometer is set as the center position
00138 }
00139 
00140 ///the joystick updates its position
00141 void updateJoystick()
00142 {
00143     joystick.x = xPot - joystick.x0;    //the center position of x is subtracted from the current position of the x potentiometer
00144     joystick.y = yPot - joystick.y0;    //the center position of y is subtracted from the current position of the y potentiometer
00145 
00146 
00147 ///based on the value calculated the joystick is given an overall direction
00148 
00149     if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00150         joystick.direction = Center;
00151     } else if (joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00152         joystick.direction = Up;
00153     } else if (joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00154         joystick.direction = Down;
00155     } else if (joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00156         joystick.direction = Left;
00157     } else if (joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00158         joystick.direction = Right;
00159     } else if (joystick.x < DIRECTION_TOLERANCE && joystick.y > DIRECTION_TOLERANCE) {
00160         joystick.direction = Upright;
00161     } else if (joystick.x < DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
00162         joystick.direction = Downright;
00163     } else if (joystick.x > DIRECTION_TOLERANCE && joystick.y > DIRECTION_TOLERANCE) {
00164         joystick.direction = Upleft;
00165     } else if (joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
00166         joystick.direction = Downleft;
00167     }
00168 
00169     printFlag  = 1;
00170 }
00171 ///the function to move and create new bullets
00172 void shoots()
00173 {
00174     /* the array shoot[4][10] is used to move and track the bullets
00175     the first coloumn is used to count the bullet numbers, the second coloumn determines the bullets' direction
00176     and the third and fourth determine the bullets' x & y position.
00177     */
00178 
00179     for(int x = 0; x<10; x++) {         //loops through each row in the array
00180 
00181         if(shoot[x][0]!=0) {            //checks to see if there is a bullet present in that array
00182 
00183             switch(shoot[x][1]) {       //selects a case based on the directionthe bullet needs to travel
00184 
00185 
00186                 case 0:                             //shooting up
00187                     if (shoot[x][2] == 0) {         //start positon of bullet
00188                         shoot[x][2] = 24;           //starting x coordinate
00189                     }
00190                     shoot[x][2] = shoot[x][2];      //the bullet's x coordinate will remain constant
00191                     if (shoot[x][3] == 0) {         //start positon of bullet
00192                         shoot[x][3] = 21;           //starting y coordinate
00193                     }
00194                     shoot[x][3] = shoot[x][3]-1;    //the bullet's y coordinate decreases to make it move up
00195                     break;
00196 
00197 
00198                 case 1:                             //shooting Up Right
00199                     if (shoot[x][2] == 0) {         //checks to see if bullet is in start position
00200                         shoot[x][2] = 27;           //starting x coordinate
00201                     }
00202                     shoot[x][2] = shoot[x][2]+1;    //the bullet's x coordinate will increase to move it right
00203                     if (shoot[x][3] == 0) {         //checks to see if in starting position
00204                         shoot[x][3] = 19;           //starting y coordinate
00205                     }
00206                     shoot[x][3] = shoot[x][3]-1;    //the bullet's y coordinate decreases to make it move up
00207                     break;
00208 
00209 
00210                 case 2:                             //Shooting Right
00211                     if (shoot[x][2] == 0) {         //checks to see if in starting position
00212                         shoot[x][2] = 28;           //starting x coordinate
00213                     }
00214                     shoot[x][2] = shoot[x][2]+1;    //the bullet's x coordinate increases to move it right
00215                     if(shoot[x][3] == 0) {          //checks to see if in starting position
00216                         shoot[x][3] = 24;           //starting y coordinate
00217                     }
00218                     shoot[x][3] = shoot[x][3];      //as y coordinate doesn't change this is kept constant
00219                     break;
00220 
00221 
00222                 case 3:                             //Shooting Down Right
00223                     if(shoot[x][2]==0) {            //checks to see if in starting position
00224                         shoot[x][2] = 28;           //starting x coordinate
00225                     }
00226                     shoot[x][2] = shoot [x][2]+1;   //x coordinate increases to move bullet right
00227                     if(shoot [x][3]==0) {           //checks to see if in starting position
00228                         shoot[x][3] = 27;           //starting y coordinate
00229                     }
00230                     shoot[x][3] = shoot[x][3]+1;    //y coordinate increase to move bullet down
00231                     break;
00232 
00233 
00234                 case 4:                             //shoot Down
00235                     if (shoot[x][2]==0) {           //checks to see if in starting position
00236                         shoot[x][2] = 24;           //starting x coordinate
00237                     }
00238                     shoot [x][2] = shoot [x][2];    //down has no x horizontal movement so kept constant
00239                     if(shoot [x][3] == 0) {         //checks to see if in starting position
00240                         shoot[x][3]= 28;            //starting y position
00241                     }
00242                     shoot[x][3] = shoot[x][3]+1;    //y coordinate increases to move bullet down
00243                     break;
00244 
00245 
00246                 case 5:                             //shoot down left
00247                     if (shoot[x][2]==0) {           //checks to see if in starting position
00248                         shoot[x][2] = 20;           //starting x coordinate
00249                     }
00250                     shoot[x][2] = shoot [x][2]-1;   //x coordinate decreases to move bullet left
00251                     if (shoot[x][3]==0) {           //checks to see if in starting position
00252                         shoot [x][3]= 27;           //starting y position
00253                     }
00254                     shoot[x][3]= shoot[x][3]+1;     //y coordinate increases to move bullet down
00255                     break;
00256 
00257 
00258                 case 6:                             //shoot Left
00259                     if (shoot[x][2]==0) {           //checks to see if in starting position
00260                         shoot[x][2] = 21;           //starting x position
00261                     }
00262                     shoot[x][2] = shoot [x][2]-1;   //x coordinate decreases causing bullet to move left
00263                     if (shoot[x][3]==0) {           //checks to see if in starting position
00264                         shoot [x][3]= 24;           //starting y position
00265                     }
00266                     shoot[x][3]= shoot[x][3];       //no vertical movement so y coordinate stays constant
00267                     break;
00268 
00269 
00270                 case 7:                             //shoots Up left
00271                     if (shoot[x][2]==0) {           //checks to see if in starting position
00272                         shoot[x][2] = 22;           //starting x position
00273                     }
00274                     shoot[x][2] = shoot [x][2]-1;   //x coordinates decrease to move bullet left
00275                     if (shoot[x][3]==0) {           //checks to see if in starting position
00276                         shoot [x][3]= 21;           //starting y positon
00277                     }
00278                     shoot[x][3]= shoot[x][3]-1;     //y coodrinates decrease to move bullet up
00279                     break;
00280 
00281             }
00282 
00283         }
00284     }
00285     for (int x=0; x<10; x++) {                          //loops through each row
00286         if(shoot[x][1]==0 || shoot[x][1]==4) {          //checks if the ship is facing up or down
00287             for(int i=0; i<1; i++) {
00288                 for(int j=0; j<2; j++) {
00289                     if(shootVertical[i][j]==1) {
00290                         lcd.setPixel(i+shoot[x][2],j+shoot[x][3]);  //loops through and sets pixels to the assigned location
00291                     }
00292                 }
00293             }
00294 
00295         }
00296         if(shoot[x][1]==2 || shoot[x][1]==6) {      //checks to see if ship is facing right or left
00297             for(int i=0; i<2; i++) {
00298                 for(int j=0; j<1; j++) {
00299                     if(shootHorizontal[i][j]==1) {
00300                         lcd.setPixel(i+shoot[x][2],j+shoot[x][3]);     //sets the horizontal bullet array
00301                     }
00302                 }
00303             }
00304         }
00305         if(shoot[x][1]==1 || shoot[x][1]==5) {              //checks if diagonal1 is the array to use
00306             for(int i=0; i<2; i++) {
00307                 for(int j=0; j<2; j++) {
00308                     if(shootDiaganol1[i][j]==1) {
00309                         lcd.setPixel(i+shoot[x][2],j+shoot[x][3]);      //sets the diagonal1 array
00310                     }
00311                 }
00312             }
00313 
00314         }
00315 
00316         if(shoot[x][1]==3 || shoot[x][1]==7) {              //diagonal2 is called for these directions
00317             for(int i=0; i<3; i++) {
00318                 for(int j=0; j<3; j++) {
00319                     if(shootDiaganol2[i][j]==1) {
00320                         lcd.setPixel(i+shoot[x][2],j+shoot[x][3]);      //diagonal2 is set
00321 
00322                     }
00323                 }
00324             }
00325         }
00326     }
00327 }
00328 
00329 ///a function to move the asteroids and check for collisions
00330 void asteroidMove()
00331 {
00332 
00333     /* the array asteroid[5][8] is used to track and move asteroids the first coloumn is asteroid number
00334        the second coloumn determines asteroid type, the third coloumn is asteroid direction
00335        then fourth and fifth determine x and y coordinates
00336        */
00337 
00338     srand(timer.read_us()); //a random number is generated using the mbed's internal timer as a seed
00339     int r = rand()%8;       //the variable r is a random number between 0 and 7 which determines where the asteroid comes from
00340     int number = rand()%4; //the variable number is a random number from 0 to 3 which determines the type of asteroid
00341 
00342 
00343     for (int x = 0; x<8; x++) {    //loops through each row in the array
00344 
00345         if(asteroid[x][0] !=0) {        //checks to see if the asteroid has not previously been assigned a number
00346             if(asteroid[x][2]==0) {     //if the astroid has no assigned type it is assigned here
00347                 if(r==0) {
00348                     asteroid[x][2]=1;   //a random number 0 assigns the direction value to be 1
00349                 }
00350                 if(r==1) {
00351                     asteroid[x][2]=2;   //a random number 1 assigns the direction value to be 2
00352                 }
00353                 if(r==2) {
00354                     asteroid[x][2]=3;   //a random number 2 assigns the direction value to be 3
00355                 }
00356                 if(r==3) {
00357                     asteroid[x][2]=4;   //a random number 3 assigns the direction value to be 4
00358                 }
00359                 if(r==4) {
00360                     asteroid[x][2]=5;   //a random number 4 assigns the direction value to be 5
00361                 }
00362                 if(r==5) {
00363                     asteroid[x][2]=6;   //a random number 5 assigns the direction value to be 6
00364                 }
00365                 if(r==6) {
00366                     asteroid[x][2]=7;   //a random number 6 assigns the direction value to be 7
00367                 }
00368                 if(r==7) {
00369                     asteroid[x][2]=8;   //a random number 7 assigns the direction value to be 8
00370                 }
00371 
00372 
00373                 ///the random variable number is used to assign the type of asteroid
00374                 if(number == 0) {
00375                     asteroid[x][1]=1;   //0 gives asteroid type 1
00376                 }
00377                 if(number == 1) {
00378                     asteroid[x][1]=2;   //1 gives asteroid type 2
00379                 }
00380                 if(number == 2) {
00381                     asteroid[x][1]=3;   //2 gives asteroid type 3
00382                 }
00383                 if(number == 3) {
00384                     asteroid[x][1]=4;   //3 gives asteroid type 4
00385                 }
00386             }
00387 
00388 
00389             switch(asteroid[x][2]) {           //the case is selected based on which direction the asteroid comes from determined by r
00390 
00391 
00392                 case 1:                 //asteroid from top left
00393 
00394                     int flag = 0;
00395 
00396 
00397                     if (flag == 1) {
00398                         flag=0;         //the flag is cleared and bypasses the setting of the array
00399                     } else if((asteroid[x][3]<=24)and (asteroid[x][3]+9>= 24) and (asteroid[x][4]<=24) and (asteroid[x][4]+9>=24)) {    //checks to see if there is a collision with the ship
00400                         game_over  = 1;  //game over is increased to 1 to end the game
00401                     }
00402 
00403                     else {
00404                         //checks if asteroids x coordinates have been set if not sets them
00405                         if(asteroid[x][3]==0) {
00406                             asteroid[x][3]=1;
00407                         }
00408                         asteroid[x][3]=asteroid[x][3]+1;    //asteroids x coordinate increase to move right
00409 
00410                         //checks if asteroids y coordinates have been set if not sets them
00411                         if(asteroid[x][4]==0) {
00412                             asteroid[x][4]=1;
00413                         }
00414                         asteroid[x][4]=asteroid[x][4]+1;   //asteroids y coordinates increase to move down
00415                         for (int z=0; z<8; z++) {
00416                             if((asteroid[x][3]<=shoot[z][2])and ((asteroid[x][3]+9)>= shoot[z][2]) and (asteroid[x][4]<=shoot[z][3]) and ((asteroid[x][4]+9)>= shoot[z][3])) { // checks to see if an asteroid collides with a bullet
00417                                 asteroid[x][0]=0;       //the asteroid number is erased
00418                                 asteroid[x][1]=0;       //the asteroid type is erased
00419                                 asteroid[x][2]=0;       //the asteroid direction is erased
00420                                 asteroid[x][3]=0;       //the asteroids x position is erased
00421                                 asteroid[x][4]=0;       //the asteroids y positionis erased
00422                                 shoot[z][0]=0;          //the bullets number is erased
00423                                 shoot[z][1]=0;          //the bullets direction is erased
00424                                 shoot[z][2]=0;          //the bullets x coordinate is erased
00425                                 shoot[z][3]=0;          // the bulets y coordinate is erased
00426                                 flag = 1;       //the flag is raised
00427                             }
00428                         }
00429                         break;
00430                     }
00431 
00432                 case 2:                 //asteroid from top
00433 
00434 
00435                     if (flag == 1) {
00436                         flag=0;                 //the flag is cleared and the setting of asteroids is bypassed
00437                     } else if((asteroid[x][3]<=24)and (asteroid[x][3]+9>= 24) and (asteroid[x][4]<=24) and (asteroid[x][4]+9>=24)) {        //checks to see if the asteroid collides with the ship
00438                         game_over  = 1;      //if the ship is hit the game over is increased to break the while loop
00439                     } else {
00440                         //checks if asteroids x coordinates have been set if not sets them
00441                         if(asteroid[x][3]==0) {
00442                             asteroid[x][3]=20;
00443                         }
00444                         asteroid[x][3]=asteroid[x][3];      //moving vertically so x coordinates don't change
00445 
00446                         //checks if asteroids y coordinates have been set if not sets them
00447                         if(asteroid[x][4]==0) {
00448                             asteroid[x][4]=1;
00449                         }
00450                         asteroid[x][4]=asteroid[x][4]+1;    //moving down so y coordinates need to increase
00451                         for (int z=0; z<8; z++) {
00452                             if((asteroid[x][3]<=shoot[z][2])and ((asteroid[x][3]+9)>= shoot[z][2]) and (asteroid[x][4]<=shoot[z][3]) and ((asteroid[x][4]+9)>= shoot[z][3])) { // checks to see if an asteroid collides with a bullet
00453                                 asteroid[x][0]=0;       //the asteroid number is erased
00454                                 asteroid[x][1]=0;       //the asteroid type is erased
00455                                 asteroid[x][2]=0;       //the asteroid direction is erased
00456                                 asteroid[x][3]=0;       //the asteroids x position is erased
00457                                 asteroid[x][4]=0;       //the asteroids y positionis erased
00458                                 shoot[z][0]=0;          //the bullets number is erased
00459                                 shoot[z][1]=0;          //the bullets direction is erased
00460                                 shoot[z][2]=0;          //the bullets x coordinate is erased
00461                                 shoot[z][3]=0;          // the bulets y coordinate is erased
00462                                 flag = 1;       //the flag is raised
00463                             }
00464                         }
00465                         break;
00466                     }
00467 
00468                 case 3:             //asteroid from top right
00469 
00470 
00471                     if (flag == 1) {
00472                         flag=0;             //the flag is cleared and the setting of asteroids is bypassed
00473                     } else if((asteroid[x][3]<=24)and (asteroid[x][3]+9>= 24) and (asteroid[x][4]<=24) and (asteroid[x][4]+9>=24)) { //checks for collisions with the ship
00474                         game_over =1;        //game over is increased to one to break the while loop
00475                     } else {
00476                         //checks if asteroids x coordinates have been set if not sets them
00477                         if(asteroid[x][3]==0) {
00478                             asteroid[x][3]=48;
00479                         }
00480                         asteroid[x][3]=asteroid[x][3]-1;        //asteroid is moving left so the x coordinate decreases
00481 
00482                         //checks if asteroids y coordinates have been set if not sets them
00483                         if(asteroid[x][4]==0) {
00484                             asteroid[x][4]=1;
00485                         }
00486                         asteroid[x][4]=asteroid[x][4]+1;        //asteroid is moving from top so the y coordinate increases
00487                         for (int z=0; z<8; z++) {
00488                             if((asteroid[x][3]<=shoot[z][2])and ((asteroid[x][3]+9)>= shoot[z][2]) and (asteroid[x][4]<=shoot[z][3]) and ((asteroid[x][4]+9)>= shoot[z][3])) { // checks to see if an asteroid collides with a bullet
00489                                 asteroid[x][0]=0;       //the asteroid number is erased
00490                                 asteroid[x][1]=0;       //the asteroid type is erased
00491                                 asteroid[x][2]=0;       //the asteroid direction is erased
00492                                 asteroid[x][3]=0;       //the asteroids x position is erased
00493                                 asteroid[x][4]=0;       //the asteroids y positionis erased
00494                                 shoot[z][0]=0;          //the bullets number is erased
00495                                 shoot[z][1]=0;          //the bullets direction is erased
00496                                 shoot[z][2]=0;          //the bullets x coordinate is erased
00497                                 shoot[z][3]=0;          // the bulets y coordinate is erased
00498                                 flag = 1;       //the flag is raised
00499                             }
00500                         }
00501                         break;
00502                     }
00503 
00504                 case 4:         //asteroid from right
00505 
00506 
00507                     if (flag == 1) {
00508                         flag=0;             //the flag is cleared and astroids being set is bypassed
00509                     } else if((asteroid[x][3]<=24)and (asteroid[x][3]+9>= 24) and (asteroid[x][4]<=24) and (asteroid[x][4]+9>= 24)) {        //checks if the ship has been hit
00510                         game_over  = 1;       //game_over is increased to 1 to break the while loop
00511                     } else {
00512                         //checks if asteroids x coordinates have been set if not sets them
00513                         if(asteroid[x][3]==0) {
00514                             asteroid[x][3]=48;
00515                         }
00516                         asteroid[x][3]=asteroid[x][3]-1;        //the x coordinate decreases to move the ship left
00517 
00518                         //checks if asteroids y coordinates have been set if not sets them
00519                         if(asteroid[x][4]==0) {
00520                             asteroid[x][4]=20;
00521                         }
00522                         asteroid[x][4]=asteroid[x][4];          // no vertical movement so stays constant
00523                         for (int z=0; z<8; z++) {
00524                             if((asteroid[x][3]<=shoot[z][2])and ((asteroid[x][3]+9)>= shoot[z][2]) and (asteroid[x][4]<=shoot[z][3]) and ((asteroid[x][4]+9)>= shoot[z][3])) { // checks to see if an asteroid collides with a bullet
00525                                 asteroid[x][0]=0;       //the asteroid number is erased
00526                                 asteroid[x][1]=0;       //the asteroid type is erased
00527                                 asteroid[x][2]=0;       //the asteroid direction is erased
00528                                 asteroid[x][3]=0;       //the asteroids x position is erased
00529                                 asteroid[x][4]=0;       //the asteroids y positionis erased
00530                                 shoot[z][0]=0;          //the bullets number is erased
00531                                 shoot[z][1]=0;          //the bullets direction is erased
00532                                 shoot[z][2]=0;          //the bullets x coordinate is erased
00533                                 shoot[z][3]=0;          // the bulets y coordinate is erased
00534                                 flag = 1;       //the flag is raised
00535                             }
00536                         }
00537                         break;
00538                     }
00539 
00540                 case 5:     //asteroid from down right
00541 
00542 
00543                     if (flag == 1) {
00544                         flag=0;                     //flag is cleared and setting of asteroids is bypassed
00545                     } else if((asteroid[x][3]<=24)and (asteroid[x][3]+9>= 24) and (asteroid[x][4]<=24) and (asteroid[x][4]+9>=24)) {      //checks if the asteroid has collided with the ship
00546                         game_over  = 1;      //game_over is increased breaking the while loop and causing the game to end
00547                     } else {
00548                         //checks if asteroids x coordinates have been set if not sets them
00549                         if(asteroid[x][3]==0) {
00550                             asteroid[x][3]=48;
00551                         }
00552                         asteroid[x][3]=asteroid[x][3]-1;        //x coordinates decrease to move ship left
00553 
00554                         //checks if asteroids y coordinates have been set if not sets them
00555                         if(asteroid[x][4]==0) {
00556                             asteroid[x][4]=48;
00557                         }
00558                         asteroid[x][4]=asteroid[x][4]-1;        //y coordinates decrease to move ship up
00559                         for (int z=0; z<8; z++) {
00560                             if((asteroid[x][3]<=shoot[z][2])and ((asteroid[x][3]+9)>= shoot[z][2]) and (asteroid[x][4]<=shoot[z][3]) and ((asteroid[x][4]+9)>= shoot[z][3])) { // checks to see if an asteroid collides with a bullet
00561                                 asteroid[x][0]=0;       //the asteroid number is erased
00562                                 asteroid[x][1]=0;       //the asteroid type is erased
00563                                 asteroid[x][2]=0;       //the asteroid direction is erased
00564                                 asteroid[x][3]=0;       //the asteroids x position is erased
00565                                 asteroid[x][4]=0;       //the asteroids y positionis erased
00566                                 shoot[z][0]=0;          //the bullets number is erased
00567                                 shoot[z][1]=0;          //the bullets direction is erased
00568                                 shoot[z][2]=0;          //the bullets x coordinate is erased
00569                                 shoot[z][3]=0;          // the bulets y coordinate is erased
00570                                 flag = 1;       //the flag is raised
00571                             }
00572                         }
00573                         break;
00574                     }
00575 
00576                 case 6:     //asteroid from bottom
00577 
00578 
00579                     if (flag == 1) {
00580                         flag=0;             //the flag is cleared and the setting of the asteroids is bypassed
00581                     } else if((asteroid[x][3]<=24)and (asteroid[x][3]+9>= 24) and (asteroid[x][4]<=24) and (asteroid[x][4]+9>=24)) {        //checks to see if the ship has been hit
00582                         game_over  = 1;  //game over is increased to 1 which ends the game
00583                     } else {
00584                         //checks if asteroids x coordinates have been set if not sets them
00585                         if(asteroid[x][3]==0) {
00586                             asteroid[x][3]=20;
00587                         }
00588                         asteroid[x][3]=asteroid[x][3]; //as no horizontal movement the x coordinates are constant
00589 
00590                         //checks if asteroids y coordinates have been set if not sets them
00591                         if(asteroid[x][4]==0) {
00592                             asteroid[x][4]=48;
00593                         }
00594                         asteroid[x][4]=asteroid[x][4]-1;        //the y coordinate decreases to cause the asteroid to move up
00595                         for (int z=0; z<8; z++) {
00596                             if((asteroid[x][3]<=shoot[z][2])and ((asteroid[x][3]+9)>= shoot[z][2]) and (asteroid[x][4]<=shoot[z][3]) and ((asteroid[x][4]+9)>= shoot[z][3])) { // checks to see if an asteroid collides with a bullet
00597                                 asteroid[x][0]=0;       //the asteroid number is erased
00598                                 asteroid[x][1]=0;       //the asteroid type is erased
00599                                 asteroid[x][2]=0;       //the asteroid direction is erased
00600                                 asteroid[x][3]=0;       //the asteroids x position is erased
00601                                 asteroid[x][4]=0;       //the asteroids y positionis erased
00602                                 shoot[z][0]=0;          //the bullets number is erased
00603                                 shoot[z][1]=0;          //the bullets direction is erased
00604                                 shoot[z][2]=0;          //the bullets x coordinate is erased
00605                                 shoot[z][3]=0;          // the bulets y coordinate is erased
00606                                 flag = 1;       //the flag is raised
00607                             }
00608                         }
00609                         break;
00610                     }
00611 
00612                 case 7:                 //asteroid from bottom left
00613 
00614                     if (flag == 1) {
00615                         flag=0;                 //clears the flag and bypasses the setting of asteroids
00616                     } else if((asteroid[x][3]<=24)and (asteroid[x][3]+9>= 24) and (asteroid[x][4]<=24) and (asteroid[x][4]+9>=24)) {      //checks to see if there is a collision with the ship
00617                         game_over =1;          //raises game over to 1 to break the while loop
00618                     } else {
00619                         //checks if asteroids x coordinates have been set if not sets them
00620                         if(asteroid[x][3]==0) {
00621                             asteroid[x][3]=1;
00622                         }
00623                         asteroid[x][3]=asteroid[x][3]+1;        //x coordinates increase to move asteroid right
00624 
00625                         //checks if asteroids y coordinates have been set if not sets them
00626                         if(asteroid[x][4]==0) {
00627                             asteroid[x][4]=48;
00628                         }
00629                         asteroid[x][4]=asteroid[x][4]-1;        //y coordinates decrease to move asteroid up
00630                         for (int z=0; z<8; z++) {
00631                             if((asteroid[x][3]<=shoot[z][2])and ((asteroid[x][3]+9)>= shoot[z][2]) and (asteroid[x][4]<=shoot[z][3]) and ((asteroid[x][4]+9)>= shoot[z][3])) { // checks to see if an asteroid collides with a bullet
00632                                 asteroid[x][0]=0;       //the asteroid number is erased
00633                                 asteroid[x][1]=0;       //the asteroid type is erased
00634                                 asteroid[x][2]=0;       //the asteroid direction is erased
00635                                 asteroid[x][3]=0;       //the asteroids x position is erased
00636                                 asteroid[x][4]=0;       //the asteroids y positionis erased
00637                                 shoot[z][0]=0;          //the bullets number is erased
00638                                 shoot[z][1]=0;          //the bullets direction is erased
00639                                 shoot[z][2]=0;          //the bullets x coordinate is erased
00640                                 shoot[z][3]=0;          // the bulets y coordinate is erased
00641                                 flag = 1;       //the flag is raised
00642                             }
00643                         }
00644                         break;
00645                     }
00646 
00647                 case 8:         //asteroid from left
00648 
00649                     if (flag == 1) {
00650                         flag=0;             //clears the flag and bypasses the asteroid placement
00651                     } else if((asteroid[x][3]<=24)and (asteroid[x][3]+9>= 24) and (asteroid[x][4]<=24) and (asteroid[x][4]+9>=24)) {   //checks to see if the ship has been hit
00652                         game_over  = 1;      //game over is increased to 1 to break the while loop
00653                     } else {
00654                         //checks if asteroids x coordinates have been set if not sets them
00655                         if(asteroid[x][3]==0) {
00656                             asteroid[x][3]=1;
00657                         }
00658                         asteroid[x][3]=asteroid[x][3]+1;        //x coordinates increase to move ship right
00659 
00660                         //checks if asteroids y coordinates have been set if not sets them
00661                         if(asteroid[x][4]==0) {
00662                             asteroid[x][4]=20;
00663                         }
00664                         asteroid[x][4]=asteroid[x][4];      //y coordinates stay constant as there is no vertical movement
00665                         for (int z=0; z<8; z++) {
00666                             if((asteroid[x][3]<=shoot[z][2])and ((asteroid[x][3]+9)>= shoot[z][2]) and (asteroid[x][4]<=shoot[z][3]) and ((asteroid[x][4]+9)>= shoot[z][3])) { // checks to see if an asteroid collides with a bullet
00667                                 asteroid[x][0]=0;       //the asteroid number is erased
00668                                 asteroid[x][1]=0;       //the asteroid type is erased
00669                                 asteroid[x][2]=0;       //the asteroid direction is erased
00670                                 asteroid[x][3]=0;       //the asteroids x position is erased
00671                                 asteroid[x][4]=0;       //the asteroids y positionis erased
00672                                 shoot[z][0]=0;          //the bullets number is erased
00673                                 shoot[z][1]=0;          //the bullets direction is erased
00674                                 shoot[z][2]=0;          //the bullets x coordinate is erased
00675                                 shoot[z][3]=0;          // the bulets y coordinate is erased
00676                                 flag = 1;       //the flag is raised
00677                             }
00678                         }
00679                         break;
00680                     }
00681             }
00682 
00683             //for asteroid type 1 the code loops through the array for asteroid1 and sets the pixels
00684             if (asteroid[x][1] == 1) {
00685                 for (int i = 0; i<9; i++) {
00686                     for (int j=0; j<9; j++) {
00687                         if (asteroid1[i][j]==1) {
00688                             lcd.setPixel(i+asteroid[x][3],j+asteroid[x][4]);
00689                         }
00690                     }
00691                 }
00692             }
00693             //for asteroid type 2 the code loops through the array for asteroid2 and sets the pixels
00694             if(asteroid[x][1] == 2) {
00695                 for (int i = 0; i<8; i++) {
00696                     for (int j=0; j<9; j++) {
00697                         if (asteroid2[i][j]==1) {
00698                             lcd.setPixel(i+ asteroid[x][3],j+asteroid[x][4]);
00699                         }
00700                     }
00701                 }
00702 
00703             }
00704             //for asteroid type 3 the code loops through the array for asteroid3 and sets the pixels
00705             if(asteroid[x][1] == 3) {
00706                 for (int i = 0; i<9; i++) {
00707                     for (int j=0; j<9; j++) {
00708                         if (asteroid3[i][j]==1) {
00709                             lcd.setPixel(i+ asteroid[x][3],j+asteroid[x][4]);
00710                         }
00711                     }
00712                 }
00713 
00714             }
00715             //for asteroid type 4 the code loops through the array for asteroid4 and sets the pixels
00716             if(asteroid[x][1] == 4) {
00717                 for (int i = 0; i<9; i++) {
00718                     for (int j=0; j<9; j++) {
00719                         if (asteroid4[i][j]==1) {
00720                             lcd.setPixel(i+ asteroid[x][3],j+asteroid[x][4]);
00721                         }
00722                     }
00723                 }
00724             }
00725         }
00726 
00727     }
00728 
00729 }
00730 void game()
00731 {
00732 
00733 
00734 
00735     if (printFlag  == 1) {
00736         timer.start();
00737         printFlag  = 0;      //resets printFlag
00738 
00739         //the joystick direction determines the direction of the FSM
00740         if(joystick.direction == Up) {
00741             direction  = UP;
00742         } else if(joystick.direction == Right) {
00743             direction  = RIGHT;
00744         } else if(joystick.direction == Down) {
00745             direction  = DOWN;
00746         } else if(joystick.direction == Left) {
00747             direction  = LEFT;
00748         } else if(joystick.direction == Upright) {
00749             direction  = UPRIGHT;
00750         } else if(joystick.direction == Downright) {
00751             direction  = DOWNRIGHT;
00752         } else if(joystick.direction == Downleft) {
00753             direction  = DOWNLEFT;
00754         } else if(joystick.direction == Upleft) {
00755             direction  = UPLEFT;
00756         }
00757     }
00758 
00759 //loops through coloumn 48 to draw a line to make the playing area square
00760     for (int j = 0;j < WIDTH;j++) {
00761         lcd.setPixel(48,j);
00762     }
00763     lcd.drawRect(48,0,35,47,1); 
00764     // set shipDirection of current state (use dot syntax to access struct members)
00765     shipDirection  = fsm [state ].shipDirection;
00766     // set the next state depending on direction
00767     state  = fsm [state ].next_state[direction ];
00768 
00769     //from the FSM the next direction is chosen and the code loops through the appropriate array to face the ship in that direction
00770     if (shipDirection  == 6) {
00771         for (int i = 0; i<9; i++) {
00772             for (int j=0; j<9; j++) {
00773                 if (shipUp[i][j]==1) {
00774                     lcd.setPixel(i+20,j+20);
00775                 }
00776             }
00777         }
00778     }
00779     //from the FSM the next direction is chosen and the code loops through the appropriate array to face the ship in that direction
00780     else if (shipDirection  == 5) {
00781         for (int i = 0; i<9; i++) {
00782             for (int j=0; j<9; j++) {
00783                 if (shipUR[i][j]==1) {
00784                     lcd.setPixel(i+20,j+20);
00785                 }
00786             }
00787         }
00788     }
00789     ///from the FSM the next direction is chosen and the code loops through the appropriate array to face the ship in that direction
00790     else if (shipDirection  == 4) {
00791         for (int i = 0; i<9; i++) {
00792             for (int j=0; j<9; j++) {
00793                 if (shipRight[i][j]==1) {
00794                     lcd.setPixel(i+20,j+20);
00795                 }
00796             }
00797         }
00798 
00799     }
00800     //from the FSM the next direction is chosen and the code loops through the appropriate array to face the ship in that direction
00801     else if (shipDirection  == 3) {
00802         for (int i = 0; i<9; i++) {
00803             for (int j=0; j<9; j++) {
00804                 if (shipDR[i][j]==1) {
00805                     lcd.setPixel(i+20,j+20);
00806                 }
00807             }
00808 
00809         }
00810     }
00811     //from the FSM the next direction is chosen and the code loops through the appropriate array to face the ship in that direction
00812     else if (shipDirection  == 2) {
00813         for (int i = 0; i<9; i++) {
00814             for (int j=0; j<9; j++) {
00815                 if (shipDown[i][j]==1) {
00816                     lcd.setPixel(i+20,j+20);
00817                 }
00818             }
00819         }
00820 
00821     }
00822     //from the FSM the next direction is chosen and the code loops through the appropriate array to face the ship in that direction
00823     else if (shipDirection  == 1) {
00824         for (int i = 0; i<9; i++) {
00825             for (int j=0; j<9; j++) {
00826                 if (shipDL[i][j]==1) {
00827                     lcd.setPixel(i+20,j+20);
00828                 }
00829             }
00830         }
00831 
00832     }
00833     //from the FSM the next direction is chosen and the code loops through the appropriate array to face the ship in that direction
00834     else if (shipDirection  == 0) {
00835         for (int i = 0; i<9; i++) {
00836             for (int j=0; j<9; j++) {
00837                 if (shipLeft[i][j]==1) {
00838                     lcd.setPixel(i+20,j+20);
00839                 }
00840             }
00841         }
00842 
00843     }
00844     //from the FSM the next direction is chosen and the code loops through the appropriate array to face the ship in that direction
00845     else {
00846         for (int i = 0; i<9; i++) {
00847             for (int j=0; j<9; j++) {
00848                 if (shipUL[i][j]==1) {
00849                     lcd.setPixel(i+20,j+20);
00850                 }
00851             }
00852         }
00853 
00854     }
00855 
00856     srand(timer.read_us());         //using the timer on the mbed a random number is generated
00857 
00858     int random = rand()%30;        //a random number from 1 to 30 is generated
00859     if(random ==1) {                //there is a 10% chance of an asteroid every iteration of code
00860         for(int x = 0; x<8; x++) {      //checks to see what number asteroid to place
00861             if (asteroid[x][0]==0) {
00862                 asteroid[x][0]= x+1;     //if there is not an asteroid in the selected row then one will be placed there
00863                 break;
00864             }
00865         }
00866     }
00867 
00868     asteroidMove(); //the asteroid move function is called
00869 
00870     if(g_b1_flag ) {             //checks to see if the interrupt has been pressed
00871         g_b1_flag  = 0;          //after the interupt the flag is reset
00872         for (int x = 0; x<10; x++) {
00873             if (shoot[x][0] == 0) {     //checks to see if there is a bullet in the row of the shoot array
00874                 bullet_number ++;        //the bullet number is increased by 1
00875                 shoot[x][0]=bullet_number ;  //the bullet number is placed in the shoot array
00876                 shoot[x][1]=shipDirection ;  //the direction the ship is facing is recorded in the shoot array
00877                 break;
00878             }
00879         }
00880     }
00881 
00882     shoots();           //calls the shoots function
00883     checkbullet();      //calls the check bullet function
00884     timer.stop();       //makes the timer stop
00885 
00886 }
00887 
00888 
00889 
00890 //a function to check if the bullet is still on screen
00891 void checkbullet()
00892 {
00893     for (int x = 0; x<10; x++) {            //loops through each row of the shoot array
00894         if ((shoot[x][2] == 0)or(shoot[x][3] == 0)or(shoot[x][2] == 48) or(shoot[x][3] == 48)) {            //checks the 4 orders
00895             shoot[x][0]=0;  //bullet_number is erased
00896             shoot[x][1]=0;  //shipDirection is erased
00897             shoot[x][2]=0;  //bullet x coordinate is erased
00898             shoot[x][3]=0;  //bullet y coordinate is erased
00899 
00900         }
00901     }
00902 }
00903 //void checkAsteroid()
00904 //{
00905 //    for (int x = 0; x<8; x++) {            //loops through each row of the shoot array
00906 //        if ((asteroid[x][3] == 0)or(asteroid[x][4] == 0)or(asteroid[x][3] == 48) or(asteroid[x][4] == 48)) {            //checks the 4 orders
00907 //            asteroid[x][0]=0;  //bullet_number is erased
00908 //            asteroid[x][1]=0;  //shipDirection is erased
00909 //            asteroid[x][2]=0;  //bullet x coordinate is erased
00910 //            asteroid[x][3]=0;  //bullet y coordinate is erased
00911 //            asteroid[x][4]=0;
00912 //        }
00913 //    }
00914 //}