jordan harper / Mbed 2 deprecated GameProject

Dependencies:   N5110 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002 @file main.cpp
00003 
00004 @brief Game Algorithm implementation
00005  */
00006 
00007 #include "main.h"
00008 
00009 int main()
00010 {
00011     //set_time(0); // enter unix time then delete from main function to set time
00012     power = 1;
00013     lcd.init();
00014     calibrateJoystick();  // get centred values of joystick
00015     pollJoystick .attach(&updateJoystick,1.0/15.0);  // read joystick 15 times per second
00016     wallMovement .attach(&flagForWall,0.2); // call function flagForWall to make flag=1, every 0.2 seconds
00017     wallMovement2 .attach(&flagForWall2,0.2); // call function flagForWall2 to make flag=1, every 0.2 seconds
00018     wallMovement3 .attach(&flagForWall3,0.2); // call function flagForWall3 to make flag=1, every 0.2 seconds
00019     flash .attach(&flagForFlash,1); // call function flagForWall3 to make flag=1, every 1 seconds
00020     button.rise(&buttonFlag); // call flag when ever joystick button is pressed
00021     menu();
00022 }
00023 
00024 void calibrateJoystick()
00025 {
00026     button.mode(PullDown); // must not move while calibrating
00027     joystick .x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
00028     joystick .y0 = yPot;
00029 }
00030 
00031 void updateJoystick()
00032 {
00033 
00034     joystick .x = xPot - joystick .x0; // reads current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
00035     joystick .y = yPot - joystick .y0;
00036     joystick .button = button;
00037 
00038     /* calculate direction depending on x,y values */
00039     /* tolerance allows a little lee-way in case joystick not exactly in the stated direction */
00040     if ( fabs(joystick .y) < DIRECTION_TOLERANCE && fabs(joystick .x) < DIRECTION_TOLERANCE) {
00041         joystick .direction = CENTRE;
00042     } else if ( joystick .y > DIRECTION_TOLERANCE && fabs(joystick .x) < DIRECTION_TOLERANCE) {
00043         joystick .direction = UP;
00044     } else if ( joystick .y < DIRECTION_TOLERANCE && fabs(joystick .x) < DIRECTION_TOLERANCE) {
00045         joystick .direction = DOWN;
00046     } else if ( joystick .x > DIRECTION_TOLERANCE && fabs(joystick .y) < DIRECTION_TOLERANCE) {
00047         joystick .direction = RIGHT;
00048     } else if ( joystick .x < DIRECTION_TOLERANCE && fabs(joystick .y) < DIRECTION_TOLERANCE) {
00049         joystick .direction = LEFT;
00050     } else if ( joystick .y > DIRECTION_TOLERANCE && joystick .x > DIRECTION_TOLERANCE) {
00051         joystick .direction = upRight;
00052     } else if ( joystick .y < DIRECTION_TOLERANCE && joystick .x > DIRECTION_TOLERANCE) {
00053         joystick .direction = downRight;
00054     } else if ( joystick .y > DIRECTION_TOLERANCE && joystick .x < DIRECTION_TOLERANCE) {
00055         joystick .direction = upLeft;
00056     } else if ( joystick .y < DIRECTION_TOLERANCE && joystick .x < DIRECTION_TOLERANCE) {
00057         joystick .direction = downLeft;
00058     }
00059 
00060     printFlag  = 1; // set flag for printing
00061 }
00062 
00063 
00064 
00065 void clearCells()
00066 {
00067     for (int i  = 0; i  < nx  ; i ++) { // loops through cells in x direction first
00068         for (int j  = 0; j  < ny  ; j ++) { // then moves in y direction
00069             lcd.clearPixel(i ,j ); // clears each pixel that is checked
00070         }
00071     }
00072     lcd.refresh(); // must refresh to write buffer display
00073 }
00074 
00075 
00076 void flagForWall()
00077 {
00078     FLAG =1;
00079 }
00080 
00081 
00082 void flagForWall2()
00083 {
00084     FLAG2 =1;
00085 }
00086 
00087 
00088 void flagForWall3()
00089 {
00090     FLAG3 =1;
00091 }
00092 
00093 void buttonFlag()
00094 {
00095     BFlag  =1;
00096 }
00097 
00098 void menu()
00099 {
00100     int m = 0;
00101     BFlag  = 0;
00102 
00103     char buffer[14]; // buffer used to store time string
00104 
00105     while(1) {
00106         powerSave(); // call sleep function
00107         clearCells();
00108         time_t seconds = time(NULL); // gets current time
00109         strftime(buffer, 14 , "%H:%M", localtime(&seconds)); // send time string to buffer
00110 
00111         lcd.printString(buffer,26,4); // show time on lcd screen
00112 
00113 
00114         if ((joystick .direction == RIGHT)||(joystick .direction == upRight)||(joystick .direction == downRight)) {
00115             m++;
00116         }
00117         if ((joystick .direction == LEFT)||(joystick .direction == upLeft)||(joystick .direction == downLeft)) {
00118             m--;
00119         }
00120         if(m > 2) {
00121             m = 0;
00122         }
00123         if(m < 0) {
00124             m = 2;
00125         }
00126         switch (m) {
00127             case 0 :
00128                 lcd.printString("<  Play Game >",0,2);
00129                 wait(0.3);
00130                 if(BFlag ) {
00131                     BFlag =0;
00132                     clearCells();
00133                     playGame();
00134                 }
00135                 break;
00136             case 1 :
00137                 lcd.printString("<  Settings  >",0,2);
00138                 wait(0.3);
00139                 if(BFlag ) {
00140                     BFlag =0;
00141                     clearCells();
00142                     BandVMenu();
00143                 }
00144                 break;
00145             case 2 :
00146                 lcd.printString("<  HighScore >",0,2);
00147                 wait(0.3);
00148                 if(BFlag ) {
00149                     BFlag =0;
00150                     clearCells();
00151                     while(1) {
00152                         readDataFromFile();
00153                         lcd.printString("HighScore",16,0);
00154                         if(joystick .direction == DOWN) {
00155                             menu();
00156                         }
00157                     }
00158                 }
00159                 break;
00160 
00161         }
00162     }
00163 }
00164 
00165 
00166 void playGame()
00167 {
00168     /* reset variables */
00169     i =24;
00170     j =42;
00171     x  = 0;
00172     y  = 0;
00173     z  = 30;
00174     a  = 0;
00175     b  = 47;
00176     d  = 30;
00177     f  = 0;
00178     g  = 0;
00179     h  = 24;
00180     q  = 83;
00181     w  = 0;
00182     e  = 24;
00183     score  = 0;
00184     u  = 0;
00185 
00186     lcd.printString("GO!",35,2);
00187     wait(1);
00188     while(1) {
00189         if(soundFlag ==1) { // if mute hasn't been toggled in settings then the song will play
00190             tone();
00191         }
00192         lcd.drawRect(i ,j ,2,2,0); // draws player object
00193         clearCells();
00194         fallingWalls();
00195         boundries();
00196         playerMovement();
00197 
00198         if (lcd.getPixel(i ,j )) { // if wall meets player object then there is a collision
00199             scoreCheck();
00200             int m = 1;
00201             GameOverFlash(); // this means that it is game over
00202             if (soundFlag  == 1) {
00203                 deadTone();
00204             }
00205 
00206             while(1) {
00207                 clearCells();
00208                 if ((joystick .direction == RIGHT)||(joystick .direction == upRight)||(joystick .direction == downRight)) { // reads joystick direction and counts down
00209                     m--;
00210                 }
00211                 if ((joystick .direction == LEFT)||(joystick .direction == upLeft)||(joystick .direction == downLeft)) { // reads joystick direction and counts up
00212                     m++;
00213                 }
00214                 if(m > 2) { // stops counter from going above total number of cases
00215                     m = 2;
00216                 }
00217                 if(m < 0) { // stops counter from going below zero
00218                     m = 0;
00219                 }
00220                 switch (m) { //switches to the case that the counter is on
00221                     case 0 :
00222                         lcd.printString("YES",0,3);
00223                         if(BFlag ) {
00224                             BFlag =0;
00225                             clearCells();
00226                             playGame();
00227                         }
00228                         break;
00229                     case 1 :
00230                         lcd.printString("Play Again?",9,1);
00231                         lcd.printString("Yes        No",0,3);
00232                         wait(0.3);
00233 
00234                         break;
00235                     case 2 :
00236                         lcd.printString("NO",70,3);
00237                         if(BFlag ) {
00238                             BFlag  = 0;
00239                             clearCells();
00240                             lcd.printString("Leaving Game",0,2);
00241                             wait(1.0);
00242                             menu();
00243                         }
00244                         break;
00245                 }
00246             }
00247         }
00248         if (lcd.getPixel(i +2,j )) { // same as above but for different side of player object
00249 
00250             scoreCheck();
00251             clearCells();
00252             int m = 1;
00253             GameOverFlash();
00254             if (soundFlag  == 1) {
00255                 deadTone();
00256             }
00257 
00258             while(1) {
00259                 clearCells();
00260                 if ((joystick .direction == RIGHT)||(joystick .direction == upRight)||(joystick .direction == downRight)) {
00261                     m--;
00262                 }
00263                 if ((joystick .direction == LEFT)||(joystick .direction == upLeft)||(joystick .direction == downLeft)) {
00264                     m++;
00265                 }
00266                 if(m > 2) {
00267                     m = 2;
00268                 }
00269                 if(m < 0) {
00270                     m = 0;
00271                 }
00272                 switch (m) {
00273                     case 0 :
00274                         lcd.printString("YES",0,3);
00275                         if(BFlag ) {
00276                             BFlag  = 0;
00277                             clearCells();
00278                             playGame();
00279                         }
00280                         break;
00281                     case 1 :
00282                         lcd.printString("Play Again?",9,1);
00283                         lcd.printString("Yes        No",0,3);
00284                         wait(0.3);
00285 
00286                         break;
00287                     case 2 :
00288                         lcd.printString("NO",70,3);
00289                         if(BFlag ) {
00290                             BFlag  = 0;
00291                             clearCells();
00292                             lcd.printString("Leaving Game",0,2);
00293                             wait(1.0);
00294                             menu();
00295                         }
00296                         break;
00297                 }
00298             }
00299         }
00300         if (lcd.getPixel(i +2,j +2)) {
00301             scoreCheck();
00302             clearCells();
00303             int m = 1;
00304             GameOverFlash();
00305             if (soundFlag  == 1) {
00306                 deadTone();
00307             }
00308 
00309             while(1) {
00310                 clearCells();
00311                 if ((joystick .direction == RIGHT)||(joystick .direction == upRight)||(joystick .direction == downRight)) {
00312                     m--;
00313                 }
00314                 if ((joystick .direction == LEFT)||(joystick .direction == upLeft)||(joystick .direction == downLeft)) {
00315                     m++;
00316                 }
00317                 if(m > 2) {
00318                     m = 2;
00319                 }
00320                 if(m < 0) {
00321                     m = 0;
00322                 }
00323                 switch (m) {
00324                     case 0 :
00325                         lcd.printString("YES",0,3);
00326                         if(BFlag ) {
00327                             BFlag  = 0;
00328                             clearCells();
00329                             playGame();
00330                         }
00331                         break;
00332                     case 1 :
00333                         lcd.printString("Play Again?",9,1);
00334                         lcd.printString("Yes        No",0,3);
00335                         wait(0.3);
00336 
00337                         break;
00338                     case 2 :
00339                         lcd.printString("NO",70,3);
00340                         if(BFlag ) {
00341                             BFlag  = 0;
00342                             clearCells();
00343                             lcd.printString("Leaving Game",0,2);
00344                             wait(1.0);
00345                             menu();
00346                         }
00347                         break;
00348 
00349                 }
00350             }
00351         }
00352         if (lcd.getPixel(i ,j +2)) {
00353             scoreCheck();
00354             clearCells();
00355             int m = 1;
00356             GameOverFlash();
00357             if (soundFlag  == 1) {
00358                 deadTone();
00359             }
00360 
00361             while(1) {
00362                 clearCells();
00363                 if ((joystick .direction == RIGHT)||(joystick .direction == upRight)||(joystick .direction == downRight)) {
00364                     m--;
00365                 }
00366                 if ((joystick .direction == LEFT)||(joystick .direction == upLeft)||(joystick .direction == downLeft)) {
00367                     m++;
00368                 }
00369                 if(m > 2) {
00370                     m = 2;
00371                 }
00372                 if(m < 0) {
00373                     m = 0;
00374                 }
00375                 switch (m) {
00376                     case 0 :
00377                         lcd.printString("YES",0,3);
00378                         if(BFlag ) {
00379                             BFlag  = 0;
00380                             clearCells();
00381                             playGame();
00382                         }
00383                         break;
00384                     case 1 :
00385                         lcd.printString("Play Again?",9,1);
00386                         lcd.printString("Yes        No",0,3);
00387                         wait(0.3);
00388 
00389                         break;
00390                     case 2 :
00391                         lcd.printString("NO",70,3);
00392                         if(BFlag ) {
00393                             BFlag  = 0;
00394                             clearCells();
00395                             lcd.printString("Leaving Game",0,2);
00396                             wait(1.0);
00397                             menu();
00398                         }
00399                         break;
00400 
00401                 }
00402             }
00403         }
00404     }
00405 }
00406 
00407 
00408 void BandVMenu()
00409 {
00410     int t = 0;
00411     while(1) {
00412         wait(0.2);
00413         clearCells();
00414         if ((joystick .direction == RIGHT)||(joystick .direction == upRight)||(joystick .direction == downRight)) {
00415             t++;
00416         }
00417         if ((joystick .direction == LEFT)||(joystick .direction == upLeft)||(joystick .direction == downLeft)) {
00418             t--;
00419         }
00420         if(t > 2) {
00421             t = 0;
00422         }
00423         if(t < 0) {
00424             t = 2;
00425         }
00426         if(joystick .direction == DOWN) {
00427             clearCells();
00428             lcd.printString("Returning",18,2);
00429             lcd.printString("to Menu",24,3);
00430             wait(0.5);
00431             menu();
00432         }
00433         switch (t) {
00434             case 0 :
00435                 lcd.printString("< Brightness >",0,2);
00436                 if(BFlag ) {
00437                     BFlag  = 0;
00438                     brightness();
00439                 }
00440                 break;
00441             case 1 :
00442                 lcd.printString("<   Volume   >",0,2);
00443                 if(BFlag ) {
00444                     BFlag  = 0;
00445                     volume();
00446                 }
00447                 break;
00448             case 2 :
00449                 lcd.printString("<Instructions>",0,2);
00450                 if(BFlag ) {
00451                     BFlag  = 0;
00452                     instructions();
00453                 }
00454                 break;
00455         }
00456     }
00457 }
00458 
00459 
00460 void brightness()
00461 {
00462     while(1) {
00463         wait(0.2);
00464         clearCells();
00465         if ((joystick .direction == RIGHT)||(joystick .direction == upRight)||(joystick .direction == downRight)) {
00466             bright --;
00467         }
00468         if ((joystick .direction == LEFT)||(joystick .direction == upLeft)||(joystick .direction == downLeft)) {
00469             bright ++;
00470         }
00471         if(bright  > 3) {
00472             bright  = 3;
00473         }
00474         if(bright  < -3) {
00475             bright  = -3;
00476         }
00477         switch (bright ) {
00478             case 0 :
00479                 lcd.printString("  Brightness ",0,2);
00480                 lcd.printString(" ||||||",0,4);
00481                 lcd.setBrightness(0.6);
00482                 if(BFlag ) {
00483                     BFlag  = 0;
00484                     BandVMenu();
00485                 }
00486                 break;
00487             case 1 :
00488                 lcd.printString("  Brightness ",0,2);
00489                 lcd.printString(" ||||||||",0,4);
00490                 lcd.setBrightness(0.8);
00491                 if(BFlag ) {
00492                     BFlag  = 0;
00493                     BandVMenu();
00494                 }
00495                 break;
00496             case 2 :
00497                 lcd.printString("  Brightness ",0,2);
00498                 lcd.printString(" ||||||||||",0,4);
00499                 lcd.setBrightness(0.9);
00500                 if(BFlag ) {
00501                     BFlag  = 0;
00502                     BandVMenu();
00503                 }
00504                 break;
00505             case 3 :
00506                 lcd.printString("  Brightness ",0,2);
00507                 lcd.printString(" ||||||||||||",0,4);
00508                 lcd.setBrightness(1);
00509                 if(BFlag ) {
00510                     BFlag  = 0;
00511                     BandVMenu();
00512                 }
00513                 break;
00514             case -1 :
00515                 lcd.printString("  Brightness ",0,2);
00516                 lcd.printString(" ||||",0,4);
00517                 lcd.setBrightness(0.4);
00518                 if(BFlag ) {
00519                     BFlag  = 0;
00520                     BandVMenu();
00521                 }
00522                 break;
00523             case -2 :
00524                 lcd.printString("  Brightness ",0,2);
00525                 lcd.printString(" ||",0,4);
00526                 lcd.setBrightness(0.2);
00527                 if(BFlag ) {
00528                     BFlag  = 0;
00529                     BandVMenu();
00530                 }
00531                 break;
00532             case -3 :
00533                 lcd.printString("  Brightness ",0,2);
00534                 lcd.setBrightness(0);
00535                 if(BFlag ) {
00536                     BFlag  = 0;
00537                     BandVMenu();
00538                 }
00539                 break;
00540         }
00541     }
00542 }
00543 
00544 
00545 void volume()
00546 {
00547     while(1) {
00548         wait(0.2);
00549         clearCells();
00550         if ((joystick .direction == RIGHT)||(joystick .direction == upRight)||(joystick .direction == downRight)) {
00551             vol --;
00552         }
00553         if ((joystick .direction == LEFT)||(joystick .direction == upLeft)||(joystick .direction == downLeft)) {
00554             vol ++;
00555         }
00556         if(vol  > 3) {
00557             vol  = 3;
00558         }
00559         if(vol  < -3) {
00560             vol  = -3;
00561         }
00562         switch (vol ) {
00563             case 0 :
00564                 lcd.printString("    Volume   ",0,2);
00565                 lcd.printString(" ||||||",0,4);
00566                 if(BFlag ) {
00567                     BFlag  = 0;
00568                     BandVMenu();
00569                 }
00570                 break;
00571             case 1 :
00572                 lcd.printString("    Volume   ",0,2);
00573                 lcd.printString(" ||||||||",0,4);
00574                 if(BFlag ) {
00575                     BFlag  = 0;
00576                     BandVMenu();
00577                 }
00578                 break;
00579             case 2 :
00580                 lcd.printString("    Volume   ",0,2);
00581                 lcd.printString(" ||||||||||",0,4);
00582                 if(BFlag ) {
00583                     BFlag  = 0;
00584                     BandVMenu();
00585                 }
00586                 break;
00587             case 3 :
00588                 lcd.printString("    Volume   ",0,2);
00589                 lcd.printString(" ||||||||||||",0,4);
00590                 if(BFlag ) {
00591                     BFlag  = 0;
00592                     BandVMenu();
00593                 }
00594                 break;
00595             case -1 :
00596                 lcd.printString("    Volume   ",0,2);
00597                 lcd.printString(" ||||",0,4);
00598                 if(BFlag ) {
00599                     BFlag  = 0;
00600                     BandVMenu();
00601                 }
00602                 break;
00603             case -2 :
00604                 lcd.printString("    Volume   ",0,2);
00605                 lcd.printString(" ||",0,4);
00606                 if(BFlag ) {
00607                     BFlag  = 0;
00608                     BandVMenu();
00609                 }
00610                 break;
00611             case -3 :
00612                 lcd.printString("    Volume    ",0,2);
00613                 lcd.printString(" MUTE",0,4);
00614                 soundFlag  = 0;
00615                 if(BFlag ) {
00616                     BFlag  = 0;
00617                     BandVMenu();
00618                 }
00619                 break;
00620         }
00621     }
00622 }
00623 
00624 
00625 void writeDataToFile()
00626 {
00627     time_t seconds = time(NULL); // get current time
00628     char buffer2[14]; // buffer used to store time string
00629     strftime(buffer2, 14, " %d/%m/%y", localtime(&seconds)); // displays unix time in day/ month/ year format and sends it to buffer
00630 
00631     FILE *fp = fopen("/local/score.txt", "w"); // open file called 'score.txt'
00632     /* if the file doesn't exist it is created, if it exists, data is updated */
00633     fprintf(fp,"%i%s",score ,buffer2); // print integer for score and date to file
00634     fclose(fp); // close file
00635 }
00636 
00637 
00638 void readDataFromFile()
00639 {
00640     int score1; // states the score to be read from the file is an integer value
00641     char buffer2recieved[14]; // buffer to recieve the string of data read from the text file
00642 
00643     FILE *fp2 = fopen("/local/score.txt", "r"); // open 'score.txt'
00644     fscanf (fp2,"%i%s",&score1,buffer2recieved); // scans text file for an integer followed by a string
00645     fclose(fp2); // close file
00646 
00647     char Points[14]; // buffer to store score value
00648     sprintf(Points,"%i",score1); // send score to buffer (Points)
00649     lcd.printString(Points,39,2); // print score on LCD
00650     lcd.printString(buffer2recieved,18,4); // print date on LCD
00651 
00652 }
00653 
00654 
00655 
00656 void tone()
00657 {
00658 
00659     if(FLAG3 ==1) {
00660         FLAG3  = 0;
00661         buz.period(1/(frequency [u ])); // set PWM period
00662         buz=0.2;
00663         u++;
00664     }
00665     if( u  > 30) {
00666         u  = 0;
00667     }
00668 
00669 }
00670 
00671 
00672 void deadTone()
00673 {
00674     int l = 0;
00675     while(1) {
00676         if(FLAG3 ==1) {
00677             FLAG3  = 0;
00678             buz.period(1/(frequency2 [l])); // set PWM period
00679             buz=0.2;
00680             l++;
00681         }
00682         if( l > 6) {
00683             break;
00684         }
00685     }
00686 }
00687 
00688 
00689 void scoreCheck()
00690 {
00691     int score1;
00692 
00693     FILE *fp2 = fopen("/local/score.txt", "r");
00694     fscanf (fp2,"%i",&score1);
00695     fclose(fp2);
00696 
00697     if(score1 < score ) {
00698         writeDataToFile(); // if the score is bigger then the data is written to the file and overwrites the previous data
00699     }
00700 }
00701 
00702 
00703 void flagForFlash()
00704 {
00705     flashFlag  = 1;
00706 }
00707 
00708 
00709 void GameOverFlash()
00710 {
00711     int flash  = 0;
00712     while(1) {
00713         if (flashFlag ==1) { // flag equals one every second
00714             flashFlag  = 0; // flag is reset
00715             lcd.printString("Game Over!",12,2); // print phrase
00716             flash++; // adds one to counter
00717         }
00718         if (flash > 3) { // moves onto next function when counter gets to 3
00719             break;
00720         }
00721     }
00722 }
00723 
00724 
00725 void powerSave()
00726 {
00727     if(joystick .direction == CENTRE) {
00728         if (flashFlag ==1) {
00729             flashFlag  = 0;
00730             sleepCounter ++;
00731         }
00732         if(sleepCounter  > 20) {
00733             Sleep(); // put mbed into sleep mode
00734             lcd.turnOff(); // turn LCD off
00735             while(1){
00736             if(BFlag ) {
00737                 BFlag  = 0;
00738                 lcd.init(); // turn LCD back on
00739                 sleepCounter  = 0;
00740                 wait(0.5);
00741                 menu();
00742             }
00743             }
00744         }
00745 
00746     } else {
00747         sleepCounter  = 0;
00748     }
00749 }
00750 
00751 
00752 void boundries()
00753 {
00754     if( j  > 45) {
00755         j  = 45;
00756     }
00757     if( i  > 81) {
00758         i  = 81;
00759     }
00760     if( j  < 0) {
00761         j  = 0;
00762     }
00763     if( i  < 0) {
00764         i  = 0;
00765     }
00766 }
00767 
00768 
00769 void playerMovement()
00770 {
00771 
00772     if (printFlag ) {  // if flag set, clear flag and implicate joystick functions
00773         printFlag  = 0;
00774         /* check joystick direction */
00775         if (joystick .direction == UP) {
00776             j --;
00777         }
00778         if (joystick .direction == DOWN) {
00779             j ++;
00780         }
00781         if (joystick .direction == LEFT) {
00782             i ++;
00783         }
00784         if (joystick .direction == RIGHT) {
00785             i --;
00786         }
00787         if (joystick .direction == upRight) {
00788             j --;
00789             i --;
00790         }
00791         if (joystick .direction == downRight) {
00792             j ++;
00793             i --;
00794         }
00795         if (joystick .direction == upLeft) {
00796             j --;
00797             i ++;
00798         }
00799         if (joystick .direction == downLeft) {
00800             j ++;
00801             i ++;
00802         }
00803         if (joystick .direction == CENTRE) {
00804             i =i ;
00805             j =j ;
00806         }
00807     }
00808 }
00809 
00810 
00811 void fallingWalls()
00812 {
00813     lcd.drawLine(x ,y ,z ,y ,1); // draws a line at the top of the screen to symbolise a wall
00814     lcd.drawLine(z +10,y ,83,y ,1); // there is a gap in the wall which is 10 pixels wide and the objective of the game is to pass through it
00815     if(FLAG ==1) { // the flag changes every 0.2 seconds
00816         FLAG =0;
00817         y ++;  // everytime the flag changes to one it is reset and the wall moves down by one pixel
00818     }
00819     if(y  > 47) { // when the wall reaches the bottom a new wall is generated at the top
00820         y  = 0;
00821         z  = rand()%74; // the gap in the is moved to a random postion in the new wall
00822         score ++; // score increases by one everytime a wall falling from the top reaches the bottom of the screen
00823     }
00824     /* there are 6 levels of difficulty */
00825     if(( score  > 1)&&(score  < 5)) {
00826         lcd.drawLine(a ,b ,d ,b ,1);
00827         lcd.drawLine(d +10,b ,83,b ,1);
00828         if(FLAG3 ==1) {
00829             FLAG3 =0;
00830             b --;
00831         }
00832     }
00833     if(b  < 0) {
00834         b  = 47;
00835         d  = rand()%74;
00836     }
00837     if(( score  > 5)&&(score  < 9)) {
00838         lcd.drawLine(f ,g ,f ,h ,1);
00839         lcd.drawLine(f ,h +10,f ,47,1);
00840         if(FLAG2 ==1) {
00841             FLAG2 =0;
00842             f ++;
00843         }
00844     }
00845     if( f  > 83) {
00846         f  = 0;
00847         h  = rand()%38;
00848     }
00849     if(( score  > 10)&&(score  < 15)) {
00850         lcd.drawLine(q ,w ,q ,e ,1);
00851         lcd.drawLine(q ,e +10,q ,47,1);
00852         if(FLAG3 ==1) {
00853             FLAG3 =0;
00854             q --;
00855         }
00856     }
00857     if(q  < 0) {
00858         q  = 83;
00859         e  = rand()%38;
00860     }
00861     if( score  > 15) {
00862         lcd.drawLine(q ,w ,q ,e ,1);
00863         lcd.drawLine(q ,e +10,q ,47,1);
00864         if(FLAG3 ==1) {
00865             FLAG3 =0;
00866             q --;
00867         }
00868     }
00869     if( score  > 20) {
00870         lcd.drawLine(a ,b ,d ,b ,1);
00871         lcd.drawLine(d +10,b ,83,b ,1);
00872         if(FLAG2 ==1) {
00873             FLAG2 =0;
00874             b --;
00875         }
00876     }
00877 }
00878 
00879 void instructions()
00880 {
00881     while(1) {
00882         lcd.printString("Welcome :-)",9,0);
00883         lcd.printString("Select using",6,1);
00884         lcd.printString("joystickButton",0,2);
00885         lcd.printString("Press >",27,4);
00886         wait(0.5);
00887         if(BFlag ) {
00888             BFlag  = 0; // press joystick button to continue
00889             clearCells();
00890             while(1) {
00891                 lcd.printString("Return to menu",0,0);
00892                 lcd.printString("by moving the",3,1);
00893                 lcd.printString("JS downward",9,2);
00894                 if(joystick .direction == DOWN) { // direct joystick down to continue
00895                     menu();
00896                     break;
00897                 }
00898             }
00899         }
00900     }
00901 }