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:
8:b119501f4ef0
Parent:
7:6e7b789f4060
Child:
9:7bd5def2115d
--- a/Game.cpp	Sat Feb 28 16:32:20 2015 +0000
+++ b/Game.cpp	Sat Feb 28 19:41:00 2015 +0000
@@ -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), 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), vFriction(-0.01, -0.01), ball(&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), vFriction(-0.005, -0.005), ball(&disp)
 {
     this->disp.setOrientation(LCD_ST7735::Rotate270, false);
     this->disp.setForegroundColor(WHITE);
@@ -55,57 +55,105 @@
 
 void Game::initialize()
 {
-    this->disp.clearScreen();
-
-    this->snd.reset();
-    this->nBalls = 4;
-    this->nScore = 0;
+//    disp.clearScreen();
+//    snd.reset();
+    nBalls = 4;
+    nScore = 0;
     
-    this->tWait.start();      // start the timer
+    tWait.start();      // start the timer
 
     //char sWalls[]="W010010A0";
     for(int i=0; i<NUM_WALLS; i++)
     {
-        aWalls[i]=Wall(&(this->disp));
+        aWalls[i]=Wall(&(disp));
+    }
+    
+    initLevel();
+
+//    setNoBall();     // reset all balls
+    newBall();     // start first ball
+    snd.play("T240 L16 O5 D E F");
+}
+
+void Game::initLevel()
+{
+    char szLevel0[]="01:1 0262 2787-2227 8187 ";
+    char szLevel1[]="01:1 0292 0343 63:3 2484 0545 65:5 1797-5257 1617 2526 3637 7677 8586 9697 ";
+    char szLevel2[]="01:1 0232 82:2 1343 6393 0484 1696-4143 6163 5355 9396 3435 7475 1617 4547 5758 6567 ";
+    char szLevel3[]="01:1 0232 82:2 3444 1545 6696-4147 6166 2223 8284 1315 5358 9397 ";
+    
+    disp.clearScreen();
+    snd.reset();
+
+    // reset current walls and holes
+    for(int i=0; i<NUM_WALLS; i++)
+        aWalls[i].fActive=false;
+    for(int i=0; i<NUM_HOLES; i++)
+        aHoles[i].fActive=false;
+
+    // add walls/holes depending on level
+    switch(nScore)
+    {
+        case 0:
+            addWalls(szLevel0);
+            addHoles("4411 6412 ");
+            break;
+        case 1:
+            addWalls(szLevel1);
+            addHoles("9111 0312 9412 4612 5612 ");
+            break;
+        case 2:
+            addWalls(szLevel1);
+            addHoles("5711 0312 9412 4612 5612 ");
+            break;
+        case 3:
+            addWalls(szLevel1);
+            addHoles("0211 0312 9412 4612 5612 ");
+            break;
+
+        case 4:
+            addWalls(szLevel2);
+            addHoles("4411 6112 5112 1512 8512 2712 7712 8612 ");
+            break;
+        case 5:
+            addWalls(szLevel2);
+            addHoles("5611 6112 5112 1512 8512 2712 7712 8612 ");
+            break;
+        case 6:
+            addWalls(szLevel2);
+            addHoles("9111 6112 5112 1512 8512 2712 7712 8612 ");
+            break;
+
+        case 7:
+            addWalls(szLevel3);
+            addHoles("3411 4112 5112 6112 2212 3312 6312 1412 7412 8512 0612 2612 6612 8612 5712 7712 ");
+            break;
+        case 8:
+            addWalls(szLevel3);
+            addHoles("5211 4112 5112 6112 2212 3312 6312 1412 7412 8512 0612 2612 6612 8612 5712 7712 ");
+            break;
+        case 9:
+            addWalls(szLevel3);
+            addHoles("9711 4112 5112 6112 2212 3312 6312 1412 7412 8512 0612 2612 6612 8612 5712 7712 ");
+            break;
+        case 10:
+            addWalls(szLevel3);
+            addHoles("9111 4112 5112 6112 2212 3312 6312 1412 7412 8512 0612 2612 6612 8612 5712 7712 ");
+            break;
+        case 11:
+        default:
+            addWalls(szLevel3);
+            addHoles("9111 4112 5112 6112 2212 3312 6312 1412 7452 8512 0612 2652 6612 8612 5712 7712 ");
+            break;
     }
 
-    // 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");
-
-    addHole("0211");
-    addHole("0312");
-    addHole("9412");
-    addHole("4612");
-    addHole("5612");
+    drawWalls();
+    drawHoles();
+    checkNumBalls();
+}
 
 
-    this->setNoBall();     // reset all balls
-    this->newBall();     // start first ball
-    this->snd.play("T240 L16 O5 D E F");
-
-    drawWalls();
-}
 
 Point Game::getGridPos(char *sPos)
 {   // get item pos based on a grid definition of 16x16 pixel squares, layed out in a 10x8 grid
@@ -138,6 +186,16 @@
     }
 }
 
+void Game::addWalls(char *sWalls)
+{
+    char sWall[]="0000";
+    for(int i=0; i<strlen(sWalls); i+=5)
+    {
+        strncpy(sWall, sWalls+i, 4);
+        addWall(sWall);
+    }
+}
+
 
 void Game::drawWalls()
 {
@@ -157,11 +215,14 @@
     {
         if(!aHoles[i].fActive)
         {
-            int x1=sHole[0]-'0';
-            int y1=sHole[1]-'0';
+            int x=(sHole[0]-'0')*16+8;
+            int y=(sHole[1]-'0')*16+8;
             int r=sHole[2]-'0';
             int c=sHole[3]-'0';
-            aHoles[i].setCirc(Circle(x1*16+8,y1*16+8, Game::HOLE_RADIUS));
+            int rnd1=0, rnd2=0;
+            if(r==2) rnd1=rand() % r - r/2;
+            if(r==2) rnd2=rand() % r - r/2;
+            aHoles[i].setCirc(Circle(x+rnd1,y+rnd2, Game::HOLE_RADIUS));
             if(c==1) aHoles[i].setColor(Color565::Green);
             if(c==2) aHoles[i].setColor(Color565::Gray);
             if(c==3) aHoles[i].setColor(Color565::Red);
@@ -175,6 +236,17 @@
     }
 }
 
+void Game::addHoles(char *sHoles)
+{
+    char sHole[]="0000";
+    for(int i=0; i<strlen(sHoles); i+=5)
+    {
+        strncpy(sHole, sHoles+i, 4);
+        addHole(sHole);
+    }
+}
+
+
 void Game::drawHoles()
 {
     for(int i=0; i<NUM_HOLES; i++)
@@ -213,7 +285,7 @@
 
     // increase speed based on gravity
     checkTilt();
-    if(ball.vSpeed.getSize()<1.0)
+    if(ball.vSpeed.getSize()<2.0)
         ball.vSpeed.add(this->vGravity);    // add some gravity
         
     // decrease speed based on friction
@@ -455,18 +527,21 @@
                 if(aHoles[i].hasGoneIn(ball.getBoundingCircle()))
                 {
                     //snd.play("T240 L128 O5 C");
+                    ball.fActive=false;
                     if(i==0)
                     {   // TODO: for now first hole is target hole
-                        this->nScore++;
-                        this->snd.play("T240 L16 O5 CDEFG");
+                        nScore++;
+                        snd.play("T240 L16 O5 CDEFG");
                     }
                     else
                     {
-                        this->nBalls--;
-                        this->snd.beepLow();
+                        nBalls--;
+                        snd.beepLow();
                     }
-                    ball.fActive=false;
-                    this->newBall();     // start a new ball
+
+                    initLevel();
+
+                    newBall();     // start a new ball
 
                 }                                    
             }