Calum Johnston / Mbed 2 deprecated BrickBreaker

Dependencies:   N5110 PinDetect PowerControl mbed

Revision:
11:d9d5bad8f5e2
Parent:
10:19c6aba3beec
--- a/BrickBreaker.cpp	Sun May 10 16:57:14 2015 +0000
+++ b/BrickBreaker.cpp	Mon May 11 20:32:56 2015 +0000
@@ -2,7 +2,6 @@
 *   @file BrickBreaker.cpp
 *
 *   @brief Function implementations
-*   @brief Credits to Craig A. Evans for his implementation of the 2-axis Joystick.
 *
 */
 
@@ -19,26 +18,24 @@
 
 int main() {
     
-    
+    //power saving
     PHY_PowerDown();
     int result = semihost_powerdown();
     
+    //initialise the lcd, joystick and level
     lcd.init();
     calibrateJoystick();
     initBricks(0);
     
-    readHighScores();
+    readHighScores();    //check the storage for saved scores
     
     pollJoystick.attach(&updateJoystick,1.0/10);  // read joystick 10 times per second
-    select.attach(&getMenuSelect,0.15);
-    
-    movePaddle.attach(&paddle,0.025);
+    select.attach(&getMenuSelect,0.15); // move the cursor every 0.15 seconds
+    movePaddle.attach(&paddle,0.025); //move the paddle every 0.025 seconds
     
-    initBricks(0);
-    
-    button.attach_asserted(&newScreen);
+    //attach functions to buttons
+    button.attach_asserted(&newScreen); 
     button.setSampleFrequency();
-    
     playPause.mode(PullUp);
     playPause.attach_asserted(&pause);
     playPause.setSampleFrequency();
@@ -73,16 +70,16 @@
 }
 
 void readHighScores() {
-    FILE *fp = fopen("/local/scores.txt","r");
-    fscanf(fp,"%i",&highscore1);
+    FILE *fp = fopen("/local/scores.txt","r"); //open scores.txt
+    fscanf(fp,"%i",&highscore1); //save the scores to each  score variable
     fscanf(fp,"%i",&highscore2);
     fscanf(fp,"%i",&highscore3);
     fclose(fp);
 }
 
 void writeHighScores() {
-    FILE *fp = fopen("/local/scores.txt","w");
-    fprintf(fp,"%i\r\n",highscore1);
+    FILE *fp = fopen("/local/scores.txt","w"); //open scores.txt in overwrite mode
+    fprintf(fp,"%i\r\n",highscore1);//write the high scores to the file
     fprintf(fp,"%i\r\n",highscore2);
     fprintf(fp,"%i\r\n",highscore3);
     fclose(fp);
@@ -90,10 +87,10 @@
 
 void newLevel()
 {
-    lcd.clear();
-    level++;
-    initBricks(level);
-    by = 30;
+    lcd.clear(); 
+    level++; //increment the level
+    initBricks(level); //initialise new level
+    by = 30; //reset the ball and paddle
     bx = 42;
     px = 38;
     d = 8;
@@ -111,36 +108,37 @@
 
 int getNumBricks() {
     int sum = 0;
-    for (int i = 0; i < 8; i++) {
+    for (int i = 0; i < 8; i++) { //loop through each brick
         for (int j = 0; j < 4; j++) {
             
-            sum+=bricks[j][i];
+            sum+=bricks[j][i]; //add one to the sum for each active brick
         }
     }
     return sum;
 }
 
 int lifeLost() {
-    if (by >= 48 && by <= 50) {
-        lives-=1;
-        by = 30;
+    if (by >= 48 && by <= 50) { //if the ball falls off the screen
+        lives-=1; //take away one life
+        by = 30; //reset the ball position
         bx = 42;
         px = 38;
         d = 8;
-        startStop = 0;
+        startStop = 0; // pause the game
         return 1;
     } else {
         return 0;
     }
 }
 
-void game(int g) {
-    if (g == 1) {
-    lifeLost();
-    borderInit();
-    dispScore();
-    dispLives();
-    ball();
+void game(int g)
+{
+    if (g == 1) { //if the game is running
+        lifeLost(); //do all these crucial game functions
+        borderInit();
+        dispScore();
+        dispLives();
+        ball();
     } else if (g == 0) {
     }
 }
@@ -219,24 +217,24 @@
     lcd.printString("score",10,1);
     lcd.printNum(score,42,1);
     lcd.printString(">menu",10,5);
-    if ((score >= highscore1) && scoreFlag) {
+    if ((score >= highscore1) && scoreFlag) { //if the score is bigger than the top score and it has not been updated already
         highscore3 = highscore2;
-        highscore2 = highscore1;
+        highscore2 = highscore1; //update all the scores
         highscore1 = score;
-        writeHighScores();
-        scoreFlag = 0;
-    } else if ((score >= highscore2) && scoreFlag) {
+        writeHighScores(); //write to file
+        scoreFlag = 0; //clear flag for checking the score
+    } else if ((score >= highscore2) && scoreFlag) { //repeat for 2nd score
         highscore3 = highscore2;
         highscore2 = score;
         writeHighScores();
         scoreFlag = 0;
-    } else if ((score >= highscore3) && scoreFlag) {
+    } else if ((score >= highscore3) && scoreFlag) { //repeat for 3rd score
         highscore3 = score;
         writeHighScores();
         scoreFlag = 0;
     }
     if (score >= highscore3) {
-        lcd.printString("NEW HIGH SCORE",0,3);
+        lcd.printString("NEW HIGH SCORE",0,3); //if any score has been beaten display message to user
     }        
     lcd.refresh();
     Sleep();
@@ -244,8 +242,8 @@
 
 void newScreen()
 {
-    if (displayMenu) {
-        switch (menuSelect) {
+    if (displayMenu) { //if on the main menu
+        switch (menuSelect) { // determine which screen to show
             case 1:
                 lcd.clear();
                 displayMenu = 0;
@@ -260,8 +258,8 @@
                 displayHighScores = 1;                
                 break;
         }
-    } else if (displayHelp) {
-        switch (helpSelect) {
+    } else if (displayHelp) { //if on the help screen
+        switch (helpSelect) { //determine which screen to show
             case 5:
                 displayMenu = 1;
                 displayHelp = 0;
@@ -273,10 +271,10 @@
                 }
                 break;
             }
-    } else if (displayHighScores) {
+    } else if (displayHighScores) { //etc
         displayMenu = 1;
         displayHighScores = 0;
-    } else if (displayGame) {
+    } else if (displayGame) { //if on the main game, use as a pause button instead
         startStop = !startStop;
     } else if (displayGameOver) {
         displayMenu = 1;
@@ -286,10 +284,10 @@
 
 void getMenuSelect()
 {
-    if (displayMenu) {
+    if (displayMenu) { //if on the menu
         
 
-        if (joystick.direction == DOWN) {
+        if (joystick.direction == DOWN) { //loop through 1 to 3
             lcd.clearRect(5,(menuSelect*8),5,8);
             if (menuSelect < 3) {
                 menuSelect++;
@@ -304,10 +302,10 @@
                 menuSelect = 3;
             }
         }
-    } else if (displayHelp) {
+    } else if (displayHelp) { //if on the help screen
         
         
-        if (joystick.direction == RIGHT) {
+        if (joystick.direction == RIGHT) { //switch between 5 and 35
             lcd.clearRect(helpSelect,40,5,8);
 
             if (helpSelect < 35) {
@@ -334,7 +332,7 @@
     lcd.printString("Help",10,2);
     lcd.printString("Scores",10,3);
     lcd.printString(">",5,menuSelect);
-    lives = 3;
+    lives = 3; //reset initial conditions
     score = 0;
     initBricks(0);
     startStop = 1;
@@ -346,23 +344,23 @@
 
 void dispLives() {
     
-    lcd.printNum(lives,77,5);
-    lcd.printChar(0x80,77,4);
+    lcd.printNum(lives,77,5); //print lives
+    lcd.printChar(0x80,77,4); //print heart icon
 }
 
 void dispScore() {
 
     
     if (score<10) {
-        lcd.printNum(score,77,0);
+        lcd.printNum(score,77,0); //print a single digit score
     } else if (score<100) {
         
-        lcd.printNum(((score/10)%10),77,0);
+        lcd.printNum(((score/10)%10),77,0); //print a 2-digit score top down
         lcd.printNum((score%10),77,1);
         
     } else if (score<1000) {
         
-        lcd.printNum(((score/100)%10),77,0);
+        lcd.printNum(((score/100)%10),77,0); //print a 3 digit score top down
         lcd.printNum(((score/10)%10),77,1);
         lcd.printNum((score%10),77,2);
         
@@ -371,50 +369,50 @@
 
 void doBricks() {
     
-    for (int x = 0; x<8; x++) {
+    for (int x = 0; x<8; x++) { //loop through all the bricks
         for (int y = 0; y<4; y++) {
-            getBrickTouch(x,y);
+            getBrickTouch(x,y); //checl if they have come into contact with anything
         }
     }
     
     for (int i = 0; i < 4; i++) {
         for (int j = 0; j < 8; j++) {
-            if (bricks[i][j] && brickDrawFlag[i][j]) {
-                lcd.drawRect(bricksx[j],bricksy[i], 6, 2, 1);
-                brickDrawFlag[i][j] = 0;
+            if (bricks[i][j] && brickDrawFlag[i][j]) { //if the bricks are set
+                lcd.drawRect(bricksx[j],bricksy[i], 6, 2, 1); //draw the bricks
+                brickDrawFlag[i][j] = 0; //clear the flag to avoid repeats
             }
         }
     }
     
-    clearBricks();
+    clearBricks(); //clear any bricks to be cleared
 }
 
 void initBricks(int l)
 {
 
-    int bricksTemp[4][8];
+    int bricksTemp[4][8]; //temporary array for random data
 
     if (l == 0) {
-        for (int i = 0; i < 4; i++) {
+        for (int i = 0; i < 4; i++) { //loop through all bricks
             for (int j = 0; j < 8; j++) {
-                bricks[i][j] = 1;
+                bricks[i][j] = 1; //set all bricks
                 brickDrawFlag[i][j] = 1;
             }
         }
     } else if (l == 1) {
-        for (int i = 0; i < 4; i++) {
+        for (int i = 0; i < 4; i++) { //loop through alternate columns
             for (int j = 0; j < 8; j+=2) {
-                bricks[i][j] = 1;
+                bricks[i][j] = 1; //set the bricks
                 brickDrawFlag[i][j] = 1;
             }
         }
     } else if (l > 1) {
-        for (int i = 0; i < 4; i++) {
+        for (int i = 0; i < 4; i++) { //loop through all the bricks
             for (int j = 0; j < 8; j++) {
-                int mint = ceil((noise.read() * 100000));
-                bricksTemp[i][j] = mint%100;
-                if (bricksTemp[i][j] > ((l*57)%73)) {
-                    bricks[i][j] = 1;
+                int mint = ceil((noise.read() * 100000)); //pull random noise and scale up
+                bricksTemp[i][j] = mint%100; //modulus 100 for ease of working
+                if (bricksTemp[i][j] > ((l*57)%73)) { //if the noise is greater than a certain value
+                    bricks[i][j] = 1; //set the brick
                     brickDrawFlag[i][j] = 1;
                 }
             }
@@ -423,12 +421,12 @@
 }
 
 void clearBricks() {
-    for (int i = 0; i < 8; i++) {
+    for (int i = 0; i < 8; i++) { //loop through all the bricks
         for (int j = 0; j < 4; j++) {
-            if (clearFlag[j][i]) {
-                lcd.clearRect(bricksx[i],bricksy[j],7,3);
+            if (clearFlag[j][i]) { //if the brick is set to be cleared
+                lcd.clearRect(bricksx[i],bricksy[j],7,3); //clear the brick
                 clearFlag[j][i] = 0;
-                score++;
+                score++; //increment the score
                 
             }
         }
@@ -440,12 +438,12 @@
 void getBrickTouch(int x, int y)
 {
 
-    if (bricks[y][x]) {
-        for (int a = -1; a < 8; a++) {
-            if (lcd.getPixel(bricksx[x]+a,bricksy[y]-1)) {
-                clearFlag[y][x] = 1;
-                bricks[y][x] = 0;
-            } else if (lcd.getPixel(bricksx[x]+a,bricksy[y]+3)) {
+    if (bricks[y][x]) { //if brick is set
+        for (int a = -1; a < 8; a++) { //loop around all the squares
+            if (lcd.getPixel(bricksx[x]+a,bricksy[y]-1)) { //if any pixel is set
+                clearFlag[y][x] = 1; //flag the brick for clearing
+                bricks[y][x] = 0; //clear the brick from the array
+            } else if (lcd.getPixel(bricksx[x]+a,bricksy[y]+3)) { //etc
                 clearFlag[y][x] = 1;
                 bricks[y][x] = 0;
             }
@@ -516,13 +514,13 @@
 //function to move the game paddle left and right with the joystick
 void paddle()
 {
-    if (!displayGame) return;
+    if (!displayGame) return; //don't run if the game isn't running
     
-    lcd.clearRect(1,py,74,3);
+    lcd.clearRect(1,py,74,3); //clear the paddle row
       
     if ((joystick.direction == RIGHT || joystick.direction == UPRIGHT || joystick.direction == DOWNRIGHT) && (px < 67)) {
-        px = px+1;
-        lcd.drawRect(px,py,pw,ph,1);
+        px = px+1; //move the paddle
+        lcd.drawRect(px,py,pw,ph,1); //redraw the paddle
     } else if ((joystick.direction == LEFT || joystick.direction == UPLEFT || joystick.direction == DOWNLEFT) && (px > 0)) {
         px = px-1;
         lcd.drawRect(px,py,pw,ph,1);
@@ -939,14 +937,14 @@
     setAngle(); //check what ball is touching get new angle
     doBricks(); //check what bricks are touching clear all necessary
     moveBall1(d); //move ball with new angle
-    wait_ms(35); //wait
+    wait_ms(32); //wait
 
-    if (d == e) {
+    if (d == e) { //if the direction is still the same
 
         setAngle();
         doBricks();
         moveBall2(d);
-        wait_ms(35);
+        wait_ms(32);
     }
 }
 
@@ -1114,20 +1112,18 @@
 void borderInit()
 {
 
-    if (!displayGame) return;
+    if (!displayGame) return; //don't run if the game isn't running
 
 
-    for (int i =0; i<=75; i++) {
+    for (int i =0; i<=75; i++) { //draw top edge
         lcd.setPixel(i,0);
     }
-    for (int i = 0; i<=48; i++) {
+    for (int i = 0; i<=48; i++) { //draw vertical lines
         lcd.setPixel(0,i);
         lcd.setPixel(75,i);
         lcd.setPixel(83,i);
     }
-    for (int i =75; i<84; i++) {
+    for (int i =75; i<84; i++) { //draw divider for game info
         lcd.setPixel(i,31);
     }
-    borderFlag = 1;
-
 }