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.

Committer:
maxint
Date:
Wed Jan 28 17:32:54 2015 +0000
Revision:
0:87ab172a74b4
Child:
1:c1ee4c699517
Separated elements as objects, added gravity and bouncespeed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maxint 0:87ab172a74b4 1 #include "Ball.h"
maxint 0:87ab172a74b4 2
maxint 0:87ab172a74b4 3 Ball::Ball() : cBall(0,0,0), vSpeed()
maxint 0:87ab172a74b4 4 { // constructor
maxint 0:87ab172a74b4 5 uColorHigh=Color565::fromRGB(0xFF, 66, 66);
maxint 0:87ab172a74b4 6 uColorMid=Color565::fromRGB(0xBB, 22, 22);
maxint 0:87ab172a74b4 7 uColorLow=Color565::fromRGB(0x88, 0, 0);
maxint 0:87ab172a74b4 8 }
maxint 0:87ab172a74b4 9
maxint 0:87ab172a74b4 10 void Ball::initialize(LCD_ST7735* pDisp, int nX, int nY, int nR)
maxint 0:87ab172a74b4 11 {
maxint 0:87ab172a74b4 12 this->pDisp=pDisp;
maxint 0:87ab172a74b4 13 this->pos.set(nX, nY);
maxint 0:87ab172a74b4 14 this->cBall=Circle(nX, nY, nR);
maxint 0:87ab172a74b4 15 }
maxint 0:87ab172a74b4 16
maxint 0:87ab172a74b4 17 void Ball::setSpeed(int X, int Y)
maxint 0:87ab172a74b4 18 {
maxint 0:87ab172a74b4 19 this->vSpeed.set(X, Y);
maxint 0:87ab172a74b4 20 }
maxint 0:87ab172a74b4 21
maxint 0:87ab172a74b4 22 void Ball::changeSpeed(bool fUp)
maxint 0:87ab172a74b4 23 {
maxint 0:87ab172a74b4 24 if(fUp)
maxint 0:87ab172a74b4 25 {
maxint 0:87ab172a74b4 26 if(this->vSpeed.getSize()<=4.0) this->vSpeed.add(1.0);
maxint 0:87ab172a74b4 27 }
maxint 0:87ab172a74b4 28 else
maxint 0:87ab172a74b4 29 {
maxint 0:87ab172a74b4 30 if(this->vSpeed.getSize()>=1.0) this->vSpeed.add(-1.0);
maxint 0:87ab172a74b4 31 }
maxint 0:87ab172a74b4 32 }
maxint 0:87ab172a74b4 33
maxint 0:87ab172a74b4 34 void Ball::update()
maxint 0:87ab172a74b4 35 {
maxint 0:87ab172a74b4 36 this->pos.move(this->vSpeed);
maxint 0:87ab172a74b4 37 this->cBall.setXY(this->pos.getX(), this->pos.getY());
maxint 0:87ab172a74b4 38 }
maxint 0:87ab172a74b4 39
maxint 0:87ab172a74b4 40 Circle Ball::getBoundingCircle()
maxint 0:87ab172a74b4 41 {
maxint 0:87ab172a74b4 42 return(cBall);
maxint 0:87ab172a74b4 43 }
maxint 0:87ab172a74b4 44
maxint 0:87ab172a74b4 45 bool Ball::collides(Rectangle r)
maxint 0:87ab172a74b4 46 {
maxint 0:87ab172a74b4 47 Rectangle rBall=this->cBall.getBoundingRectangle();
maxint 0:87ab172a74b4 48
maxint 0:87ab172a74b4 49 /*
maxint 0:87ab172a74b4 50 char szBuffer[256];
maxint 0:87ab172a74b4 51 sprintf(szBuffer, "c:%d,%d ", cBall.getX(), cBall.getY());
maxint 0:87ab172a74b4 52 this->pDisp->drawString(font_ibm, 0, 0, szBuffer);
maxint 0:87ab172a74b4 53 */
maxint 0:87ab172a74b4 54 return(rBall.collides(r));
maxint 0:87ab172a74b4 55 }
maxint 0:87ab172a74b4 56
maxint 0:87ab172a74b4 57 void Ball::Bounce(Vector vBounce)
maxint 0:87ab172a74b4 58 { // change the direction in a certain direction
maxint 0:87ab172a74b4 59 this->vSpeed.multiply(vBounce);
maxint 0:87ab172a74b4 60
maxint 0:87ab172a74b4 61 // check speed w/max
maxint 0:87ab172a74b4 62 if(this->vSpeed.y>5.0) this->vSpeed.y=5;
maxint 0:87ab172a74b4 63 }
maxint 0:87ab172a74b4 64
maxint 0:87ab172a74b4 65
maxint 0:87ab172a74b4 66
maxint 0:87ab172a74b4 67 void Ball::clear()
maxint 0:87ab172a74b4 68 {
maxint 0:87ab172a74b4 69 Point p=pos.getCur();
maxint 0:87ab172a74b4 70 this->pDisp->fillCircle(p.getX(),p.getY(), cBall.getRadius(), Color565::Black, Color565::Black);
maxint 0:87ab172a74b4 71 }
maxint 0:87ab172a74b4 72
maxint 0:87ab172a74b4 73 void Ball::clearPrev()
maxint 0:87ab172a74b4 74 {
maxint 0:87ab172a74b4 75 Point p=pos.getPrev();
maxint 0:87ab172a74b4 76 this->pDisp->fillCircle(p.getX(),p.getY(), cBall.getRadius(), Color565::Black, Color565::Black);
maxint 0:87ab172a74b4 77 }
maxint 0:87ab172a74b4 78
maxint 0:87ab172a74b4 79
maxint 0:87ab172a74b4 80 void Ball::draw()
maxint 0:87ab172a74b4 81 { // render the object on the screen, based on its current position
maxint 0:87ab172a74b4 82 Point p=pos.getCur();
maxint 0:87ab172a74b4 83 if(cBall.getRadius()>3)
maxint 0:87ab172a74b4 84 {
maxint 0:87ab172a74b4 85 this->pDisp->fillCircle(p.getX(), p.getY(), cBall.getRadius(), this->uColorLow, this->uColorLow);
maxint 0:87ab172a74b4 86 this->pDisp->fillCircle(p.getX()-cBall.getRadius()/3, p.getY()-cBall.getRadius()/3, cBall.getRadius()/2, this->uColorMid, this->uColorMid);
maxint 0:87ab172a74b4 87 this->pDisp->setPixel(p.getX()-cBall.getRadius()/2, p.getY()-cBall.getRadius()/2, this->uColorHigh);
maxint 0:87ab172a74b4 88 }
maxint 0:87ab172a74b4 89 else
maxint 0:87ab172a74b4 90 this->pDisp->fillCircle(p.getX(), p.getY(), cBall.getRadius(), this->uColorMid, this->uColorMid);
maxint 0:87ab172a74b4 91 }
maxint 0:87ab172a74b4 92
maxint 0:87ab172a74b4 93 void Ball::redraw()
maxint 0:87ab172a74b4 94 { // redraw the ball if its position has changed
maxint 0:87ab172a74b4 95 if(pos.hasChanged())
maxint 0:87ab172a74b4 96 {
maxint 0:87ab172a74b4 97 clearPrev();
maxint 0:87ab172a74b4 98 draw();
maxint 0:87ab172a74b4 99 }
maxint 0:87ab172a74b4 100 }