James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Revision:
91:c01a736fb0d9
Parent:
86:01f33d94e496
Child:
92:2e6d88e44f56
--- a/BreakoutEngine/BreakoutEngine.cpp	Mon May 06 19:24:32 2019 +0000
+++ b/BreakoutEngine/BreakoutEngine.cpp	Mon May 06 22:57:04 2019 +0000
@@ -1,5 +1,5 @@
 #include "BreakoutEngine.h"
-    
+
 BreakoutEngine::BreakoutEngine()
 {
 
@@ -23,36 +23,36 @@
     _score = 0;
     _prev_score = 0;
     _multiplier = 1;
-    
+
     // y position on screen - WIDTH is defined in N5110.h
     _paddley = HEIGHT - GAP - 1;
 
     // puts paddles and ball in middle
     _paddle.init(_paddley,_paddle_height,_paddle_width);
     _ball.init(_ball_size,_speed,_paddle.get_pos().x + 7);
-    
-    _brick11.init(3,GAP_TOP+1,3);                           
+
+    _brick11.init(3,GAP_TOP+1,3);
     _brick12.init(16,GAP_TOP+1,3);
     _brick13.init(29,GAP_TOP+1,3);
     _brick14.init(42,GAP_TOP+1,3);
     _brick15.init(55,GAP_TOP+1,3);
     _brick16.init(68,GAP_TOP+1,3);
-    
-    _brick21.init(3,GAP_TOP+BRICK_HEIGHT+2,2);                              
+
+    _brick21.init(3,GAP_TOP+BRICK_HEIGHT+2,2);
     _brick22.init(16,GAP_TOP+BRICK_HEIGHT+2,2);
     _brick23.init(29,GAP_TOP+BRICK_HEIGHT+2,2);
     _brick24.init(42,GAP_TOP+BRICK_HEIGHT+2,2);
     _brick25.init(55,GAP_TOP+BRICK_HEIGHT+2,2);
     _brick26.init(68,GAP_TOP+BRICK_HEIGHT+2,2);
-    
-    _brick31.init(3,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);                              
+
+    _brick31.init(3,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
     _brick32.init(16,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
     _brick33.init(29,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
     _brick34.init(42,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
     _brick35.init(55,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
     _brick36.init(68,GAP_TOP+1+((BRICK_HEIGHT+1)*2),1);
-    
-    listofBricks.push_back(_brick11);  
+
+    listofBricks.push_back(_brick11);
     listofBricks.push_back(_brick12);
     listofBricks.push_back(_brick13);
     listofBricks.push_back(_brick14);
@@ -70,61 +70,58 @@
     listofBricks.push_back(_brick34);
     listofBricks.push_back(_brick35);
     listofBricks.push_back(_brick36);
-    
+
     _laser1.init(-10);
     _laser2.init(-10);
     _laser3.init(-10);
-    
+
     listofLasers.push_back(_laser1);
     listofLasers.push_back(_laser2);
     listofLasers.push_back(_laser3);
 }
 
 void BreakoutEngine::reset_game()  // rename to reset add in ball reset and use to increase the number of balls each time
-{    
+{
     reset_num_left();
     _ball.init(_ball_size,_speed + _multiplier/2, _paddle.get_pos().x + 7); // replace the 1 with a multiplier private variable that tracks the number of times continued from victory
     _paddle.recentre();
     int pointer = 0;
-    for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R){
-        if (pointer <= 5) {   
+    for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
+        if (pointer <= 5) {
             it_R -> set_posx((pointer * 13) + 3);
-        }
-        else if (pointer <= 11) {   
+        } else if (pointer <= 11) {
             it_R -> set_posx(((pointer-6) * 13) + 3);
-        }
-        else if (pointer <= 17) {   
+        } else if (pointer <= 17) {
             it_R -> set_posx(((pointer-12) * 13) + 3);
         }
-        
-        it_R -> reset_lives(_multiplier);   
+
+        it_R -> reset_lives(_multiplier);
         pointer ++;
     }
-    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){
+    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
         it_L -> set_posx(-10);
     }
 }
-    
+
 
 void BreakoutEngine::read_input(Gamepad &pad, bool tilt, float sens)
 {
     _d = pad.get_direction();
     _mag = pad.get_mag();
-    
+
     if (tilt == true) {
         _paddle.set_tilt();
-    }
-    else {
+    } else {
         _paddle.set_joy();
     }
-    
+
     _paddle.set_sens(sens);
-    
+
     if (pad.check_event(Gamepad::B_PRESSED) && _cool_time <= 0) {  // max of 3 lasers on screen at once
-        
+
         Vector2D p_pos = _paddle.get_pos();
         it_L = listofLasers.begin();
-        switch(_index){
+        switch(_index) {
             case 0:
                 advance(it_L, 0);
                 it_L -> set_posx(p_pos.x+7);
@@ -144,21 +141,22 @@
                 reset_index();
                 break;
         }
-        
-        
+
+
         _cool_time = 0.75f;
-    }
-    else {
+    } else {
         _cool_time -= 0.125; // 1/8 as fps is 8
     }
-    
+
 }
 
-void BreakoutEngine::inc_index() {
+void BreakoutEngine::inc_index()
+{
     _index ++;
 }
 
-void BreakoutEngine::reset_index() {
+void BreakoutEngine::reset_index()
+{
     _index = 0;
 }
 
@@ -173,13 +171,13 @@
     _paddle.draw(lcd);
     // ball
     _ball.draw(lcd);
-    
+
     //print_scores(lcd);
-    
-    for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R){
+
+    for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
         it_R->draw(lcd);
     }
-    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){
+    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
         it_L->draw(lcd);
     }
 
@@ -193,19 +191,19 @@
     // correct for it before updating the display
     _paddle.update(_d,_mag);
     _ball.update();
-    
-    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){
+
+    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
         it_L->update();
     }
-    
+
     lives_leds(pad);
-    
+
     check_wall_collisions(pad);
     check_paddle_collisions(pad);
     check_brick_collisions(pad);
     check_laser_collisions(pad);
-    
-    
+
+
 }
 
 void BreakoutEngine::lives_leds(Gamepad &pad)
@@ -213,20 +211,20 @@
     if (_paddle.get_lives() == 0) {
         pad.leds_off();
     }
-    
+
     else if (_paddle.get_lives() == 1) {
         //turn leftmost led on
         pad.leds_off();
         pad.led(1,1);
     }
-    
+
     else if (_paddle.get_lives() == 2) {
         //turn leftmost led on
         pad.leds_off();
         pad.led(1,1);
         pad.led(2,1);
     }
-    
+
     else if (_paddle.get_lives() == 3) {
         //turn leftmost led on
         pad.leds_off();
@@ -240,18 +238,19 @@
         pad.led(5,0);
         pad.led(6,0);
     }
-    
+
     else if (_paddle.get_lives() == 5) {
         pad.leds_on();
         pad.led(6,0);
     }
-    
+
     else if (_paddle.get_lives() == 6) {
         pad.leds_on();
     }
 }
 
-int BreakoutEngine::get_lives() {
+int BreakoutEngine::get_lives()
+{
     return _paddle.get_lives();
 }
 
@@ -267,14 +266,14 @@
         // audio feedback
         pad.tone(750.0,0.1);
     }
-    
+
     else if (ball_pos.x <= 1) {  //  1 due to 1 pixel boundary
         ball_pos.x = 1;  // bounce off ceiling without going off screen
         ball_velocity.x = -ball_velocity.x;
         // audio feedback
         pad.tone(750.0,0.1);
     }
-        
+
     // check if hit bottom wall
     else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
         // hit bottom
@@ -287,16 +286,16 @@
     // update ball parameters
     _ball.set_velocity(ball_velocity);
     _ball.set_pos(ball_pos);
-    
-     for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){
+
+    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
         if (
             (it_L -> get_y() <= GAP_TOP - 2)
         ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
-            // if it has, fix position and reflect x velocity  
+            // if it has, fix position and reflect x velocity
             it_L -> set_posx(-10);
         }
     }
-    
+
 }
 
 void BreakoutEngine::check_paddle_collisions(Gamepad &pad)
@@ -310,16 +309,16 @@
 
     // see if ball has hit the paddle by checking for overlaps
     if (
-        (ball_pos.x >= p1_pos.x) && //left 
+        (ball_pos.x >= p1_pos.x) && //left
         (ball_pos.x <= p1_pos.x + _paddle_width) && //right
         (ball_pos.y >= _paddley) && //bottom
         (ball_pos.y <= _paddley + _paddle_height)  //top
     ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
-        // if it has, fix position and reflect x velocity  
+        // if it has, fix position and reflect x velocity
         pad.tone(1000.0,0.1);
         ball_pos.y = _paddley + _paddle_height - 1;
         ball_velocity.y = -ball_velocity.y;
-    
+
 //        if (ball_pos.x == p1_pos.x + PADDLE_WIDTH/2) {     // check ballxpos in relation to paddle xpos. translate the distance from the centre to an angle between 30 and 60 degrees in that direction
 //            ball_pos.y = _paddley + _paddle_height - 1;
 //            ball_velocity.y = -ball_velocity.y;
@@ -329,17 +328,17 @@
 //            if (ball_velocity.x > 0) {
 //                ball_velocity.x = -ball_velocity.x;
 //            }
- //           ball_pos.y = _paddley + _paddle_heigh - 1;
- //           ball_velocity.y = -tan(ang);
- //       }
- //       else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
- //           float ang = 40*(((p1_pos.x + PADDLE_WIDTH/2)-ball_pos.x)/(PADDLE_WIDTH/2 - p1_pos.x)) + 30;  //converts the distance from the centre to an angle between 30 and 60
- //           if (ball_velocity.x < 0) {
- //               ball_velocity.x = -ball_velocity.x;
- //           }
- //           ball_pos.y = _paddley + _paddle_height - 1;
- //           ball_velocity.y = -tan(ang);
- //       }
+//           ball_pos.y = _paddley + _paddle_heigh - 1;
+//           ball_velocity.y = -tan(ang);
+//       }
+//       else if (ball_pos.x >= p1_pos.x + PADDLE_WIDTH/2) {
+//           float ang = 40*(((p1_pos.x + PADDLE_WIDTH/2)-ball_pos.x)/(PADDLE_WIDTH/2 - p1_pos.x)) + 30;  //converts the distance from the centre to an angle between 30 and 60
+//           if (ball_velocity.x < 0) {
+//               ball_velocity.x = -ball_velocity.x;
+//           }
+//           ball_pos.y = _paddley + _paddle_height - 1;
+//           ball_velocity.y = -tan(ang);
+//       }
     }
 
     // write new attributes
@@ -352,21 +351,18 @@
     // read current ball attributes
     Vector2D ball_pos = _ball.get_pos();
     Vector2D ball_velocity = _ball.get_velocity();
-    
-    for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R){
+
+    for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
         if (
             (ball_pos.x >= it_R -> get_x()) && //left
             (ball_pos.x <= it_R -> get_x() + BRICK_WIDTH) && //right
             (ball_pos.y >= it_R -> get_y()) && //bottom
             (ball_pos.y <= it_R -> get_y() + BRICK_HEIGHT)  //top
         ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
-            // if it has, fix position and reflect x velocity  
-            if (ball_velocity.y < 0) 
-            {   
+            // if it has, fix position and reflect x velocity
+            if (ball_velocity.y < 0) {
                 ball_pos.y = it_R -> get_y() + BRICK_HEIGHT;
-            }
-            else 
-            {
+            } else {
                 ball_pos.y = it_R -> get_y();
             }
             ball_velocity.y = -ball_velocity.y;
@@ -389,15 +385,15 @@
 {
     // read current ball attributes
     // check p1 first
-    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L){
-        for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R){
+    for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
+        for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
             if (
                 (it_L -> get_x() >= it_R -> get_x()) && //left
                 (it_L -> get_x() <= it_R -> get_x() + BRICK_WIDTH) && //right
                 (it_L -> get_y() >= it_R -> get_y()) && //bottom
                 (it_L -> get_y()<= it_R -> get_y() + BRICK_HEIGHT)  //top
             ) {    // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
-                // if it has, fix position and reflect x velocity  
+                // if it has, fix position and reflect x velocity
                 it_L -> set_posx(-10);
                 // audio feedback
                 pad.tone(1000.0,0.1);
@@ -417,12 +413,11 @@
     if (ball_pos.y > HEIGHT) {
         _paddle.lose_life();
         //lose_screen(); // go to loss screen then initialise again
-        
+
         _ball.init(_ball_size,_speed+_multiplier/2,_paddle.get_pos().x + 7);
         pad.tone(1500.0,0.5);
         return true;
-    }
-    else {
+    } else {
         return false;
     }
 }
@@ -431,38 +426,46 @@
 {
     // get scores from paddles
     int score = _prev_score + (18 - get_num_left())*100 * _multiplier;     //maybe add a previous score so the score carries over through victory screens. add hi score fix so restart restarts from 321
-                                // also, add a multiplier of 2 for every contiue on victory
-    _score = score;                            
-    
+    // also, add a multiplier of 2 for every contiue on victory
+    _score = score;
+
     // print to LCD i
     char buffer1[14];
     sprintf(buffer1,"%2d",score);
-    lcd.printString("SCORE: ",2 ,0);
+    lcd.printString("SCORE: ",2,0);
     lcd.printString(buffer1,WIDTH/2 -2,0);  // font is 8 wide, so leave 4 pixel gap from middle assuming two digits
 }
 
 
-int BreakoutEngine::get_prev_score(){
+int BreakoutEngine::get_prev_score()
+{
     return _prev_score;
 }
-void BreakoutEngine::set_prev_score(int prev_score){
+void BreakoutEngine::set_prev_score(int prev_score)
+{
     _prev_score = prev_score;
 }
-int BreakoutEngine::get_num_left(){
+int BreakoutEngine::get_num_left()
+{
     return _number_left;
 }
-void BreakoutEngine::one_less() {
+void BreakoutEngine::one_less()
+{
     _number_left -= 1;
 }
-void BreakoutEngine::reset_num_left() {
+void BreakoutEngine::reset_num_left()
+{
     _number_left = 18;
 }
-int BreakoutEngine::get_score() {  
+int BreakoutEngine::get_score()
+{
     return _score;
 }
-void BreakoutEngine::inc_mult() {
+void BreakoutEngine::inc_mult()
+{
     _multiplier ++;
 }
-void BreakoutEngine::set_mult_zero() {
+void BreakoutEngine::set_mult_zero()
+{
     _multiplier = 0;
 }