Some random attempts at programming the retro console
Fork of RETRO_Pong_Mod by
Game.cpp@3:2f09c90a732d, 2015-01-15 (annotated)
- Committer:
- devhammer
- Date:
- Thu Jan 15 12:48:21 2015 +0000
- Revision:
- 3:2f09c90a732d
- Parent:
- 2:6ab46f2e851a
- Child:
- 4:84be90860d7c
First commit of modified Pong game for RETRO.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
devhammer | 3:2f09c90a732d | 1 | // Updated version of the official firmware for the Outrageous Circuits RETRO |
devhammer | 3:2f09c90a732d | 2 | // Modified by G. Andrew Duthie (devhammer) |
devhammer | 3:2f09c90a732d | 3 | // Changes: |
devhammer | 3:2f09c90a732d | 4 | // - Added sounds for all ball bounces |
devhammer | 3:2f09c90a732d | 5 | // - Changed ball from square to circle |
devhammer | 3:2f09c90a732d | 6 | // - Adjusted collision detection to add ball speed every 10 paddle hits |
devhammer | 3:2f09c90a732d | 7 | // - Added scoring |
devhammer | 3:2f09c90a732d | 8 | // - Added mute function (not fully implemented...needs to be set up on a button). |
devhammer | 3:2f09c90a732d | 9 | |
john_ghielec | 1:cd8a3926f263 | 10 | #include "Game.h" |
john_ghielec | 1:cd8a3926f263 | 11 | |
john_ghielec | 1:cd8a3926f263 | 12 | const char* Game::LOSE_1 = "You lose."; |
john_ghielec | 1:cd8a3926f263 | 13 | const char* Game::LOSE_2 = "Press ship to restart."; |
john_ghielec | 2:6ab46f2e851a | 14 | const char* Game::SPLASH_1 = "Press ship to start."; |
john_ghielec | 2:6ab46f2e851a | 15 | const char* Game::SPLASH_2 = "Press robot to switch."; |
devhammer | 3:2f09c90a732d | 16 | const char* Game::LIVES = "Lives: "; |
devhammer | 3:2f09c90a732d | 17 | const char* Game::SCORE = "Score: "; |
john_ghielec | 1:cd8a3926f263 | 18 | |
john_ghielec | 1:cd8a3926f263 | 19 | 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), pwm(P0_18), ain(P0_15), i2c(P0_5, P0_4) { |
john_ghielec | 1:cd8a3926f263 | 20 | srand(this->ain.read_u16()); |
john_ghielec | 1:cd8a3926f263 | 21 | |
john_ghielec | 2:6ab46f2e851a | 22 | this->lastUp = false; |
john_ghielec | 2:6ab46f2e851a | 23 | this->lastDown = false; |
john_ghielec | 2:6ab46f2e851a | 24 | this->mode = true; |
john_ghielec | 2:6ab46f2e851a | 25 | |
john_ghielec | 2:6ab46f2e851a | 26 | this->i2c.frequency(400); |
john_ghielec | 2:6ab46f2e851a | 27 | this->writeRegister(0x2A, 0x01); |
john_ghielec | 2:6ab46f2e851a | 28 | |
john_ghielec | 2:6ab46f2e851a | 29 | this->colors[0] = DisplayN18::RED; |
john_ghielec | 2:6ab46f2e851a | 30 | this->colors[1] = DisplayN18::GREEN; |
john_ghielec | 2:6ab46f2e851a | 31 | this->colors[2] = DisplayN18::BLUE; |
john_ghielec | 2:6ab46f2e851a | 32 | |
john_ghielec | 1:cd8a3926f263 | 33 | this->initialize(); |
john_ghielec | 1:cd8a3926f263 | 34 | } |
john_ghielec | 1:cd8a3926f263 | 35 | |
john_ghielec | 2:6ab46f2e851a | 36 | void Game::readRegisters(char address, char* buffer, int len) { |
john_ghielec | 2:6ab46f2e851a | 37 | this->i2c.write(Game::I2C_ADDR, &address, 1, true); |
john_ghielec | 2:6ab46f2e851a | 38 | this->i2c.read(Game::I2C_ADDR | 1, buffer, len); |
john_ghielec | 2:6ab46f2e851a | 39 | } |
john_ghielec | 2:6ab46f2e851a | 40 | |
john_ghielec | 2:6ab46f2e851a | 41 | int Game::writeRegister(char address, char value) { |
john_ghielec | 2:6ab46f2e851a | 42 | char buffer[2] = { address, value }; |
john_ghielec | 2:6ab46f2e851a | 43 | |
john_ghielec | 2:6ab46f2e851a | 44 | return this->i2c.write(Game::I2C_ADDR, buffer, 2); |
john_ghielec | 2:6ab46f2e851a | 45 | } |
john_ghielec | 2:6ab46f2e851a | 46 | |
john_ghielec | 2:6ab46f2e851a | 47 | double Game::convert(char* buffer) { |
john_ghielec | 2:6ab46f2e851a | 48 | double val = ((buffer[0] << 2) | (buffer[1] >> 6)); |
john_ghielec | 2:6ab46f2e851a | 49 | |
john_ghielec | 2:6ab46f2e851a | 50 | if (val > 511.0) |
john_ghielec | 2:6ab46f2e851a | 51 | val -= 1024.0; |
john_ghielec | 2:6ab46f2e851a | 52 | |
john_ghielec | 2:6ab46f2e851a | 53 | return val / 512.0; |
john_ghielec | 2:6ab46f2e851a | 54 | } |
john_ghielec | 2:6ab46f2e851a | 55 | |
john_ghielec | 2:6ab46f2e851a | 56 | void Game::getXYZ(double& x, double& y, double& z) { |
john_ghielec | 2:6ab46f2e851a | 57 | char buffer[6]; |
john_ghielec | 2:6ab46f2e851a | 58 | |
john_ghielec | 2:6ab46f2e851a | 59 | this->readRegisters(0x01, buffer, 6); |
john_ghielec | 2:6ab46f2e851a | 60 | |
john_ghielec | 2:6ab46f2e851a | 61 | x = this->convert(buffer); |
john_ghielec | 2:6ab46f2e851a | 62 | y = this->convert(buffer + 2); |
john_ghielec | 2:6ab46f2e851a | 63 | z = this->convert(buffer + 4); |
john_ghielec | 2:6ab46f2e851a | 64 | } |
john_ghielec | 2:6ab46f2e851a | 65 | |
john_ghielec | 2:6ab46f2e851a | 66 | void Game::printDouble(double value, int x, int y) { |
john_ghielec | 2:6ab46f2e851a | 67 | char buffer[10]; |
john_ghielec | 2:6ab46f2e851a | 68 | int len = sprintf(buffer, "%.1f", value); |
john_ghielec | 2:6ab46f2e851a | 69 | |
john_ghielec | 2:6ab46f2e851a | 70 | this->disp.drawString(x, y, buffer, DisplayN18::WHITE, DisplayN18::BLACK); |
john_ghielec | 2:6ab46f2e851a | 71 | } |
john_ghielec | 2:6ab46f2e851a | 72 | |
john_ghielec | 2:6ab46f2e851a | 73 | void Game::drawAxes() { |
john_ghielec | 2:6ab46f2e851a | 74 | for (int i = 0; i < 3; i++) { |
john_ghielec | 2:6ab46f2e851a | 75 | this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING), 0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT, DisplayN18::WHITE); |
john_ghielec | 2:6ab46f2e851a | 76 | this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, DisplayN18::WIDTH, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, DisplayN18::WHITE); |
john_ghielec | 2:6ab46f2e851a | 77 | } |
john_ghielec | 2:6ab46f2e851a | 78 | } |
john_ghielec | 2:6ab46f2e851a | 79 | |
john_ghielec | 2:6ab46f2e851a | 80 | void Game::drawPoint(int axis, double value) { |
john_ghielec | 2:6ab46f2e851a | 81 | if (value < -1.0) |
john_ghielec | 2:6ab46f2e851a | 82 | value = -1.0; |
john_ghielec | 2:6ab46f2e851a | 83 | |
john_ghielec | 2:6ab46f2e851a | 84 | if (value > 1.0) |
john_ghielec | 2:6ab46f2e851a | 85 | value = 1.0; |
john_ghielec | 2:6ab46f2e851a | 86 | |
john_ghielec | 2:6ab46f2e851a | 87 | value += 1.0; |
john_ghielec | 2:6ab46f2e851a | 88 | value /= 2.0; |
john_ghielec | 2:6ab46f2e851a | 89 | value = 1.0 - value; |
john_ghielec | 2:6ab46f2e851a | 90 | value *= Game::GRAPH_HEIGHT; |
john_ghielec | 2:6ab46f2e851a | 91 | |
john_ghielec | 2:6ab46f2e851a | 92 | this->disp.setPixel(this->graphX, axis * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + (int)value, this->colors[axis]); |
john_ghielec | 2:6ab46f2e851a | 93 | } |
john_ghielec | 2:6ab46f2e851a | 94 | |
john_ghielec | 2:6ab46f2e851a | 95 | void Game::checkGraphReset() { |
john_ghielec | 2:6ab46f2e851a | 96 | if (this->graphX > DisplayN18::WIDTH) { |
john_ghielec | 2:6ab46f2e851a | 97 | this->graphX = 0; |
john_ghielec | 2:6ab46f2e851a | 98 | this->disp.clear(); |
john_ghielec | 2:6ab46f2e851a | 99 | this->drawAxes(); |
john_ghielec | 2:6ab46f2e851a | 100 | } |
john_ghielec | 2:6ab46f2e851a | 101 | } |
john_ghielec | 2:6ab46f2e851a | 102 | |
john_ghielec | 1:cd8a3926f263 | 103 | void Game::initialize() { |
john_ghielec | 1:cd8a3926f263 | 104 | this->initializeBall(); |
john_ghielec | 1:cd8a3926f263 | 105 | |
john_ghielec | 1:cd8a3926f263 | 106 | this->paddleX = DisplayN18::WIDTH / 2 - Game::PADDLE_WIDTH / 2; |
john_ghielec | 1:cd8a3926f263 | 107 | this->pwmTicksLeft = 0; |
john_ghielec | 1:cd8a3926f263 | 108 | this->lives = 4; |
devhammer | 3:2f09c90a732d | 109 | this->score = 0; |
devhammer | 3:2f09c90a732d | 110 | this->muted = false; |
john_ghielec | 1:cd8a3926f263 | 111 | |
devhammer | 3:2f09c90a732d | 112 | this->pwm.period(1); |
john_ghielec | 1:cd8a3926f263 | 113 | this->pwm.write(0.00); |
john_ghielec | 1:cd8a3926f263 | 114 | |
john_ghielec | 1:cd8a3926f263 | 115 | this->disp.clear(); |
john_ghielec | 1:cd8a3926f263 | 116 | } |
john_ghielec | 1:cd8a3926f263 | 117 | |
john_ghielec | 1:cd8a3926f263 | 118 | void Game::initializeBall() { |
john_ghielec | 1:cd8a3926f263 | 119 | this->ballX = DisplayN18::WIDTH / 2 - Game::BALL_RADIUS; |
john_ghielec | 1:cd8a3926f263 | 120 | this->ballY = DisplayN18::HEIGHT / 4 - Game::BALL_RADIUS; |
john_ghielec | 1:cd8a3926f263 | 121 | |
devhammer | 3:2f09c90a732d | 122 | this->ballSpeedX = Game::BALL_STARTING_SPEED; |
devhammer | 3:2f09c90a732d | 123 | this->ballSpeedY = Game::BALL_STARTING_SPEED; |
devhammer | 3:2f09c90a732d | 124 | |
devhammer | 3:2f09c90a732d | 125 | this->ballSpeedX *= (rand() % 2 ? 1 : -1); |
devhammer | 3:2f09c90a732d | 126 | this->ballSpeedY *= (rand() % 2 ? 1 : -1); |
john_ghielec | 2:6ab46f2e851a | 127 | } |
john_ghielec | 2:6ab46f2e851a | 128 | |
john_ghielec | 2:6ab46f2e851a | 129 | void Game::tick() { |
john_ghielec | 2:6ab46f2e851a | 130 | this->checkButtons(); |
john_ghielec | 1:cd8a3926f263 | 131 | |
john_ghielec | 2:6ab46f2e851a | 132 | if (this->mode) { |
john_ghielec | 2:6ab46f2e851a | 133 | this->clearPaddle(); |
john_ghielec | 2:6ab46f2e851a | 134 | this->clearBall(); |
john_ghielec | 2:6ab46f2e851a | 135 | |
john_ghielec | 2:6ab46f2e851a | 136 | this->updatePaddle(); |
john_ghielec | 2:6ab46f2e851a | 137 | this->updateBall(); |
john_ghielec | 1:cd8a3926f263 | 138 | |
john_ghielec | 2:6ab46f2e851a | 139 | this->checkCollision(); |
john_ghielec | 2:6ab46f2e851a | 140 | |
john_ghielec | 2:6ab46f2e851a | 141 | this->drawPaddle(); |
john_ghielec | 2:6ab46f2e851a | 142 | this->drawBall(); |
john_ghielec | 2:6ab46f2e851a | 143 | |
john_ghielec | 2:6ab46f2e851a | 144 | this->checkPwm(); |
john_ghielec | 2:6ab46f2e851a | 145 | this->checkLives(); |
john_ghielec | 2:6ab46f2e851a | 146 | |
john_ghielec | 2:6ab46f2e851a | 147 | wait_ms(25); |
john_ghielec | 2:6ab46f2e851a | 148 | } |
john_ghielec | 2:6ab46f2e851a | 149 | else { |
john_ghielec | 2:6ab46f2e851a | 150 | double x, y, z; |
john_ghielec | 2:6ab46f2e851a | 151 | |
john_ghielec | 2:6ab46f2e851a | 152 | this->getXYZ(x, y, z); |
john_ghielec | 2:6ab46f2e851a | 153 | |
john_ghielec | 2:6ab46f2e851a | 154 | this->checkGraphReset(); |
john_ghielec | 2:6ab46f2e851a | 155 | this->drawPoint(0, x); |
john_ghielec | 2:6ab46f2e851a | 156 | this->drawPoint(1, y); |
john_ghielec | 2:6ab46f2e851a | 157 | this->drawPoint(2, z); |
john_ghielec | 2:6ab46f2e851a | 158 | this->graphX++; |
john_ghielec | 2:6ab46f2e851a | 159 | } |
john_ghielec | 1:cd8a3926f263 | 160 | } |
john_ghielec | 1:cd8a3926f263 | 161 | |
john_ghielec | 2:6ab46f2e851a | 162 | void Game::checkButtons() { |
john_ghielec | 2:6ab46f2e851a | 163 | if (!this->square.read()) { |
devhammer | 3:2f09c90a732d | 164 | //this->muted = !this->muted; |
john_ghielec | 2:6ab46f2e851a | 165 | this->mode = !this->mode; |
john_ghielec | 2:6ab46f2e851a | 166 | |
john_ghielec | 2:6ab46f2e851a | 167 | this->disp.clear(); |
john_ghielec | 2:6ab46f2e851a | 168 | |
john_ghielec | 2:6ab46f2e851a | 169 | if (!this->mode) { |
john_ghielec | 2:6ab46f2e851a | 170 | this->graphX = 0; |
john_ghielec | 2:6ab46f2e851a | 171 | |
john_ghielec | 2:6ab46f2e851a | 172 | this->drawAxes(); |
john_ghielec | 2:6ab46f2e851a | 173 | } |
john_ghielec | 2:6ab46f2e851a | 174 | |
devhammer | 3:2f09c90a732d | 175 | //this->led1.write(this->muted); |
devhammer | 3:2f09c90a732d | 176 | //this->led2.write(!this->muted); |
john_ghielec | 2:6ab46f2e851a | 177 | this->led1.write(this->mode); |
john_ghielec | 2:6ab46f2e851a | 178 | this->led2.write(!this->mode); |
john_ghielec | 2:6ab46f2e851a | 179 | } |
john_ghielec | 2:6ab46f2e851a | 180 | |
john_ghielec | 2:6ab46f2e851a | 181 | bool xDir = this->ballSpeedX > 0; |
john_ghielec | 2:6ab46f2e851a | 182 | bool yDir = this->ballSpeedY > 0; |
john_ghielec | 2:6ab46f2e851a | 183 | bool isUp = !this->up.read(); |
john_ghielec | 2:6ab46f2e851a | 184 | bool isDown = !this->down.read(); |
john_ghielec | 1:cd8a3926f263 | 185 | |
john_ghielec | 2:6ab46f2e851a | 186 | if (isUp && isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 187 | if (!isUp && !isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 188 | |
john_ghielec | 2:6ab46f2e851a | 189 | if (isUp && this->lastUp) goto end; |
john_ghielec | 2:6ab46f2e851a | 190 | if (isDown && this->lastDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 191 | |
john_ghielec | 2:6ab46f2e851a | 192 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 193 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 194 | |
john_ghielec | 2:6ab46f2e851a | 195 | if (isUp) { |
john_ghielec | 2:6ab46f2e851a | 196 | if (++this->ballSpeedX > 5) this->ballSpeedX = 5; |
john_ghielec | 2:6ab46f2e851a | 197 | if (++this->ballSpeedY > 5) this->ballSpeedY = 5; |
john_ghielec | 2:6ab46f2e851a | 198 | } |
john_ghielec | 2:6ab46f2e851a | 199 | else if (isDown) { |
john_ghielec | 2:6ab46f2e851a | 200 | if (--this->ballSpeedX == 0) this->ballSpeedX = 1; |
john_ghielec | 2:6ab46f2e851a | 201 | if (--this->ballSpeedY == 0) this->ballSpeedY = 1; |
john_ghielec | 2:6ab46f2e851a | 202 | } |
john_ghielec | 1:cd8a3926f263 | 203 | |
john_ghielec | 2:6ab46f2e851a | 204 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 205 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 2:6ab46f2e851a | 206 | |
john_ghielec | 2:6ab46f2e851a | 207 | end: |
john_ghielec | 2:6ab46f2e851a | 208 | this->lastUp = isUp; |
john_ghielec | 2:6ab46f2e851a | 209 | this->lastDown = isDown; |
john_ghielec | 1:cd8a3926f263 | 210 | } |
john_ghielec | 1:cd8a3926f263 | 211 | |
john_ghielec | 1:cd8a3926f263 | 212 | void Game::drawString(const char* str, int y) { |
john_ghielec | 1:cd8a3926f263 | 213 | this->disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) * strlen(str) / 2, y, str, DisplayN18::WHITE, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 214 | } |
john_ghielec | 1:cd8a3926f263 | 215 | |
john_ghielec | 1:cd8a3926f263 | 216 | void Game::showSplashScreen() { |
john_ghielec | 2:6ab46f2e851a | 217 | this->drawString(Game::SPLASH_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT / 2); |
john_ghielec | 2:6ab46f2e851a | 218 | this->drawString(Game::SPLASH_2, DisplayN18::HEIGHT / 2 + DisplayN18::CHAR_HEIGHT / 2); |
john_ghielec | 1:cd8a3926f263 | 219 | |
john_ghielec | 1:cd8a3926f263 | 220 | while (this->circle.read()) |
john_ghielec | 1:cd8a3926f263 | 221 | wait_ms(1); |
john_ghielec | 1:cd8a3926f263 | 222 | |
john_ghielec | 1:cd8a3926f263 | 223 | this->disp.clear(); |
john_ghielec | 1:cd8a3926f263 | 224 | } |
john_ghielec | 1:cd8a3926f263 | 225 | |
john_ghielec | 1:cd8a3926f263 | 226 | void Game::clearPaddle() { |
john_ghielec | 1:cd8a3926f263 | 227 | this->disp.fillRect(this->paddleX, DisplayN18::HEIGHT - Game::PADDLE_HEIGHT, Game::PADDLE_WIDTH, Game::PADDLE_HEIGHT, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 228 | } |
john_ghielec | 1:cd8a3926f263 | 229 | |
john_ghielec | 1:cd8a3926f263 | 230 | void Game::drawPaddle() { |
john_ghielec | 1:cd8a3926f263 | 231 | this->disp.fillRect(this->paddleX, DisplayN18::HEIGHT - Game::PADDLE_HEIGHT, Game::PADDLE_WIDTH, Game::PADDLE_HEIGHT, DisplayN18::BLUE); |
john_ghielec | 1:cd8a3926f263 | 232 | } |
john_ghielec | 1:cd8a3926f263 | 233 | |
john_ghielec | 1:cd8a3926f263 | 234 | void Game::updatePaddle() { |
john_ghielec | 1:cd8a3926f263 | 235 | if (this->left.read()) |
john_ghielec | 1:cd8a3926f263 | 236 | this->paddleX += Game::PADDLE_SPEED; |
john_ghielec | 1:cd8a3926f263 | 237 | |
john_ghielec | 1:cd8a3926f263 | 238 | if (this->right.read()) |
john_ghielec | 1:cd8a3926f263 | 239 | this->paddleX -= Game::PADDLE_SPEED; |
john_ghielec | 1:cd8a3926f263 | 240 | } |
john_ghielec | 1:cd8a3926f263 | 241 | |
john_ghielec | 1:cd8a3926f263 | 242 | void Game::clearBall() { |
devhammer | 3:2f09c90a732d | 243 | //this->disp.fillRect(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS * 2, Game::BALL_RADIUS * 2, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 244 | //this->disp.fillCircle(this->ballX - Game::BALL_RADIUS, this->ballY - Game::BALL_RADIUS, Game::BALL_RADIUS, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 245 | this->disp.fillCircle(this->ballX, this->ballY, Game::BALL_RADIUS, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 246 | this->disp.setPixel(this->ballX, this->ballY, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 247 | } |
john_ghielec | 1:cd8a3926f263 | 248 | |
john_ghielec | 1:cd8a3926f263 | 249 | void Game::drawBall() { |
devhammer | 3:2f09c90a732d | 250 | //this->disp.fillRect(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS * 2, Game::BALL_RADIUS * 2, DisplayN18::RED); |
devhammer | 3:2f09c90a732d | 251 | //this->disp.fillCircle(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS, DisplayN18::RED); |
devhammer | 3:2f09c90a732d | 252 | this->disp.fillCircle(this->ballX, ballY, Game::BALL_RADIUS, DisplayN18::RED); |
devhammer | 3:2f09c90a732d | 253 | this->disp.setPixel(this->ballX, this->ballY, DisplayN18::GREEN); |
john_ghielec | 1:cd8a3926f263 | 254 | } |
john_ghielec | 1:cd8a3926f263 | 255 | |
john_ghielec | 1:cd8a3926f263 | 256 | void Game::updateBall() { |
john_ghielec | 1:cd8a3926f263 | 257 | this->ballX += this->ballSpeedX; |
john_ghielec | 1:cd8a3926f263 | 258 | this->ballY += this->ballSpeedY; |
john_ghielec | 1:cd8a3926f263 | 259 | } |
john_ghielec | 1:cd8a3926f263 | 260 | |
john_ghielec | 1:cd8a3926f263 | 261 | void Game::checkCollision() { |
john_ghielec | 1:cd8a3926f263 | 262 | if (this->paddleX < 0) |
john_ghielec | 1:cd8a3926f263 | 263 | this->paddleX = 0; |
john_ghielec | 1:cd8a3926f263 | 264 | |
john_ghielec | 1:cd8a3926f263 | 265 | if (this->paddleX + Game::PADDLE_WIDTH > DisplayN18::WIDTH) |
john_ghielec | 1:cd8a3926f263 | 266 | this->paddleX = DisplayN18::WIDTH - Game::PADDLE_WIDTH; |
john_ghielec | 1:cd8a3926f263 | 267 | |
devhammer | 3:2f09c90a732d | 268 | //if ((this->ballX - Game::BALL_RADIUS < 0 && this->ballSpeedX < 0) || (this->ballX + Game::BALL_RADIUS >= DisplayN18::WIDTH && this->ballSpeedX > 0)) { |
devhammer | 3:2f09c90a732d | 269 | if ((this->ballX - Game::BALL_RADIUS < 0 && this->ballSpeedX < 0) || (this->ballX + Game::BALL_RADIUS * 2 >= DisplayN18::WIDTH && this->ballSpeedX > 0)) { |
john_ghielec | 1:cd8a3926f263 | 270 | this->ballSpeedX *= -1; |
devhammer | 3:2f09c90a732d | 271 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 272 | this->pwm.period_ms(2); |
devhammer | 3:2f09c90a732d | 273 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 274 | } |
devhammer | 3:2f09c90a732d | 275 | } |
john_ghielec | 1:cd8a3926f263 | 276 | |
devhammer | 3:2f09c90a732d | 277 | if (this->ballY - Game::BALL_RADIUS < (0 + DisplayN18::CHAR_HEIGHT) && this->ballSpeedY < 0){ |
john_ghielec | 1:cd8a3926f263 | 278 | this->ballSpeedY *= -1; |
devhammer | 3:2f09c90a732d | 279 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 280 | this->pwm.period_ms(2); |
devhammer | 3:2f09c90a732d | 281 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 282 | } |
devhammer | 3:2f09c90a732d | 283 | } |
john_ghielec | 1:cd8a3926f263 | 284 | |
john_ghielec | 1:cd8a3926f263 | 285 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT - Game::PADDLE_HEIGHT && this->ballSpeedY > 0) { |
john_ghielec | 1:cd8a3926f263 | 286 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT) { |
john_ghielec | 1:cd8a3926f263 | 287 | this->initializeBall(); |
john_ghielec | 1:cd8a3926f263 | 288 | |
john_ghielec | 1:cd8a3926f263 | 289 | this->lives--; |
devhammer | 3:2f09c90a732d | 290 | |
devhammer | 3:2f09c90a732d | 291 | if(this->lives > 0) { |
devhammer | 3:2f09c90a732d | 292 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 293 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 294 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 295 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 296 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 297 | } |
devhammer | 3:2f09c90a732d | 298 | } |
devhammer | 3:2f09c90a732d | 299 | |
john_ghielec | 1:cd8a3926f263 | 300 | } |
john_ghielec | 1:cd8a3926f263 | 301 | else if (this->ballX > this->paddleX && this->ballX < this->paddleX + Game::PADDLE_WIDTH) { |
john_ghielec | 1:cd8a3926f263 | 302 | this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 303 | |
devhammer | 3:2f09c90a732d | 304 | if(!this->muted){ |
devhammer | 3:2f09c90a732d | 305 | this->pwm.period_ms(1); |
devhammer | 3:2f09c90a732d | 306 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 307 | } |
devhammer | 3:2f09c90a732d | 308 | this->score = this->score + 10; |
devhammer | 3:2f09c90a732d | 309 | if(this->score % 100 == 0) { |
devhammer | 3:2f09c90a732d | 310 | if(this->ballSpeedX < 0){ |
devhammer | 3:2f09c90a732d | 311 | this->ballSpeedX -= 1; |
devhammer | 3:2f09c90a732d | 312 | } |
devhammer | 3:2f09c90a732d | 313 | else { |
devhammer | 3:2f09c90a732d | 314 | this->ballSpeedX += 1; |
devhammer | 3:2f09c90a732d | 315 | } |
devhammer | 3:2f09c90a732d | 316 | this->ballSpeedY -= 1; |
devhammer | 3:2f09c90a732d | 317 | } |
john_ghielec | 1:cd8a3926f263 | 318 | } |
john_ghielec | 1:cd8a3926f263 | 319 | } |
devhammer | 3:2f09c90a732d | 320 | char buf[10]; |
devhammer | 3:2f09c90a732d | 321 | int a = this->score; |
devhammer | 3:2f09c90a732d | 322 | sprintf(buf, "%d", a); |
devhammer | 3:2f09c90a732d | 323 | this->disp.drawString(DisplayN18::WIDTH - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, DisplayN18::WHITE, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 324 | this->disp.drawString(DisplayN18::WIDTH - (DisplayN18::CHAR_WIDTH * 4), 0, buf, DisplayN18::WHITE, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 325 | } |
john_ghielec | 1:cd8a3926f263 | 326 | |
john_ghielec | 1:cd8a3926f263 | 327 | void Game::checkPwm() { |
john_ghielec | 1:cd8a3926f263 | 328 | if (this->pwmTicksLeft == 0) { |
devhammer | 3:2f09c90a732d | 329 | this->pwm.write(0.0); |
john_ghielec | 1:cd8a3926f263 | 330 | } |
john_ghielec | 1:cd8a3926f263 | 331 | else { |
john_ghielec | 1:cd8a3926f263 | 332 | this->pwmTicksLeft--; |
john_ghielec | 1:cd8a3926f263 | 333 | this->pwm.write(0.5); |
john_ghielec | 1:cd8a3926f263 | 334 | } |
john_ghielec | 1:cd8a3926f263 | 335 | } |
john_ghielec | 1:cd8a3926f263 | 336 | |
john_ghielec | 1:cd8a3926f263 | 337 | void Game::checkLives() { |
john_ghielec | 1:cd8a3926f263 | 338 | if (this->lives == 0) { |
john_ghielec | 1:cd8a3926f263 | 339 | this->disp.clear(); |
devhammer | 3:2f09c90a732d | 340 | |
john_ghielec | 1:cd8a3926f263 | 341 | this->drawString(Game::LOSE_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT); |
john_ghielec | 1:cd8a3926f263 | 342 | this->drawString(Game::LOSE_2, DisplayN18::HEIGHT / 2); |
john_ghielec | 1:cd8a3926f263 | 343 | |
devhammer | 3:2f09c90a732d | 344 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 345 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 346 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 347 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 348 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 349 | |
devhammer | 3:2f09c90a732d | 350 | this->pwm.period(1.0/196); |
devhammer | 3:2f09c90a732d | 351 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 352 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 353 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 354 | |
devhammer | 3:2f09c90a732d | 355 | this->pwm.period(1.0/164.81); |
devhammer | 3:2f09c90a732d | 356 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 357 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 358 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 359 | } |
devhammer | 3:2f09c90a732d | 360 | |
john_ghielec | 1:cd8a3926f263 | 361 | while (this->circle.read()) |
john_ghielec | 1:cd8a3926f263 | 362 | wait_ms(1); |
john_ghielec | 1:cd8a3926f263 | 363 | |
john_ghielec | 1:cd8a3926f263 | 364 | this->initialize(); |
john_ghielec | 1:cd8a3926f263 | 365 | } |
john_ghielec | 1:cd8a3926f263 | 366 | else { |
devhammer | 3:2f09c90a732d | 367 | this->disp.drawString(0, 0, Game::LIVES, DisplayN18::WHITE, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 368 | this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), DisplayN18::WHITE, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 369 | } |
john_ghielec | 1:cd8a3926f263 | 370 | } |