Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Revision:
4:0e01cbb95434
Parent:
3:fe856d0890ee
Child:
5:12c179da4788
--- a/Ball/Ball.cpp	Sat Mar 23 11:17:33 2019 +0000
+++ b/Ball/Ball.cpp	Sat Mar 23 12:07:33 2019 +0000
@@ -1,6 +1,8 @@
 #include "Ball.h"
 #include "PlayerControl.h"
 
+PlayerControl pl;
+
 int g_xBall = WIDTH/2; // draw ball in the middle of the columns initially
 int g_yBall = HEIGHT - GAP - 2; // draw ball close to the bottom of the LCD
 int g_counterx = 1;
@@ -20,7 +22,7 @@
 
 void Ball::drawBall(N5110 &lcd) 
 {
-    lcd.drawRect(g_xBall,g_yBall,2,2,FILL_BLACK);
+    lcd.drawRect(g_xBall,g_yBall,1,1,FILL_BLACK);
 }
 
 void Ball::moveBall() 
@@ -29,7 +31,7 @@
     g_xBall += g_counterx;
     g_yBall -= g_countery;
     
-    if (g_xBall > 82) 
+    if (g_xBall > 83) 
     {   
         g_counterx = -1;
     }
@@ -41,7 +43,7 @@
     {   
         g_countery = -1;
     }
-    else if (g_yBall > 46) 
+    else if (g_yBall > 47) 
     {
         g_countery = 1;
     }
@@ -49,10 +51,9 @@
 
 void Ball::hitPad(Gamepad &pad) 
 {
-    PlayerControl pl;
     Vector2D posBall = get_ballPos(pad);
     Vector2D posPad = pl.get_padPos(pad);
-    if (posBall.y == posPad.y - 2 && (posBall.x >= posPad.x && posBall.x <= posPad.x + 12))
+    if (posBall.y == posPad.y - 1 && (posBall.x >= posPad.x && posBall.x <= posPad.x + 12))
     {
         g_countery = !g_countery;
         printf("\nball x = %f, ball y = %f \n pad x = %f, pad y = %f ",posBall.x, posBall.y, posPad.x, posPad.y);
@@ -67,14 +68,36 @@
     return posBall;
 }
 
-bool Ball::endCondition(Gamepad &pad)
+void Ball::endCondition(Gamepad &pad, N5110 &lcd)
 {
-    PlayerControl pl;
     Vector2D posBall = get_ballPos(pad);
     Vector2D posPad = pl.get_padPos(pad);
     if (posBall.y > posPad.y) 
     {
-        return true;
+        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;           
+            }
+        }
     }
-    else return false;
+}
+
+void Ball::resetGame()
+{
+    g_xBall = WIDTH/2;
+    g_yBall = HEIGHT - GAP - 2;
+    g_counterx = 1;
+    g_countery = 1;
+    g_xBall += g_counterx;
+    g_yBall -= g_countery; 
+    pl.padReset();
 }
\ No newline at end of file