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.

Game.cpp

Committer:
maxint
Date:
2015-02-28
Revision:
7:6e7b789f4060
Parent:
6:e2eead4e53fb
Child:
8:b119501f4ef0

File content as of revision 7:6e7b789f4060:

#include "Game.h"

const char* Game::LOSE_1 = "Game over.";
const char* Game::LOSE_2 = "Press ship to restart.";
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";


#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), 
    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);
    this->disp.setBackgroundColor(BLACK);
    this->disp.clearScreen();

    srand(this->ain.read_u16());
    
    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

    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();
}

void Game::printDouble(double value, int x, int y)
{
    char buffer[10];
    int len = sprintf(buffer, "%.1f ", value);
    
    this->disp.drawString(font_oem, x, y, buffer);
}

void Game::initialize()
{
    this->disp.clearScreen();

    this->snd.reset();
    this->nBalls = 4;
    this->nScore = 0;
    
    this->tWait.start();      // start the timer

    //char sWalls[]="W010010A0";
    for(int i=0; i<NUM_WALLS; i++)
    {
        aWalls[i]=Wall(&(this->disp));
    }

    // 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");


    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
    // 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::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+1,y2*16+1));
            aWalls[i].fActive=true;
            //printf(0, 0, "W: %d,%d - %d,%d", x1, y1, x2, y2);            
            break;

            //aWalls[i].draw();
            //snd.beepShort();
        }
    }
}


void Game::drawWalls()
{
    for(int i=0; i<NUM_WALLS; i++)
        if(aWalls[i].fActive)
        {
            aWalls[i].draw();
            //snd.beepShort();
        }
}

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
/*    for(int i=0; i<NUM_BALLS; i++)
        this->aBalls[i].fActive=false;
*/
}

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;
}

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);
}





void Game::tick()
{  
    checkButtons();
    
    if(mode)
    {
/*
        if(this->tWait.read_ms()>100)
        {
            this->tWait.reset();
        }
*/

        updateBall();                    // update the ball positions
    
        checkBallCollision();

        redrawBall();
        
        if(this->tWait.read_ms()>100)
        {   // redraw walls and holes every tenth second
            drawWalls();
            drawHoles();

            checkNumBalls();

            this->tWait.reset();
        }
        
//        this->snd.checkPwm();
        //this->checkScore(); 
        //this->checkBall(); 
        
        wait_ms(nGameTickDelay);  // can be adjusted using up/down
    }
    else
    {
        accel.updateGraph();
        wait_ms(100);
    } 
}

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();
    
    //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 ); 
*/

    //return(Vector(0,0));
}

void Game::checkButtons()
{
    if(!this->square.read())       // note: button.read() is false (LOW/0) when pressed
    {
        wait_ms(250);   // el-cheapo deboounce
        this->mode = !this->mode;
        
        //this->disp.clear();
        this->disp.clearScreen();
        
        if (!this->mode)
        {
            this->accel.resetGraph();
        }
        
        this->led1.write(this->mode);
        this->led2.write(!this->mode);
    }
    else if(!this->circle.read() && this->mode)       // note: button.read() is false (LOW/0) when pressed
    {
        bool fMute=this->snd.getMute();
        fMute=!fMute;
        this->snd.setMute(fMute);
        this->led2.write(fMute);
        wait_ms(250);   // el-cheapo deboounce
    }
    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)
        {
            if(this->nGameTickDelay<1000) this->nGameTickDelay=(float)this->nGameTickDelay*1.20;
            this->printf(100, 0, "Speed: %d  ", this->nGameTickDelay);   
        }
        else if (isDown)
        {
            if(this->nGameTickDelay>5) this->nGameTickDelay=(float)this->nGameTickDelay/1.20;
            this->printf(100, 0, "Speed: %d  ", this->nGameTickDelay);   
        }
    
end:
        this->lastUp = isUp;
        this->lastDown = isDown;
    }
}

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()
{
    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())
    {
//checkTilt();
//printf(00, 120, "tilt: %.2f, %.2f  ", vGravity.x, vGravity.y);

        wait_ms(1);
    }
    wait_ms(250);   // el-cheapo deboounce

    //this->disp.clearScreen();
    this->initialize();     // start a new game
}

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

    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++)      // test maze walls
    {
        if(aWalls[i].fActive)
        {
            Rectangle rWall=aWalls[i].getRect();
            if(ball.collides(rWall))
            {
                //printf(30, 0, "b: %.2f", ball.vSpeed.getSize());
                if(rWall.isHorizontal())
                {
                    if(fabs(ball.vSpeed.y)>0.8)
                        snd.play("T240 L128 O4 A");
                    ball.Bounce(Vector(1,-0.1));
                }
                else
                {
                    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

                }                                    
            }
        }
    }
    
    
}


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_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);
    }
}