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:
1:c1ee4c699517
Parent:
0:87ab172a74b4
Child:
2:d4de5a5866fe
--- a/Game.cpp	Wed Jan 28 17:32:54 2015 +0000
+++ b/Game.cpp	Sun Feb 01 16:21:24 2015 +0000
@@ -21,77 +21,40 @@
 
 
 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), 
-    pwm(P0_18), 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), vGravity(0, 0.1)
+    pwm(P0_18), 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)
 {
+    this->disp.setOrientation(LCD_ST7735::Rotate270, false);
+    this->disp.setForegroundColor(WHITE);
+    this->disp.setBackgroundColor(BLACK);
+    this->disp.clearScreen();
+
     srand(this->ain.read_u16());
     
     this->lastUp = false;
     this->lastDown = false;
     this->mode = true;
 
-    this->colors[0] = Color565::Red;
-    this->colors[1] = Color565::Green;
-    this->colors[2] = Color565::Blue;
-    
     this->initialize();
 }
 
-/*
-void Game::getXYZ(double& x, double& y, double& z) {
-    this->accel.getXYZ(x, y, z);
-}
-*/
-
 void Game::printDouble(double value, int x, int y) {
     char buffer[10];
     int len = sprintf(buffer, "%.1f ", value);
     
-    this->disp.drawString(font_ibm, x, y, buffer);
-}
-
-void Game::drawAxes() {
-    for (int i = 0; i < 3; i++) {
-        this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING), 0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT, WHITE);
-        this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, WIDTH, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, WHITE);
-    }
+    this->disp.drawString(font_oem, x, y, buffer);
 }
 
-void Game::drawPoint(int axis, double value) {
-    if (value < -1.0)
-        value = -1.0;
-
-    if (value > 1.0)
-        value = 1.0;
-
-    value += 1.0;
-    value /= 2.0;
-    value = 1.0 - value;
-    value *= Game::GRAPH_HEIGHT;
-
-    this->disp.setPixel(this->graphX, axis * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + (int)value, this->colors[axis]);
-}
-
-void Game::checkGraphReset() {
-    if (this->graphX > WIDTH) {
-        this->graphX = 0;
-        this->disp.clearScreen();
-        this->drawAxes();
-    }
-}
-
-void Game::initialize() {    
-    //this->disp.clear();
-    this->disp.setOrientation(LCD_ST7735::Rotate270, false);
-    this->disp.setForegroundColor(WHITE);
-    this->disp.setBackgroundColor(BLACK);
+void Game::initialize()
+{
     this->disp.clearScreen();
 
-    this->initializeBall();
+    this->initializeBall();     // start first ball
     this->initializePaddle();
        
-    this->paddleX = WIDTH / 2 - Game::PADDLE_WIDTH / 2;
+//    this->paddleX = WIDTH / 2 - Game::PADDLE_WIDTH / 2;
     this->pwmTicksLeft = 0;
-    this->lives = 4;
+    this->nLives = 4;
+    this->nScore = 0;
     
     this->pwm.period_ms(1);
     this->pwm.write(0.00);
@@ -102,21 +65,19 @@
     
 void Game::initializeBall()
 {
-    this->ball.initialize(&(this->disp),WIDTH / 2 - Game::BALL_RADIUS, HEIGHT / 4 - Game::BALL_RADIUS, Game::BALL_RADIUS);
+    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::initializePaddle()
 {
-    this->paddle.initialize(&(this->disp), WIDTH / 2 - Game::PADDLE_WIDTH/2, HEIGHT - Game::PADDLE_HEIGHT, Game::PADDLE_WIDTH, Game::PADDLE_HEIGHT);
+    this->paddle.initialize(WIDTH / 2 - Game::PADDLE_WIDTH/2, HEIGHT - Game::PADDLE_HEIGHT, Game::PADDLE_WIDTH, Game::PADDLE_HEIGHT);
 }
 
 void Game::tick() {  
     this->checkButtons();
     
     if (this->mode) {
-        //this->clearPaddle();
-        //this->paddle.clear();        
 /*
         if(this->tWait.read_ms()>100)
         {
@@ -129,27 +90,17 @@
         this->ball.update();                    // update the ball position 
     
         this->checkCollision();
-        //this->drawPaddle();
-        //this->paddle.draw();
         this->paddle.redraw();
-
         this->ball.redraw();
         
         this->checkPwm();
+        //this->checkScore(); 
         this->checkLives(); 
         
         wait_ms(25);
     }
-    else {    
-        double x, y, z;
-        this->accel.getXYZ(x, y, z);
-        
-        this->checkGraphReset();
-        this->drawPoint(0, x);
-        this->drawPoint(1, y);
-        this->drawPoint(2, z);
-        this->graphX++;
-
+    else {
+        this->accel.updateGraph();
         wait_ms(100);
     } 
 }
@@ -184,9 +135,7 @@
         
         if (!this->mode)
         {
-            this->graphX = 0;
-            
-            this->drawAxes();
+            this->accel.resetGraph();
         }
         
         this->led1.write(this->mode);
@@ -218,8 +167,14 @@
     }
 }
 
-void Game::drawString(const char* str, int y) {
-    this->disp.drawString(font_ibm, WIDTH / 2 - CHAR_WIDTH * strlen(str) / 2, y, str);         
+void Game::drawString(const char* str, int y)
+{
+    uint8_t width;
+    uint8_t height;
+    
+    this->disp.measureString(font_oem, str, width, height);
+    this->disp.drawString(font_oem, WIDTH / 2 - width / 2, y, str);
+    
 }
 
 void Game::showSplashScreen() {
@@ -231,7 +186,7 @@
     {
 int i=this->checkTilt();
 char buf[256];
-sprintf(buf,"tilt:%d ", i);
+sprintf(buf,"  tilt:%d  ", i);
 this->drawString(buf, HEIGHT / 2 - CHAR_HEIGHT / 2 + (4*CHAR_HEIGHT) ); 
 
         wait_ms(1);
@@ -243,28 +198,16 @@
 
 void Game::updatePaddle() {
     if (!this->left.read())  // note: read is LOW (0) when button pressed
-    {
-        this->paddleX -= Game::PADDLE_SPEED;
         this->paddle.move(Vector(-1 * Game::PADDLE_SPEED, 0));
-    }
     else if (!this->right.read())
-    {
-        this->paddleX += Game::PADDLE_SPEED;
         this->paddle.move(Vector(Game::PADDLE_SPEED, 0));
-    }
     else
     {
         int i=this->checkTilt();        // don't call too often as this I2C is slow and will delay the game
         if(i>0)
-        {
-            this->paddleX += Game::PADDLE_SPEED;
             this->paddle.move(Vector(Game::PADDLE_SPEED, 0));
-        }
         else if(i<0)
-        {
-            this->paddleX -= Game::PADDLE_SPEED;
             this->paddle.move(Vector(-1 * Game::PADDLE_SPEED, 0));
-        }
     }
 }
 
@@ -277,15 +220,16 @@
     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
 
-
+/*
     if (this->paddle.pos.getX() < 0)
         this->paddle.pos.setX(0);
     if (this->paddle.pos.getX() + Game::PADDLE_WIDTH > WIDTH)
         this->paddle.pos.setX(WIDTH - Game::PADDLE_WIDTH);
-
-
-    
+*/
+    this->paddle.checkBoundary(rScreen);
+   
     if(ball.collides(rTop) && this->ball.vSpeed.isUp())      // top wall
     {
         this->ball.Bounce(Vector(1,-1));        // bounce vertical
@@ -303,20 +247,22 @@
     }
     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
+
         //this->ball.Bounce(Vector(1,-1));        // bounce vertical at same speed
-        this->ball.Bounce(Vector(1,-1.1));        // bounce from paddle at higher speed
-        if(ball.collides(rPaddleLeft)) ball.vSpeed.add(Vector(-1,0));
-        if(ball.collides(rPaddleRight)) ball.vSpeed.add(Vector(1,0));
+        ball.Bounce(Vector(1,-1.1));        // bounce from paddle at higher speed
 
-        this->pwmTicksLeft = Game::BOUNCE2_SOUND_TICKS;                       
+        this->pwmTicksLeft = Game::BOUNCE2_SOUND_TICKS;
+        this->nScore++;
+        this->printf(100, 0, "Score: %d ", this->nScore);   
     }
     if(ball.collides(rBottom) && this->ball.vSpeed.isDown())      // bottom gap
     {
-        ball.clearPrev();
-        this->initializeBall();
-        this->lives--;
+        ball.clearPrev();   // clear the ball from its previous position
+        this->nLives--;
+        this->initializeBall();     // start a new ball
     }
-
 }
 
 void Game::checkPwm() {
@@ -329,7 +275,7 @@
     }
 }
 
-void Game::Printf(int x, int y, const char *szFormat, ...)
+void Game::printf(int x, int y, const char *szFormat, ...)
 {
     char szBuffer[256];
     va_list args;
@@ -337,12 +283,12 @@
     va_start(args, szFormat);
     vsprintf(szBuffer, szFormat, args);
     va_end(args);
-    this->disp.drawString(font_ibm, x, y, szBuffer);
+    this->disp.drawString(font_oem, x, y, szBuffer);
 }
 
 
 void Game::checkLives() {
-    if (this->lives == 0) {
+    if (this->nLives == 0) {
         this->disp.clearScreen();
         
         this->drawString(Game::LOSE_1, HEIGHT / 2 - CHAR_HEIGHT); 
@@ -354,6 +300,6 @@
         this->initialize();
     }
     else {
-        this->Printf(0, 0, "%d", this->lives);   
+        this->printf(0, 0, "%d", this->nLives);   
     }
 }
\ No newline at end of file