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