Michael Birney / Mbed 2 deprecated SnakeM_Birney

Dependencies:   N5110 PinDetect PowerControl mbed

Fork of DocTest by Craig Evans

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "main.h"
00002 
00003 // create enumerated type (0,1,2,3 etc. for direction)
00004 enum DirectionName {
00005     UP,
00006     DOWN,
00007     LEFT,
00008     RIGHT,
00009     CENTRE,
00010     UNKNOWN
00011 };
00012 
00013 // create enumerated type (0,1,2 for difficulty)
00014 enum Difficulty {
00015     EASY,
00016     MEDIUM,
00017     HARD,
00018     
00019 };
00020 
00021 // create enumerated type (0,1 for Game Mode)
00022 enum GameMode {
00023     CLASSIC,
00024     BOUNDARY,
00025 };
00026 
00027 Difficulty currentDifficulty=EASY;// initialise to easy mode
00028 GameMode   selectedGameMode=CLASSIC; //initialise to Classic Mode
00029 
00030 // struct for Joystick
00031 typedef struct JoyStick Joystick;
00032 struct JoyStick {
00033     float x;    // current x value
00034     float x0;   // 'centred' x value
00035     float y;    // current y value
00036     float y0;   // 'centred' y value
00037     int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
00038     DirectionName direction;  // current direction
00039 };
00040 // create struct variable
00041 Joystick joystick;
00042 
00043 DirectionName previousDirection =RIGHT;//initial direction =Right
00044 
00045 // read default positions of the joystick to calibrate later readings
00046 void calibrateJoystick()
00047 {
00048     button.mode(PullDown);
00049     // must not move during calibration
00050     joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
00051     joystick.y0 = yPot;
00052 }
00053 void updateJoystick()
00054 {
00055     // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
00056     joystick.x = xPot - joystick.x0;
00057     joystick.y = yPot - joystick.y0;
00058     // read button state
00059     joystick.button = button;
00060 
00061     // calculate direction depending on x,y values
00062     // tolerance allows a little lee-way in case joystick not exactly in the stated direction
00063     if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00064         joystick.direction = CENTRE;
00065     } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00066         joystick.direction = DOWN;
00067     } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00068         joystick.direction = UP;
00069     } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00070         joystick.direction = RIGHT; // remember switched this with right
00071     } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00072         joystick.direction = LEFT;
00073     } else {
00074         joystick.direction = UNKNOWN;
00075     }
00076 }
00077 
00078 //GAME FUNCTIONS
00079 
00080 //For start MENU
00081 
00082 void displaySplash()
00083 {
00084 
00085     lcd.inverseMode();      // change colour mode
00086     lcd.setBrightness(0.5); // put LED backlight on 50%
00087 
00088     //Draw S
00089     lcd.drawRect(28,10,2,5,1);
00090     lcd.drawRect(15,10,15,2,1);
00091     lcd.drawRect(15,10,2,10,1);
00092     lcd.drawRect(15,20,15,2,1);
00093     lcd.drawRect(28,20,2,10,1);
00094     lcd.drawRect(15,28,15,2,1);
00095     lcd.drawRect(15,25,2,3,1);
00096 
00097     lcd.printString("NAKE ",34,3);
00098     lcd.printString("By M.Birney",10,5);
00099     lcd.drawRect(10,5,65,30,0); // outline for splash
00100 
00101     // need to refresh display after setting pixels
00102 
00103     lcd.refresh();
00104 }
00105 
00106 
00107 
00108 void easySelected() // display when easy is selected
00109 {
00110     currentDifficulty=EASY;
00111     lcd.clear();
00112     lcd.printString("Please Select",2,0);
00113     lcd.printString("Difficulty:",2,1);
00114     lcd.printString("Easy",20,3);
00115     lcd.printString("Medium",20,4);
00116     lcd.printString("Hard",20,5);
00117 
00118     lcd.drawCircle(10,27,2,1);
00119     lcd.drawCircle(10,35,2,0);
00120     lcd.drawCircle(10,43,2,0);
00121     lcd.refresh();
00122 
00123     gameSpeed = 1.0/5; // set easy game speed(for game time)
00124 }
00125 void mediumSelected() // display when medium is selected
00126 {
00127     currentDifficulty=MEDIUM;
00128     lcd.clear();
00129     lcd.printString("Please Select",2,0);
00130     lcd.printString("Difficulty:",2,1);
00131     lcd.printString("Easy",20,3);
00132     lcd.printString("Medium",20,4);
00133     lcd.printString("Hard",20,5);
00134 
00135     lcd.drawCircle(10,27,2,0);
00136     lcd.drawCircle(10,35,2,1);
00137     lcd.drawCircle(10,43,2,0);
00138     gameSpeed =1.0/7; // set medium game speed
00139     lcd.refresh();
00140 }
00141 
00142 void hardSelected() // display when hard is selected
00143 {
00144     currentDifficulty=HARD;
00145     lcd.clear();
00146     lcd.printString("Please Select",2,0);
00147     lcd.printString("Difficulty:",2,1);
00148     lcd.printString("Easy",20,3);
00149     lcd.printString("Medium",20,4);
00150     lcd.printString("Hard",20,5);
00151 
00152     lcd.drawCircle(10,27,2,0);
00153     lcd.drawCircle(10,35,2,0);
00154     lcd.drawCircle(10,43,2,1);
00155     lcd.refresh();
00156     gameSpeed =1.0/12; // set hard game speed
00157 }
00158 
00159 void classicModeSelected() //display when classic mode selected
00160 {
00161 
00162 
00163     selectedGameMode=CLASSIC;
00164     lcd.clear();
00165     lcd.printString("Please Select",2,0);
00166     lcd.printString("Game Mode:",2,1);
00167     lcd.printString("Classic",20,3);
00168     lcd.printString("Boundary",20,4);
00169 
00170     lcd.drawCircle(10,27,2,1);
00171     lcd.drawCircle(10,35,2,0);
00172     lcd.refresh();
00173 
00174 }
00175 
00176 void boundaryModeSelected() // display when boundary mode selected
00177 {
00178     selectedGameMode=BOUNDARY;//update selecected game mode
00179     lcd.clear();
00180     lcd.printString("Please Select",2,0);
00181     lcd.printString("Game Mode:",2,1);
00182     lcd.printString("Classic",20,3);
00183     lcd.printString("Boundary",20,4);
00184 
00185     lcd.drawCircle(10,27,2,0);
00186     lcd.drawCircle(10,35,2,1);
00187     lcd.refresh();
00188 
00189 }
00190 
00191 
00192 void checkSelectedGameMode()// updates selected game mode menu depending on joystick direction
00193 {
00194 
00195     switch(selectedGameMode) {
00196         case CLASSIC:
00197 
00198             switch (joystick.direction) {
00199 
00200                 case UP:
00201                     boundaryModeSelected();
00202                     break;
00203 
00204 
00205                 case DOWN:
00206                     boundaryModeSelected();
00207                     break;
00208             }
00209             break;
00210 
00211 
00212         case BOUNDARY:
00213 
00214             switch (joystick.direction) {
00215 
00216                 case UP:
00217                     classicModeSelected();
00218                     break;
00219 
00220 
00221                 case DOWN:
00222                     classicModeSelected();
00223                     break;
00224             }
00225 
00226             break;
00227     }
00228     wait(0.2);
00229 
00230 }
00231 
00232 void checkSelectedDifficulty()
00233 {
00234 
00235     switch(currentDifficulty) {
00236         case EASY:
00237 
00238             switch (joystick.direction) {
00239 
00240                 case UP:
00241                     hardSelected();
00242                     break;
00243 
00244 
00245                 case DOWN:
00246                     mediumSelected();
00247                     break;
00248             }
00249             break;
00250 
00251 
00252         case MEDIUM:
00253 
00254             switch (joystick.direction) {
00255 
00256                 case UP:
00257                     easySelected();
00258                     break;
00259 
00260 
00261                 case DOWN:
00262                     hardSelected();
00263                     break;
00264             }
00265 
00266 
00267 
00268             break;
00269         case HARD:
00270             switch (joystick.direction) {
00271 
00272                 case UP:
00273                     mediumSelected();
00274                     break;
00275 
00276 
00277                 case DOWN:
00278                     easySelected();
00279                     break;
00280             }
00281             break;
00282     }
00283     wait(0.2);
00284 }
00285 
00286 
00287 
00288 
00289 //for gamePLAY
00290 
00291 
00292 
00293 void startingSnake()
00294 {
00295     snakeX .resize(5); // ensure that when game is reset snake vectors are resized back to 5
00296     snakeY .resize(5);//
00297     snakeX [0]=20; // initial snake position
00298     snakeX [1]=22;
00299     snakeX [2]=24;
00300     snakeX [3]=26;
00301     snakeX [4]=28;
00302     snakeY [0]=27;
00303     snakeY [1]=27;
00304     snakeY [2]=27;
00305     snakeY [3]=27;
00306     snakeY [4]=27;
00307 
00308 
00309     for (int i=0; i<5; i++) {
00310 
00311         lcd.drawRect(snakeX [i],snakeY [i],1,1,1); // snake is 2X2 pixels so draw rect with one width and 1 height
00312     }
00313 
00314 }
00315 
00316 
00317 void randomiseFood()
00318 {
00319 //   http://stackoverflow.com/questions/3383286/create-a-random-even-number-between-range
00320 
00321 
00322     srand(time(NULL)); // using time as a seed for rand
00323 
00324     int randomX = 2 * (1 + rand() % (40 - 1 + 1)) ; //generate random even number between 2 and 80 // these are the only coordinates the snake operates in
00325 
00326     int randomY = (2 * (4 + rand() % (22 - 4 + 1))) +1; // generate random even number between 8 and 44 then plus 1 for odd number in range of 9-45
00327 
00328 
00329     while(lcd.getPixel(randomX,randomY)==1) { // if that pixel is already filled keep generating till a empty space is found
00330 
00331         int randomX = 2 * (1 + rand() % (40 - 1 + 1)) ;
00332         int randomY = (2 * (4 + rand() % (22 - 4 + 1))) +1;
00333 
00334 
00335     }
00336 
00337     lcd.drawRect(randomX,randomY,1,1,1); // set the food to screen
00338 
00339     foodX [0]=randomX; // update food position
00340     foodY [0]=randomY;// update food position
00341     lcd.refresh();
00342 //serial.printf("%d",randomX);
00343 }
00344 
00345 void hardBoundary() // use these boundary conditions if gamemode is BOUNDARY
00346 {
00347 
00348 
00349     for(int x = 1; x< 83; x++) { // draw 1 to 82 at y=8
00350         lcd.setPixel(x,8);
00351     }
00352     for(int x = 1; x< 83; x++) { // draw 1 to 82 at y=47
00353         lcd.setPixel(x,47);
00354     }
00355     for(int y = 8; y< 48; y++) {// draw 8 tp 47 at x=1
00356         lcd.setPixel(1,y);
00357     }
00358     for(int y = 8; y< 48; y++) {// draw 8 to 47 at x =82
00359         lcd.setPixel(82,y);
00360     }
00361 
00362     lcd.refresh();
00363 
00364 }
00365 void classicBoundary() // use these boundaries if gamemode is classic
00366 {
00367 
00368 
00369     for(int x = 1; x< 83; x++) { // draw 1 to 82 at y=8
00370         lcd.setPixel(x,8);
00371     }
00372     for(int x = 1; x< 83; x+=2) { // draw 1 to 82 at y=47
00373         lcd.setPixel(x,47);
00374     }
00375     for(int y = 8; y< 48; y+=2) {// draw 8 tp 47 at x=1
00376         lcd.setPixel(1,y);
00377     }
00378     for(int y = 8; y< 48; y+=2) {// draw 8 to 47 at x =82
00379         lcd.setPixel(82,y);
00380     }
00381 
00382     lcd.refresh();
00383 
00384 }
00385 
00386 
00387 void updateSnakeVector()
00388 {
00389 // code to prevent snake moving in opposite direction to its current path of travel
00390     if (joystick.direction==LEFT && previousDirection==RIGHT) { // if snake is travelling right but joystick is left
00391         joystick.direction=RIGHT; // cary on right
00392     }
00393 
00394     if (joystick.direction==RIGHT && previousDirection==LEFT) { // if snake is travelling left but joystick is right
00395         joystick.direction=LEFT; //carry on left
00396     }
00397 
00398     if (joystick.direction==UP && previousDirection==DOWN) {// if snake is travelling down but joystick is up
00399         joystick.direction=DOWN; // carry on down
00400     }
00401 
00402     if (joystick.direction==DOWN && previousDirection==UP) { // if snake is travelling up but joystick is down
00403         joystick.direction=UP; // carry on up
00404     }
00405 
00406     if (joystick.direction==UNKNOWN || joystick.direction==CENTRE) { // if joystick is unknown or centred
00407         joystick.direction= previousDirection;  // carry on in previous direction
00408     }
00409 
00410 
00411     lcd.drawRect(snakeX [0],snakeY [0],1,1,2); // delete tail of snake by drawing a clear 2x2 block at first element in vectors
00412 
00413     for (int i =0; i<snakeX .size(); i++) {  // shift elements
00414         snakeX [i]=snakeX [i + 1]; // apart from head
00415         snakeY [i]=snakeY [i+ 1];
00416     }
00417 
00418 //code to set new head coordinates
00419     switch(joystick.direction) {
00420 
00421         case UP:  // if travelling up
00422             snakeX [snakeX .size()-1]=snakeX [snakeX .size()-2]; // snake new head x coordinate = previous head c coordinate
00423             snakeY [snakeY .size()-1]=snakeY [snakeY .size()-2]-2; // snake new head y cordinate = previous head y coordinate -2 as it moves up
00424 
00425             if (selectedGameMode==CLASSIC) { // when in classic mode no boundaries so need to check if new head is beyond edge limit
00426                 if(snakeY [snakeY .size()-1] <9) snakeY [snakeY .size()-1]=45; // when travelling up check head against upper edge
00427             }
00428             break;
00429 
00430         case DOWN:// if travelling down
00431             snakeX [snakeX .size()-1]=snakeX [snakeX .size()-2];   // snake new head x coordinate = previous head c coordinate
00432             snakeY [snakeY .size()-1]=snakeY [snakeY .size()-2]+2;// snake new head y cordinate = previous head y coordinate +2 as it moves down
00433 
00434             if (selectedGameMode==CLASSIC) { // when in classic mode no boundaries so need to check if new head is beyond edge limit
00435                 if(snakeY [snakeY .size()-1] >45) snakeY [snakeY .size()-1]=9; // check new head against bottom edge
00436             }
00437             break;
00438 
00439         case LEFT:
00440             snakeX [snakeX .size()-1]=snakeX [snakeX .size()-2]-2;  // snake new head x coordinate = previous head x coordinate - 2
00441             snakeY [snakeY .size()-1]=snakeY [snakeY .size()-2];// snake new head y coordinate = previous head y coordinate
00442 
00443             if (selectedGameMode==CLASSIC) { //  when in classic mode no boundaries so need to check if new head is beyond edge limit
00444                 if(snakeX [snakeX .size()-1] <2) snakeX [snakeX .size()-1]=80; // check head against left edge
00445             }
00446             break;
00447 
00448         case RIGHT:
00449             snakeX [snakeX .size()-1]= snakeX [snakeX .size()-2]+2; // snake new head x coordinate = previous head x coordinate + 2
00450             snakeY [snakeY .size()-1]=snakeY [snakeY .size()-2];// snake new head y coordinate = previous head y coordinate
00451 
00452             if (selectedGameMode==CLASSIC) { //  when in classic mode no boundaries so need to check if new head is beyond edge limit
00453                 if(snakeX [snakeX .size()-1] >80) snakeX [snakeX .size()-1]=2;
00454             }
00455 
00456             break;
00457 
00458             /* case CENTRE:
00459                  snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2]+2;
00460                  snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2];
00461                  break;
00462              case UNKNOWN:
00463                  snakeX[snakeX.size()-1]=snakeX[snakeX.size()-2]+2;
00464                  snakeY[snakeY.size()-1]=snakeY[snakeY.size()-2];
00465                  break;*/
00466 
00467     }
00468 }
00469 void gameOverTune() //
00470 {
00471 
00472 //   float frequency[]={440,659};
00473     //float beat[]={1,1,};
00474 
00475     Buzzer.period(1.0/(220)); // set PWM period 1/f  A
00476     Buzzer=0.5; // set duty cycle
00477     wait(0.5); // wait time
00478     Buzzer.period(1.0/(164)); // E
00479     wait(0.5);//
00480     Buzzer=0;// set duty cycle 0
00481 
00482     //   set PWM period
00483 
00484 }
00485 
00486 void eatFoodTune()
00487 {
00488     Buzzer.period(1.0/(440)); // set PWM period 1/f  A
00489     Buzzer=0.5; // set duty cycle
00490     wait(0.1); // wait time
00491     Buzzer=0;// set duty cycle 0
00492 }
00493 
00494 void gameOver()
00495 {
00496     leds=1;
00497     startGame.detach(); // stop snake game from updating
00498     lcd.drawRect(snakeX .back(),snakeY .back(),1,1,2); // highlight the point of game over
00499     wait(0.2);
00500     lcd.refresh();
00501     lcd.drawRect(snakeX .back(),snakeY .back(),1,1,1);
00502     wait(0.3);
00503     lcd.refresh();
00504     lcd.drawRect(snakeX .back(),snakeY .back(),1,1,2);
00505     wait(0.2);
00506     lcd.refresh();
00507     lcd.drawRect(snakeX .back(),snakeY .back(),1,1,1);
00508     wait(0.2);
00509     lcd.refresh();
00510     lcd.clear();
00511     lcd.inverseMode();
00512 
00513 
00514     lcd.printString("Your Score" ,12,0);// print score
00515     lcd.printString("=" ,34,1);
00516 
00517     
00518     int length = sprintf(buffer ,"%2d",score );
00519 
00520     if (length <= 14)  // if string will fit on display
00521         lcd.printString(buffer ,40,1);
00522 
00523     lcd.printString("Press Reset" ,2,3);
00524     lcd.printString("Button To" ,10,4);
00525     lcd.printString("Play Again" ,20,5);
00526 
00527     //
00528     lcd.refresh();
00529     gameOverTune();
00530 
00531     //gamePlaying=0;
00532 }
00533 
00534 void checkForCollision() // when in BOUNDARY MODE check snake head every move against walls
00535 {
00536 
00537     if (snakeX .back()==0|| snakeX .back()==82 || snakeY .back()==7 ||snakeY .back()>=47) {
00538         myleds=15;
00539 
00540         gameOver(); // if collision then end game
00541     }
00542 }
00543 
00544 void checkForFood() // check if snake has eaten food
00545 {
00546 
00547     if (snakeX .back()==foodX [0] && snakeY .back()==foodY [0]) {   // if  x and y of head match food
00548 
00549 
00550         switch(joystick.direction) {
00551 
00552             case RIGHT:
00553                 snakeX .insert (snakeX .begin() +0,foodX [0]-2 ); // insert new element to tail of snake vector
00554                 snakeY .insert (snakeY .begin() ,foodY [0]);
00555                 //snakeX.push_back(foodX[0]+2);
00556                 // snakeY.push_back(foodY[0]);
00557                 break;
00558             case LEFT:
00559 
00560                 snakeX .insert (snakeX .begin() +0,foodX [0]+2 );
00561                 snakeY .insert (snakeY .begin() ,foodY [0]);
00562 
00563                 //  snakeX.push_back(foodX[0]-2);
00564                 // snakeY.push_back(foodY[0]);
00565                 break;
00566 
00567 
00568             case UP:
00569 
00570                 snakeX .insert (snakeX .begin() +0,foodX [0] );
00571                 snakeY .insert (snakeY .begin() ,foodY [0]+2);
00572                 //  snakeX.push_back(foodX[0]);
00573                 // snakeY.push_back(foodY[0]-2);
00574                 break;
00575 
00576             case DOWN:
00577                 snakeX .insert (snakeX .begin() +0,foodX [0] );
00578                 snakeY .insert (snakeY .begin() ,foodY [0]-2);
00579                 // snakeX.push_back(foodX[0]);
00580                 // snakeY.push_back(foodY[0]+2);
00581                 break;
00582         }
00583         eatFoodTune();
00584         lcd.drawRect(snakeX [0],snakeY [0],1,1,1); // draw the new tail
00585 
00586      
00587        score +=5; // 5 points for every food eaten
00588         
00589         int length = sprintf(buffer ,"%2d",score );// print updated score to screen
00590 
00591         if (length <= 14)  // if string will fit on display
00592             lcd.printString(buffer ,0,0);
00593         // lcd.refresh();
00594 
00595         randomiseFood(); // randomise new food
00596 
00597     }
00598 
00599 
00600 }
00601 
00602 void startButtonPressed()
00603 {
00604     if (gameState ==0) { // when at first menu if pressed
00605         gameState =1; // move to second menu
00606     }
00607 
00608     else  if (gameState ==1) { // when at second menu
00609         gameState =2; // move to gameplay
00610     }
00611 
00612 }
00613 
00614 
00615 
00616 void updateBrightness()
00617 {
00618     float ain ;
00619     ain=pot.read();
00620     lcd.setBrightness(1-ain);
00621 
00622 }
00623 void updateGameISR()
00624 {
00625     updateGameFlag =1;
00626 }
00627 
00628 void printVectorContent()
00629 {
00630 
00631     //for( int i=0; i<snakeX.size(); i++)
00632     // serial.printf( "%d \n \r"  ,snakeX[i]);
00633 
00634 }
00635 
00636 
00637 
00638 void pauseButtonPressed()
00639 {
00640 
00641     if (gamePaused ==0) { // if game isnt already paused
00642 
00643         startGame.detach(); // stop game updating
00644 
00645         gamePaused =1; // update paused status
00646         leds=2; // show amber led
00647 
00648     }
00649 
00650     else { // if game is paused start game
00651         startGame.attach(&updateGameISR,gameSpeed );
00652 
00653         gamePaused =0;
00654     }
00655 
00656 
00657 
00658 }
00659 
00660 void checkForCollisionWithSelf(int i) // checks snake head with all other parts of snake
00661 {
00662     if(snakeX .back()==snakeX [i] && snakeY .back()==snakeY [i]) { // if both x and y coordinates of any part of snake match head
00663         gameOver(); // end game
00664     }
00665 
00666 
00667 }
00668 void startUpMenu()
00669 {
00670     lcd.inverseMode();
00671     easySelected();
00672     joystick.direction=UNKNOWN; // unknown so does not start scrolling
00673     calibrateJoystick();  // get centred values of joystick
00674 
00675 }
00676 
00677 void ModeMenu()
00678 {
00679     classicModeSelected();
00680     joystick.direction=UNKNOWN;// unknown so does not start scrolling
00681     calibrateJoystick();  // get centred values of joystick
00682 }
00683 
00684 void resetButtonPressed()
00685 {
00686 
00687     gameState =0; // when reset button pressed bring back to first menu screen
00688 }
00689 
00690 
00691 int main()
00692 {
00693     startButton.setSampleFrequency(); //for pindetect
00694     button.setSampleFrequency();// for pindetect
00695     lcd.init();
00696     resetButton.mode(PullDown); 
00697     startButton.mode(PullDown);
00698     button.mode(PullDown);
00699     startButton.attach_asserted( &startButtonPressed );
00700     resetButton.rise(&resetButtonPressed);
00701     button.attach_asserted(&pauseButtonPressed);
00702     displaySplash();
00703     leds=1;
00704     wait(4);
00705 int result = semihost_powerdown(); //USB POWERDOWN
00706  PHY_PowerDown(); //ETHERNET POWERDOWN
00707  
00708     while(1) {
00709         leds=1;// red light
00710         startUpMenu();
00711         while (gameState ==0) { // wait until user has selected difficulty
00712             updateJoystick();
00713             checkSelectedDifficulty();
00714             updateBrightness();
00715             //serial.printf("check difficulty loop");
00716 
00717             if (gameState ==1) { // initialise mode menu
00718                 ModeMenu();
00719             }
00720         }
00721 
00722 
00723         while(gameState ==1) { // wait unit user has pressed start button and selected a game mode
00724 
00725             updateJoystick();
00726             checkSelectedGameMode();
00727             updateBrightness();
00728 
00729         }
00730 
00731         if (gameState ==2) { // initialise game
00732 
00733             lcd.clear();
00734             lcd.normalMode(); // normal colours for gameplay
00735             if (selectedGameMode==BOUNDARY) hardBoundary(); // depending on game mode selcted set walls
00736             if (selectedGameMode==CLASSIC) classicBoundary();
00737             previousDirection=RIGHT; // snake has to be moving right when reset (reset bug fix)
00738             joystick.direction=RIGHT; // make sure when game reset that joystick is reset to right
00739             startingSnake(); // print starting snake
00740             randomiseFood(); // show random food
00741          score =5;// initial score is 5
00742         
00743             int length = sprintf(buffer ,"%2d",score );
00744             if (length <= 14)  // if string will fit on display
00745                 lcd.printString(buffer ,0,0);
00746           
00747             lcd.refresh();
00748             startGame.attach(&updateGameISR,gameSpeed ); // start game isr speed determined by difficulty chosen
00749 
00750             while (gameState ==2) { // while user doesnt press reset
00751 
00752                 //  serial.printf("enter game loop");
00753                 if(updateGameFlag ==1) { // check flag
00754                     leds=4; // green LED when game playing
00755 
00756                     updateGameFlag =0; // reset flag
00757                     updateJoystick(); // check joystick direction
00758 
00759                     updateSnakeVector(); // update the Vector
00760                     for (int i=0; i<snakeX .size(); i++) { // for all elements of snake draw 2x2 image
00761                         lcd.drawRect(snakeX [i],snakeY [i],1,1,1);
00762 
00763                     }
00764                     lcd.refresh();
00765                     previousDirection=joystick.direction; // update previous joystick direction
00766 
00767                     if (selectedGameMode==BOUNDARY) checkForCollision();// if in boundary mode check for a collision
00768 
00769                     checkForFood(); // check if snake has eaten food
00770 
00771                     for (int i=0; i<snakeX .size()-1; i++) {
00772                         checkForCollisionWithSelf(i);
00773                         updateBrightness();
00774 
00775                     }
00776 
00777                 }
00778                 sleep();
00779             }
00780         }
00781 
00782     }
00783 }