James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Revision:
93:18f81996ea89
Parent:
92:2e6d88e44f56
Child:
94:8fa117200f6b
--- a/BreakoutEngine/BreakoutEngine.cpp	Mon May 06 23:03:22 2019 +0000
+++ b/BreakoutEngine/BreakoutEngine.cpp	Mon May 06 23:31:30 2019 +0000
@@ -14,23 +14,24 @@
 {
     // initialise the game parameters
     _paddle_width = paddle_width;
-    _paddle_height = paddle_height;
-    _ball_size = ball_size;
-    _speed = speed;
-    _number_left = 18;
-    _index = 0;
-    _cool_time = 0.0f;
-    _score = 0;
-    _prev_score = 0;
-    _multiplier = 1;
+    _paddle_height = paddle_height; 
+    _ball_size = ball_size; // size of the ball
+    _speed = speed; // speed of the ball
+    _number_left = 18; // number of bricks that remain on screen
+    _index = 0;  // index of which laser to move on screen at any time, starts at 0
+    _cool_time = 0.0f; // time remaining before laser can be fired again, starts at 0
+    _score = 0; // initialised as 0
+    _prev_score = 0;  // initialised as 0 for the first round
+    _multiplier = 1;  // initialised as 1 for the first round
 
-    // y position on screen - WIDTH is defined in N5110.h
+    // y position of the paddle on screen - HEIGHT is defined in N5110.h
     _paddley = HEIGHT - GAP - 1;
 
-    // puts paddles and ball in middle
+    // initialises paddles and ball in middle
     _paddle.init(_paddley,_paddle_height,_paddle_width);
-    _ball.init(_ball_size,_speed,_paddle.get_pos().x + 7);
-
+    _ball.init(_ball_size,_speed,_paddle.get_pos().x + (PADDLE_WIDTH/2));
+    
+    // initialises all the bricks in their set x/y positions
     _brick11.init(3,GAP_TOP+1,3);
     _brick12.init(16,GAP_TOP+1,3);
     _brick13.init(29,GAP_TOP+1,3);
@@ -51,7 +52,8 @@
     _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);
-
+    
+    // appends all bricks to a list so that they are easier to check for collisions
     listofBricks.push_back(_brick11);
     listofBricks.push_back(_brick12);
     listofBricks.push_back(_brick13);
@@ -70,36 +72,39 @@
     listofBricks.push_back(_brick34);
     listofBricks.push_back(_brick35);
     listofBricks.push_back(_brick36);
-
-    _laser1.init(-10);
-    _laser2.init(-10);
-    _laser3.init(-10);
-
+    
+    // initialises lasers off screen
+    _laser1.init();
+    _laser2.init();
+    _laser3.init();
+    
+    // appends lasers to a list so that they are easier to check for collisions
     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
+void BreakoutEngine::reset_game()  // resets the game after victory screen
 {
-    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;
+    reset_num_left();   // resets _number_left to 18
+    _paddle.recentre();  // recentres the paddle on screen
+    _ball.init(_ball_size,_speed + _multiplier/2, _paddle.get_pos().x + (PADDLE_WIDTH/2)); // resets the ball position to above the paddle, and increases its speed depending on the _multiplier
+    
+    int pointer = 0;  // tracks the rows, if 6 bricks are placed, it moves to the next row
     for (it_R = listofBricks.begin(); it_R != listofBricks.end(); ++it_R) {
-        if (pointer <= 5) {
+        if (pointer <= 5) {  // 6 bricks in the first row
             it_R -> set_posx((pointer * 13) + 3);
         } else if (pointer <= 11) {
-            it_R -> set_posx(((pointer-6) * 13) + 3);
+            it_R -> set_posx(((pointer-6) * 13) + 3);  // pointer - 6 to offset it back to the first column of bricks
         } else if (pointer <= 17) {
-            it_R -> set_posx(((pointer-12) * 13) + 3);
+            it_R -> set_posx(((pointer-12) * 13) + 3);  // pointer - 12 to offset it back to the first column of bricks
         }
 
-        it_R -> reset_lives(_multiplier);
-        pointer ++;
+        it_R -> reset_lives(_multiplier);  // increases the bricks number of lives by the multiplier + their _base_lives
+        pointer ++;  // increment the pointer
     }
     for (it_L = listofLasers.begin(); it_L != listofLasers.end(); ++it_L) {
-        it_L -> set_posx(-10);
+        it_L -> set_posx(-10);  // moves all the lasers off screen
     }
 }
 
@@ -173,7 +178,6 @@
 
 }
 
-
 void BreakoutEngine::update(Gamepad &pad)
 {
     check_loss(pad);
@@ -239,11 +243,6 @@
     }
 }
 
-int BreakoutEngine::get_lives()
-{
-    return _paddle.get_lives();
-}
-
 void BreakoutEngine::check_wall_collisions(Gamepad &pad)
 {
     // read current ball attributes
@@ -404,7 +403,7 @@
         _paddle.lose_life();
         //lose_screen(); // go to loss screen then initialise again
 
-        _ball.init(_ball_size,_speed+_multiplier/2,_paddle.get_pos().x + 7);
+        _ball.init(_ball_size,_speed+_multiplier/2,_paddle.get_pos().x + (PADDLE_WIDTH/2));
         pad.tone(1500.0,0.5);
         return true;
     } else {
@@ -426,43 +425,56 @@
     lcd.printString(buffer1,WIDTH/2 -2,0);  // font is 8 wide, so leave 4 pixel gap from middle assuming two digits
 }
 
+int BreakoutEngine::get_lives()
+{
+    return _paddle.get_lives();
+}
 
 int BreakoutEngine::get_prev_score()
 {
     return _prev_score;
 }
+
 void BreakoutEngine::set_prev_score(int prev_score)
 {
     _prev_score = prev_score;
 }
+
 int BreakoutEngine::get_num_left()
 {
     return _number_left;
 }
+
 void BreakoutEngine::dec_num_left()
 {
     _number_left -= 1;
 }
+
 void BreakoutEngine::reset_num_left()
 {
     _number_left = 18;
 }
+
 int BreakoutEngine::get_score()
 {
     return _score;
 }
+
 void BreakoutEngine::inc_mult()
 {
     _multiplier ++;
 }
+
 void BreakoutEngine::set_mult_zero()
 {
     _multiplier = 0;
 }
+
 void BreakoutEngine::inc_index()
 {
     _index ++;
 }
+
 void BreakoutEngine::reset_index()
 {
     _index = 0;