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:
3:ca8b21da67dc
Parent:
2:d4de5a5866fe
Child:
4:4a5f33d845d9
--- a/Game.cpp	Tue Feb 03 19:02:27 2015 +0000
+++ b/Game.cpp	Wed Feb 04 13:10:36 2015 +0000
@@ -1,6 +1,6 @@
 #include "Game.h"
 
-const char* Game::LOSE_1 = "You lose.";
+const char* Game::LOSE_1 = "Game over.";
 const char* Game::LOSE_2 = "Press ship to restart.";
 const char* Game::SPLASH_1 = "-*- Balls and paddle -*-";
 const char* Game::SPLASH_2 = "Press ship to start.";
@@ -32,14 +32,13 @@
     
     this->lastUp = false;
     this->lastDown = false;
-    this->mode = true;
+    this->mode = true;          // mode: true=game, false=graph
 
+    this->nGameTickDelay=25;    // game tickdelay can be adjusted using up/down
 
     //this->aBalls[2]={ Ball(&disp),  Ball(&disp) };
     for(int i=0; i<NUM_BALLS; i++)
         this->aBalls[i]=Ball(&(this->disp));
-
-    //this->initialize();
 }
 
 void Game::printDouble(double value, int x, int y)
@@ -65,8 +64,9 @@
        
 //    this->paddleX = WIDTH / 2 - Game::PADDLE_WIDTH / 2;
     this->snd.reset();
-    this->nLives = 4;
+    this->nBalls = 4;
     this->nScore = 0;
+    this->nTopWall = 8;
     
     this->tWait.start();      // start the timer
     
@@ -76,7 +76,8 @@
 void Game::initializePaddle()
 {
     this->paddle.initialize(WIDTH / 2 - Game::PADDLE_WIDTH/2, HEIGHT - Game::PADDLE_HEIGHT, Game::PADDLE_WIDTH, Game::PADDLE_HEIGHT);
-    this->paddle.draw();
+    this->fDrawPaddle=true;
+    //this->paddle.draw();
 }
 
 
@@ -98,39 +99,20 @@
     }
 }
 
-
-
-
-/*
-void Game::initializeBall()
-{
-    this->ball.initialize(WIDTH / 2 - Game::BALL_RADIUS, HEIGHT / 4 - Game::BALL_RADIUS, Game::BALL_RADIUS, Color565::fromRGB(0xFF, 0x33, 0x33));
-    this->ball.setSpeed(rand() % 2 ? 1 : -1, rand() % 2 ? 1 : -1);
+void Game::redrawPaddle()
+{   // redraw the paddle when moved, or when forced by this->fDrawPaddle (set at bounce)
+    this->paddle.redraw(this->fDrawPaddle);
+    this->fDrawPaddle=false;
 }
-*/
-
-/*
-void Game::initializeBalls()
-{
-    for(int i=0; i<NUM_BALLS; i++)
-    {
-        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));
-        //float ftRandX=rand() % 2 ? 1 : -1;
-        float ftRandX=((rand() % 20) - 10)/10 ;
-        float ftRandY=rand() % 2 ? 1 : -1;
-        this->aBalls[i].setSpeed(ftRandX, ftRandY);
-    }
-}
-*/
 
 void Game::setNoBalls()
-{
+{   // make sure no balls are active
     for(int i=0; i<NUM_BALLS; i++)
         this->aBalls[i].fActive=false;
 }
 
 void Game::newBall()
-{
+{   // add a new ball to the game
     for(int i=0; i<NUM_BALLS; i++)
     {
         if(this->aBalls[i].fActive)
@@ -140,9 +122,9 @@
             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));
             //float ftRandX=rand() % 2 ? 1 : -1;
             //float ftRandY=rand() % 2 ? 1 : -1;
-            float ftRandX=((rand() % 20) - 10)/5.0;
-            float ftRandY=((rand() % 10) - 10)/5.0;
             //this->aBalls[i].setSpeed(ftRandX, ftRandY);
+            float ftRandX=((rand() % 20) - 10)/5.0;     // left/right at random speed
+            float ftRandY=((rand() % 10) - 10)/5.0;     // up at random speed
             this->aBalls[i].vSpeed.set(ftRandX, ftRandY);
             this->aBalls[i].fActive=true;
             break;
@@ -160,7 +142,7 @@
         this->aBalls[i].update();                    // update the ball position 
 
         // add downward gravity
-        if(this->aBalls[i].vSpeed.getSize() != 0 && this->aBalls[i].vSpeed.getSize()<10.0)            // TODO: added if statement to allow zero speed pause of ball
+        if(this->aBalls[i].vSpeed.getSize()<10.0)
             this->aBalls[i].vSpeed.add(this->vGravity);    // add some gravity
 
     }
@@ -220,13 +202,13 @@
         this->checkBallsCollision();
 //        this->ball.redraw();
         this->redrawBalls();
-        this->paddle.redraw();
+        this->redrawPaddle();
         
         this->snd.checkPwm();
         //this->checkScore(); 
-        this->checkLives(); 
+        this->checkBalls(); 
         
-        wait_ms(25);
+        wait_ms(this->nGameTickDelay);  // can be adjusted using up/down
     }
     else {
         this->accel.updateGraph();
@@ -255,8 +237,9 @@
 
 void Game::checkButtons()
 {
-    if (!this->square.read())
+    if(!this->square.read())       // note: button.read() is false (LOW/0) when pressed
     {
+        wait_ms(250);   // el-cheapo deboounce
         this->mode = !this->mode;
         
         //this->disp.clear();
@@ -270,6 +253,14 @@
         this->led1.write(this->mode);
         this->led2.write(!this->mode);
     }
+    else if(!this->circle.read() && this->mode)       // note: button.read() is false (LOW/0) when pressed
+    {
+        bool fMute=this->snd.getMute();
+        fMute=!fMute;
+        this->snd.setMute(fMute);
+        this->led2.write(fMute);
+        wait_ms(250);   // el-cheapo deboounce
+    }
     else
     {  
         bool isUp = !this->up.read();
@@ -283,11 +274,13 @@
         
         if (isUp)
         {
-            this->ball.changeSpeed(true);
+            if(this->nGameTickDelay<1000) this->nGameTickDelay=(float)this->nGameTickDelay*1.20;
+            this->printf(100, 0, "Speed: %d  ", this->nGameTickDelay);   
         }
         else if (isDown)
         {
-            this->ball.changeSpeed(false);
+            if(this->nGameTickDelay>5) this->nGameTickDelay=(float)this->nGameTickDelay/1.20;
+            this->printf(100, 0, "Speed: %d  ", this->nGameTickDelay);   
         }
     
 end:
@@ -320,7 +313,8 @@
 
         wait_ms(1);
     }
-        
+    wait_ms(250);   // el-cheapo deboounce
+
     //this->disp.clearScreen();
     this->initialize();     // start a new game
 }
@@ -329,7 +323,7 @@
 
 void Game::checkBallsCollision()
 {
-    Rectangle rTop=Rectangle(0, -10, WIDTH, 0);                // Rectangle(0, 0, WIDTH, 1);       // top wall
+    Rectangle rTop=Rectangle(0, -10, WIDTH, this->nTopWall);                // Rectangle(0, 0, WIDTH, 1);       // top wall
     Rectangle rBottom=Rectangle(0, HEIGHT, WIDTH, HEIGHT+10);  // Rectangle(0, HEIGHT, WIDTH, HEIGHT);       // bottom gap
     Rectangle rLeft=Rectangle(-10, 0, 0, HEIGHT);              // Rectangle(0, 0, 0, HEIGHT);       // left wall
     Rectangle rRight=Rectangle(WIDTH, 0, WIDTH+10, HEIGHT);       // Rectangle(WIDTH, 0, WIDTH, HEIGHT);       // right wall
@@ -366,7 +360,7 @@
             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
     
-            
+            // 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;
@@ -381,15 +375,29 @@
             else
                 pBall->Bounce(Vector(1,-1));        // bounce vertical at same speed
     
+            // force drawing the paddle after redrawing the bounced ball
+            this->fDrawPaddle=true;
+
+            // make sound and update the score
             this->snd.beepLong();
             this->nScore++;
             this->printf(100, 0, "Score: %d ", this->nScore);   
 
+            // add a new ball every 10 points
             if(this->nScore>0 && this->nScore%10==0)
             {
                 this->newBall();
-                this->nLives++;
+                this->nBalls++;
             }
+
+            
+            // lower the ceiling every 25 points
+            if(this->nScore>0 && this->nScore%25==0)
+            {
+                this->nTopWall+=2;
+                this->disp.fillRect(rTop.getX1(), 8, rTop.getX2(), rTop.getY2(), Color565::Purple);
+            }
+
         }
         if(pBall->collides(rBottom) && pBall->vSpeed.isDown())      // bottom gap
         {
@@ -397,7 +405,7 @@
             pBall->clear();   // clear the ball from its current position
             pBall->vSpeed.set(0,0);
             pBall->fActive=false;
-            this->nLives--;
+            this->nBalls--;
             //this->initializeBall();     // start a new ball
             if(countBalls()==0)
             {
@@ -415,55 +423,6 @@
     this->paddle.checkBoundary(rScreen);
 }
 
-/*
-void Game::checkCollision()
-{
-    Rectangle rTop=Rectangle(0, -10, WIDTH, 0);                // Rectangle(0, 0, WIDTH, 1);       // top wall
-    Rectangle rBottom=Rectangle(0, HEIGHT, WIDTH, HEIGHT+10);  // Rectangle(0, HEIGHT, WIDTH, HEIGHT);       // bottom gap
-    Rectangle rLeft=Rectangle(-10, 0, 0, HEIGHT);              // Rectangle(0, 0, 0, HEIGHT);       // left wall
-    Rectangle rRight=Rectangle(WIDTH, 0, WIDTH+10, HEIGHT);       // Rectangle(WIDTH, 0, WIDTH, HEIGHT);       // right wall
-    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
-
-    this->paddle.checkBoundary(rScreen);
-   
-    if(ball.collides(rTop) && this->ball.vSpeed.isUp())      // top wall
-    {
-        this->ball.Bounce(Vector(1,-1));        // bounce vertical
-        this->snd.beepShort();
-    }
-    if(ball.collides(rRight) && this->ball.vSpeed.isRight())      // right wall
-    {
-        this->ball.Bounce(Vector(-1,1));        // bounce horizontal
-        this->snd.beepShort();
-    }
-    if(ball.collides(rLeft) && this->ball.vSpeed.isLeft())      // left wall
-    {
-        this->ball.Bounce(Vector(-1,1));        // bounce horizontal
-        this->snd.beepShort();
-    }
-    if(ball.collides(rPaddle) && this->ball.vSpeed.isDown())      // paddle
-    {
-        if(ball.collides(rPaddleLeft))   ball.vSpeed.add(Vector(-1,0));       // left side of paddle has bias to the left
-        if(ball.collides(rPaddleRight))  ball.vSpeed.add(Vector(1,0));       // right side of paddle has bias to the right
-
-        ball.Bounce(Vector(1,-1));        // bounce vertical at same speed
-        //ball.Bounce(Vector(1,-1.05));        // bounce from paddle at higher speed
-
-        this->snd.beepLong();
-        this->nScore++;
-        this->printf(100, 0, "Score: %d ", this->nScore);   
-    }
-    if(ball.collides(rBottom) && this->ball.vSpeed.isDown())      // bottom gap
-    {
-        ball.clearPrev();   // clear the ball from its previous position
-        this->nLives--;
-        this->initializeBall();     // start a new ball
-    }
-}
-*/
 
 void Game::printf(int x, int y, const char *szFormat, ...)
 {
@@ -477,20 +436,26 @@
 }
 
 
-void Game::checkLives() {
-    if (this->nLives == 0) {
+void Game::checkBalls()
+{
+    if (this->nBalls == 0)
+    {   // game over
+        char buf[256];
         this->disp.clearScreen();
-        
+
         this->drawString(Game::LOSE_1, HEIGHT / 2 - CHAR_HEIGHT); 
         this->drawString(Game::LOSE_2, HEIGHT / 2);  
+        sprintf(buf,"Your score: %d  ", this->nScore);
+        this->drawString(buf, HEIGHT / 2 + CHAR_HEIGHT / 2 + CHAR_HEIGHT ); 
         
         this->snd.playTune();
         while (this->circle.read())
             wait_ms(1);
-            
+        wait_ms(250);   // el-cheapo deboounce
         this->initialize();
     }
-    else {
-        this->printf(0, 0, "%d", this->nLives);   
+    else
+    {
+        this->printf(0, 0, "Balls: %d  ", this->nBalls);   
     }
 }
\ No newline at end of file