Retro game that let's the player steer a ball through a hole filled maze. Has multiple levels of increasing difficulty.

Dependencies:   LCD_ST7735 MusicEngine RETRO_BallsAndThings mbed

Ball and Holes

In this game I attempted to create somewhat natural movement of the ball by implementing gravity and friction which combined over time determine the speed of the ball. Playing with the settings (aka. the magic numbers) that are spread out all over game.cpp, gives different effects, such as an icy, rough or liquid-like surface.

It took some time to figure out how to post my very first youtube video. Sorry for the shaky recording. Trying to record the video with my phone while playing the game in one hand was quite challenging, but here it is;

The left and right buttons are used to cheat: restart the current or go to the next level. Up and down control the game-tick. During game-play the robot-button shows the accelerator graph and the ship-button mutes the sound.

BTW. If your ball happens to get stuck, tilting the console in the opposite direction will set it free. For sake of argument: these magnetic wall-ends are in the words of Bob Ross "a happy accident". Since there is no specific code for it, others might call it a bug. As it results in more interesting game-play, I didn't attempt to fix it, but left a comment for those who dare to look at the mess I call code.

Revision:
5:67e75a4f0b52
Parent:
4:4a5f33d845d9
Child:
6:e2eead4e53fb
--- a/Game.cpp	Wed Feb 04 13:30:29 2015 +0000
+++ b/Game.cpp	Thu Feb 05 14:41:21 2015 +0000
@@ -39,6 +39,8 @@
     //this->aBalls[2]={ Ball(&disp),  Ball(&disp) };
     for(int i=0; i<NUM_BALLS; i++)
         this->aBalls[i]=Ball(&(this->disp));
+
+    this->snd.reset();
 }
 
 void Game::printDouble(double value, int x, int y)
@@ -53,23 +55,18 @@
 {
     this->disp.clearScreen();
 
-//    this->initializeBall();     // start first ball
-//    this->initializeBalls();     // start first ball
-
-    this->setNoBalls();     // reset all balls
-    this->newBall();     // start first ball
-    this->initializePaddle();
-    //this->paddle.draw();
-
-       
-//    this->paddleX = WIDTH / 2 - Game::PADDLE_WIDTH / 2;
     this->snd.reset();
     this->nBalls = 4;
     this->nScore = 0;
     this->nTopWall = 8;
+    this->fDrawTopWall=true;
     
-    this->tWait.start();      // start the timer
-    
+//    this->tWait.start();      // start the timer
+
+    this->initializePaddle();
+    this->setNoBalls();     // reset all balls
+    this->newBall();     // start first ball
+    this->snd.play("T240 L16 O5 D E F");
 }
 
 
@@ -77,7 +74,6 @@
 {
     this->paddle.initialize(WIDTH / 2 - Game::PADDLE_WIDTH/2, HEIGHT - Game::PADDLE_HEIGHT, Game::PADDLE_WIDTH, Game::PADDLE_HEIGHT);
     this->fDrawPaddle=true;
-    //this->paddle.draw();
 }
 
 
@@ -119,7 +115,7 @@
             continue;
         else
         {
-            this->aBalls[i].initialize(WIDTH / 2 - Game::BALL_RADIUS, HEIGHT / 4 - Game::BALL_RADIUS, Game::BALL_RADIUS, Color565::fromRGB(i==0?0xFF:0x33, i==1?0xFF:0x33, i==2?0xFF:0x33));
+            this->aBalls[i].initialize(WIDTH / 2 - Game::BALL_RADIUS, this->nTopWall + (HEIGHT-this->nTopWall) / 4 - Game::BALL_RADIUS, Game::BALL_RADIUS, Color565::fromRGB(i==0?0xFF:0x33, i==1?0xFF:0x33, i==2?0xFF:0x33));
             //float ftRandX=rand() % 2 ? 1 : -1;
             //float ftRandY=rand() % 2 ? 1 : -1;
             //this->aBalls[i].setSpeed(ftRandX, ftRandY);
@@ -186,25 +182,17 @@
         }
 */
 
-/*
-        if(this->ball.vSpeed.getSize() != 0)            // TODO: added if statement to allow zero speed pause of ball
-            this->ball.vSpeed.add(this->vGravity);    // add some gravity
-            //this->ball.vSpeed.add(Vector(0, 0.1));    // add some gravity
-        this->ball.update();                    // update the ball position 
-*/
-
         this->updateBalls();                    // update the ball positions
-
         this->updatePaddle();
     
-//        this->checkCollision();
         this->checkPaddle();
         this->checkBallsCollision();
-//        this->ball.redraw();
+
         this->redrawBalls();
         this->redrawPaddle();
+        this->redrawTopWall();
         
-        this->snd.checkPwm();
+//        this->snd.checkPwm();
         //this->checkScore(); 
         this->checkBalls(); 
         
@@ -302,14 +290,14 @@
 void Game::showSplashScreen() {
     this->drawString(Game::SPLASH_1, HEIGHT / 2 - CHAR_HEIGHT / 2);  
     this->drawString(Game::SPLASH_2, HEIGHT / 2 + CHAR_HEIGHT / 2); 
-    this->drawString(Game::SPLASH_3, HEIGHT / 2 + CHAR_HEIGHT / 2 + 2*(8)); 
+    this->drawString(Game::SPLASH_3, HEIGHT / 2 + CHAR_HEIGHT / 2 + 2*CHAR_HEIGHT); 
            
     while (this->circle.read())
     {
 int i=this->checkTilt();
 char buf[256];
 sprintf(buf,"  tilt:%d  ", i);
-this->drawString(buf, HEIGHT / 2 - CHAR_HEIGHT / 2 + (4*CHAR_HEIGHT) ); 
+this->drawString(buf, HEIGHT / 2 + CHAR_HEIGHT / 2 + (4*CHAR_HEIGHT) ); 
 
         wait_ms(1);
     }
@@ -330,7 +318,6 @@
     Rectangle rPaddle=Rectangle(paddle.pos.getX(), paddle.pos.getY(), paddle.pos.getX() + Game::PADDLE_WIDTH, HEIGHT+10);       // Rectangle(this->paddleX, HEIGHT - Game::PADDLE_HEIGHT, this->paddleX + Game::PADDLE_WIDTH, HEIGHT);       // paddle
     Rectangle rPaddleLeft=Rectangle(paddle.pos.getX(), paddle.pos.getY(), paddle.pos.getX() + Game::PADDLE_WIDTH/3, HEIGHT+10);      // paddle left 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
-    //Rectangle rScreen=Rectangle(0,0, WIDTH, HEIGHT);            // screen boundary
 
     Ball* pBall;
     for(int i=0; i<NUM_BALLS; i++)
@@ -344,6 +331,7 @@
         {
             pBall->Bounce(Vector(1,-1));        // bounce vertical
             this->snd.beepShort();
+            this->fDrawTopWall=true;
         }
         if(pBall->collides(rRight) && pBall->vSpeed.isRight())      // right wall
         {
@@ -357,12 +345,11 @@
         }
         if(pBall->collides(rPaddle) && pBall->vSpeed.isDown())      // paddle
         {
-            if(pBall->collides(rPaddleLeft))   pBall->vSpeed.add(Vector(-1,0));       // left side of paddle has bias to the left
-            if(pBall->collides(rPaddleRight))  pBall->vSpeed.add(Vector(1,0));       // right side of paddle has bias to the right
+            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
-            //pBall->Bounce(Vector(1,-1));        // bounce vertical at same speed
             float ftSpeedMax=3.0;
             if(this->nScore>50)
                 ftSpeedMax=5.0;
@@ -370,7 +357,7 @@
                 ftSpeedMax=10.0;
             if(this->nScore>150)
                 ftSpeedMax=999.0;
-            if(pBall->vSpeed.getSize()<ftSpeedMax)            // TODO: added if statement to allow zero speed pause of ball
+            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
@@ -388,14 +375,16 @@
             {
                 this->newBall();
                 this->nBalls++;
+                this->snd.play("T240 L16 O5 D E F");
             }
 
-
             // lower the ceiling every 25 points
             if(this->nScore>0 && this->nScore%25==0)
             {
-                this->nTopWall+=2;
-                this->disp.fillRect(0, 8, WIDTH, this->nTopWall, Color565::Purple);
+                this->nTopWall+=3;
+                this->fDrawTopWall=true;
+                //this->snd.play("T240 L32 O5 GFEDC");
+                this->snd.play("T240 L16 O5 CDEFG");
             }
 
         }
@@ -406,7 +395,6 @@
             pBall->vSpeed.set(0,0);
             pBall->fActive=false;
             this->nBalls--;
-            //this->initializeBall();     // start a new ball
             if(countBalls()==0)
             {
                 this->newBall();     // start a new ball
@@ -416,6 +404,17 @@
     }
 }
 
+void Game::redrawTopWall()
+{
+    if(this->fDrawTopWall)
+    {
+        int nTop=max(this->nTopWall-2, 8);
+        this->disp.fillRect(0, 8, WIDTH, nTop, Color565::Black);
+        this->disp.fillRect(0, nTop, WIDTH, this->nTopWall, Color565::Purple);
+        this->fDrawTopWall=false;
+    }
+}
+
 void Game::checkPaddle()
 {
     Rectangle rScreen=Rectangle(0,0, WIDTH, HEIGHT);            // screen boundary