Some random attempts at programming the retro console
Fork of RETRO_Pong_Mod by
Game.cpp@4:84be90860d7c, 2015-02-25 (annotated)
- Committer:
- loop
- Date:
- Wed Feb 25 22:53:35 2015 +0000
- Revision:
- 4:84be90860d7c
- Parent:
- 3:2f09c90a732d
- Child:
- 5:8a26ad9d9ea1
Begin removing paddle related stuff and start bouncing on all walls
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 | this->pwmTicksLeft = 0; |
john_ghielec | 1:cd8a3926f263 | 106 | this->lives = 4; |
devhammer | 3:2f09c90a732d | 107 | this->score = 0; |
devhammer | 3:2f09c90a732d | 108 | this->muted = false; |
john_ghielec | 1:cd8a3926f263 | 109 | |
devhammer | 3:2f09c90a732d | 110 | this->pwm.period(1); |
john_ghielec | 1:cd8a3926f263 | 111 | this->pwm.write(0.00); |
john_ghielec | 1:cd8a3926f263 | 112 | |
john_ghielec | 1:cd8a3926f263 | 113 | this->disp.clear(); |
john_ghielec | 1:cd8a3926f263 | 114 | } |
john_ghielec | 1:cd8a3926f263 | 115 | |
john_ghielec | 1:cd8a3926f263 | 116 | void Game::initializeBall() { |
john_ghielec | 1:cd8a3926f263 | 117 | this->ballX = DisplayN18::WIDTH / 2 - Game::BALL_RADIUS; |
john_ghielec | 1:cd8a3926f263 | 118 | this->ballY = DisplayN18::HEIGHT / 4 - Game::BALL_RADIUS; |
loop | 4:84be90860d7c | 119 | this->ballSpeedX = 3; |
loop | 4:84be90860d7c | 120 | this->ballSpeedY = 5; |
loop | 4:84be90860d7c | 121 | this->ballAccelX = 0; |
loop | 4:84be90860d7c | 122 | this->ballAccelY = 0; |
john_ghielec | 2:6ab46f2e851a | 123 | } |
john_ghielec | 2:6ab46f2e851a | 124 | |
john_ghielec | 2:6ab46f2e851a | 125 | void Game::tick() { |
john_ghielec | 2:6ab46f2e851a | 126 | this->checkButtons(); |
john_ghielec | 1:cd8a3926f263 | 127 | |
john_ghielec | 2:6ab46f2e851a | 128 | if (this->mode) { |
john_ghielec | 2:6ab46f2e851a | 129 | this->clearBall(); |
john_ghielec | 2:6ab46f2e851a | 130 | this->updateBall(); |
john_ghielec | 2:6ab46f2e851a | 131 | this->checkCollision(); |
john_ghielec | 2:6ab46f2e851a | 132 | this->drawBall(); |
john_ghielec | 2:6ab46f2e851a | 133 | this->checkPwm(); |
john_ghielec | 2:6ab46f2e851a | 134 | this->checkLives(); |
john_ghielec | 2:6ab46f2e851a | 135 | wait_ms(25); |
john_ghielec | 2:6ab46f2e851a | 136 | } |
john_ghielec | 2:6ab46f2e851a | 137 | else { |
john_ghielec | 2:6ab46f2e851a | 138 | double x, y, z; |
john_ghielec | 2:6ab46f2e851a | 139 | |
john_ghielec | 2:6ab46f2e851a | 140 | this->getXYZ(x, y, z); |
john_ghielec | 2:6ab46f2e851a | 141 | |
john_ghielec | 2:6ab46f2e851a | 142 | this->checkGraphReset(); |
john_ghielec | 2:6ab46f2e851a | 143 | this->drawPoint(0, x); |
john_ghielec | 2:6ab46f2e851a | 144 | this->drawPoint(1, y); |
john_ghielec | 2:6ab46f2e851a | 145 | this->drawPoint(2, z); |
john_ghielec | 2:6ab46f2e851a | 146 | this->graphX++; |
john_ghielec | 2:6ab46f2e851a | 147 | } |
john_ghielec | 1:cd8a3926f263 | 148 | } |
john_ghielec | 1:cd8a3926f263 | 149 | |
john_ghielec | 2:6ab46f2e851a | 150 | void Game::checkButtons() { |
john_ghielec | 2:6ab46f2e851a | 151 | if (!this->square.read()) { |
devhammer | 3:2f09c90a732d | 152 | //this->muted = !this->muted; |
john_ghielec | 2:6ab46f2e851a | 153 | this->mode = !this->mode; |
john_ghielec | 2:6ab46f2e851a | 154 | |
john_ghielec | 2:6ab46f2e851a | 155 | this->disp.clear(); |
john_ghielec | 2:6ab46f2e851a | 156 | |
john_ghielec | 2:6ab46f2e851a | 157 | if (!this->mode) { |
john_ghielec | 2:6ab46f2e851a | 158 | this->graphX = 0; |
john_ghielec | 2:6ab46f2e851a | 159 | |
john_ghielec | 2:6ab46f2e851a | 160 | this->drawAxes(); |
john_ghielec | 2:6ab46f2e851a | 161 | } |
john_ghielec | 2:6ab46f2e851a | 162 | |
devhammer | 3:2f09c90a732d | 163 | //this->led1.write(this->muted); |
devhammer | 3:2f09c90a732d | 164 | //this->led2.write(!this->muted); |
john_ghielec | 2:6ab46f2e851a | 165 | this->led1.write(this->mode); |
john_ghielec | 2:6ab46f2e851a | 166 | this->led2.write(!this->mode); |
john_ghielec | 2:6ab46f2e851a | 167 | } |
john_ghielec | 2:6ab46f2e851a | 168 | |
john_ghielec | 2:6ab46f2e851a | 169 | bool xDir = this->ballSpeedX > 0; |
john_ghielec | 2:6ab46f2e851a | 170 | bool yDir = this->ballSpeedY > 0; |
john_ghielec | 2:6ab46f2e851a | 171 | bool isUp = !this->up.read(); |
john_ghielec | 2:6ab46f2e851a | 172 | bool isDown = !this->down.read(); |
john_ghielec | 1:cd8a3926f263 | 173 | |
john_ghielec | 2:6ab46f2e851a | 174 | if (isUp && isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 175 | if (!isUp && !isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 176 | |
john_ghielec | 2:6ab46f2e851a | 177 | if (isUp && this->lastUp) goto end; |
john_ghielec | 2:6ab46f2e851a | 178 | if (isDown && this->lastDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 179 | |
john_ghielec | 2:6ab46f2e851a | 180 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 181 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 182 | |
john_ghielec | 2:6ab46f2e851a | 183 | if (isUp) { |
john_ghielec | 2:6ab46f2e851a | 184 | if (++this->ballSpeedX > 5) this->ballSpeedX = 5; |
john_ghielec | 2:6ab46f2e851a | 185 | if (++this->ballSpeedY > 5) this->ballSpeedY = 5; |
john_ghielec | 2:6ab46f2e851a | 186 | } |
john_ghielec | 2:6ab46f2e851a | 187 | else if (isDown) { |
john_ghielec | 2:6ab46f2e851a | 188 | if (--this->ballSpeedX == 0) this->ballSpeedX = 1; |
john_ghielec | 2:6ab46f2e851a | 189 | if (--this->ballSpeedY == 0) this->ballSpeedY = 1; |
john_ghielec | 2:6ab46f2e851a | 190 | } |
john_ghielec | 1:cd8a3926f263 | 191 | |
john_ghielec | 2:6ab46f2e851a | 192 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 193 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 2:6ab46f2e851a | 194 | |
john_ghielec | 2:6ab46f2e851a | 195 | end: |
john_ghielec | 2:6ab46f2e851a | 196 | this->lastUp = isUp; |
john_ghielec | 2:6ab46f2e851a | 197 | this->lastDown = isDown; |
john_ghielec | 1:cd8a3926f263 | 198 | } |
john_ghielec | 1:cd8a3926f263 | 199 | |
john_ghielec | 1:cd8a3926f263 | 200 | void Game::drawString(const char* str, int y) { |
john_ghielec | 1:cd8a3926f263 | 201 | 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 | 202 | } |
john_ghielec | 1:cd8a3926f263 | 203 | |
john_ghielec | 1:cd8a3926f263 | 204 | void Game::showSplashScreen() { |
john_ghielec | 2:6ab46f2e851a | 205 | this->drawString(Game::SPLASH_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT / 2); |
john_ghielec | 2:6ab46f2e851a | 206 | this->drawString(Game::SPLASH_2, DisplayN18::HEIGHT / 2 + DisplayN18::CHAR_HEIGHT / 2); |
john_ghielec | 1:cd8a3926f263 | 207 | |
john_ghielec | 1:cd8a3926f263 | 208 | while (this->circle.read()) |
john_ghielec | 1:cd8a3926f263 | 209 | wait_ms(1); |
john_ghielec | 1:cd8a3926f263 | 210 | |
john_ghielec | 1:cd8a3926f263 | 211 | this->disp.clear(); |
john_ghielec | 1:cd8a3926f263 | 212 | } |
john_ghielec | 1:cd8a3926f263 | 213 | |
john_ghielec | 1:cd8a3926f263 | 214 | void Game::clearBall() { |
devhammer | 3:2f09c90a732d | 215 | //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 | 216 | //this->disp.fillCircle(this->ballX - Game::BALL_RADIUS, this->ballY - Game::BALL_RADIUS, Game::BALL_RADIUS, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 217 | this->disp.fillCircle(this->ballX, this->ballY, Game::BALL_RADIUS, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 218 | this->disp.setPixel(this->ballX, this->ballY, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 219 | } |
john_ghielec | 1:cd8a3926f263 | 220 | |
john_ghielec | 1:cd8a3926f263 | 221 | void Game::drawBall() { |
devhammer | 3:2f09c90a732d | 222 | //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 | 223 | //this->disp.fillCircle(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS, DisplayN18::RED); |
devhammer | 3:2f09c90a732d | 224 | this->disp.fillCircle(this->ballX, ballY, Game::BALL_RADIUS, DisplayN18::RED); |
devhammer | 3:2f09c90a732d | 225 | this->disp.setPixel(this->ballX, this->ballY, DisplayN18::GREEN); |
john_ghielec | 1:cd8a3926f263 | 226 | } |
john_ghielec | 1:cd8a3926f263 | 227 | |
john_ghielec | 1:cd8a3926f263 | 228 | void Game::updateBall() { |
john_ghielec | 1:cd8a3926f263 | 229 | this->ballX += this->ballSpeedX; |
john_ghielec | 1:cd8a3926f263 | 230 | this->ballY += this->ballSpeedY; |
john_ghielec | 1:cd8a3926f263 | 231 | } |
john_ghielec | 1:cd8a3926f263 | 232 | |
loop | 4:84be90860d7c | 233 | void Game::checkCollision() { |
loop | 4:84be90860d7c | 234 | // Does bounds checking first.. |
loop | 4:84be90860d7c | 235 | if ((this->ballX - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedX < 0) || |
loop | 4:84be90860d7c | 236 | (this->ballX + Game::BALL_RADIUS * 2 >= DisplayN18::WIDTH && this->ballSpeedX > 0)) { |
john_ghielec | 1:cd8a3926f263 | 237 | this->ballSpeedX *= -1; |
devhammer | 3:2f09c90a732d | 238 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 239 | this->pwm.period_ms(2); |
devhammer | 3:2f09c90a732d | 240 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 241 | } |
devhammer | 3:2f09c90a732d | 242 | } |
loop | 4:84be90860d7c | 243 | if ((this->ballY - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedY < 0) || |
loop | 4:84be90860d7c | 244 | (this->ballY + Game::BALL_RADIUS * 2 >= DisplayN18::HEIGHT && this->ballSpeedY > 0)){ |
john_ghielec | 1:cd8a3926f263 | 245 | this->ballSpeedY *= -1; |
devhammer | 3:2f09c90a732d | 246 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 247 | this->pwm.period_ms(2); |
devhammer | 3:2f09c90a732d | 248 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 249 | } |
devhammer | 3:2f09c90a732d | 250 | } |
loop | 4:84be90860d7c | 251 | /* |
john_ghielec | 1:cd8a3926f263 | 252 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT - Game::PADDLE_HEIGHT && this->ballSpeedY > 0) { |
john_ghielec | 1:cd8a3926f263 | 253 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT) { |
john_ghielec | 1:cd8a3926f263 | 254 | this->initializeBall(); |
john_ghielec | 1:cd8a3926f263 | 255 | |
john_ghielec | 1:cd8a3926f263 | 256 | this->lives--; |
devhammer | 3:2f09c90a732d | 257 | |
devhammer | 3:2f09c90a732d | 258 | if(this->lives > 0) { |
devhammer | 3:2f09c90a732d | 259 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 260 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 261 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 262 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 263 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 264 | } |
devhammer | 3:2f09c90a732d | 265 | } |
devhammer | 3:2f09c90a732d | 266 | |
john_ghielec | 1:cd8a3926f263 | 267 | } |
john_ghielec | 1:cd8a3926f263 | 268 | else if (this->ballX > this->paddleX && this->ballX < this->paddleX + Game::PADDLE_WIDTH) { |
john_ghielec | 1:cd8a3926f263 | 269 | this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 270 | |
devhammer | 3:2f09c90a732d | 271 | if(!this->muted){ |
devhammer | 3:2f09c90a732d | 272 | this->pwm.period_ms(1); |
devhammer | 3:2f09c90a732d | 273 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 274 | } |
devhammer | 3:2f09c90a732d | 275 | this->score = this->score + 10; |
devhammer | 3:2f09c90a732d | 276 | if(this->score % 100 == 0) { |
devhammer | 3:2f09c90a732d | 277 | if(this->ballSpeedX < 0){ |
devhammer | 3:2f09c90a732d | 278 | this->ballSpeedX -= 1; |
devhammer | 3:2f09c90a732d | 279 | } |
devhammer | 3:2f09c90a732d | 280 | else { |
devhammer | 3:2f09c90a732d | 281 | this->ballSpeedX += 1; |
devhammer | 3:2f09c90a732d | 282 | } |
devhammer | 3:2f09c90a732d | 283 | this->ballSpeedY -= 1; |
devhammer | 3:2f09c90a732d | 284 | } |
john_ghielec | 1:cd8a3926f263 | 285 | } |
john_ghielec | 1:cd8a3926f263 | 286 | } |
loop | 4:84be90860d7c | 287 | */ |
devhammer | 3:2f09c90a732d | 288 | char buf[10]; |
devhammer | 3:2f09c90a732d | 289 | int a = this->score; |
devhammer | 3:2f09c90a732d | 290 | sprintf(buf, "%d", a); |
devhammer | 3:2f09c90a732d | 291 | this->disp.drawString(DisplayN18::WIDTH - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, DisplayN18::WHITE, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 292 | this->disp.drawString(DisplayN18::WIDTH - (DisplayN18::CHAR_WIDTH * 4), 0, buf, DisplayN18::WHITE, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 293 | } |
john_ghielec | 1:cd8a3926f263 | 294 | |
john_ghielec | 1:cd8a3926f263 | 295 | void Game::checkPwm() { |
john_ghielec | 1:cd8a3926f263 | 296 | if (this->pwmTicksLeft == 0) { |
devhammer | 3:2f09c90a732d | 297 | this->pwm.write(0.0); |
john_ghielec | 1:cd8a3926f263 | 298 | } |
john_ghielec | 1:cd8a3926f263 | 299 | else { |
john_ghielec | 1:cd8a3926f263 | 300 | this->pwmTicksLeft--; |
john_ghielec | 1:cd8a3926f263 | 301 | this->pwm.write(0.5); |
john_ghielec | 1:cd8a3926f263 | 302 | } |
john_ghielec | 1:cd8a3926f263 | 303 | } |
john_ghielec | 1:cd8a3926f263 | 304 | |
john_ghielec | 1:cd8a3926f263 | 305 | void Game::checkLives() { |
john_ghielec | 1:cd8a3926f263 | 306 | if (this->lives == 0) { |
john_ghielec | 1:cd8a3926f263 | 307 | this->disp.clear(); |
devhammer | 3:2f09c90a732d | 308 | |
john_ghielec | 1:cd8a3926f263 | 309 | this->drawString(Game::LOSE_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT); |
john_ghielec | 1:cd8a3926f263 | 310 | this->drawString(Game::LOSE_2, DisplayN18::HEIGHT / 2); |
john_ghielec | 1:cd8a3926f263 | 311 | |
devhammer | 3:2f09c90a732d | 312 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 313 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 314 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 315 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 316 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 317 | |
devhammer | 3:2f09c90a732d | 318 | this->pwm.period(1.0/196); |
devhammer | 3:2f09c90a732d | 319 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 320 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 321 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 322 | |
devhammer | 3:2f09c90a732d | 323 | this->pwm.period(1.0/164.81); |
devhammer | 3:2f09c90a732d | 324 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 325 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 326 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 327 | } |
devhammer | 3:2f09c90a732d | 328 | |
john_ghielec | 1:cd8a3926f263 | 329 | while (this->circle.read()) |
john_ghielec | 1:cd8a3926f263 | 330 | wait_ms(1); |
john_ghielec | 1:cd8a3926f263 | 331 | |
john_ghielec | 1:cd8a3926f263 | 332 | this->initialize(); |
john_ghielec | 1:cd8a3926f263 | 333 | } |
john_ghielec | 1:cd8a3926f263 | 334 | else { |
devhammer | 3:2f09c90a732d | 335 | this->disp.drawString(0, 0, Game::LIVES, DisplayN18::WHITE, DisplayN18::BLACK); |
devhammer | 3:2f09c90a732d | 336 | this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), DisplayN18::WHITE, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 337 | } |
john_ghielec | 1:cd8a3926f263 | 338 | } |