Balls & Paddle game for RETRO Pong inspired game featuring multi-directional tilt-sensitive paddle, multiple balls, shrinking ceiling and a bit of gravity.
Dependencies: LCD_ST7735 MusicEngine RETRO_BallsAndThings mbed
Balls and Paddle
After doing some work on the Pong mod I decided to put my efforts into making my version object oriented and try to make a generic object-library that could be use for other ball-and-things games. To add some challenges to the gameplay, the following features were added:
- extra-free additional balls to please the juglers
- gravity for pulling the ball down to create some dynamic movement
- directional power-paddle that counters the ball with a bit more speed
- lowering ceiling to make endless gameplay impossible
Diff: Game.cpp
- Revision:
- 4:8643d2d93a5f
- Parent:
- 3:a68d4d515c20
- Child:
- 5:8441b390a15f
--- a/Game.cpp Wed Feb 25 10:43:41 2015 +0000 +++ b/Game.cpp Thu Feb 26 11:55:13 2015 +0000 @@ -251,6 +251,9 @@ Rectangle rPaddleMiddle=Rectangle(paddle.pos.getX() + Game::PADDLE_WIDTH/3, paddle.pos.getY(), paddle.pos.getX() + Game::PADDLE_WIDTH/3 + Game::PADDLE_WIDTH/3, HEIGHT+10); // paddle middle 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 + Line lPaddleLeft=Line(paddle.pos.getX(), paddle.pos.getY(), paddle.pos.getX() + Game::PADDLE_WIDTH/3, HEIGHT+10); // paddle left part + Line lPaddleRight=Line(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 + //printf(0, 20, "Paddle: %d-%d %d-%d ", rPaddleLeft.getX1(), rPaddleLeft.getX2(), rPaddleRight.getX1(), rPaddleRight.getX2()); Ball* pBall; @@ -279,70 +282,72 @@ } if(pBall->collides(rPaddle) && pBall->vSpeed.isDown()) // paddle { - if(pBall->collides(rPaddleLeft)) pBall->vSpeed.add(Vector(-1.3,0)); // left side of paddle has bias to the left - if(pBall->collides(rPaddleRight)) pBall->vSpeed.add(Vector(1.3,0)); // right side of paddle has bias to the right - pBall->Bounce(Vector(1,-1)); -/* - if(pBall->collides(rPaddleMiddle)) - { - pBall->Bounce(Vector(1,-1)); -// printf(10, 10, "Mid: %0.2f, %0.2f ", pBall->vSpeed.x, pBall->vSpeed.y); - } - else if(pBall->collides(rPaddleLeft)) - { - Vector vDirection(-1,-2); - pBall->Bounce(vDirection.getNormalized()); - //pBall->BounceAgainst(Vector(12,-4)); // left side of paddle has bias to the left -// printf(10, 10, "Left: %0.2f, %0.2f ", pBall->vSpeed.x, pBall->vSpeed.y); - } - else if(pBall->collides(rPaddleRight)) - { - Vector vDirection(1,-2); - pBall->Bounce(vDirection.getNormalized()); - //pBall->BounceAgainst(Vector(12,4)); // right side of paddle has bias to the right -// printf(10, 10, "Right: %0.2f, %0.2f ", pBall->vSpeed.x, pBall->vSpeed.y); - } -*/ - + if(pBall->collides(lPaddleLeft) || pBall->collides(lPaddleRight) || pBall->collides(rPaddleMiddle)) { - // increase the speed of the ball when hitting the paddle to increase difficulty - float ftSpeedMax=3.0; - if(this->nScore>50) - ftSpeedMax=5.0; - if(this->nScore>100) - ftSpeedMax=10.0; - if(this->nScore>150) - ftSpeedMax=999.0; - if(pBall->vSpeed.getSize()<ftSpeedMax) - pBall->vSpeed.multiply(Vector(1,1.02)); // bounce up from paddle at higher speed - } - //printf(10, 10, "Bounce: %0.2f, %0.2f ", pBall->vSpeed.x, pBall->vSpeed.y); - + if(pBall->collides(lPaddleLeft)) pBall->vSpeed.add(Vector(-1.3,0)); // left side of paddle has bias to the left + if(pBall->collides(lPaddleRight)) pBall->vSpeed.add(Vector(1.3,0)); // right side of paddle has bias to the right + pBall->Bounce(Vector(1,-1)); + /* + if(pBall->collides(rPaddleMiddle)) + { + pBall->Bounce(Vector(1,-1)); + // printf(10, 10, "Mid: %0.2f, %0.2f ", pBall->vSpeed.x, pBall->vSpeed.y); + } + else if(pBall->collides(rPaddleLeft)) + { + Vector vDirection(-1,-2); + pBall->Bounce(vDirection.getNormalized()); + //pBall->BounceAgainst(Vector(12,-4)); // left side of paddle has bias to the left + // printf(10, 10, "Left: %0.2f, %0.2f ", pBall->vSpeed.x, pBall->vSpeed.y); + } + else if(pBall->collides(rPaddleRight)) + { + Vector vDirection(1,-2); + pBall->Bounce(vDirection.getNormalized()); + //pBall->BounceAgainst(Vector(12,4)); // right side of paddle has bias to the right + // printf(10, 10, "Right: %0.2f, %0.2f ", pBall->vSpeed.x, pBall->vSpeed.y); + } + */ + + { + // increase the speed of the ball when hitting the paddle to increase difficulty + float ftSpeedMax=3.0; + if(this->nScore>50) + ftSpeedMax=5.0; + if(this->nScore>100) + ftSpeedMax=10.0; + if(this->nScore>150) + ftSpeedMax=999.0; + if(pBall->vSpeed.getSize()<ftSpeedMax) + pBall->vSpeed.multiply(Vector(1,1.02)); // bounce up from paddle at higher speed + } + //printf(10, 10, "Bounce: %0.2f, %0.2f ", pBall->vSpeed.x, pBall->vSpeed.y); - // force drawing the paddle after redrawing the bounced ball - this->fDrawPaddle=true; - - // make sound and update the score - this->snd.beepLong(); - this->nScore++; - this->printf(100, 0, "Score: %d ", this->nScore); - - // add a new ball every 10 points - if(this->nScore>0 && this->nScore%10==0) - { - this->newBall(); - this->nBalls++; - this->snd.play("T240 L16 O5 D E F"); + + // force drawing the paddle after redrawing the bounced ball + this->fDrawPaddle=true; + + // make sound and update the score + this->snd.beepLong(); + this->nScore++; + this->printf(100, 0, "Score: %d ", this->nScore); + + // add a new ball every 10 points + if(this->nScore>0 && this->nScore%10==0) + { + this->newBall(); + this->nBalls++; + this->snd.play("T240 L16 O5 D E F"); + } + + // lower the ceiling every 25 points + if(this->nScore>0 && this->nScore%25==0) + { + this->nTopWall+=3; + this->fDrawTopWall=true; + this->snd.play("T240 L16 O5 CDEFG"); + } } - - // lower the ceiling every 25 points - if(this->nScore>0 && this->nScore%25==0) - { - this->nTopWall+=3; - this->fDrawTopWall=true; - this->snd.play("T240 L16 O5 CDEFG"); - } - } if(pBall->collides(rBottom) && pBall->vSpeed.isDown()) // bottom gap { @@ -356,6 +361,7 @@ this->newBall(); // start a new ball this->snd.beepLow(); } + this->fDrawPaddle=true; } } }