Balls & Paddle game for RETRO Pong inspired game featuring multi-directional tilt-sensitive paddle, multiple balls, shrinking ceiling and a bit of gravity.

Dependencies:   LCD_ST7735 MusicEngine RETRO_BallsAndThings mbed

Balls and Paddle

After doing some work on the Pong mod I decided to put my efforts into making my version object oriented and try to make a generic object-library that could be use for other ball-and-things games. To add some challenges to the gameplay, the following features were added:

  • extra-free additional balls to please the juglers
  • gravity for pulling the ball down to create some dynamic movement
  • directional power-paddle that counters the ball with a bit more speed
  • lowering ceiling to make endless gameplay impossible

Files at this revision

API Documentation at this revision

Comitter:
maxint
Date:
Wed Feb 25 10:43:41 2015 +0000
Parent:
2:0caa0607f7bf
Child:
4:8643d2d93a5f
Commit message:
experimenting with better bouncing

Changed in this revision

Game.cpp Show annotated file Show diff for this revision Revisions of this file
Game.h Show annotated file Show diff for this revision Revisions of this file
RETRO_BallsAndThings.lib Show annotated file Show diff for this revision Revisions of this file
--- a/Game.cpp	Mon Feb 09 08:40:03 2015 +0000
+++ b/Game.cpp	Wed Feb 25 10:43:41 2015 +0000
@@ -83,7 +83,7 @@
 }
 
 void Game::printf(int x, int y, const char *szFormat, ...)
-{
+{   // formats: %s, %d, %0.2f
     char szBuffer[256];
     va_list args;
 
@@ -248,8 +248,11 @@
     Rectangle rRight=Rectangle(WIDTH, 0, WIDTH+10, HEIGHT);        // right wall
     Rectangle rPaddle=Rectangle(paddle.pos.getX(), paddle.pos.getY(), paddle.pos.getX() + Game::PADDLE_WIDTH, HEIGHT+10);        // paddle
     Rectangle rPaddleLeft=Rectangle(paddle.pos.getX(), paddle.pos.getY(), paddle.pos.getX() + Game::PADDLE_WIDTH/3, HEIGHT+10);      // paddle left part
+    Rectangle rPaddleMiddle=Rectangle(paddle.pos.getX() + Game::PADDLE_WIDTH/3, paddle.pos.getY(), paddle.pos.getX() + Game::PADDLE_WIDTH/3 + Game::PADDLE_WIDTH/3, HEIGHT+10);      // paddle middle part
     Rectangle rPaddleRight=Rectangle(paddle.pos.getX()+ Game::PADDLE_WIDTH/3 + Game::PADDLE_WIDTH/3, paddle.pos.getY(), paddle.pos.getX() + Game::PADDLE_WIDTH, HEIGHT+10);      // paddle right part
 
+    //printf(0, 20, "Paddle: %d-%d  %d-%d  ", rPaddleLeft.getX1(), rPaddleLeft.getX2(), rPaddleRight.getX1(), rPaddleRight.getX2());
+
     Ball* pBall;
     for(int i=0; i<NUM_BALLS; i++)
     {
@@ -278,20 +281,43 @@
         {
             if(pBall->collides(rPaddleLeft))   pBall->vSpeed.add(Vector(-1.3,0));      // left side of paddle has bias to the left
             if(pBall->collides(rPaddleRight))  pBall->vSpeed.add(Vector(1.3,0));       // right side of paddle has bias to the right
-    
-            // bounce the ball
-            // increase the speed of the ball when hitting the paddle to increase difficulty
-            float ftSpeedMax=3.0;
-            if(this->nScore>50)
-                ftSpeedMax=5.0;
-            if(this->nScore>100)
-                ftSpeedMax=10.0;
-            if(this->nScore>150)
-                ftSpeedMax=999.0;
-            if(pBall->vSpeed.getSize()<ftSpeedMax)
-                pBall->Bounce(Vector(1,-1.02));        // bounce from paddle at higher speed
-            else
-                pBall->Bounce(Vector(1,-1));        // bounce vertical at same speed
+            pBall->Bounce(Vector(1,-1));
+/*
+            if(pBall->collides(rPaddleMiddle))
+            {
+                pBall->Bounce(Vector(1,-1));
+//                printf(10, 10, "Mid: %0.2f, %0.2f  ", pBall->vSpeed.x, pBall->vSpeed.y);
+            }
+            else if(pBall->collides(rPaddleLeft))
+            {
+                Vector vDirection(-1,-2);
+                pBall->Bounce(vDirection.getNormalized());
+                //pBall->BounceAgainst(Vector(12,-4));      // left side of paddle has bias to the left
+//                printf(10, 10, "Left: %0.2f, %0.2f  ", pBall->vSpeed.x, pBall->vSpeed.y);
+            }
+            else if(pBall->collides(rPaddleRight))
+            {
+                Vector vDirection(1,-2);
+                pBall->Bounce(vDirection.getNormalized());
+                //pBall->BounceAgainst(Vector(12,4));       // right side of paddle has bias to the right
+//                printf(10, 10, "Right: %0.2f, %0.2f  ", pBall->vSpeed.x, pBall->vSpeed.y);
+            }
+*/
+            
+            {
+                // increase the speed of the ball when hitting the paddle to increase difficulty
+                float ftSpeedMax=3.0;
+                if(this->nScore>50)
+                    ftSpeedMax=5.0;
+                if(this->nScore>100)
+                    ftSpeedMax=10.0;
+                if(this->nScore>150)
+                    ftSpeedMax=999.0;
+                if(pBall->vSpeed.getSize()<ftSpeedMax)
+                    pBall->vSpeed.multiply(Vector(1,1.02));        // bounce up from paddle at higher speed
+            }
+            //printf(10, 10, "Bounce: %0.2f, %0.2f  ", pBall->vSpeed.x, pBall->vSpeed.y);
+
     
             // force drawing the paddle after redrawing the bounced ball
             this->fDrawPaddle=true;
--- a/Game.h	Mon Feb 09 08:40:03 2015 +0000
+++ b/Game.h	Wed Feb 25 10:43:41 2015 +0000
@@ -26,7 +26,7 @@
     
     //static const int BALL_RADIUS = 3;
     static const int BALL_RADIUS = 6;
-    static const int PADDLE_WIDTH = 38;
+    static const int PADDLE_WIDTH = 39;
     static const int PADDLE_HEIGHT = 4;
     static const int PADDLE_SPEED = 4;
     static const char I2C_ADDR = 0x1C << 1;
--- a/RETRO_BallsAndThings.lib	Mon Feb 09 08:40:03 2015 +0000
+++ b/RETRO_BallsAndThings.lib	Wed Feb 25 10:43:41 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/maxint/code/RETRO_BallsAndThings/#71185a0aadfc
+http://developer.mbed.org/users/maxint/code/RETRO_BallsAndThings/#74bc9b16fb88