My 2645 project Chenyu Li 200985958

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 
00004 int main()
00005 {
00006 
00007     // first need to initialise display
00008     lcd.init();/// turn on the lcd screen.
00009     button.rise(&button_isr);/// return the value of button_isr to here.
00010     button.mode(PullDown);/// set the mode of the button to make the user can press the botton.
00011     /* while(1){
00012          lcd.clear();
00013      if(g_button_flag) {
00014         // g_button_flag = 0;
00015          lcd.printString("button is on",0,0);
00016      }else {
00017          lcd.printString("button is off",0,0);
00018      }
00019      lcd.refresh();
00020      wait(0.1);
00021      }*/
00022     buttonjoystick.mode(PullDown);/// set the mode of the button of joystick to make the user can press the botton.
00023     calibrateJoystick();  // get centred values of joystick
00024     pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
00025 
00026 
00027 
00028     // these are default settings so not strictly needed
00029     lcd.normalMode();      // normal colour mode
00030     lcd.setBrightness(1.0); // put LED backlight on 50%
00031     
00032     // welcome screen
00033     lcd.printString("Welcome To",15,1);
00034     lcd.printString("Starcraft III!",4,3);
00035     lcd.refresh();
00036     wait(5.0);
00037     lcd.clear();
00038     
00039     lcd.printString("How to Play",10,0);
00040     
00041     // draw Enemy.
00042     lcd.printString("Enemy",30,2);
00043     // draw the arrow.
00044     lcd.drawLine(15,19,22,19,1);
00045     lcd.drawLine(22,19,20,17,1);
00046     lcd.drawLine(22,19,20,21,1);
00047     
00048     lcd.drawLine(0,10,83,10,1);
00049     
00050     lcd.drawLine(2,18,3,17,1);
00051     lcd.drawLine(3,17,7,17,1);
00052     lcd.drawLine(7,17,8,18,1);
00053     lcd.drawLine(5,17,5,21,1);
00054     //You
00055     lcd.printString("You",30,4);
00056     lcd.drawLine(15,35,22,35,1);
00057     lcd.drawLine(22,35,20,33,1);
00058     lcd.drawLine(22,35,20,37,1);
00059     
00060     lcd.drawLine(6,32,6,39,1);
00061     lcd.drawLine(6-4,34,6+4,34,1);
00062     lcd.drawLine(6-1,35,6+1,35,1);
00063     lcd.drawLine(6-1,39,6+1,39,1);
00064     lcd.refresh();
00065     
00066     //to make the screen stick in this while loop when the user press the botton then goes to next page.
00067     while(1) {
00068         if (button) {
00069             //buzzer.period_ms(2.9);
00070             //buzzer.pulsewidth_ms(1.9);
00071             wait(0.5);
00072             lcd.clear();
00073             lcd.refresh();
00074             break;
00075         }
00076     }
00077     
00078     // the screen to tell user how to control the Movement
00079     lcd.printString("How to Play",10,0);
00080     lcd.drawLine(0,10,83,10,1);
00081     lcd.drawLine(41,15,41,22,1);
00082     lcd.drawLine(41-4,17,41+4,17,1);
00083     lcd.drawLine(41-1,18,41+1,18,1);
00084     lcd.drawLine(41-1,22,41+1,22,1);
00085     
00086     lcd.drawLine(48,19,55,19,1);
00087     lcd.drawLine(55,19,53,17,1);
00088     lcd.drawLine(55,19,53,21,1);
00089     
00090     lcd.drawLine(27,19,34,19,1);
00091     lcd.drawLine(27,19,29,17,1);
00092     lcd.drawLine(27,19,29,21,1);
00093 
00094     lcd.drawCircle(41,33,3,1);
00095 
00096     lcd.drawLine(48,33,55,33,1);
00097     lcd.drawLine(55,33,53,31,1);
00098     lcd.drawLine(55,33,53,35,1);
00099     
00100     lcd.drawLine(27,33,34,33,1);
00101     lcd.drawLine(27,33,29,31,1);
00102     lcd.drawLine(27,33,29,35,1);
00103     lcd.printString("JoyStick",19,5);
00104     lcd.refresh();
00105     
00106     //to make the screen stick in this while loop when the user press the botton then goes to next page.
00107     while(1) {
00108         if (button) {
00109             //buzzer.period_ms(2.9);
00110             //buzzer.pulsewidth_ms(1.9);
00111             wait(0.5);
00112             lcd.clear();
00113             lcd.refresh();
00114             break;
00115         }
00116     }
00117     
00118     // describe how to use the button and the joystick button
00119     lcd.printString("How to Play",10,0);
00120     lcd.drawLine(0,10,83,10,1);
00121     
00122     lcd.printString("Button:",20,2);
00123     lcd.printString("Viking Missile",0,3);
00124     lcd.printString("JoyStick:",18,4);
00125     lcd.printString("Bomb X3",19,5);
00126     //to make the screen stick in this while loop when the user press the botton then goes to next page.
00127     while(1) {
00128         if (button) {
00129             //buzzer.period_ms(2.9);
00130             //buzzer.pulsewidth_ms(1.9);
00131             wait(0.5);
00132             lcd.clear();
00133             lcd.refresh();
00134             break;
00135         }
00136     }
00137     
00138     //prepare for battle screen
00139     lcd.printString("Prepare for",10,0);
00140     lcd.printString("Battle",23,1);
00141     lcd.drawLine(0,20,83,20,1);
00142     lcd.printString("Start!",25,4);
00143     lcd.refresh();
00144     //to make the screen stick in this while loop when the user press the joystick botton then goes to next page.
00145     while(1) {
00146         if (buttonjoystick) {
00147             //buzzer.period_ms(2.9);
00148             //buzzer.pulsewidth_ms(1.9);
00149             wait(0.5);
00150             lcd.clear();
00151             lcd.refresh();
00152             break;
00153         }
00154 
00155     }
00156 //loading screen
00157     lcd.printString("Loading......",6,1);
00158 
00159     for (int x = 0; x < WIDTH ; x+=10) {
00160 
00161         lcd.drawRect(0,36,10+x,10,1);
00162         wait(0.1);
00163         lcd.refresh();
00164     }
00165     lcd.clear();
00166 
00167 
00168     int x = 41 ;/// set 'x' to 41 originally.
00169     // draw the player's unit.
00170     lcd.drawLine(x,40,x,47,1);
00171     lcd.drawLine(x-4,42,x+4,42,1);
00172     lcd.drawLine(x-1,43,x+1,43,1);
00173     lcd.drawLine(x-1,47,x+1,47,1);
00174     lcd.refresh();
00175     //the constant will be used in the code below
00176     int z=41; ///the x axis value of the bullet, start from the middle of the screen 
00177     int q = 0; ///the y axis value of the enemy unit, start from 0.
00178     int p = 0;
00179     int a;
00180     int b;
00181     a = rand() % 76; /// 'a' is a random number from 0-76.
00182     //b = rand() % 76;
00183     int yHit = 39;/// y axis value of the bullet. 
00184     int buttonjoystickpress = 0;/// player can only use the bomb for 3 time, set the value to 0 originally.
00185     int bu = 0;/// the constant which keep the bullet move in a straight line.
00186     int score = 0;/// set the score value start from 0.
00187     int rect = 0;/// y axis value of the left hand side wall.
00188     int rect1 = 0;/// y axis value of the right hand side wall.
00189     int width;/// the width of the left hand side wall.
00190     width = rand() %30;/// set the width of the left hand side wall as a random value between 0-30.
00191     int width2;/// the width of the right hand side wall.
00192     width2 = 83 - rand() %30;// set the width of the right hand side wall as a random value between 53-83.
00193     //bullet.attach(&createbullet,0.1);
00194     //enermy.attach(&createenermy,2.0);
00195 
00196 
00197     while(1) {
00198 
00199 
00200         if (printFlag) {  // if flag set, clear flag and print joystick values to serial port
00201             printFlag = 0;
00202             // check joystick direction
00203             //a-b > 7;
00204             //b-a > 7;
00205             //a + b < 83;
00206             
00207             // make the movement of the left hand side wall
00208             if(!wall) {
00209                 lcd.drawRect(0,rect,width,5,2);
00210                 
00211                 rect++;/// set the wall moves 1 pixels per time.
00212                 
00213                 lcd.drawRect(0,rect,width,5,1);
00214                 lcd.refresh();
00215                 }
00216             // return the 'rect' value back to 0 and set the random number again after the wall go out of the screen.
00217             if(rect > 50) {
00218                 rect = 0;
00219                 width = rand() %30;
00220             }
00221             // make the movement of the right hand side wall
00222             if(!wall) {
00223                 lcd.drawRect(width2,rect1,83-width2,5,2);
00224                 
00225                 rect1++;/// set the wall moves 1 pixels per time.
00226                 
00227                 lcd.drawRect(width2,rect1,83-width2,5,1);
00228                 lcd.refresh();
00229                 }
00230             // return the 'rect1' value back to 0 and set the random number again after the wall go out of the screen.
00231             if(rect1 > 50) {
00232                 rect1 = 0;
00233                 width2 = 83 - rand()%30;
00234             }
00235             
00236             if(!hit) {
00237                 // make the movement of the enemy.
00238                 lcd.drawLine(a,q+1,a+1,q,0);
00239                 lcd.drawLine(a+1,q,a+5,q,0);
00240                 lcd.drawLine(a+5,q,a+6,q+1,0);
00241                 lcd.drawLine(a+3,q,a+3,q+4,0);
00242                 q+=2;/// set the enemy moves 2 pixels per time.
00243 
00244                 lcd.drawLine(a,q+1,a+1,q,1);
00245                 lcd.drawLine(a+1,q,a+5,q,1);
00246                 lcd.drawLine(a+5,q,a+6,q+1,1);
00247                 lcd.drawLine(a+3,q,a+3,q+4,1);
00248 
00249                 lcd.refresh();
00250             }
00251             hit = false;
00252             /*
00253             if(!hit){
00254             lcd.drawLine(a+b,p+1,a+1+b,p,0);
00255             lcd.drawLine(a+1+b,p,a+5+b,p,0);
00256             lcd.drawLine(a+5+b,p,a+6+b,p+1,0);
00257             lcd.drawLine(a+3+b,p,a+3+b,p+4,0);
00258 
00259             p++;
00260             lcd.drawLine(a+b,p+1,a+1+b,p,1);
00261             lcd.drawLine(a+1+b,p,a+5+b,p,1);
00262             lcd.drawLine(a+5+b,p,a+6+b,p+1,1);
00263             lcd.drawLine(a+3+b,p,a+3+b,p+4,1);
00264             lcd.refresh();
00265                 }
00266             hit = false;
00267             */
00268             
00269             // creat a new enemy when it has been hit or go out of the screen.
00270             if(q > 50) {
00271                 q = 0;
00272                 p = 0;
00273                 a = rand () % 76;
00274                 b = rand () % 76;
00275             }
00276 
00277             // movement of the player's unit controlled by the joystick.
00278             if (joystick.direction == UP && x > 4) {
00279 
00280                 score += 1;/// get 1 score after moving the joystick once and move for 1 pixel
00281                 lcd.drawLine(x,40,x,47,0);
00282                 lcd.drawLine(x-4,42,x+4,42,0);
00283                 lcd.drawLine(x-1,43,x+1,43,0);
00284                 lcd.drawLine(x-1,47,x+1,47,0);
00285                 x-=1;/// set the player's unit move 1 pixels per time.
00286 
00287                 lcd.drawLine(x,40,x,47,1);
00288                 lcd.drawLine(x-4,42,x+4,42,1);
00289                 lcd.drawLine(x-1,43,x+1,43,1);
00290                 lcd.drawLine(x-1,47,x+1,47,1);
00291                 lcd.refresh();
00292 
00293             }
00294             
00295             if (joystick.direction == DOWN && x < 78) {
00296                 score += 1;/// get 1 score after moving the joystick once and move for 1 pixel
00297                 lcd.drawLine(x,40,x,47,0);
00298                 lcd.drawLine(x-4,42,x+4,42,0);
00299                 lcd.drawLine(x-1,43,x+1,43,0);
00300                 lcd.drawLine(x-1,47,x+1,47,0);
00301                 x+=1;/// set the player's unit move 1 pixels per time.
00302 
00303                 lcd.drawLine(x,40,x,47,1);
00304                 lcd.drawLine(x-4,42,x+4,42,1);
00305                 lcd.drawLine(x-1,43,x+1,43,1);
00306                 lcd.drawLine(x-1,47,x+1,47,1);
00307                 lcd.refresh();
00308             }
00309             /*
00310             if(z < width && yHit > rect && yHit < rect+5){
00311                 bullethit = true;
00312             }
00313             */
00314             
00315             //bullet movement and the calculation which make the bullet start from wherever the player's unit is and make it move in a stright line.
00316             if(g_button_flag && !bullethit) {
00317                 
00318                 
00319                 //g_button_flag = 0;
00320                 if(joystick.direction == UP && x > 4) {
00321                     bu--;
00322                 }
00323                 if(joystick.direction == DOWN && x < 78) {
00324                     bu++;
00325                 }
00326                 z = x -bu;
00327                 lcd.drawLine(z,yHit,z,yHit-1,0);
00328                 
00329                 yHit-=2;/// set the bullet move 2 pixels per time.
00330 
00331                 lcd.drawLine(z,yHit,z,yHit-1,1);
00332                 
00333                 // detect if the bullet hits the enemy.
00334                 if(z>=a && z <=a+7 && yHit<=q) {
00335                     hit = true;
00336                     // clear the enemy.
00337                     lcd.drawLine(a,q+1,a+1,q,0);
00338                     lcd.drawLine(a+1,q,a+5,q,0);
00339                     lcd.drawLine(a+5,q,a+6,q+1,0);
00340                     lcd.drawLine(a+3,q,a+3,q+4,0);
00341                     // recreate the enemy
00342                     q=0;
00343                     a = rand() % 76;
00344                     score += 100; /// get 100 score after player hit an enermy
00345                 }
00346 
00347                 /*
00348                 if(z == a+1 && q == yHit) {
00349                     hit = true;
00350                     lcd.drawLine(a,q+1,a+1,q,0);
00351                     lcd.drawLine(a+1,q,a+5,q,0);
00352                     lcd.drawLine(a+5,q,a+6,q+1,0);
00353                     lcd.drawLine(a+3,q,a+3,q+4,0);
00354                     q=0;
00355                     a = rand() % 76;
00356                 }
00357                 if(z == a+2 && q == yHit) {
00358                     hit = true;
00359                     lcd.drawLine(a,q+1,a+1,q,0);
00360                     lcd.drawLine(a+1,q,a+5,q,0);
00361                     lcd.drawLine(a+5,q,a+6,q+1,0);
00362                     lcd.drawLine(a+3,q,a+3,q+4,0);
00363                     q=0;
00364                     a = rand() % 76;
00365                 }
00366                 if(z == a+3 && q+4 == yHit) {
00367                     hit = true;
00368                     lcd.drawLine(a,q+1,a+1,q,0);
00369                     lcd.drawLine(a+1,q,a+5,q,0);
00370                     lcd.drawLine(a+5,q,a+6,q+1,0);
00371                     lcd.drawLine(a+3,q,a+3,q+4,0);
00372                     q=0;
00373                     a = rand() % 76;
00374                 }
00375                 if(z == a+5 && q == yHit) {
00376                     hit = true;
00377                     lcd.drawLine(a,q+1,a+1,q,0);
00378                     lcd.drawLine(a+1,q,a+5,q,0);
00379                     lcd.drawLine(a+5,q,a+6,q+1,0);
00380                     lcd.drawLine(a+3,q,a+3,q+4,0);
00381                     q=0;
00382                     a = rand() % 76;
00383                 }
00384                 if(z == a+6 && q+1 == yHit) {
00385                     hit = true;
00386                     lcd.drawLine(a,q+1,a+1,q,0);
00387                     lcd.drawLine(a+1,q,a+5,q,0);
00388                     lcd.drawLine(a+5,q,a+6,q+1,0);
00389                     lcd.drawLine(a+3,q,a+3,q+4,0);
00390                     q=0;
00391                     a = rand() % 76;
00392                 }
00393 */
00394 
00395                 //lcd.drawLine(z,yHit,z,yHit-1,0);
00396                 //yHit--;
00397                 //lcd.drawLine(z,yHit,z,yHit-1,1);
00398                 lcd.refresh();
00399                 wait(0.1);
00400             }
00401             if (yHit < -3) {
00402                 g_button_flag = 0;/// set g_button_flag to 0 
00403                 yHit = 39;/// set 'yHit' back to 39
00404                 bu=0;/// set 'bu' back to 0.
00405                 z = x -bu;/// reset the x axis value of the bullet
00406             }
00407         }
00408         //detect if the enemy hit the left hand side wall, the player won't get any score.
00409         if(width>=a && rect <= q+4 && rect >= q) {
00410             
00411             hit = true;
00412             lcd.drawLine(a,q+1,a+1,q,0);
00413             lcd.drawLine(a+1,q,a+5,q,0);
00414             lcd.drawLine(a+5,q,a+6,q+1,0);
00415             lcd.drawLine(a+3,q,a+3,q+4,0);
00416             q=0;
00417             a = rand() % 76;
00418         }
00419         //detect if the enemy hit the right hand side wall, the player won't get any score.
00420         if(a+6 >= width2 && rect <= q+4 && rect >= q){
00421             hit = true;
00422             lcd.drawLine(a,q+1,a+1,q,0);
00423             lcd.drawLine(a+1,q,a+5,q,0);
00424             lcd.drawLine(a+5,q,a+6,q+1,0);
00425             lcd.drawLine(a+3,q,a+3,q+4,0);
00426             q=0;
00427             a = rand() % 76;
00428             }
00429         //set the player can only use 3 bombs per game using joystick button.
00430         if(buttonjoystick && buttonjoystickpress < 3) {
00431             ++buttonjoystickpress;
00432             hit = true;
00433             lcd.drawLine(a,q+1,a+1,q,0);
00434             lcd.drawLine(a+1,q,a+5,q,0);
00435             lcd.drawLine(a+5,q,a+6,q+1,0);
00436             lcd.drawLine(a+3,q,a+3,q+4,0);
00437             q=0;
00438             a = rand() % 76;
00439             score += 300;// player get 300 score after using the bomb.
00440             
00441             // destroy all enemy and keep the wall and the play's unit on the screen.
00442             for (int r = 5; r < 100; r+=8) {
00443                 lcd.drawCircle(x,45,r,0);
00444                 if(r < 100) {
00445                     lcd.drawCircle(x,45,r-8,2);/// the action of the bomb.
00446                     //draw the player's unit again
00447                     lcd.drawLine(x,40,x,47,1);
00448                     lcd.drawLine(x-4,42,x+4,42,1);
00449                     lcd.drawLine(x-1,43,x+1,43,1);
00450                     lcd.drawLine(x-1,47,x+1,47,1);
00451                     //draw the wall again
00452                     lcd.drawRect(0,rect,width,5,1);
00453                     lcd.drawRect(width2,rect1,83-width2,5,1);
00454                 }
00455                 lcd.refresh();
00456                 wait(0.1);
00457 
00458             }
00459         }
00460         
00461         //detect Game Over if the wall or the enemy hit the player.
00462 
00463         if(a == x-4 && q+1 == 42) {
00464             lcd.clear();/// clear the screen first.
00465             // print out the game over information.
00466             lcd.printString ("Game Over",18,1);
00467             lcd.printString ("Viking",24,2);
00468             lcd.printString ("Destroyed",18,3);
00469             lcd.refresh();
00470             wait(2.0);
00471             // after 2 sec show score on the screen.
00472             lcd.clear();
00473             char buffer[14];/// put the score which have calculated into the array.
00474             int length = sprintf(buffer,"Score: %d",score);
00475             if (length <= 14)
00476             lcd.printString(buffer,0,1);/// print out the score.
00477             lcd.refresh();
00478             break;
00479         }
00480         // the code below also dectect if the player hit the wall or enemy than jump into 'game over' condition.
00481         if(a+1 == x-4 && q ==42) {
00482             lcd.clear();
00483             lcd.printString ("Game Over",18,1);
00484             lcd.printString ("Viking",24,2);
00485             lcd.printString ("Destroyed",18,3);
00486             lcd.refresh();
00487             wait(2.0);
00488             lcd.clear();
00489             char buffer[14];
00490             int length = sprintf(buffer,"Score: %d",score);
00491             if (length <= 14)
00492             lcd.printString(buffer,0,1);
00493             lcd.refresh();
00494             break;
00495         }
00496         if(a+2 == x-4 && q == 42) {
00497             lcd.clear();
00498             lcd.printString ("Game Over",18,1);
00499             lcd.printString ("Viking",24,2);
00500             lcd.printString ("Destroyed",18,3);
00501             lcd.refresh();
00502             wait(2.0);
00503             lcd.clear();
00504             char buffer[14];
00505             int length = sprintf(buffer,"Score: %d",score);
00506             if (length <= 14)
00507             lcd.printString(buffer,0,1);
00508             lcd.refresh();
00509             break;
00510         }
00511         if(a+3 == x-4 && q+4 ==42) {
00512             lcd.clear();
00513             lcd.printString ("Game Over",18,1);
00514             lcd.printString ("Viking",24,2);
00515             lcd.printString ("Destroyed",18,3);
00516             lcd.refresh();
00517             wait(2.0);
00518             lcd.clear();
00519             char buffer[14];
00520             int length = sprintf(buffer,"Score: %d",score);
00521             if (length <= 14)
00522             lcd.printString(buffer,0,1);
00523             lcd.refresh();
00524             break;
00525         }
00526         if(a+4 == x-4 && q ==42) {
00527             lcd.clear();
00528             lcd.printString ("Game Over",18,1);
00529             lcd.printString ("Viking",24,2);
00530             lcd.printString ("Destroyed",18,3);
00531             lcd.refresh();
00532             wait(2.0);
00533             lcd.clear();
00534             char buffer[14];
00535             int length = sprintf(buffer,"Score: %d",score);
00536             if (length <= 14)
00537             lcd.printString(buffer,0,1);
00538             lcd.refresh();
00539             break;
00540         }
00541         if(a+5 == x-4 && q ==42) {
00542             lcd.clear();
00543             lcd.printString ("Game Over",18,1);
00544             lcd.printString ("Viking",24,2);
00545             lcd.printString ("Destroyed",18,3);
00546             lcd.refresh();
00547             wait(2.0);
00548             lcd.clear();
00549             char buffer[14];
00550             int length = sprintf(buffer,"Score: %d",score);
00551             if (length <= 14)
00552             lcd.printString(buffer,0,1);
00553             lcd.refresh();
00554             break;
00555         }
00556         if(a+6 == x-4 && q+1 ==42) {
00557             lcd.clear();
00558             lcd.printString ("Game Over",18,1);
00559             lcd.printString ("Viking",24,2);
00560             lcd.printString ("Destroyed",18,3);
00561             lcd.refresh();
00562             wait(2.0);
00563             lcd.clear();
00564             char buffer[14];
00565             int length = sprintf(buffer,"Score: %d",score);
00566             if (length <= 14)
00567             lcd.printString(buffer,0,1);
00568             lcd.refresh();
00569             break;
00570         }
00571         if(a+3 == x-4 && q == 42) {
00572             lcd.clear();
00573             lcd.printString ("Game Over",18,1);
00574             lcd.printString ("Viking",24,2);
00575             lcd.printString ("Destroyed",18,3);
00576             lcd.refresh();
00577             wait(2.0);
00578             lcd.clear();
00579             char buffer[14];
00580             int length = sprintf(buffer,"Score: %d",score);
00581             if (length <= 14)
00582             lcd.printString(buffer,0,1);
00583             lcd.refresh();
00584             break;
00585         }
00586         if(a+3 == x-3 && q ==42) {
00587             lcd.clear();
00588             lcd.printString ("Game Over",18,1);
00589             lcd.printString ("Viking",24,2);
00590             lcd.printString ("Destroyed",18,3);
00591             lcd.refresh();
00592             wait(2.0);
00593             lcd.clear();
00594             char buffer[14];
00595             int length = sprintf(buffer,"Score: %d",score);
00596             if (length <= 14)
00597             lcd.printString(buffer,0,1);
00598             lcd.refresh();
00599             break;
00600         }
00601         if(a+3 == x-2 && q == 42) {
00602             lcd.clear();
00603             lcd.printString ("Game Over",18,1);
00604             lcd.printString ("Viking",24,2);
00605             lcd.printString ("Destroyed",18,3);
00606             lcd.refresh();
00607             wait(2.0);
00608             lcd.clear();
00609             char buffer[14];
00610             int length = sprintf(buffer,"Score: %d",score);
00611             if (length <= 14)
00612             lcd.printString(buffer,0,1);
00613             lcd.refresh();
00614             break;
00615         }
00616         if(a+3 == x-1 && q ==42) {
00617             lcd.clear();
00618             lcd.printString ("Game Over",18,1);
00619             lcd.printString ("Viking",24,2);
00620             lcd.printString ("Destroyed",18,3);
00621             lcd.refresh();
00622             wait(2.0);
00623             lcd.clear();
00624             char buffer[14];
00625             int length = sprintf(buffer,"Score: %d",score);
00626             if (length <= 14)
00627             lcd.printString(buffer,0,1);
00628             lcd.refresh();
00629             break;
00630         }
00631         if(a+3 == x && q == 40) {
00632             lcd.clear();
00633             lcd.printString ("Game Over",18,1);
00634             lcd.printString ("Viking",24,2);
00635             lcd.printString ("Destroyed",18,3);
00636             lcd.refresh();
00637             wait(2.0);
00638             lcd.clear();
00639             char buffer[14];
00640             int length = sprintf(buffer,"Score: %d",score);
00641             if (length <= 14)
00642             lcd.printString(buffer,0,1);
00643             lcd.refresh();
00644             break;
00645         }
00646         if(a+3 == x+1 && q ==42) {
00647             lcd.clear();
00648             lcd.printString ("Game Over",18,1);
00649             lcd.printString ("Viking",24,2);
00650             lcd.printString ("Destroyed",18,3);
00651             lcd.refresh();
00652             wait(2.0);
00653             lcd.clear();
00654             char buffer[14];
00655             int length = sprintf(buffer,"Score: %d",score);
00656             if (length <= 14)
00657             lcd.printString(buffer,0,1);
00658             lcd.refresh();
00659             break;
00660         }
00661         if(a+3 == x+2 && q == 42) {
00662             lcd.clear();
00663             lcd.printString ("Game Over",18,1);
00664             lcd.printString ("Viking",24,2);
00665             lcd.printString ("Destroyed",18,3);
00666             lcd.refresh();
00667             wait(2.0);
00668             lcd.clear();
00669             char buffer[14];
00670             int length = sprintf(buffer,"Score: %d",score);
00671             if (length <= 14)
00672             lcd.printString(buffer,0,1);
00673             lcd.refresh();
00674             break;
00675         }
00676         if(a+3 == x+3 && q ==42) {
00677             lcd.clear();
00678             lcd.printString ("Game Over",18,1);
00679             lcd.printString ("Viking",24,2);
00680             lcd.printString ("Destroyed",18,3);
00681             lcd.refresh();
00682             wait(2.0);
00683             lcd.clear();
00684              char buffer[14];
00685             int length = sprintf(buffer,"Score: %d",score);
00686             if (length <= 14)
00687             lcd.printString(buffer,0,1);
00688             lcd.refresh();
00689             break;
00690         }
00691         if(a+3 == x+4 && q ==42) {
00692             lcd.clear();
00693             lcd.printString ("Game Over",18,1);
00694             lcd.printString ("Viking",24,2);
00695             lcd.printString ("Destroyed",18,3);
00696             lcd.refresh();
00697             wait(2.0);
00698             lcd.clear();
00699              char buffer[14];
00700             int length = sprintf(buffer,"Score: %d",score);
00701             if (length <= 14)
00702             lcd.printString(buffer,0,1);
00703             lcd.refresh();
00704             break;
00705         }
00706         if(a == x+4 && q+1 == 42) {
00707             lcd.clear();
00708             lcd.printString ("Game Over",18,1);
00709             lcd.printString ("Viking",24,2);
00710             lcd.printString ("Destroyed",18,3);
00711             lcd.refresh();
00712             wait(2.0);
00713             lcd.clear();
00714             char buffer[14];
00715             int length = sprintf(buffer,"Score: %d",score);
00716             if (length <= 14)
00717             lcd.printString(buffer,0,1);
00718             lcd.refresh();
00719             break;
00720         }
00721         if(a+1 == x+4 && q ==42) {
00722             lcd.clear();
00723             lcd.printString ("Game Over",18,1);
00724             lcd.printString ("Viking",24,2);
00725             lcd.printString ("Destroyed",18,3);
00726             lcd.refresh();
00727             wait(2.0);
00728             lcd.clear();
00729             char buffer[14];
00730             int length = sprintf(buffer,"Score: %d",score);
00731             if (length <= 14)
00732             lcd.printString(buffer,0,1);
00733             lcd.refresh();
00734             break;
00735         }
00736         if(a+2 == x+4 && q == 42) {
00737             lcd.clear();
00738             lcd.printString ("Game Over",18,1);
00739             lcd.printString ("Viking",24,2);
00740             lcd.printString ("Destroyed",18,3);
00741             lcd.refresh();
00742             wait(2.0);
00743             lcd.clear();
00744             char buffer[14];
00745             int length = sprintf(buffer,"Score: %d",score);
00746             if (length <= 14)
00747             lcd.printString(buffer,0,1);
00748             lcd.refresh();
00749             break;
00750         }
00751         if(a+3 == x+4 && q+4 ==42) {
00752             lcd.clear();
00753             lcd.printString ("Game Over",18,1);
00754             lcd.printString ("Viking",24,2);
00755             lcd.printString ("Destroyed",18,3);
00756             lcd.refresh();
00757             wait(2.0);
00758             lcd.clear();
00759             char buffer[14];
00760             int length = sprintf(buffer,"Score: %d",score);
00761             if (length <= 14)
00762             lcd.printString(buffer,0,1);
00763             lcd.refresh();
00764             break;
00765         }
00766         if(a+4 == x+4 && q ==42) {
00767             lcd.clear();
00768             lcd.printString ("Game Over",18,1);
00769             lcd.printString ("Viking",24,2);
00770             lcd.printString ("Destroyed",18,3);
00771             lcd.refresh();
00772             wait(2.0);
00773             lcd.clear();
00774             char buffer[14];
00775             int length = sprintf(buffer,"Score: %d",score);
00776             if (length <= 14)
00777             lcd.printString(buffer,0,1);
00778             lcd.refresh();
00779             break;
00780         }
00781         if(a+5 == x+4 && q ==42) {
00782             lcd.clear();
00783             lcd.printString ("Game Over",18,1);
00784             lcd.printString ("Viking",24,2);
00785             lcd.printString ("Destroyed",18,3);
00786             lcd.refresh();
00787             wait(2.0);
00788             lcd.clear();
00789             char buffer[14];
00790             int length = sprintf(buffer,"Score: %d",score);
00791             if (length <= 14)
00792             lcd.printString(buffer,0,1);
00793             lcd.refresh();
00794             break;
00795         }
00796         if(a+6 == x+4 && q+1 ==42) {
00797             lcd.clear();
00798             lcd.printString ("Game Over",18,1);
00799             lcd.printString ("Viking",24,2);
00800             lcd.printString ("Destroyed",18,3);
00801             lcd.refresh();
00802             wait(2.0);
00803             lcd.clear();
00804             char buffer[14];
00805             int length = sprintf(buffer,"Score: %d",score);
00806             if (length <= 14)
00807             lcd.printString(buffer,0,1);
00808             lcd.refresh();
00809             break;
00810         }
00811         
00812         //dectect if the player hit the left hand side wall.
00813         if(width+9 > x+4 && rect+5 >= 40){
00814             lcd.clear();
00815             lcd.printString ("Game Over",18,1);
00816             lcd.printString ("Viking",24,2);
00817             lcd.printString ("Destroyed",18,3);
00818             lcd.refresh();
00819             wait(2.0);
00820             lcd.clear();
00821             char buffer[14];
00822             int length = sprintf(buffer,"Score: %d",score);
00823             if (length <= 14)
00824             lcd.printString(buffer,0,1);
00825             lcd.refresh();
00826             break;
00827             }
00828         //dectect if the player hit the right hand side wall.
00829         if(x+4 >= width2 && rect1+5 >= 40){
00830             lcd.clear();
00831             lcd.printString ("Game Over",18,1);
00832             lcd.printString ("Viking",24,2);
00833             lcd.printString ("Destroyed",18,3);
00834             lcd.refresh();
00835             wait(2.0);
00836             lcd.clear();
00837             char buffer[14];
00838             int length = sprintf(buffer,"Score: %d",score);
00839             if (length <= 14)
00840             lcd.printString(buffer,0,1);
00841             lcd.refresh();
00842             break;
00843             }
00844         //detect victory. Whenever player's score is over 5000.
00845         if (score >= 5000)
00846         {
00847             lcd.clear();
00848             lcd.printString("Congratulation!",0,1);
00849             lcd.printString("Mission",20,2);
00850             lcd.printString("Complete!",18,3);
00851             lcd.refresh();
00852             break;
00853             }
00854 
00855     }
00856 }
00857 
00858 void calibrateJoystick()
00859 {
00860     button.mode(PullDown);
00861     // must not move during calibration
00862     joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
00863     joystick.y0 = yPot;
00864 }
00865 void updateJoystick()
00866 {
00867     // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
00868     joystick.x = xPot - joystick.x0;
00869     joystick.y = yPot - joystick.y0;
00870     // read button state
00871     joystick.buttonjoystick = button;
00872 
00873     // calculate direction depending on x,y values
00874     // tolerance allows a little lee-way in case joystick not exactly in the stated direction
00875     if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00876         joystick.direction = CENTRE;
00877     } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00878         joystick.direction = UP;
00879     } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00880         joystick.direction = DOWN;
00881     } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00882         joystick.direction = RIGHT;
00883     } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00884         joystick.direction = LEFT;
00885     } else {
00886         joystick.direction = UNKNOWN;
00887     }
00888 
00889     // set flag for printing
00890     printFlag = 1;
00891 }
00892 // code below is just used to testing.
00893 void createbullet()
00894 {
00895     int z = 41;
00896     if (joystick.direction == UP && z > 4) {
00897         --z;
00898     }
00899     if (joystick.direction == UP && z > 78) {
00900         ++z;
00901     }
00902     if(button) {
00903 
00904         for (int y = 39; y > -5 ; y-=7) {
00905 
00906             lcd.drawLine(z,y,z,y-2,1);
00907             if(y<39) {
00908                 lcd.drawLine(z,y+7,z,y-2+7,0);
00909             }
00910             lcd.refresh();
00911 
00912 
00913         }
00914     }
00915     bulletFlag  = 1;
00916 
00917 }
00918 // code below is just used to testing.
00919 void createenermy()
00920 {
00921     int a;
00922     int b;
00923     a = rand() % 76;
00924     b = rand() % 76;
00925     for (int q=0 ; q < 52; q+=2) {
00926         lcd.drawLine(a,q+1,a+1,q,1);
00927         lcd.drawLine(a+1,q,a+5,q,1);
00928         lcd.drawLine(a+5,q,a+6,q+1,1);
00929         lcd.drawLine(a+3,q,a+3,q+4,1);
00930 
00931 
00932         lcd.drawLine(a+b,q+1,a+1+b,q,1);
00933         lcd.drawLine(a+1+b,q,a+5+b,q,1);
00934         lcd.drawLine(a+5+b,q,a+6+b,q+1,1);
00935         lcd.drawLine(a+3+b,q,a+3+b,q+4,1);
00936         if (q < 52) {
00937             lcd.drawLine(a,q+1-2,a+1,q-2,0);
00938             lcd.drawLine(a+1,q-2,a+5,q-2,0);
00939             lcd.drawLine(a+5,q-2,a+6,q+1-2,0);
00940             lcd.drawLine(a+3,q-2,a+3,q+4-2,0);
00941 
00942             lcd.drawLine(a+b,q+1-2,a+1+b,q-2,0);
00943             lcd.drawLine(a+1+b,q-2,a+5+b,q-2,0);
00944             lcd.drawLine(a+5+b,q-2,a+6+b,q+1-2,0);
00945             lcd.drawLine(a+3+b,q-2,a+3+b,q+4-2,0);
00946         }
00947     }
00948     enermyFlag  = 1;
00949 
00950 }
00951 // set g_button_flag equals to 1 and return to button_isr().
00952 void button_isr()
00953 {
00954     g_button_flag =1;
00955 }