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.

Files at this revision

API Documentation at this revision

Comitter:
maxint
Date:
Thu Feb 05 14:41:21 2015 +0000
Parent:
4:4a5f33d845d9
Child:
6:e2eead4e53fb
Commit message:
added MusicEngine for more tunes

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
MusicEngine.lib Show annotated file Show diff for this revision Revisions of this file
SoundFX.h Show annotated file Show diff for this revision Revisions of this file
SoundFx.cpp Show annotated file Show diff for this revision Revisions of this file
--- 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
--- a/Game.h	Wed Feb 04 13:30:29 2015 +0000
+++ b/Game.h	Thu Feb 05 14:41:21 2015 +0000
@@ -1,5 +1,6 @@
 #pragma once
 #include <stdarg.h>
+#include <algorithm>        // required for min() and max(), see http://www.cplusplus.com/reference/algorithm/
 #include "mbed.h"
 
 #include "Color565.h"
@@ -44,7 +45,7 @@
 
     SoundFX snd;
     Accelerometer accel;
-    Timer tWait;    // timer used for tickcounts
+//    Timer tWait;    // timer used for tickcounts
 
     Vector vGravity;
     Ball ball;
@@ -55,6 +56,7 @@
     int nBalls;
     int nScore;
     bool fDrawPaddle;
+    bool fDrawTopWall;
     
     int nTopWall;
 
@@ -84,6 +86,7 @@
     void redrawBalls();
     int countBalls();
     void checkBallsCollision();
+    void redrawTopWall();
     
     int checkTilt();
     void checkButtons();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MusicEngine.lib	Thu Feb 05 14:41:21 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/taylorza/code/MusicEngine/#1742b6b435ff
--- a/SoundFX.h	Wed Feb 04 13:30:29 2015 +0000
+++ b/SoundFX.h	Thu Feb 05 14:41:21 2015 +0000
@@ -1,5 +1,6 @@
 #pragma once
 #include "mbed.h"
+#include "MusicEngine.h"
 
 class SoundFX
 {
@@ -11,15 +12,20 @@
         void beepShort();
         void beepLong();
         void beepLow();
-        void playTune();
+        void play(char *szPlay);
+        void playTune();        
         void setMute(bool fMute);
         bool getMute();
 
+        void musicCompleted(void);
+
     private:
         static const int BOUNCE1_SOUND_TICKS = 1;
         static const int BOUNCE2_SOUND_TICKS = 2;
 
-        PwmOut pwm;
-        int pwmTicksLeft;
+//        PwmOut pwm;
+//        int pwmTicksLeft;
         bool fMute;
+        MusicEngine music;
+        
 };
--- a/SoundFx.cpp	Wed Feb 04 13:30:29 2015 +0000
+++ b/SoundFx.cpp	Thu Feb 05 14:41:21 2015 +0000
@@ -1,13 +1,17 @@
 #include "SoundFX.h"
 
-SoundFX::SoundFX() : pwm(P0_18)
+SoundFX::SoundFX() :
+// pwm(P0_18),
+ music(P0_18)
 {
-    this->pwmTicksLeft=0;
+//    this->pwmTicksLeft=0;
     this->fMute=false;
+    music.setCompletionCallback(this, &SoundFX::musicCompleted);
 }
 
 void SoundFX::checkPwm()
 {
+/*
     if(this->fMute || this->pwmTicksLeft == 0)
         this->pwm.write(0.0);
     else
@@ -15,14 +19,31 @@
         this->pwmTicksLeft--;
         this->pwm.write(0.5); 
     }
+*/
+}
+
+void SoundFX::musicCompleted(void)
+{
+/*
+    PwmOut pwm(P0_18);
+    pwm = 0.0;
+    pwm.period(0.1);
+    pwm.write(0.00);
+*/
 }
 
 void SoundFX::reset()
 {
+/*
     this->pwmTicksLeft=0;
 
     this->pwm.period_ms(1);
     this->pwm.write(0.00);
+*/
+/*
+    PwmOut pwm(P0_18);
+    pwm.write(0.00);
+*/
 }
 
 void SoundFX::setMute(bool fMute)
@@ -41,39 +62,57 @@
 {
     if(this->fMute)
         return;
-    this->pwmTicksLeft = nDuration;
+//    this->pwmTicksLeft = nDuration;
 }
 
 void SoundFX::beepShort()
 {
     if(this->fMute)
         return;
+/*
     this->pwm.period_ms(2);
     this->pwmTicksLeft = SoundFX::BOUNCE1_SOUND_TICKS;
+*/
+    music.play("T240 L32 O6 C");
 }
 
 void SoundFX::beepLong()
 {
     if(this->fMute)
         return;
+
+/*
     this->pwm.period_ms(1);
     this->pwmTicksLeft = SoundFX::BOUNCE2_SOUND_TICKS;
+*/
+    music.play("T240 L8 O5 C");
 }
 
 void SoundFX::beepLow()
 {
     if(this->fMute)
         return;
+/*
     this->pwm.period(1.0/220);
     this->pwm.write(0.5);
     wait_ms(150);
     this->pwm.write(0.0);
+*/
+    music.play("T180 L4 O3 C");
+}
+
+void SoundFX::play(char *szPlay)
+{
+    if(this->fMute)
+        return;
+    music.play(szPlay);
 }
 
 void SoundFX::playTune()
 {
     if(this->fMute)
         return;
+/*
     this->pwm.period(1.0/220);
     this->pwm.write(0.5);
     wait_ms(150);
@@ -88,4 +127,9 @@
     this->pwm.write(0.5);
     wait_ms(150);
     this->pwm.write(0.0);
+*/
+//    music.setCompletionCallback(this, &SoundFX::musicCompleted);
+    //music.play("T224L8O5CL16>C<P16GP16L8EL16P16>C<GP16L8E.L16P16L8C#L16>C#<P16G#P16L8FL16P16>C#<G#P16L8F.L16P16L8CL16>C<P16GP16L8EL16P16>C<GP16L8E.L16P16D#EFP16FF#GP16GG#AP16L8>C<P8L4>C");
+    music.play("T120 O3 L16 F L4 C F L8 C");
+
 }
\ No newline at end of file