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:
7:6e7b789f4060
Parent:
6:e2eead4e53fb
Child:
8:b119501f4ef0
--- a/Game.cpp	Sat Feb 28 11:40:39 2015 +0000
+++ b/Game.cpp	Sat Feb 28 16:32:20 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.1), vFriction(-0.02, -0.02), 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.01, -0.01), ball(&disp)
 {
     this->disp.setOrientation(LCD_ST7735::Rotate270, false);
     this->disp.setForegroundColor(WHITE);
@@ -39,6 +39,8 @@
     nTopWall=8;
     for(int i=0; i<NUM_WALLS; i++)
         aWalls[i]=Wall(&(this->disp));
+    for(int i=0; i<NUM_HOLES; i++)
+        aHoles[i]=Hole(&(this->disp));
 
     this->snd.reset();
 }
@@ -59,13 +61,12 @@
     this->nBalls = 4;
     this->nScore = 0;
     
-//    this->tWait.start();      // start the timer
+    this->tWait.start();      // start the timer
 
     //char sWalls[]="W010010A0";
     for(int i=0; i<NUM_WALLS; i++)
     {
         aWalls[i]=Wall(&(this->disp));
-        //aWalls[i].fActive=false;
     }
 
     // hor
@@ -85,13 +86,20 @@
     addWall("1617");
     addWall("2526");
     addWall("3637");
-    addWall("4546");
+    //addWall("4546");
 
-    addWall("6566");
+    //addWall("6566");
     addWall("7677");
     addWall("8586");
     addWall("9697");
 
+    addHole("0211");
+    addHole("0312");
+    addHole("9412");
+    addHole("4612");
+    addHole("5612");
+
+
     this->setNoBall();     // reset all balls
     this->newBall();     // start first ball
     this->snd.play("T240 L16 O5 D E F");
@@ -119,9 +127,9 @@
             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].setRect(Rectangle(x1*16,y1*16,x2*16+1,y2*16+1));
             aWalls[i].fActive=true;
-            printf(0, 0, "W: %d,%d - %d,%d", x1, y1, x2, y2);            
+            //printf(0, 0, "W: %d,%d - %d,%d", x1, y1, x2, y2);            
             break;
 
             //aWalls[i].draw();
@@ -130,6 +138,7 @@
     }
 }
 
+
 void Game::drawWalls()
 {
     for(int i=0; i<NUM_WALLS; i++)
@@ -140,6 +149,43 @@
         }
 }
 
+void Game::addHole(char *sHole)
+{   // 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(!aHoles[i].fActive)
+        {
+            int x1=sHole[0]-'0';
+            int y1=sHole[1]-'0';
+            int r=sHole[2]-'0';
+            int c=sHole[3]-'0';
+            aHoles[i].setCirc(Circle(x1*16+8,y1*16+8, 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);
+            aHoles[i].fActive=true;
+            //printf(0, 0, "H: %d,%d - %d,%d", x1, y1, r, c);            
+            break;
+
+            //aWalls[i].draw();
+            //snd.beepShort();
+        }
+    }
+}
+
+void Game::drawHoles()
+{
+    for(int i=0; i<NUM_HOLES; i++)
+        if(aHoles[i].fActive)
+        {
+            aHoles[i].draw();
+            //snd.beepShort();
+        }
+}
+
+
 
 void Game::setNoBall()
 {   // make sure no balls are active
@@ -217,7 +263,15 @@
 
         redrawBall();
         
-        drawWalls();
+        if(this->tWait.read_ms()>100)
+        {   // redraw walls and holes every tenth second
+            drawWalls();
+            drawHoles();
+
+            checkNumBalls();
+
+            this->tWait.reset();
+        }
         
 //        this->snd.checkPwm();
         //this->checkScore(); 
@@ -241,7 +295,6 @@
     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);
@@ -325,8 +378,8 @@
 
     while (this->circle.read())
     {
-checkTilt();
-printf(10, 150, "tilt: %.2f, %.2f  ", vGravity.x, vGravity.y);
+//checkTilt();
+//printf(00, 120, "tilt: %.2f, %.2f  ", vGravity.x, vGravity.y);
 
         wait_ms(1);
     }
@@ -364,24 +417,61 @@
         //this->snd.beepShort();
     }
 
-    for(int i=0; i<NUM_WALLS; i++)
+    for(int i=0; i<NUM_WALLS; i++)      // test maze walls
     {
         if(aWalls[i].fActive)
         {
             Rectangle rWall=aWalls[i].getRect();
             if(ball.collides(rWall))
             {
-                //snd.play("T240 L128 O4 A");
+                //printf(30, 0, "b: %.2f", ball.vSpeed.getSize());
                 if(rWall.isHorizontal())
-                    ball.Bounce(Vector(0,-0.1));
+                {
+                    if(fabs(ball.vSpeed.y)>0.8)
+                        snd.play("T240 L128 O4 A");
+                    ball.Bounce(Vector(1,-0.1));
+                }
                 else
-                    ball.Bounce(Vector(-0.1,-0));
+                {
+                    if(fabs(ball.vSpeed.x)>0.8)
+                        snd.play("T240 L128 O4 A");
+                    ball.Bounce(Vector(-0.1,1));
+                }
                 //ball.vSpeed.multiply(Vector(0,0));
                 aWalls[i].draw();
             }
             //printf(0, 100, "W: %d,%d - %d,%d", rWall.getX1(), rWall.getY1(), rWall.getX2(), rWall.getY2());
         }
     }
+
+    for(int i=0; i<NUM_HOLES; i++)      // test holes
+    {
+        if(aHoles[i].fActive)
+        {
+            Circle cHole=aHoles[i].getCirc();
+            if(ball.collides(cHole))
+            {
+                //snd.play("T240 L128 O5 C");
+                if(aHoles[i].hasGoneIn(ball.getBoundingCircle()))
+                {
+                    //snd.play("T240 L128 O5 C");
+                    if(i==0)
+                    {   // TODO: for now first hole is target hole
+                        this->nScore++;
+                        this->snd.play("T240 L16 O5 CDEFG");
+                    }
+                    else
+                    {
+                        this->nBalls--;
+                        this->snd.beepLow();
+                    }
+                    ball.fActive=false;
+                    this->newBall();     // start a new ball
+
+                }                                    
+            }
+        }
+    }
     
     
 }
@@ -397,3 +487,28 @@
     va_end(args);
     this->disp.drawString(font_oem, x, y, szBuffer);
 }
+
+void Game::checkNumBalls()
+{
+    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.play("T120 O3 L4 R4 F C F2 C");
+        while (this->circle.read())
+            wait_ms(1);
+        wait_ms(250);   // el-cheapo deboounce
+        this->initialize();
+    }
+    else
+    {
+        printf(0, 0, "Balls: %d  ", this->nBalls);   
+        printf(100, 0, "Score: %d ", this->nScore);
+    }
+}