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:
Sat Feb 28 11:40:39 2015 +0000
Revision:
6:e2eead4e53fb
Parent:
5:67e75a4f0b52
Child:
7:6e7b789f4060
Added walls to maze

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maxint 0:87ab172a74b4 1 #include "Game.h"
maxint 0:87ab172a74b4 2
maxint 3:ca8b21da67dc 3 const char* Game::LOSE_1 = "Game over.";
maxint 0:87ab172a74b4 4 const char* Game::LOSE_2 = "Press ship to restart.";
maxint 6:e2eead4e53fb 5 const char* Game::SPLASH_1 = "-*- Ball and holes -*-";
maxint 2:d4de5a5866fe 6 const char* Game::SPLASH_2 = "Press ship to start.";
maxint 2:d4de5a5866fe 7 const char* Game::SPLASH_3 = "Made by Maxint";
maxint 0:87ab172a74b4 8
maxint 0:87ab172a74b4 9
maxint 0:87ab172a74b4 10 #define WHITE Color565::White
maxint 0:87ab172a74b4 11 #define BLACK Color565::Black
maxint 0:87ab172a74b4 12 #define BLUE Color565::Blue
maxint 0:87ab172a74b4 13 #define RED Color565::Red
maxint 0:87ab172a74b4 14 #define YELLOW Color565::Yellow
maxint 0:87ab172a74b4 15
maxint 0:87ab172a74b4 16 #define CHAR_WIDTH 8
maxint 0:87ab172a74b4 17 #define CHAR_HEIGHT 8
maxint 0:87ab172a74b4 18 #define HEIGHT this->disp.getHeight()
maxint 0:87ab172a74b4 19 #define WIDTH this->disp.getWidth()
maxint 0:87ab172a74b4 20
maxint 0:87ab172a74b4 21
maxint 0:87ab172a74b4 22
maxint 0:87ab172a74b4 23 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),
maxint 6:e2eead4e53fb 24 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.1), vFriction(-0.02, -0.02), ball(&disp)
maxint 0:87ab172a74b4 25 {
maxint 1:c1ee4c699517 26 this->disp.setOrientation(LCD_ST7735::Rotate270, false);
maxint 1:c1ee4c699517 27 this->disp.setForegroundColor(WHITE);
maxint 1:c1ee4c699517 28 this->disp.setBackgroundColor(BLACK);
maxint 1:c1ee4c699517 29 this->disp.clearScreen();
maxint 1:c1ee4c699517 30
maxint 0:87ab172a74b4 31 srand(this->ain.read_u16());
maxint 0:87ab172a74b4 32
maxint 0:87ab172a74b4 33 this->lastUp = false;
maxint 0:87ab172a74b4 34 this->lastDown = false;
maxint 3:ca8b21da67dc 35 this->mode = true; // mode: true=game, false=graph
maxint 6:e2eead4e53fb 36
maxint 3:ca8b21da67dc 37 this->nGameTickDelay=25; // game tickdelay can be adjusted using up/down
maxint 2:d4de5a5866fe 38
maxint 6:e2eead4e53fb 39 nTopWall=8;
maxint 6:e2eead4e53fb 40 for(int i=0; i<NUM_WALLS; i++)
maxint 6:e2eead4e53fb 41 aWalls[i]=Wall(&(this->disp));
maxint 5:67e75a4f0b52 42
maxint 5:67e75a4f0b52 43 this->snd.reset();
maxint 0:87ab172a74b4 44 }
maxint 0:87ab172a74b4 45
maxint 2:d4de5a5866fe 46 void Game::printDouble(double value, int x, int y)
maxint 2:d4de5a5866fe 47 {
maxint 0:87ab172a74b4 48 char buffer[10];
maxint 0:87ab172a74b4 49 int len = sprintf(buffer, "%.1f ", value);
maxint 0:87ab172a74b4 50
maxint 1:c1ee4c699517 51 this->disp.drawString(font_oem, x, y, buffer);
maxint 0:87ab172a74b4 52 }
maxint 0:87ab172a74b4 53
maxint 1:c1ee4c699517 54 void Game::initialize()
maxint 1:c1ee4c699517 55 {
maxint 0:87ab172a74b4 56 this->disp.clearScreen();
maxint 0:87ab172a74b4 57
maxint 2:d4de5a5866fe 58 this->snd.reset();
maxint 3:ca8b21da67dc 59 this->nBalls = 4;
maxint 1:c1ee4c699517 60 this->nScore = 0;
maxint 0:87ab172a74b4 61
maxint 5:67e75a4f0b52 62 // this->tWait.start(); // start the timer
maxint 5:67e75a4f0b52 63
maxint 6:e2eead4e53fb 64 //char sWalls[]="W010010A0";
maxint 6:e2eead4e53fb 65 for(int i=0; i<NUM_WALLS; i++)
maxint 6:e2eead4e53fb 66 {
maxint 6:e2eead4e53fb 67 aWalls[i]=Wall(&(this->disp));
maxint 6:e2eead4e53fb 68 //aWalls[i].fActive=false;
maxint 6:e2eead4e53fb 69 }
maxint 6:e2eead4e53fb 70
maxint 6:e2eead4e53fb 71 // hor
maxint 6:e2eead4e53fb 72 addWall("01:1"); // top edge
maxint 6:e2eead4e53fb 73 addWall("0292");
maxint 6:e2eead4e53fb 74 addWall("0343");
maxint 6:e2eead4e53fb 75 addWall("63:3");
maxint 6:e2eead4e53fb 76 addWall("2484");
maxint 6:e2eead4e53fb 77 addWall("0545");
maxint 6:e2eead4e53fb 78 addWall("65:5");
maxint 6:e2eead4e53fb 79 addWall("1797");
maxint 6:e2eead4e53fb 80 //addWall("08:8"); // bottom
maxint 6:e2eead4e53fb 81
maxint 6:e2eead4e53fb 82 // vert
maxint 6:e2eead4e53fb 83 addWall("5257");
maxint 6:e2eead4e53fb 84
maxint 6:e2eead4e53fb 85 addWall("1617");
maxint 6:e2eead4e53fb 86 addWall("2526");
maxint 6:e2eead4e53fb 87 addWall("3637");
maxint 6:e2eead4e53fb 88 addWall("4546");
maxint 6:e2eead4e53fb 89
maxint 6:e2eead4e53fb 90 addWall("6566");
maxint 6:e2eead4e53fb 91 addWall("7677");
maxint 6:e2eead4e53fb 92 addWall("8586");
maxint 6:e2eead4e53fb 93 addWall("9697");
maxint 6:e2eead4e53fb 94
maxint 6:e2eead4e53fb 95 this->setNoBall(); // reset all balls
maxint 5:67e75a4f0b52 96 this->newBall(); // start first ball
maxint 5:67e75a4f0b52 97 this->snd.play("T240 L16 O5 D E F");
maxint 2:d4de5a5866fe 98
maxint 6:e2eead4e53fb 99 drawWalls();
maxint 2:d4de5a5866fe 100 }
maxint 2:d4de5a5866fe 101
maxint 6:e2eead4e53fb 102 Point Game::getGridPos(char *sPos)
maxint 6:e2eead4e53fb 103 { // get item pos based on a grid definition of 16x16 pixel squares, layed out in a 10x8 grid
maxint 6:e2eead4e53fb 104 // sPos is a 2 character string containing the coordinates in decimal notation: xy
maxint 6:e2eead4e53fb 105 int x=(sPos[0]-'0')*16+8;
maxint 6:e2eead4e53fb 106 int y=(sPos[1]-'0')*16+8;
maxint 6:e2eead4e53fb 107 return(Point(x,y));
maxint 2:d4de5a5866fe 108 }
maxint 2:d4de5a5866fe 109
maxint 6:e2eead4e53fb 110 void Game::addWall(char *sWall)
maxint 6:e2eead4e53fb 111 { // add a wall based on a grid definition of 16x16 pixel squares, layed out in a 10x8 grid
maxint 6:e2eead4e53fb 112 // sWall is a 4 character string containing the edge coordinates in decimal notation: xyXY
maxint 6:e2eead4e53fb 113 // grid axis range from x: '0'-'9', ':'=10 - y: '0'-'8'
maxint 6:e2eead4e53fb 114 for(int i=0; i<NUM_WALLS; i++)
maxint 6:e2eead4e53fb 115 {
maxint 6:e2eead4e53fb 116 if(!aWalls[i].fActive)
maxint 6:e2eead4e53fb 117 {
maxint 6:e2eead4e53fb 118 int x1=sWall[0]-'0';
maxint 6:e2eead4e53fb 119 int y1=sWall[1]-'0';
maxint 6:e2eead4e53fb 120 int x2=sWall[2]-'0';
maxint 6:e2eead4e53fb 121 int y2=sWall[3]-'0';
maxint 6:e2eead4e53fb 122 aWalls[i].setRect(Rectangle(x1*16,y1*16,x2*16,y2*16));
maxint 6:e2eead4e53fb 123 aWalls[i].fActive=true;
maxint 6:e2eead4e53fb 124 printf(0, 0, "W: %d,%d - %d,%d", x1, y1, x2, y2);
maxint 6:e2eead4e53fb 125 break;
maxint 2:d4de5a5866fe 126
maxint 6:e2eead4e53fb 127 //aWalls[i].draw();
maxint 6:e2eead4e53fb 128 //snd.beepShort();
maxint 2:d4de5a5866fe 129 }
maxint 2:d4de5a5866fe 130 }
maxint 0:87ab172a74b4 131 }
maxint 0:87ab172a74b4 132
maxint 6:e2eead4e53fb 133 void Game::drawWalls()
maxint 2:d4de5a5866fe 134 {
maxint 6:e2eead4e53fb 135 for(int i=0; i<NUM_WALLS; i++)
maxint 6:e2eead4e53fb 136 if(aWalls[i].fActive)
maxint 6:e2eead4e53fb 137 {
maxint 6:e2eead4e53fb 138 aWalls[i].draw();
maxint 6:e2eead4e53fb 139 //snd.beepShort();
maxint 6:e2eead4e53fb 140 }
maxint 6:e2eead4e53fb 141 }
maxint 2:d4de5a5866fe 142
maxint 2:d4de5a5866fe 143
maxint 6:e2eead4e53fb 144 void Game::setNoBall()
maxint 6:e2eead4e53fb 145 { // make sure no balls are active
maxint 6:e2eead4e53fb 146 /* for(int i=0; i<NUM_BALLS; i++)
maxint 6:e2eead4e53fb 147 this->aBalls[i].fActive=false;
maxint 6:e2eead4e53fb 148 */
maxint 2:d4de5a5866fe 149 }
maxint 2:d4de5a5866fe 150
maxint 6:e2eead4e53fb 151 void Game::newBall()
maxint 6:e2eead4e53fb 152 { // add a ball to the game
maxint 6:e2eead4e53fb 153 Point ptBall=getGridPos("01");
maxint 6:e2eead4e53fb 154 ball.initialize(ptBall.getX(), ptBall.getY(), Game::BALL_RADIUS, Color565::White);
maxint 6:e2eead4e53fb 155 //float ftRandX=rand() % 2 ? 1 : -1;
maxint 6:e2eead4e53fb 156 //float ftRandY=rand() % 2 ? 1 : -1;
maxint 6:e2eead4e53fb 157 //this->aBalls[i].setSpeed(ftRandX, ftRandY);
maxint 6:e2eead4e53fb 158 // float ftRandX=((rand() % 20) - 10)/5.0; // left/right at random speed
maxint 6:e2eead4e53fb 159 // float ftRandY=((rand() % 10) - 10)/5.0; // up at random speed
maxint 6:e2eead4e53fb 160 // ball.vSpeed.set(ftRandX, ftRandY);
maxint 6:e2eead4e53fb 161 //this->aBalls[i].fActive=true;
maxint 2:d4de5a5866fe 162 }
maxint 2:d4de5a5866fe 163
maxint 6:e2eead4e53fb 164 void Game::updateBall()
maxint 6:e2eead4e53fb 165 {
maxint 6:e2eead4e53fb 166 ball.update(); // update the ball position
maxint 6:e2eead4e53fb 167
maxint 6:e2eead4e53fb 168 // increase speed based on gravity
maxint 6:e2eead4e53fb 169 checkTilt();
maxint 6:e2eead4e53fb 170 if(ball.vSpeed.getSize()<1.0)
maxint 6:e2eead4e53fb 171 ball.vSpeed.add(this->vGravity); // add some gravity
maxint 6:e2eead4e53fb 172
maxint 6:e2eead4e53fb 173 // decrease speed based on friction
maxint 6:e2eead4e53fb 174 Vector vDecel=ball.vSpeed.getNormalized();
maxint 6:e2eead4e53fb 175 vDecel.multiply(vFriction);
maxint 6:e2eead4e53fb 176 ball.vSpeed.add(vDecel);
maxint 6:e2eead4e53fb 177 }
maxint 6:e2eead4e53fb 178
maxint 6:e2eead4e53fb 179 void Game::redrawBall()
maxint 6:e2eead4e53fb 180 {
maxint 6:e2eead4e53fb 181 ball.redraw(); // update the ball position
maxint 6:e2eead4e53fb 182 }
maxint 6:e2eead4e53fb 183
maxint 6:e2eead4e53fb 184 int Game::countBall()
maxint 2:d4de5a5866fe 185 {
maxint 2:d4de5a5866fe 186 int nResult=0;
maxint 6:e2eead4e53fb 187 /*
maxint 2:d4de5a5866fe 188 for(int i=0; i<NUM_BALLS; i++)
maxint 2:d4de5a5866fe 189 {
maxint 2:d4de5a5866fe 190 if(this->aBalls[i].fActive)
maxint 2:d4de5a5866fe 191 nResult++;
maxint 2:d4de5a5866fe 192 }
maxint 6:e2eead4e53fb 193 */
maxint 2:d4de5a5866fe 194 return(nResult);
maxint 2:d4de5a5866fe 195 }
maxint 2:d4de5a5866fe 196
maxint 2:d4de5a5866fe 197
maxint 2:d4de5a5866fe 198
maxint 2:d4de5a5866fe 199
maxint 2:d4de5a5866fe 200
maxint 2:d4de5a5866fe 201 void Game::tick()
maxint 2:d4de5a5866fe 202 {
maxint 6:e2eead4e53fb 203 checkButtons();
maxint 0:87ab172a74b4 204
maxint 6:e2eead4e53fb 205 if(mode)
maxint 6:e2eead4e53fb 206 {
maxint 0:87ab172a74b4 207 /*
maxint 0:87ab172a74b4 208 if(this->tWait.read_ms()>100)
maxint 0:87ab172a74b4 209 {
maxint 0:87ab172a74b4 210 this->tWait.reset();
maxint 0:87ab172a74b4 211 }
maxint 0:87ab172a74b4 212 */
maxint 2:d4de5a5866fe 213
maxint 6:e2eead4e53fb 214 updateBall(); // update the ball positions
maxint 0:87ab172a74b4 215
maxint 6:e2eead4e53fb 216 checkBallCollision();
maxint 5:67e75a4f0b52 217
maxint 6:e2eead4e53fb 218 redrawBall();
maxint 6:e2eead4e53fb 219
maxint 6:e2eead4e53fb 220 drawWalls();
maxint 0:87ab172a74b4 221
maxint 5:67e75a4f0b52 222 // this->snd.checkPwm();
maxint 1:c1ee4c699517 223 //this->checkScore();
maxint 6:e2eead4e53fb 224 //this->checkBall();
maxint 0:87ab172a74b4 225
maxint 6:e2eead4e53fb 226 wait_ms(nGameTickDelay); // can be adjusted using up/down
maxint 0:87ab172a74b4 227 }
maxint 6:e2eead4e53fb 228 else
maxint 6:e2eead4e53fb 229 {
maxint 6:e2eead4e53fb 230 accel.updateGraph();
maxint 0:87ab172a74b4 231 wait_ms(100);
maxint 0:87ab172a74b4 232 }
maxint 0:87ab172a74b4 233 }
maxint 0:87ab172a74b4 234
maxint 6:e2eead4e53fb 235 void Game::checkTilt()
maxint 6:e2eead4e53fb 236 { // move the gravity direction and weight by tilting the board
maxint 0:87ab172a74b4 237 double x, y, z;
maxint 0:87ab172a74b4 238 //int nStart=this->tWait.read_ms();
maxint 0:87ab172a74b4 239 this->accel.getXYZ(x, y, z);
maxint 0:87ab172a74b4 240
maxint 6:e2eead4e53fb 241 vGravity=Vector(x,y);
maxint 6:e2eead4e53fb 242 // vGravity=vGravity.getNormalized();
maxint 6:e2eead4e53fb 243
maxint 6:e2eead4e53fb 244 // TODO: make vector based on tilting
maxint 0:87ab172a74b4 245 //printDouble((double)this->tWait.read_ms()-nStart, 10, 10);
maxint 0:87ab172a74b4 246 /*
maxint 0:87ab172a74b4 247 printDouble(x, 0, 0);
maxint 0:87ab172a74b4 248 char buf[256];
maxint 0:87ab172a74b4 249 sprintf(buf,"tilt:%0.1f", x);
maxint 0:87ab172a74b4 250 this->drawString(buf, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT / 2 + 4*DisplayN18::CHAR_HEIGHT );
maxint 0:87ab172a74b4 251 */
maxint 0:87ab172a74b4 252
maxint 6:e2eead4e53fb 253 //return(Vector(0,0));
maxint 0:87ab172a74b4 254 }
maxint 0:87ab172a74b4 255
maxint 0:87ab172a74b4 256 void Game::checkButtons()
maxint 0:87ab172a74b4 257 {
maxint 3:ca8b21da67dc 258 if(!this->square.read()) // note: button.read() is false (LOW/0) when pressed
maxint 0:87ab172a74b4 259 {
maxint 3:ca8b21da67dc 260 wait_ms(250); // el-cheapo deboounce
maxint 0:87ab172a74b4 261 this->mode = !this->mode;
maxint 0:87ab172a74b4 262
maxint 0:87ab172a74b4 263 //this->disp.clear();
maxint 0:87ab172a74b4 264 this->disp.clearScreen();
maxint 0:87ab172a74b4 265
maxint 0:87ab172a74b4 266 if (!this->mode)
maxint 0:87ab172a74b4 267 {
maxint 1:c1ee4c699517 268 this->accel.resetGraph();
maxint 0:87ab172a74b4 269 }
maxint 0:87ab172a74b4 270
maxint 0:87ab172a74b4 271 this->led1.write(this->mode);
maxint 0:87ab172a74b4 272 this->led2.write(!this->mode);
maxint 0:87ab172a74b4 273 }
maxint 3:ca8b21da67dc 274 else if(!this->circle.read() && this->mode) // note: button.read() is false (LOW/0) when pressed
maxint 3:ca8b21da67dc 275 {
maxint 3:ca8b21da67dc 276 bool fMute=this->snd.getMute();
maxint 3:ca8b21da67dc 277 fMute=!fMute;
maxint 3:ca8b21da67dc 278 this->snd.setMute(fMute);
maxint 3:ca8b21da67dc 279 this->led2.write(fMute);
maxint 3:ca8b21da67dc 280 wait_ms(250); // el-cheapo deboounce
maxint 3:ca8b21da67dc 281 }
maxint 0:87ab172a74b4 282 else
maxint 0:87ab172a74b4 283 {
maxint 0:87ab172a74b4 284 bool isUp = !this->up.read();
maxint 0:87ab172a74b4 285 bool isDown = !this->down.read();
maxint 0:87ab172a74b4 286
maxint 0:87ab172a74b4 287 if (isUp && isDown) goto end;
maxint 0:87ab172a74b4 288 if (!isUp && !isDown) goto end;
maxint 0:87ab172a74b4 289
maxint 0:87ab172a74b4 290 if (isUp && this->lastUp) goto end;
maxint 0:87ab172a74b4 291 if (isDown && this->lastDown) goto end;
maxint 0:87ab172a74b4 292
maxint 0:87ab172a74b4 293 if (isUp)
maxint 0:87ab172a74b4 294 {
maxint 3:ca8b21da67dc 295 if(this->nGameTickDelay<1000) this->nGameTickDelay=(float)this->nGameTickDelay*1.20;
maxint 3:ca8b21da67dc 296 this->printf(100, 0, "Speed: %d ", this->nGameTickDelay);
maxint 0:87ab172a74b4 297 }
maxint 0:87ab172a74b4 298 else if (isDown)
maxint 0:87ab172a74b4 299 {
maxint 3:ca8b21da67dc 300 if(this->nGameTickDelay>5) this->nGameTickDelay=(float)this->nGameTickDelay/1.20;
maxint 3:ca8b21da67dc 301 this->printf(100, 0, "Speed: %d ", this->nGameTickDelay);
maxint 0:87ab172a74b4 302 }
maxint 0:87ab172a74b4 303
maxint 0:87ab172a74b4 304 end:
maxint 0:87ab172a74b4 305 this->lastUp = isUp;
maxint 0:87ab172a74b4 306 this->lastDown = isDown;
maxint 0:87ab172a74b4 307 }
maxint 0:87ab172a74b4 308 }
maxint 0:87ab172a74b4 309
maxint 1:c1ee4c699517 310 void Game::drawString(const char* str, int y)
maxint 1:c1ee4c699517 311 {
maxint 1:c1ee4c699517 312 uint8_t width;
maxint 1:c1ee4c699517 313 uint8_t height;
maxint 1:c1ee4c699517 314
maxint 1:c1ee4c699517 315 this->disp.measureString(font_oem, str, width, height);
maxint 1:c1ee4c699517 316 this->disp.drawString(font_oem, WIDTH / 2 - width / 2, y, str);
maxint 1:c1ee4c699517 317
maxint 0:87ab172a74b4 318 }
maxint 0:87ab172a74b4 319
maxint 6:e2eead4e53fb 320 void Game::showSplashScreen()
maxint 6:e2eead4e53fb 321 {
maxint 0:87ab172a74b4 322 this->drawString(Game::SPLASH_1, HEIGHT / 2 - CHAR_HEIGHT / 2);
maxint 0:87ab172a74b4 323 this->drawString(Game::SPLASH_2, HEIGHT / 2 + CHAR_HEIGHT / 2);
maxint 5:67e75a4f0b52 324 this->drawString(Game::SPLASH_3, HEIGHT / 2 + CHAR_HEIGHT / 2 + 2*CHAR_HEIGHT);
maxint 6:e2eead4e53fb 325
maxint 0:87ab172a74b4 326 while (this->circle.read())
maxint 0:87ab172a74b4 327 {
maxint 6:e2eead4e53fb 328 checkTilt();
maxint 6:e2eead4e53fb 329 printf(10, 150, "tilt: %.2f, %.2f ", vGravity.x, vGravity.y);
maxint 0:87ab172a74b4 330
maxint 0:87ab172a74b4 331 wait_ms(1);
maxint 0:87ab172a74b4 332 }
maxint 3:ca8b21da67dc 333 wait_ms(250); // el-cheapo deboounce
maxint 3:ca8b21da67dc 334
maxint 2:d4de5a5866fe 335 //this->disp.clearScreen();
maxint 2:d4de5a5866fe 336 this->initialize(); // start a new game
maxint 0:87ab172a74b4 337 }
maxint 0:87ab172a74b4 338
maxint 6:e2eead4e53fb 339 void Game::checkBallCollision()
maxint 2:d4de5a5866fe 340 {
maxint 3:ca8b21da67dc 341 Rectangle rTop=Rectangle(0, -10, WIDTH, this->nTopWall); // Rectangle(0, 0, WIDTH, 1); // top wall
maxint 2:d4de5a5866fe 342 Rectangle rBottom=Rectangle(0, HEIGHT, WIDTH, HEIGHT+10); // Rectangle(0, HEIGHT, WIDTH, HEIGHT); // bottom gap
maxint 2:d4de5a5866fe 343 Rectangle rLeft=Rectangle(-10, 0, 0, HEIGHT); // Rectangle(0, 0, 0, HEIGHT); // left wall
maxint 2:d4de5a5866fe 344 Rectangle rRight=Rectangle(WIDTH, 0, WIDTH+10, HEIGHT); // Rectangle(WIDTH, 0, WIDTH, HEIGHT); // right wall
maxint 2:d4de5a5866fe 345
maxint 6:e2eead4e53fb 346 if(ball.collides(rTop) && ball.vSpeed.isUp()) // top wall
maxint 6:e2eead4e53fb 347 {
maxint 6:e2eead4e53fb 348 ball.vSpeed.y=0;
maxint 6:e2eead4e53fb 349 //this->snd.beepShort();
maxint 6:e2eead4e53fb 350 }
maxint 6:e2eead4e53fb 351 if(ball.collides(rRight) && ball.vSpeed.isRight()) // right wall
maxint 6:e2eead4e53fb 352 {
maxint 6:e2eead4e53fb 353 ball.vSpeed.x=0;
maxint 6:e2eead4e53fb 354 //this->snd.beepShort();
maxint 6:e2eead4e53fb 355 }
maxint 6:e2eead4e53fb 356 if(ball.collides(rLeft) && ball.vSpeed.isLeft()) // left wall
maxint 6:e2eead4e53fb 357 {
maxint 6:e2eead4e53fb 358 ball.vSpeed.x=0;
maxint 6:e2eead4e53fb 359 //this->snd.beepShort();
maxint 6:e2eead4e53fb 360 }
maxint 6:e2eead4e53fb 361 if(ball.collides(rBottom) && ball.vSpeed.isDown()) // bottom wall
maxint 6:e2eead4e53fb 362 {
maxint 6:e2eead4e53fb 363 ball.vSpeed.y=0;
maxint 6:e2eead4e53fb 364 //this->snd.beepShort();
maxint 6:e2eead4e53fb 365 }
maxint 6:e2eead4e53fb 366
maxint 6:e2eead4e53fb 367 for(int i=0; i<NUM_WALLS; i++)
maxint 6:e2eead4e53fb 368 {
maxint 6:e2eead4e53fb 369 if(aWalls[i].fActive)
maxint 2:d4de5a5866fe 370 {
maxint 6:e2eead4e53fb 371 Rectangle rWall=aWalls[i].getRect();
maxint 6:e2eead4e53fb 372 if(ball.collides(rWall))
maxint 2:d4de5a5866fe 373 {
maxint 6:e2eead4e53fb 374 //snd.play("T240 L128 O4 A");
maxint 6:e2eead4e53fb 375 if(rWall.isHorizontal())
maxint 6:e2eead4e53fb 376 ball.Bounce(Vector(0,-0.1));
maxint 6:e2eead4e53fb 377 else
maxint 6:e2eead4e53fb 378 ball.Bounce(Vector(-0.1,-0));
maxint 6:e2eead4e53fb 379 //ball.vSpeed.multiply(Vector(0,0));
maxint 6:e2eead4e53fb 380 aWalls[i].draw();
maxint 3:ca8b21da67dc 381 }
maxint 6:e2eead4e53fb 382 //printf(0, 100, "W: %d,%d - %d,%d", rWall.getX1(), rWall.getY1(), rWall.getX2(), rWall.getY2());
maxint 2:d4de5a5866fe 383 }
maxint 0:87ab172a74b4 384 }
maxint 6:e2eead4e53fb 385
maxint 6:e2eead4e53fb 386
maxint 2:d4de5a5866fe 387 }
maxint 2:d4de5a5866fe 388
maxint 0:87ab172a74b4 389
maxint 1:c1ee4c699517 390 void Game::printf(int x, int y, const char *szFormat, ...)
maxint 0:87ab172a74b4 391 {
maxint 0:87ab172a74b4 392 char szBuffer[256];
maxint 0:87ab172a74b4 393 va_list args;
maxint 0:87ab172a74b4 394
maxint 0:87ab172a74b4 395 va_start(args, szFormat);
maxint 0:87ab172a74b4 396 vsprintf(szBuffer, szFormat, args);
maxint 0:87ab172a74b4 397 va_end(args);
maxint 1:c1ee4c699517 398 this->disp.drawString(font_oem, x, y, szBuffer);
maxint 0:87ab172a74b4 399 }