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