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 11:ef3cbc443f27, committed 2015-03-02
- Comitter:
- maxint
- Date:
- Mon Mar 02 10:16:55 2015 +0000
- Parent:
- 10:1d75861242f7
- Commit message:
- more clean-up
Changed in this revision
--- a/Game.cpp Sun Mar 01 13:16:19 2015 +0000 +++ b/Game.cpp Mon Mar 02 10:16:55 2015 +0000 @@ -36,7 +36,7 @@ this->nGameTickDelay=25; // game tickdelay can be adjusted using up/down - nTopWall=8; + nTopWall=8; // room for display of game stats such as the score for(int i=0; i<NUM_WALLS; i++) aWalls[i]=Wall(&(this->disp)); for(int i=0; i<NUM_HOLES; i++) @@ -55,22 +55,16 @@ void Game::initialize() { -// disp.clearScreen(); -// snd.reset(); nBalls = 4; nScore = 0; tWait.start(); // start the timer - //char sWalls[]="W010010A0"; for(int i=0; i<NUM_WALLS; i++) - { aWalls[i]=Wall(&(disp)); - } initLevel(); -// setNoBall(); // reset all balls newBall(); // start first ball snd.play("T240 L16 O5 D E F"); } @@ -100,7 +94,7 @@ break; case 1: addWalls(szLevel1); - addHoles("9111 0312 9412 4612 5612 "); + addHoles("6411 0312 9412 4612 5612 "); break; case 2: addWalls(szLevel1); @@ -177,11 +171,7 @@ 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(); } } } @@ -203,7 +193,6 @@ if(aWalls[i].fActive) { aWalls[i].draw(); - //snd.beepShort(); } } @@ -227,11 +216,7 @@ 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(); } } } @@ -253,30 +238,13 @@ 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() @@ -299,22 +267,6 @@ 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() { @@ -361,23 +313,13 @@ 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); + + // don't let the tilt generate too much gravity while(vGravity.getSize()>0.5) vGravity.multiply(Vector(0.9, 0.9)); -// 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() @@ -387,7 +329,6 @@ wait_ms(250); // el-cheapo deboounce this->mode = !this->mode; - //this->disp.clear(); this->disp.clearScreen(); if (!this->mode) @@ -472,45 +413,27 @@ 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 + Rectangle rTop=Rectangle(0, -10, WIDTH, this->nTopWall); // top wall + Rectangle rBottom=Rectangle(0, HEIGHT, WIDTH, HEIGHT+10); // bottom wall + Rectangle rLeft=Rectangle(-10, 0, 0, HEIGHT); // left wall + Rectangle rRight=Rectangle(WIDTH, 0, WIDTH+10, HEIGHT); // right wall - if(ball.collides(rTop) && ball.vSpeed.isUp()) // top 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 - { + 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 - { + 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 - { + if(ball.collides(rBottom) && ball.vSpeed.isDown()) // bottom wall ball.vSpeed.y=0; - //this->snd.beepShort(); - } for(int i=0; i<NUM_WALLS; i++) // check maze walls { @@ -532,7 +455,10 @@ snd.play("T240 L128 O4 A"); ball.Bounce(Vector(-0.1,1)); } - //ball.vSpeed.multiply(Vector(0,0)); + // TODO: the ball can stick to the end of wall. + // To fix, the above code should compare the position of the ball relative to the wall, not just the speed and wall-orientation + // Although this is a bug, its causes some interesting game-play, so leave it in for now + // Magnetic walls are fun! aWalls[i].draw(); } @@ -547,13 +473,11 @@ 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"); ball.fActive=false; if(i==0) - { // TODO: for now first hole is target hole + { // TODO: for now first hole array is assumed to be the target hole nScore++; snd.play("T240 L16 O5 CDEFG"); } @@ -567,13 +491,10 @@ wait_ms(500); newBall(); // start a new ball - } } } } - - }
--- a/Game.h Sun Mar 01 13:16:19 2015 +0000 +++ b/Game.h Mon Mar 02 10:16:55 2015 +0000 @@ -24,9 +24,7 @@ static const char* SPLASH_1; static const char* SPLASH_2; static const char* SPLASH_3; - //char buf[256]; - //static const int BALL_RADIUS = 3; static const int BALL_RADIUS = 5; static const int HOLE_RADIUS = 6; static const char I2C_ADDR = 0x1C << 1; @@ -40,7 +38,6 @@ DigitalOut led1; DigitalOut led2; AnalogIn ain; - //I2C i2c; LCD_ST7735 disp; SoundFX snd; @@ -76,20 +73,15 @@ void drawString(const char* str, int y); - void setNoBall(); void newBall(); void updateBall(); void redrawBall(); - int countBall(); void checkBallCollision(); - //void redrawTopWall(); void checkTilt(); void checkButtons(); - //void checkCollision(); void printf(int x, int y, const char *szFormat, ...); - //void checkBall(); void checkNumBalls(); public:
--- a/LCD_ST7735.lib Sun Mar 01 13:16:19 2015 +0000 +++ b/LCD_ST7735.lib Mon Mar 02 10:16:55 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/taylorza/code/LCD_ST7735/#c94d0a2c2ba0 +http://mbed.org/users/taylorza/code/LCD_ST7735/#516f15979b53
--- a/RETRO_BallsAndThings.lib Sun Mar 01 13:16:19 2015 +0000 +++ b/RETRO_BallsAndThings.lib Mon Mar 02 10:16:55 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/maxint/code/RETRO_BallsAndThings/#5fd14e840fa8 +http://developer.mbed.org/users/maxint/code/RETRO_BallsAndThings/#19dd2a538cbe