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:
0:87ab172a74b4
Child:
1:c1ee4c699517
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Game.cpp	Wed Jan 28 17:32:54 2015 +0000
@@ -0,0 +1,359 @@
+#include "Game.h"
+
+const char* Game::LOSE_1 = "You lose.";
+const char* Game::LOSE_2 = "Press ship to restart.";
+const char* Game::SPLASH_1 = "Press ship to start.";
+const char* Game::SPLASH_2 = "Press robot to switch.";
+const char* Game::SPLASH_3 = "mod:Tilt by Maxint.";
+
+
+#define WHITE Color565::White
+#define BLACK Color565::Black
+#define BLUE Color565::Blue
+#define RED Color565::Red
+#define YELLOW Color565::Yellow
+
+#define CHAR_WIDTH 8
+#define CHAR_HEIGHT 8
+#define HEIGHT this->disp.getHeight()
+#define WIDTH this->disp.getWidth()
+
+
+
+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)
+{
+    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);
+    }
+}
+
+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);
+    this->disp.clearScreen();
+
+    this->initializeBall();
+    this->initializePaddle();
+       
+    this->paddleX = WIDTH / 2 - Game::PADDLE_WIDTH / 2;
+    this->pwmTicksLeft = 0;
+    this->lives = 4;
+    
+    this->pwm.period_ms(1);
+    this->pwm.write(0.00);
+
+    this->tWait.start();      // start the timer
+    
+}
+    
+void Game::initializeBall()
+{
+    this->ball.initialize(&(this->disp),WIDTH / 2 - Game::BALL_RADIUS, HEIGHT / 4 - Game::BALL_RADIUS, Game::BALL_RADIUS);
+    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);
+}
+
+void Game::tick() {  
+    this->checkButtons();
+    
+    if (this->mode) {
+        //this->clearPaddle();
+        //this->paddle.clear();        
+/*
+        if(this->tWait.read_ms()>100)
+        {
+            this->updatePaddle();
+            this->tWait.reset();
+        }
+*/
+        this->updatePaddle();
+        this->ball.vSpeed.add(Vector(0, 0.1));    // add some gravity
+        this->ball.update();                    // update the ball position 
+    
+        this->checkCollision();
+        //this->drawPaddle();
+        //this->paddle.draw();
+        this->paddle.redraw();
+
+        this->ball.redraw();
+        
+        this->checkPwm();
+        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++;
+
+        wait_ms(100);
+    } 
+}
+
+int Game::checkTilt()
+{    // move the paddle by tilting the board left or righr
+    double x, y, z;
+    //int nStart=this->tWait.read_ms();
+    this->accel.getXYZ(x, y, z);
+
+    //printDouble((double)this->tWait.read_ms()-nStart, 10, 10);
+/*
+printDouble(x, 0, 0);
+char buf[256];
+sprintf(buf,"tilt:%0.1f", x);
+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);
+}
+
+void Game::checkButtons()
+{
+    if (!this->square.read())
+    {
+        this->mode = !this->mode;
+        
+        //this->disp.clear();
+        this->disp.clearScreen();
+        
+        if (!this->mode)
+        {
+            this->graphX = 0;
+            
+            this->drawAxes();
+        }
+        
+        this->led1.write(this->mode);
+        this->led2.write(!this->mode);
+    }
+    else
+    {  
+        bool isUp = !this->up.read();
+        bool isDown = !this->down.read();
+        
+        if (isUp && isDown) goto end;
+        if (!isUp && !isDown) goto end;
+        
+        if (isUp && this->lastUp) goto end;
+        if (isDown && this->lastDown) goto end;
+        
+        if (isUp)
+        {
+            this->ball.changeSpeed(true);
+        }
+        else if (isDown)
+        {
+            this->ball.changeSpeed(false);
+        }
+    
+end:
+        this->lastUp = isUp;
+        this->lastDown = isDown;
+    }
+}
+
+void Game::drawString(const char* str, int y) {
+    this->disp.drawString(font_ibm, WIDTH / 2 - CHAR_WIDTH * strlen(str) / 2, y, str);         
+}
+
+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)); 
+           
+    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) ); 
+
+        wait_ms(1);
+    }
+        
+    this->disp.clearScreen();
+}
+
+
+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));
+        }
+    }
+}
+
+void Game::checkCollision()
+{
+    Rectangle rTop=Rectangle(0, -10, WIDTH, 0);                // 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
+
+
+    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);
+
+
+    
+    if(ball.collides(rTop) && this->ball.vSpeed.isUp())      // top wall
+    {
+        this->ball.Bounce(Vector(1,-1));        // bounce vertical
+        this->pwmTicksLeft = Game::BOUNCE1_SOUND_TICKS;
+    }
+    if(ball.collides(rRight) && this->ball.vSpeed.isRight())      // right wall
+    {
+        this->ball.Bounce(Vector(-1,1));        // bounce horizontal
+        this->pwmTicksLeft = Game::BOUNCE1_SOUND_TICKS;
+    }
+    if(ball.collides(rLeft) && this->ball.vSpeed.isLeft())      // left wall
+    {
+        this->ball.Bounce(Vector(-1,1));        // bounce horizontal
+        this->pwmTicksLeft = Game::BOUNCE1_SOUND_TICKS;
+    }
+    if(ball.collides(rPaddle) && this->ball.vSpeed.isDown())      // paddle
+    {
+        //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));
+
+        this->pwmTicksLeft = Game::BOUNCE2_SOUND_TICKS;                       
+    }
+    if(ball.collides(rBottom) && this->ball.vSpeed.isDown())      // bottom gap
+    {
+        ball.clearPrev();
+        this->initializeBall();
+        this->lives--;
+    }
+
+}
+
+void Game::checkPwm() {
+    if (this->pwmTicksLeft == 0) {
+        this->pwm.write(0.0);
+    }
+    else {
+        this->pwmTicksLeft--;
+        this->pwm.write(0.5); 
+    }
+}
+
+void Game::Printf(int x, int y, const char *szFormat, ...)
+{
+    char szBuffer[256];
+    va_list args;
+
+    va_start(args, szFormat);
+    vsprintf(szBuffer, szFormat, args);
+    va_end(args);
+    this->disp.drawString(font_ibm, x, y, szBuffer);
+}
+
+
+void Game::checkLives() {
+    if (this->lives == 0) {
+        this->disp.clearScreen();
+        
+        this->drawString(Game::LOSE_1, HEIGHT / 2 - CHAR_HEIGHT); 
+        this->drawString(Game::LOSE_2, HEIGHT / 2);  
+        
+        while (this->circle.read())
+            wait_ms(1);
+            
+        this->initialize();
+    }
+    else {
+        this->Printf(0, 0, "%d", this->lives);   
+    }
+}
\ No newline at end of file