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:
6:e2eead4e53fb
Parent:
5:67e75a4f0b52
Child:
7:6e7b789f4060
--- a/Game.cpp	Thu Feb 05 14:41:21 2015 +0000
+++ b/Game.cpp	Sat Feb 28 11:40:39 2015 +0000
@@ -2,7 +2,7 @@
 
 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_1 = "-*- Ball and holes -*-";
 const char* Game::SPLASH_2 = "Press ship to start.";
 const char* Game::SPLASH_3 = "Made by Maxint";
 
@@ -21,7 +21,7 @@
 
 
 Game::Game() : left(P0_14, PullUp), right(P0_11, PullUp), down(P0_12, PullUp), up(P0_13, PullUp), square(P0_16, PullUp), circle(P0_1, PullUp), led1(P0_9), led2(P0_8), 
-    ain(P0_15), i2c(P0_5, P0_4), disp(P0_19, P0_20, P0_7, P0_21, P0_22, P1_15, P0_2, LCD_ST7735::RGB), accel(this->I2C_ADDR, &disp), vGravity(0, 0.1), ball(&disp), paddle(&disp)
+    ain(P0_15), disp(P0_19, P0_20, P0_7, P0_21, P0_22, P1_15, P0_2, LCD_ST7735::RGB), accel(this->I2C_ADDR, &disp), vGravity(0, 0.1), vFriction(-0.02, -0.02), ball(&disp)
 {
     this->disp.setOrientation(LCD_ST7735::Rotate270, false);
     this->disp.setForegroundColor(WHITE);
@@ -33,12 +33,12 @@
     this->lastUp = false;
     this->lastDown = false;
     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));
+    nTopWall=8;
+    for(int i=0; i<NUM_WALLS; i++)
+        aWalls[i]=Wall(&(this->disp));
 
     this->snd.reset();
 }
@@ -58,110 +58,139 @@
     this->snd.reset();
     this->nBalls = 4;
     this->nScore = 0;
-    this->nTopWall = 8;
-    this->fDrawTopWall=true;
     
 //    this->tWait.start();      // start the timer
 
-    this->initializePaddle();
-    this->setNoBalls();     // reset all balls
+    //char sWalls[]="W010010A0";
+    for(int i=0; i<NUM_WALLS; i++)
+    {
+        aWalls[i]=Wall(&(this->disp));
+        //aWalls[i].fActive=false;
+    }
+
+    // hor
+    addWall("01:1");    // top edge
+    addWall("0292");
+    addWall("0343");
+    addWall("63:3");
+    addWall("2484");
+    addWall("0545");
+    addWall("65:5");
+    addWall("1797");
+    //addWall("08:8"); // bottom
+
+    // vert
+    addWall("5257");
+
+    addWall("1617");
+    addWall("2526");
+    addWall("3637");
+    addWall("4546");
+
+    addWall("6566");
+    addWall("7677");
+    addWall("8586");
+    addWall("9697");
+
+    this->setNoBall();     // reset all balls
     this->newBall();     // start first ball
     this->snd.play("T240 L16 O5 D E F");
-}
 
-
-void Game::initializePaddle()
-{
-    this->paddle.initialize(WIDTH / 2 - Game::PADDLE_WIDTH/2, HEIGHT - Game::PADDLE_HEIGHT, Game::PADDLE_WIDTH, Game::PADDLE_HEIGHT);
-    this->fDrawPaddle=true;
+    drawWalls();
 }
 
-
-void Game::updatePaddle()
-{
-    if (!this->left.read())  // note: read is LOW (0) when button pressed
-        this->paddle.move(Vector(-1 * Game::PADDLE_SPEED, 0));
-    else if (!this->right.read())
-        this->paddle.move(Vector(Game::PADDLE_SPEED, 0));
-    else
-    {
-        int i=this->checkTilt();
-        if(i>0)
-            this->paddle.move(Vector(Game::PADDLE_SPEED, 0));
-        else if(i<0)
-            this->paddle.move(Vector(-1 * Game::PADDLE_SPEED, 0));
-        else if(this->paddle.hasChanged())
-            paddle.move(Vector(0, 0));  // move to same place to restrict redraws
-    }
+Point Game::getGridPos(char *sPos)
+{   // get item pos based on a grid definition of 16x16 pixel squares, layed out in a 10x8 grid
+    // sPos is a 2 character string containing the coordinates in decimal notation: xy
+    int x=(sPos[0]-'0')*16+8;
+    int y=(sPos[1]-'0')*16+8;
+    return(Point(x,y));
 }
 
-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::setNoBalls()
-{   // make sure no balls are active
-    for(int i=0; i<NUM_BALLS; i++)
-        this->aBalls[i].fActive=false;
-}
+void Game::addWall(char *sWall)
+{   // add a wall based on a grid definition of 16x16 pixel squares, layed out in a 10x8 grid
+    // sWall is a 4 character string containing the edge coordinates in decimal notation: xyXY
+    // grid axis range from x: '0'-'9', ':'=10  - y:  '0'-'8'
+    for(int i=0; i<NUM_WALLS; i++)
+    {
+        if(!aWalls[i].fActive)
+        {
+            int x1=sWall[0]-'0';
+            int y1=sWall[1]-'0';
+            int x2=sWall[2]-'0';
+            int y2=sWall[3]-'0';
+            aWalls[i].setRect(Rectangle(x1*16,y1*16,x2*16,y2*16));
+            aWalls[i].fActive=true;
+            printf(0, 0, "W: %d,%d - %d,%d", x1, y1, x2, y2);            
+            break;
 
-void Game::newBall()
-{   // add a new ball to the game
-    for(int i=0; i<NUM_BALLS; i++)
-    {
-        if(this->aBalls[i].fActive)
-            continue;
-        else
-        {
-            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);
-            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;
+            //aWalls[i].draw();
+            //snd.beepShort();
         }
     }
 }
 
-void Game::updateBalls()
+void Game::drawWalls()
 {
-    for(int i=0; i<NUM_BALLS; i++)
-    {
-        if(!this->aBalls[i].fActive)
-            continue;
+    for(int i=0; i<NUM_WALLS; i++)
+        if(aWalls[i].fActive)
+        {
+            aWalls[i].draw();
+            //snd.beepShort();
+        }
+}
 
-        this->aBalls[i].update();                    // update the ball position 
 
-        // add downward gravity
-        if(this->aBalls[i].vSpeed.getSize()<10.0)
-            this->aBalls[i].vSpeed.add(this->vGravity);    // add some gravity
-
-    }
+void Game::setNoBall()
+{   // make sure no balls are active
+/*    for(int i=0; i<NUM_BALLS; i++)
+        this->aBalls[i].fActive=false;
+*/
 }
 
-void Game::redrawBalls()
-{
-    for(int i=0; i<NUM_BALLS; i++)
-    {
-        if(!this->aBalls[i].fActive)
-            continue;
-        this->aBalls[i].redraw();                    // update the ball position 
-    }
+void Game::newBall()
+{   // add a ball to the game
+    Point ptBall=getGridPos("01");
+    ball.initialize(ptBall.getX(), ptBall.getY(), Game::BALL_RADIUS, Color565::White);
+    //float ftRandX=rand() % 2 ? 1 : -1;
+    //float ftRandY=rand() % 2 ? 1 : -1;
+    //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
+//    ball.vSpeed.set(ftRandX, ftRandY);
+    //this->aBalls[i].fActive=true;
 }
 
-int Game::countBalls()
+void Game::updateBall()
+{
+    ball.update();                    // update the ball position 
+
+    // increase speed based on gravity
+    checkTilt();
+    if(ball.vSpeed.getSize()<1.0)
+        ball.vSpeed.add(this->vGravity);    // add some gravity
+        
+    // decrease speed based on friction
+    Vector vDecel=ball.vSpeed.getNormalized();
+    vDecel.multiply(vFriction);
+    ball.vSpeed.add(vDecel);
+}
+
+void Game::redrawBall()
+{
+    ball.redraw();                    // update the ball position 
+}
+
+int Game::countBall()
 {
     int nResult=0;
+/*
     for(int i=0; i<NUM_BALLS; i++)
     {
         if(this->aBalls[i].fActive)
             nResult++;
     }
+*/
     return(nResult);
 }
 
@@ -171,45 +200,48 @@
 
 void Game::tick()
 {  
-    this->checkButtons();
+    checkButtons();
     
-    if (this->mode) {
+    if(mode)
+    {
 /*
         if(this->tWait.read_ms()>100)
         {
-            this->updatePaddle();
             this->tWait.reset();
         }
 */
 
-        this->updateBalls();                    // update the ball positions
-        this->updatePaddle();
+        updateBall();                    // update the ball positions
     
-        this->checkPaddle();
-        this->checkBallsCollision();
+        checkBallCollision();
 
-        this->redrawBalls();
-        this->redrawPaddle();
-        this->redrawTopWall();
+        redrawBall();
+        
+        drawWalls();
         
 //        this->snd.checkPwm();
         //this->checkScore(); 
-        this->checkBalls(); 
+        //this->checkBall(); 
         
-        wait_ms(this->nGameTickDelay);  // can be adjusted using up/down
+        wait_ms(nGameTickDelay);  // can be adjusted using up/down
     }
-    else {
-        this->accel.updateGraph();
+    else
+    {
+        accel.updateGraph();
         wait_ms(100);
     } 
 }
 
-int Game::checkTilt()
-{    // move the paddle by tilting the board left or righr
+void Game::checkTilt()
+{    // move the gravity direction and weight by tilting the board
     double x, y, z;
     //int nStart=this->tWait.read_ms();
     this->accel.getXYZ(x, y, z);
 
+    vGravity=Vector(x,y);
+//    vGravity=vGravity.getNormalized();
+    
+    // TODO: make vector based on tilting
     //printDouble((double)this->tWait.read_ms()-nStart, 10, 10);
 /*
 printDouble(x, 0, 0);
@@ -218,9 +250,7 @@
 this->drawString(buf, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT / 2 + 4*DisplayN18::CHAR_HEIGHT ); 
 */
 
-    if(x<-0.1) return(-1);
-    else if(x>0.1) return(1);
-    else return(0);
+    //return(Vector(0,0));
 }
 
 void Game::checkButtons()
@@ -287,17 +317,16 @@
     
 }
 
-void Game::showSplashScreen() {
+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*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) ); 
+checkTilt();
+printf(10, 150, "tilt: %.2f, %.2f  ", vGravity.x, vGravity.y);
 
         wait_ms(1);
     }
@@ -307,119 +336,54 @@
     this->initialize();     // start a new game
 }
 
-
-
-void Game::checkBallsCollision()
+void Game::checkBallCollision()
 {
     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
-    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
-
-    Ball* pBall;
-    for(int i=0; i<NUM_BALLS; i++)
-    {
-        if(!this->aBalls[i].fActive)
-            continue;
-
-        pBall=&(this->aBalls[i]);
 
-        if(pBall->collides(rTop) && pBall->vSpeed.isUp())      // top wall
-        {
-            pBall->Bounce(Vector(1,-1));        // bounce vertical
-            this->snd.beepShort();
-            this->fDrawTopWall=true;
-        }
-        if(pBall->collides(rRight) && pBall->vSpeed.isRight())      // right wall
-        {
-            pBall->Bounce(Vector(-1,1));        // bounce horizontal
-            this->snd.beepShort();
-        }
-        if(pBall->collides(rLeft) && pBall->vSpeed.isLeft())      // left wall
-        {
-            pBall->Bounce(Vector(-1,1));        // bounce horizontal
-            this->snd.beepShort();
-        }
-        if(pBall->collides(rPaddle) && pBall->vSpeed.isDown())      // paddle
+    if(ball.collides(rTop) && ball.vSpeed.isUp())      // top wall
+    {
+        ball.vSpeed.y=0;
+        //this->snd.beepShort();
+    }
+    if(ball.collides(rRight) && ball.vSpeed.isRight())      // right wall
+    {
+        ball.vSpeed.x=0;
+        //this->snd.beepShort();
+    }
+    if(ball.collides(rLeft) && ball.vSpeed.isLeft())      // left wall
+    {
+        ball.vSpeed.x=0;
+        //this->snd.beepShort();
+    }
+    if(ball.collides(rBottom) && ball.vSpeed.isDown())      // bottom wall
+    {
+        ball.vSpeed.y=0;
+        //this->snd.beepShort();
+    }
+
+    for(int i=0; i<NUM_WALLS; i++)
+    {
+        if(aWalls[i].fActive)
         {
-            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
-    
-            // 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)
+            Rectangle rWall=aWalls[i].getRect();
+            if(ball.collides(rWall))
             {
-                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+=3;
-                this->fDrawTopWall=true;
-                //this->snd.play("T240 L32 O5 GFEDC");
-                this->snd.play("T240 L16 O5 CDEFG");
+                //snd.play("T240 L128 O4 A");
+                if(rWall.isHorizontal())
+                    ball.Bounce(Vector(0,-0.1));
+                else
+                    ball.Bounce(Vector(-0.1,-0));
+                //ball.vSpeed.multiply(Vector(0,0));
+                aWalls[i].draw();
             }
-
-        }
-        if(pBall->collides(rBottom) && pBall->vSpeed.isDown())      // bottom gap
-        {
-            pBall->clearPrev();   // clear the ball from its previous position
-            pBall->clear();   // clear the ball from its current position
-            pBall->vSpeed.set(0,0);
-            pBall->fActive=false;
-            this->nBalls--;
-            if(countBalls()==0)
-            {
-                this->newBall();     // start a new ball
-                this->snd.beepLow();
-            }
+            //printf(0, 100, "W: %d,%d - %d,%d", rWall.getX1(), rWall.getY1(), rWall.getX2(), rWall.getY2());
         }
     }
-}
-
-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
-
-    this->paddle.checkBoundary(rScreen);
+    
+    
 }
 
 
@@ -433,28 +397,3 @@
     va_end(args);
     this->disp.drawString(font_oem, x, y, szBuffer);
 }
-
-
-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, "Balls: %d  ", this->nBalls);   
-    }
-}
\ No newline at end of file