Calum Johnston / Mbed 2 deprecated BrickBreaker

Dependencies:   N5110 PinDetect PowerControl mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BrickBreaker.cpp Source File

BrickBreaker.cpp

Go to the documentation of this file.
00001 /**
00002 *   @file BrickBreaker.cpp
00003 *
00004 *   @brief Function implementations
00005 *
00006 */
00007 
00008 
00009 #include "BrickBreaker.h"
00010 #include "mbed.h"
00011 
00012 
00013 
00014 // Initialise the N5110 screen
00015 //     VCC,SCE,RST,D/C,MOSI,SCLK,LED
00016 //       \   \  |   |   /    /   /
00017 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
00018 
00019 int main() {
00020     
00021     //power saving
00022     PHY_PowerDown();
00023     int result = semihost_powerdown();
00024     
00025     //initialise the lcd, joystick and level
00026     lcd.init();
00027     calibrateJoystick();
00028     initBricks(0);
00029     
00030     readHighScores();    //check the storage for saved scores
00031     
00032     pollJoystick.attach(&updateJoystick,1.0/10);  // read joystick 10 times per second
00033     select.attach(&getMenuSelect,0.15); // move the cursor every 0.15 seconds
00034     movePaddle.attach(&paddle,0.025); //move the paddle every 0.025 seconds
00035     
00036     //attach functions to buttons
00037     button.attach_asserted(&newScreen); 
00038     button.setSampleFrequency();
00039     playPause.mode(PullUp);
00040     playPause.attach_asserted(&pause);
00041     playPause.setSampleFrequency();
00042     
00043     while(1) {
00044 
00045         while(displayMenu == 1) {
00046             menu();
00047         }
00048         while(displayGame == 1) {
00049             game(startStop);
00050             if (lives < 0) {
00051                 displayGame = 0;
00052                 displayGameOver = 1;   
00053             }  
00054             if (getNumBricks() == 0) {
00055                 newLevel();
00056             }
00057         }
00058         while(displayHelp == 1) {
00059             help();
00060         }
00061         while(displayHighScores == 1) {
00062             highScores();
00063         }
00064         while(displayGameOver == 1) {
00065             gameOver();
00066         }
00067     lcd.refresh();
00068     
00069     }
00070 }
00071 
00072 void readHighScores() {
00073     FILE *fp = fopen("/local/scores.txt","r"); //open scores.txt
00074     fscanf(fp,"%i",&highscore1); //save the scores to each  score variable
00075     fscanf(fp,"%i",&highscore2);
00076     fscanf(fp,"%i",&highscore3);
00077     fclose(fp);
00078 }
00079 
00080 void writeHighScores() {
00081     FILE *fp = fopen("/local/scores.txt","w"); //open scores.txt in overwrite mode
00082     fprintf(fp,"%i\r\n",highscore1);//write the high scores to the file
00083     fprintf(fp,"%i\r\n",highscore2);
00084     fprintf(fp,"%i\r\n",highscore3);
00085     fclose(fp);
00086 }
00087 
00088 void newLevel()
00089 {
00090     lcd.clear(); 
00091     level++; //increment the level
00092     initBricks(level); //initialise new level
00093     by = 30; //reset the ball and paddle
00094     bx = 42;
00095     px = 38;
00096     d = 8;
00097     lcd.refresh();
00098 
00099     
00100 }
00101 
00102 void pause()
00103 {
00104     if (displayGame == 1) {
00105         startStop = !startStop;
00106     }
00107 }
00108 
00109 int getNumBricks() {
00110     int sum = 0;
00111     for (int i = 0; i < 8; i++) { //loop through each brick
00112         for (int j = 0; j < 4; j++) {
00113             
00114             sum+=bricks[j][i]; //add one to the sum for each active brick
00115         }
00116     }
00117     return sum;
00118 }
00119 
00120 int lifeLost() {
00121     if (by >= 48 && by <= 50) { //if the ball falls off the screen
00122         lives-=1; //take away one life
00123         by = 30; //reset the ball position
00124         bx = 42;
00125         px = 38;
00126         d = 8;
00127         startStop = 0; // pause the game
00128         return 1;
00129     } else {
00130         return 0;
00131     }
00132 }
00133 
00134 void game(int g)
00135 {
00136     if (g == 1) { //if the game is running
00137         lifeLost(); //do all these crucial game functions
00138         borderInit();
00139         dispScore();
00140         dispLives();
00141         ball();
00142     } else if (g == 0) {
00143     }
00144 }
00145 
00146 void highScores() {
00147     readHighScores();
00148     lcd.clear();
00149     lcd.printString("HIGH SCORES",10,0);
00150     lcd.printString("1/",10,1);
00151     lcd.printNum(highscore1,30,1);
00152     lcd.printString("2/",10,2);
00153     lcd.printNum(highscore2,30,2);
00154     lcd.printString("3/",10,3);
00155     lcd.printNum(highscore3,30,3);
00156     lcd.printString("menu",10,5);
00157     lcd.printString(">",5,5);
00158     lcd.refresh();
00159     Sleep();
00160 }
00161 
00162 void help()
00163 {
00164     //menuSelect = 35;
00165     if (helpScreen == 0) {
00166         lcd.clear();
00167         lcd.printString("HELP",10,0);
00168         lcd.printString("Destroy bricks",0,1);
00169         lcd.printString("to reach the",0,2);
00170         lcd.printString("next level",0,3);
00171         lcd.printString("more",40,5);
00172         lcd.printString("menu",10,5);
00173         lcd.printString(">",helpSelect,5);
00174         lcd.refresh();
00175         Sleep();        
00176     } else if (helpScreen == 1) {    
00177         lcd.clear();
00178         lcd.printString("HELP",10,0);
00179         lcd.printString("Use joystick",0,1);
00180         lcd.printString("to control",0,2);
00181         lcd.printString("the paddle",0,3);
00182         lcd.printString("more",40,5);
00183         lcd.printString("menu",10,5);
00184         lcd.printString(">",helpSelect,5);
00185         lcd.refresh();
00186         Sleep();
00187     } else if (helpScreen == 2) {
00188         lcd.clear();
00189         lcd.printString("HELP",10,0);
00190         lcd.printString("Pause the game",0,1);
00191         lcd.printString("using the >/||",0,2);
00192         lcd.printString("button",0,3);
00193         lcd.printString("more",40,5);
00194         lcd.printString("menu",10,5);
00195         lcd.printString(">",helpSelect,5);
00196         lcd.refresh();
00197         Sleep();       
00198     } else if (helpScreen == 3) {
00199         lcd.clear();
00200         lcd.printString("HELP",10,0);
00201         lcd.printString("Control volume",0,1);
00202         lcd.printString("using the blue",0,2);
00203         lcd.printString("potentiometer",0,3);
00204         lcd.printString("more",40,5);
00205         lcd.printString("menu",10,5);
00206         lcd.printString(">",helpSelect,5);
00207         lcd.refresh();
00208         Sleep();
00209     }
00210 }
00211 
00212 void gameOver() {
00213     
00214     
00215     lcd.clear();
00216     lcd.printString("GAME OVER",10,0);
00217     lcd.printString("score",10,1);
00218     lcd.printNum(score,42,1);
00219     lcd.printString(">menu",10,5);
00220     if ((score >= highscore1) && scoreFlag) { //if the score is bigger than the top score and it has not been updated already
00221         highscore3 = highscore2;
00222         highscore2 = highscore1; //update all the scores
00223         highscore1 = score;
00224         writeHighScores(); //write to file
00225         scoreFlag = 0; //clear flag for checking the score
00226     } else if ((score >= highscore2) && scoreFlag) { //repeat for 2nd score
00227         highscore3 = highscore2;
00228         highscore2 = score;
00229         writeHighScores();
00230         scoreFlag = 0;
00231     } else if ((score >= highscore3) && scoreFlag) { //repeat for 3rd score
00232         highscore3 = score;
00233         writeHighScores();
00234         scoreFlag = 0;
00235     }
00236     if (score >= highscore3) {
00237         lcd.printString("NEW HIGH SCORE",0,3); //if any score has been beaten display message to user
00238     }        
00239     lcd.refresh();
00240     Sleep();
00241 }
00242 
00243 void newScreen()
00244 {
00245     if (displayMenu) { //if on the main menu
00246         switch (menuSelect) { // determine which screen to show
00247             case 1:
00248                 lcd.clear();
00249                 displayMenu = 0;
00250                 displayGame = 1;
00251                 break;
00252             case 2:
00253                 displayMenu = 0;
00254                 displayHelp = 1;
00255                 break;
00256             case 3:
00257                 displayMenu = 0;
00258                 displayHighScores = 1;                
00259                 break;
00260         }
00261     } else if (displayHelp) { //if on the help screen
00262         switch (helpSelect) { //determine which screen to show
00263             case 5:
00264                 displayMenu = 1;
00265                 displayHelp = 0;
00266                 break;
00267             case 35:
00268                 helpScreen++;
00269                 if (helpScreen > 3) {
00270                     helpScreen = 0;
00271                 }
00272                 break;
00273             }
00274     } else if (displayHighScores) { //etc
00275         displayMenu = 1;
00276         displayHighScores = 0;
00277     } else if (displayGame) { //if on the main game, use as a pause button instead
00278         startStop = !startStop;
00279     } else if (displayGameOver) {
00280         displayMenu = 1;
00281         displayGameOver = 0;
00282     }
00283 }
00284 
00285 void getMenuSelect()
00286 {
00287     if (displayMenu) { //if on the menu
00288         
00289 
00290         if (joystick.direction == DOWN) { //loop through 1 to 3
00291             lcd.clearRect(5,(menuSelect*8),5,8);
00292             if (menuSelect < 3) {
00293                 menuSelect++;
00294             } else if (menuSelect == 3) {
00295                 menuSelect = 1;
00296             }
00297         } else if (joystick.direction == UP) {
00298             lcd.clearRect(5,(menuSelect*8),5,8);
00299             if (menuSelect > 1) {
00300                 menuSelect-=1;
00301             } else if (menuSelect == 1) {
00302                 menuSelect = 3;
00303             }
00304         }
00305     } else if (displayHelp) { //if on the help screen
00306         
00307         
00308         if (joystick.direction == RIGHT) { //switch between 5 and 35
00309             lcd.clearRect(helpSelect,40,5,8);
00310 
00311             if (helpSelect < 35) {
00312                 helpSelect+=30;
00313             } else if (menuSelect == 35) {
00314                 helpSelect = 5;
00315             }
00316         } else if (joystick.direction == LEFT) {
00317             lcd.clearRect(helpSelect,40,5,8);
00318 
00319             if (helpSelect > 5) {
00320                 helpSelect-=30;
00321             } else if (menuSelect == 5) {
00322                 helpSelect = 35;
00323             }     
00324         }       
00325     }
00326 }
00327 
00328 void menu() {
00329     lcd.clear();
00330     lcd.printString("BRICK BREAKER",0,0);
00331     lcd.printString("Start",10,1);   
00332     lcd.printString("Help",10,2);
00333     lcd.printString("Scores",10,3);
00334     lcd.printString(">",5,menuSelect);
00335     lives = 3; //reset initial conditions
00336     score = 0;
00337     initBricks(0);
00338     startStop = 1;
00339     level = 0;
00340     scoreFlag = 1;
00341     lcd.refresh();
00342     Sleep();
00343 }
00344 
00345 void dispLives() {
00346     
00347     lcd.printNum(lives,77,5); //print lives
00348     lcd.printChar(0x80,77,4); //print heart icon
00349 }
00350 
00351 void dispScore() {
00352 
00353     
00354     if (score<10) {
00355         lcd.printNum(score,77,0); //print a single digit score
00356     } else if (score<100) {
00357         
00358         lcd.printNum(((score/10)%10),77,0); //print a 2-digit score top down
00359         lcd.printNum((score%10),77,1);
00360         
00361     } else if (score<1000) {
00362         
00363         lcd.printNum(((score/100)%10),77,0); //print a 3 digit score top down
00364         lcd.printNum(((score/10)%10),77,1);
00365         lcd.printNum((score%10),77,2);
00366         
00367     }
00368 }
00369 
00370 void doBricks() {
00371     
00372     for (int x = 0; x<8; x++) { //loop through all the bricks
00373         for (int y = 0; y<4; y++) {
00374             getBrickTouch(x,y); //checl if they have come into contact with anything
00375         }
00376     }
00377     
00378     for (int i = 0; i < 4; i++) {
00379         for (int j = 0; j < 8; j++) {
00380             if (bricks[i][j] && brickDrawFlag[i][j]) { //if the bricks are set
00381                 lcd.drawRect(bricksx[j],bricksy[i], 6, 2, 1); //draw the bricks
00382                 brickDrawFlag[i][j] = 0; //clear the flag to avoid repeats
00383             }
00384         }
00385     }
00386     
00387     clearBricks(); //clear any bricks to be cleared
00388 }
00389 
00390 void initBricks(int l)
00391 {
00392 
00393     int bricksTemp[4][8]; //temporary array for random data
00394 
00395     if (l == 0) {
00396         for (int i = 0; i < 4; i++) { //loop through all bricks
00397             for (int j = 0; j < 8; j++) {
00398                 bricks[i][j] = 1; //set all bricks
00399                 brickDrawFlag[i][j] = 1;
00400             }
00401         }
00402     } else if (l == 1) {
00403         for (int i = 0; i < 4; i++) { //loop through alternate columns
00404             for (int j = 0; j < 8; j+=2) {
00405                 bricks[i][j] = 1; //set the bricks
00406                 brickDrawFlag[i][j] = 1;
00407             }
00408         }
00409     } else if (l > 1) {
00410         for (int i = 0; i < 4; i++) { //loop through all the bricks
00411             for (int j = 0; j < 8; j++) {
00412                 int mint = ceil((noise.read() * 100000)); //pull random noise and scale up
00413                 bricksTemp[i][j] = mint%100; //modulus 100 for ease of working
00414                 if (bricksTemp[i][j] > ((l*57)%73)) { //if the noise is greater than a certain value
00415                     bricks[i][j] = 1; //set the brick
00416                     brickDrawFlag[i][j] = 1;
00417                 }
00418             }
00419         }
00420     }
00421 }
00422 
00423 void clearBricks() {
00424     for (int i = 0; i < 8; i++) { //loop through all the bricks
00425         for (int j = 0; j < 4; j++) {
00426             if (clearFlag[j][i]) { //if the brick is set to be cleared
00427                 lcd.clearRect(bricksx[i],bricksy[j],7,3); //clear the brick
00428                 clearFlag[j][i] = 0;
00429                 score++; //increment the score
00430                 
00431             }
00432         }
00433     }
00434     
00435 }
00436 
00437 
00438 void getBrickTouch(int x, int y)
00439 {
00440 
00441     if (bricks[y][x]) { //if brick is set
00442         for (int a = -1; a < 8; a++) { //loop around all the squares
00443             if (lcd.getPixel(bricksx[x]+a,bricksy[y]-1)) { //if any pixel is set
00444                 clearFlag[y][x] = 1; //flag the brick for clearing
00445                 bricks[y][x] = 0; //clear the brick from the array
00446             } else if (lcd.getPixel(bricksx[x]+a,bricksy[y]+3)) { //etc
00447                 clearFlag[y][x] = 1;
00448                 bricks[y][x] = 0;
00449             }
00450         }
00451         for (int b = -1; b < 3; b++) {
00452             if (lcd.getPixel(bricksx[x]-1,bricksy[y]+b)) {
00453                 clearFlag[y][x] = 1;
00454                 bricks[y][x] = 0;
00455             } else if (lcd.getPixel(bricksx[x]+7,bricksy[y]+b)) {
00456                 clearFlag[y][x] = 1;
00457                 bricks[y][x] = 0;
00458             }
00459         }
00460     }
00461 }
00462 
00463  
00464 // read default positions of the joystick to calibrate later readings
00465 void calibrateJoystick()
00466 {
00467     button.mode(PullUp);
00468     // must not move during calibration
00469     joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
00470     joystick.y0 = yPot;
00471 }
00472 
00473 
00474 void updateJoystick()
00475 {
00476     if (startStop == 1) {
00477         
00478     
00479     // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
00480     joystick.x = xPot - joystick.x0;
00481     joystick.y = yPot - joystick.y0;
00482     // read button state
00483     
00484     // calculate direction depending on x,y values
00485     // tolerance allows a little lee-way in case joystick not exactly in the stated direction
00486     if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00487         joystick.direction = CENTRE;
00488     } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00489         joystick.direction = UP;
00490     } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
00491         joystick.direction = DOWN;
00492     } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00493         joystick.direction = RIGHT;
00494     } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
00495         joystick.direction = LEFT;
00496     } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
00497         joystick.direction = UPRIGHT;
00498     } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y > -1*DIRECTION_TOLERANCE) {
00499         joystick.direction = DOWNRIGHT;
00500     } else if ( joystick.y < DIRECTION_TOLERANCE && joystick.x < -1*DIRECTION_TOLERANCE) {
00501         joystick.direction = UPLEFT;
00502     } else if ( joystick.x < -1*DIRECTION_TOLERANCE && joystick.y > -1*DIRECTION_TOLERANCE) {
00503         joystick.direction = DOWNLEFT;
00504     } else {
00505         joystick.direction = UNKNOWN;
00506     }
00507     } else if (startStop == 0) {
00508         joystick.direction = CENTRE;
00509     }
00510 
00511 }
00512 
00513 
00514 //function to move the game paddle left and right with the joystick
00515 void paddle()
00516 {
00517     if (!displayGame) return; //don't run if the game isn't running
00518     
00519     lcd.clearRect(1,py,74,3); //clear the paddle row
00520       
00521     if ((joystick.direction == RIGHT || joystick.direction == UPRIGHT || joystick.direction == DOWNRIGHT) && (px < 67)) {
00522         px = px+1; //move the paddle
00523         lcd.drawRect(px,py,pw,ph,1); //redraw the paddle
00524     } else if ((joystick.direction == LEFT || joystick.direction == UPLEFT || joystick.direction == DOWNLEFT) && (px > 0)) {
00525         px = px-1;
00526         lcd.drawRect(px,py,pw,ph,1);
00527     } else {
00528         lcd.drawRect(px,py,pw,ph,1);  
00529     }
00530 
00531 }
00532 
00533 
00534 
00535 //set the touchFlag if any of the pixels touching the ball are set
00536 //store which pixels are set in an array
00537 void getTouchFlag()
00538 {
00539     if (lcd.getPixel(bx-1,by)) {//11
00540         touchFlag = 1;
00541         surround[11] = 1;
00542     } if (lcd.getPixel(bx-1,by-1)) {//0
00543         touchFlag = 1;
00544         surround[0] = 1;
00545     } if (lcd.getPixel(bx,by-1)) {//1
00546         touchFlag = 1;
00547         surround[1] = 1;
00548     } if (lcd.getPixel(bx+1,by-1)) {//2
00549         touchFlag = 1;
00550         surround[2] = 1;
00551     } if (lcd.getPixel(bx+2,by-1)) {//3
00552         touchFlag = 1;
00553         surround[3] = 1;
00554     } if (lcd.getPixel(bx+2,by)) {//4
00555         touchFlag = 1;
00556         surround[4] = 1;
00557     } if (lcd.getPixel(bx+2,by+1)) {//5
00558         touchFlag = 1;
00559         surround[5] = 1;
00560     } if (lcd.getPixel(bx+2,by+2)) {//6
00561         touchFlag = 1;
00562         surround[6] = 1;
00563     } if (lcd.getPixel(bx+1,by+2)) {//7
00564         touchFlag = 1;
00565         surround[7] = 1;
00566     } if (lcd.getPixel(bx,by+2)) {//8
00567         touchFlag = 1;
00568         surround[8] = 1;
00569     } if (lcd.getPixel(bx-1,by+2)) {//9
00570         touchFlag = 1;
00571         surround[9] = 1;
00572     } if (lcd.getPixel(bx-1,by+1)) {//10
00573         touchFlag = 1;
00574         surround[10] = 1;
00575     }
00576 }
00577 
00578 //work out what the ball has hit and calculate/set the new angle accordingly
00579 int setAngle()
00580 {
00581     getTouchFlag();
00582     
00583     if (touchFlag) {
00584         
00585         //FOR 3 CORNER SQUARES
00586         if (surround[11] && surround[0] && surround[1]) { //top right corner
00587             d = 6;
00588         } else if (surround[2] && surround[3] && surround[4]) { //bottom right corner
00589             d = 10;
00590         } else if (surround[5] && surround[6] && surround[7]) { //bottom left corner
00591             d = 14;
00592         } else if (surround[8] && surround[9] && surround[10]) { //top left corner
00593             d = 2;
00594         } 
00595         //FOR 3 TOUCHING OFFCENTRE EDGE SQUARES
00596         else if (surround[0] && surround[1] && surround[2] && !surround[3]) { //top edge left
00597             d = 5;
00598         } else if (surround[3] && surround[1] && surround[2] && !surround[0]) { //top edge right
00599             d = 11;
00600         } else if (surround[6] && surround[7] && surround[8] && !surround[9]) { // low edge right
00601             d = 13;
00602         } else if (surround[7] && surround[8] && surround[9] && !surround[6]) { //low edge left
00603             d = 3;
00604         } else if (surround[0] && surround[11] && surround[10] && !surround[9]) { //left edge top
00605             d = 5;
00606         } else if (surround[3] && surround[4] && surround[5] && !surround[6]) { //right edge top
00607             d = 11;
00608         } else if (surround[11] && surround[10] && surround[9] && !surround[0]) { //left edge low
00609             d = 3;
00610         } else if (surround[4] && surround[5] && surround[6] && !surround[3]) { //right edge low
00611             d = 13;
00612         }
00613         //FOR CENTRE SQUARES        
00614         else if (surround[1] && surround[2]) { //top edge
00615             switch (d) {
00616                 case 3:
00617                     d = 5;
00618                     break;
00619                 case 2:
00620                     d = 6;
00621                     break;
00622                 case 1:
00623                     d = 7;
00624                     break;
00625                 case 0:
00626                     d = 8;
00627                     break;
00628                 case 15:
00629                     d = 9;
00630                     break;
00631                 case 14:
00632                     d = 10;
00633                     break;
00634                 case 13:
00635                     d = 11;
00636                     break;
00637             }
00638 
00639         } else if (surround[4] && surround[5]) { //RHS edge
00640             switch (d) {
00641                 case 1:
00642                     d = 15;
00643                     break;
00644                 case 2:
00645                     d = 14;
00646                     break;
00647                 case 3:
00648                     d = 13;
00649                     break;
00650                 case 4:
00651                     d = 12;
00652                     break;
00653                 case 5:
00654                     d = 11;
00655                     break;
00656                 case 6:
00657                     d = 10;
00658                     break;
00659                 case 7:
00660                     d = 9;
00661                     break;
00662             }
00663 
00664         } else if (surround[10] && surround[11]) { //LHS edge
00665             switch (d) {
00666                 case 9:
00667                     d = 7;
00668                     break;
00669                 case 10:
00670                     d = 6;
00671                     break;
00672                 case 11:
00673                     d = 5;
00674                     break;
00675                 case 12:
00676                     d = 4;
00677                     break;
00678                 case 13:
00679                     d = 3;
00680                     break;
00681                 case 14:
00682                     d = 2;
00683                     break;
00684                 case 15:
00685                     d = 1;
00686                     break;
00687             }
00688         } else if (surround[7] && surround[8]) { //bottom edge
00689             switch (d) {
00690                 case 5:
00691                     d = 3;
00692                     break;
00693                 case 6:
00694                     d = 2;
00695                     break;
00696                 case 7:
00697                     d = 1;
00698                     break;
00699                 case 8:
00700                     d = 0;
00701                     break;
00702                 case 9:
00703                     d = 15;
00704                     break;
00705                 case 10:
00706                     d = 14;
00707                     break;
00708                 case 11:
00709                     d = 13;
00710                     break;
00711             }
00712         }
00713        
00714         //FOR 2 TOUCHING OFFCENTRE EDGE SQUARES
00715         else if (surround[0] && surround[1]) { //top edge left
00716             switch (d) {
00717                 case 0:
00718                     d = 7;
00719                     break;
00720                 case 1:
00721                     d = 6;
00722                     break;
00723                 case 2: 
00724                     d = 5;
00725                     break;
00726                 case 3:
00727                     d = 5;
00728                     break;
00729                 case 15:
00730                     d = 8;
00731                     break;
00732                 case 14:
00733                     d = 9;
00734                     break;
00735                 case 13:
00736                     d = 10;
00737                     break;
00738             }        
00739         } else if (surround[2] && surround[3]) { //top edge right
00740             switch (d) {
00741                 case 0:
00742                     d = 9;
00743                     break;
00744                 case 1:
00745                     d = 8;
00746                     break;
00747                 case 2: 
00748                     d = 7;
00749                     break;
00750                 case 3:
00751                     d = 6;
00752                     break;
00753                 case 15:
00754                     d = 10;
00755                     break;
00756                 case 14:
00757                     d = 11;
00758                     break;
00759                 case 13:
00760                     d = 11;
00761                     break;
00762             } 
00763         } else if (surround[9] && surround[8]) { // low edge right
00764             switch (d) {
00765                 case 11:
00766                     d = 14;
00767                     break;
00768                 case 10:
00769                     d = 15;
00770                     break;
00771                 case 9: 
00772                     d = 0;
00773                     break;
00774                 case 8:
00775                     d = 1;
00776                     break;
00777                 case 7:
00778                     d = 2;
00779                     break;
00780                 case 6:
00781                     d = 3;
00782                     break;
00783                 case 5:
00784                     d = 3;
00785                     break;
00786             } 
00787         } else if (surround[7] && surround[6]) { //low edge left
00788             switch (d) {
00789                 case 11:
00790                     d = 13;
00791                     break;
00792                 case 10:
00793                     d = 13;
00794                     break;
00795                 case 9: 
00796                     d = 14;
00797                     break;
00798                 case 8:
00799                     d = 15;
00800                     break;
00801                 case 7:
00802                     d = 0;
00803                     break;
00804                 case 6:
00805                     d = 1;
00806                     break;
00807                 case 5:
00808                     d = 2;
00809                     break;
00810             } 
00811         } else if (surround[0] && surround[11]) { //left edge top
00812             switch (d) {
00813                 case 9:
00814                     d = 8;
00815                     break;
00816                 case 10:
00817                     d = 7;
00818                     break;
00819                 case 11: 
00820                     d = 6;
00821                     break;
00822                 case 13:
00823                     d = 5;
00824                     break;
00825                 case 14:
00826                     d = 3;
00827                     break;
00828                 case 15:
00829                     d = 2;
00830                     break;
00831                 case 0:
00832                     d = 1;
00833                     break;                    
00834             } 
00835         } else if (surround[3] && surround[4]) { //right edge top
00836             switch (d) {
00837                 case 0:
00838                     d = 15;
00839                     break;
00840                 case 1:
00841                     d = 14;
00842                     break;
00843                 case 2: 
00844                     d = 13;
00845                     break;
00846                 case 3:
00847                     d = 11;
00848                     break;
00849                 case 5:
00850                     d = 10;
00851                     break;
00852                 case 6:
00853                     d = 9;
00854                     break;
00855                 case 7:
00856                     d = 8;
00857                     break;
00858             } 
00859         } else if (surround[10] && surround[9]) { //left edge low
00860             switch (d) {
00861                 case 8:
00862                     d = 7;
00863                     break;
00864                 case 9:
00865                     d = 6;
00866                     break;
00867                 case 10:
00868                     d = 5;
00869                     break;
00870                 case 11: 
00871                     d = 3;
00872                     break;
00873                 case 13:
00874                     d = 2;
00875                     break;
00876                 case 14:
00877                     d = 1;
00878                     break;
00879                 case 15:
00880                     d = 0;
00881                     break;
00882             } 
00883         } else if (surround[5] && surround[6]) { //right edge low
00884             switch (d) {
00885                 case 8:
00886                     d = 9;
00887                     break;
00888                 case 1:
00889                     d = 0;
00890                     break;
00891                 case 2: 
00892                     d = 15;
00893                     break;
00894                 case 3:
00895                     d = 14;
00896                     break;
00897                 case 5:
00898                     d = 13;
00899                     break;
00900                 case 6:
00901                     d = 11;
00902                     break;
00903                 case 7:
00904                     d = 10;
00905                     break;
00906             } 
00907         } 
00908         // FOR  1 TOUCHING CORNER SQUARE
00909         else if (surround[3]) { //top right
00910             d = 10;
00911         } else if (surround[6]) { // bottom right
00912             d = 14;
00913         } else if (surround[9]) { //bottom left
00914             d = 2;
00915         } else if (surround[0]) { //top left
00916             d = 6;
00917         } 
00918         
00919         
00920         
00921         touchFlag = 0; //clear the touch flag
00922         
00923         for (int i = 0; i<12; i++) { //clear the set points
00924             surround[i] = 0;
00925         }
00926     }
00927     
00928     return d;
00929 
00930 }
00931 
00932 void ball()
00933 
00934 {
00935     int e = d;
00936 
00937     setAngle(); //check what ball is touching get new angle
00938     doBricks(); //check what bricks are touching clear all necessary
00939     moveBall1(d); //move ball with new angle
00940     wait_ms(32); //wait
00941 
00942     if (d == e) { //if the direction is still the same
00943 
00944         setAngle();
00945         doBricks();
00946         moveBall2(d);
00947         wait_ms(32);
00948     }
00949 }
00950 
00951 void moveBall1(int d)   //move the ball, quantised into 16 directions of motion
00952 {
00953     lcd.clearRect(bx,by,bw+1,bh+1);
00954 
00955     if (d == 0) { //0deg (vertical up)
00956         by-=1;
00957         lcd.drawRect(bx,by,bw,bh,bf);
00958         lcd.refresh();
00959     } else if (d == 1) { //22.5deg
00960         by-=1;
00961         bx+=1;
00962         lcd.drawRect(bx,by,bw,bh,bf);
00963         lcd.refresh();
00964     } else if (d == 2) { //45deg
00965         by-=1;
00966         bx+=1;
00967         lcd.drawRect(bx,by,bw,bh,bf);
00968         lcd.refresh();
00969     } else if (d == 3) { //62.5deg
00970         by-=1;
00971         bx+=1;
00972         lcd.drawRect(bx,by,bw,bh,bf);
00973         lcd.refresh();
00974     } else if (d == 4) { //90deg (right)
00975         bx+=1;
00976         lcd.drawRect(bx,by,bw,bh,bf);
00977         lcd.refresh();
00978     } else if (d == 5) { //112.5deg
00979         bx+=1;
00980         by+=1;
00981         lcd.drawRect(bx,by,bw,bh,bf);
00982         lcd.refresh();
00983     } else if (d == 6) { //135deg
00984         bx+=1;
00985         by+=1;
00986         lcd.drawRect(bx,by,bw,bh,bf);
00987         lcd.refresh();
00988     } else if (d == 7) { //157.5deg
00989         bx+=1;
00990         by+=1;
00991         lcd.drawRect(bx,by,bw,bh,bf);
00992         lcd.refresh();
00993     } else if (d == 8) { //180deg (vertical down)
00994         by++;
00995         lcd.drawRect(bx,by,bw,bh,bf);
00996         lcd.refresh();
00997     } else if (d == 9) { //202.5deg
00998         by++;
00999         bx-=1;
01000         lcd.drawRect(bx,by,bw,bh,bf);
01001         lcd.refresh();
01002     } else if (d == 10) { //225deg
01003         by+=1;
01004         bx-=1;
01005         lcd.drawRect(bx,by,bw,bh,bf);
01006         lcd.refresh();
01007     } else if (d == 11) { //247.5deg
01008         by+=1;
01009         bx-=1;
01010         lcd.drawRect(bx,by,bw,bh,bf);
01011         lcd.refresh();
01012     } else if (d == 12) { //270deg (left)
01013         bx-=1;
01014         lcd.drawRect(bx,by,bw,bh,bf);
01015         lcd.refresh();
01016     } else if (d == 13) { //292.5deg
01017         bx-=1;
01018         by-=1;
01019         lcd.drawRect(bx,by,bw,bh,bf);
01020         lcd.refresh();
01021     } else if (d == 14) { //315deg
01022         bx-=1;
01023         by-=1;
01024         lcd.drawRect(bx,by,bw,bh,bf);
01025         lcd.refresh();
01026     } else if (d == 15) { //337.5deg
01027         bx-=1;
01028         by-=1;
01029         lcd.drawRect(bx,by,bw,bh,bf);
01030         lcd.refresh();
01031     }
01032 }
01033 
01034 
01035 void moveBall2(int d)   //move the ball, quantised into 16 directions of motion
01036 {
01037     lcd.clearRect(bx,by,bw+1,bh+1);
01038     
01039     if (d == 0) { //0deg (vertical up)
01040         by-=1;
01041         lcd.drawRect(bx,by,bw,bh,bf);
01042         lcd.refresh();
01043     } else if (d == 1) { //22.5deg
01044         by-=1;
01045         lcd.drawRect(bx,by,bw,bh,bf);
01046         lcd.refresh();
01047     } else if (d == 2) { //45deg
01048         by-=1;
01049         bx+=1;
01050         lcd.drawRect(bx,by,bw,bh,bf);
01051         lcd.refresh();
01052     } else if (d == 3) { //62.5deg
01053         bx+=1;
01054         lcd.drawRect(bx,by,bw,bh,bf);
01055         lcd.refresh();
01056     } else if (d == 4) { //90deg (right)
01057         bx+=1;
01058         lcd.drawRect(bx,by,bw,bh,bf);
01059         lcd.refresh();
01060     } else if (d == 5) { //112.5deg
01061         bx+=1;
01062         lcd.drawRect(bx,by,bw,bh,bf);
01063         lcd.refresh();
01064     } else if (d == 6) { //135deg
01065         bx+=1;
01066         by+=1;
01067         lcd.drawRect(bx,by,bw,bh,bf);
01068         lcd.refresh();
01069     } else if (d == 7) { //157.5deg
01070         by+=1;
01071         lcd.drawRect(bx,by,bw,bh,bf);
01072         lcd.refresh();
01073     } else if (d == 8) { //180deg (vertical down)
01074         by++;
01075         lcd.drawRect(bx,by,bw,bh,bf);
01076         lcd.refresh();
01077     } else if (d == 9) { //202.5deg
01078         by++;
01079         lcd.drawRect(bx,by,bw,bh,bf);
01080         lcd.refresh();
01081     } else if (d == 10) { //225deg
01082         by+=1;
01083         bx-=1;
01084         lcd.drawRect(bx,by,bw,bh,bf);
01085         lcd.refresh();
01086     } else if (d == 11) { //247.5deg
01087         bx-=1;
01088         lcd.drawRect(bx,by,bw,bh,bf);
01089         lcd.refresh();
01090     } else if (d == 12) { //270deg (left)
01091         bx-=1;
01092         lcd.drawRect(bx,by,bw,bh,bf);
01093         lcd.refresh();
01094     } else if (d == 13) { //292.5deg
01095         bx-=1;
01096         lcd.drawRect(bx,by,bw,bh,bf);
01097         lcd.refresh();
01098     } else if (d == 14) { //315deg
01099         bx-=1;
01100         by-=1;
01101         lcd.drawRect(bx,by,bw,bh,bf);
01102         lcd.refresh();
01103     } else if (d == 15) { //337.5deg
01104         by-=1;
01105         lcd.drawRect(bx,by,bw,bh,bf);
01106         lcd.refresh();
01107     }
01108 }
01109 
01110 
01111 //set pixels around the edge of the board
01112 void borderInit()
01113 {
01114 
01115     if (!displayGame) return; //don't run if the game isn't running
01116 
01117 
01118     for (int i =0; i<=75; i++) { //draw top edge
01119         lcd.setPixel(i,0);
01120     }
01121     for (int i = 0; i<=48; i++) { //draw vertical lines
01122         lcd.setPixel(0,i);
01123         lcd.setPixel(75,i);
01124         lcd.setPixel(83,i);
01125     }
01126     for (int i =75; i<84; i++) { //draw divider for game info
01127         lcd.setPixel(i,31);
01128     }
01129 }