Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Revision:
7:cd3cafda3dd4
Parent:
6:39bda45efeed
Child:
8:9b77eea95088
--- a/main.cpp	Mon Apr 08 09:14:33 2019 +0000
+++ b/main.cpp	Wed Apr 10 09:18:25 2019 +0000
@@ -5,7 +5,8 @@
 Username: el17kec
 Student ID Number:201171978 Date:11/03/2019
 */
-// Breakout++ logo sprite
+
+// Breakout logo sprite
 const int breakout[10][75] =   {
     { 1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0 },
     { 1,0,1,0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1 },
@@ -44,15 +45,13 @@
 ///////////// functions ////////////////
 int main() 
 {   
-    init();
+    init();     //initialise LCD and Gamepad
     welcome();  // waiting for the user to start
-    
-    render();
+    render();   //game loop
 }
 
 void init() 
 {
-  // need to initialise LCD and Gamepad
   lcd.init();
   pad.init();
   lcd.setContrast(0.55); // setting contrast to 55% seems good
@@ -80,6 +79,61 @@
     }
 }
 
+
+void resetGame()
+{
+    ball.reset();
+    cont.reset();
+    map.reset();
+}
+
+void checkLost()
+{
+    Vector2D posPad = cont.get_padPos();
+    if (ball.getPos().y > posPad.y) 
+    {
+        while (1) 
+        {
+            lcd.clear();
+            lcd.printString("You Lost",17,2);
+            lcd.printString("Press A",20,3);
+            lcd.printString("to restart",12,4);
+            wait(0.1);
+            lcd.refresh();
+            if (pad.check_event(Gamepad::A_PRESSED) == true)
+            {
+                resetGame();
+                break;           
+            }
+        }
+    }
+}
+
+void checkWon() {
+    if (map.checkLevel()) {
+        // we have cleared a level
+        ball.reset();
+        cont.reset();
+        
+        if (map.hasWon()) {
+             while (1) 
+            {
+                lcd.clear();
+                lcd.printString("You WON!",17,2);
+                lcd.printString("Press A",20,3);
+                lcd.printString("to restart",12,4);
+                wait(0.1);
+                lcd.refresh();
+                if (pad.check_event(Gamepad::A_PRESSED) == true)
+                {
+                    resetGame();
+                    break;           
+                }
+            }
+        }
+    }
+}
+
 void render() 
 {
     map.initBricks();
@@ -88,12 +142,13 @@
     {   
         // physics, input + game logic
         cont.controlPlayer(pad); 
-        cont.get_padPos(pad); 
-        ball.getballPos();
+        cont.get_padPos(); 
+        ball.getPos();
         ball.move();
-        ball.hitPad(pad, cont);
+        ball.hitPad(cont);
         map.checkCollision(ball);
-        ball.endCondition(pad, lcd, cont);
+        checkLost();
+        checkWon();
         
         // rendering
         lcd.clear(); // initialise the LCD