Some random attempts at programming the retro console
Fork of RETRO_Pong_Mod by
Game.cpp@7:c0f12f624832, 2015-02-27 (annotated)
- Committer:
- loop
- Date:
- Fri Feb 27 20:21:32 2015 +0000
- Revision:
- 7:c0f12f624832
- Parent:
- 6:20788dfdb7b8
- Child:
- 8:c63981a45c95
(Almost) ported to LCD_ST7735 lib - faster!
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). |
loop | 7:c0f12f624832 | 9 | // Further mods buy loop (loop23) |
loop | 7:c0f12f624832 | 10 | // Changes: |
loop | 7:c0f12f624832 | 11 | // - Removed useless stuff |
loop | 7:c0f12f624832 | 12 | // - Read accelerometer in game |
devhammer | 3:2f09c90a732d | 13 | |
john_ghielec | 1:cd8a3926f263 | 14 | #include "Game.h" |
john_ghielec | 1:cd8a3926f263 | 15 | |
john_ghielec | 1:cd8a3926f263 | 16 | const char* Game::LOSE_1 = "You lose."; |
john_ghielec | 1:cd8a3926f263 | 17 | const char* Game::LOSE_2 = "Press ship to restart."; |
john_ghielec | 2:6ab46f2e851a | 18 | const char* Game::SPLASH_1 = "Press ship to start."; |
john_ghielec | 2:6ab46f2e851a | 19 | const char* Game::SPLASH_2 = "Press robot to switch."; |
devhammer | 3:2f09c90a732d | 20 | const char* Game::LIVES = "Lives: "; |
devhammer | 3:2f09c90a732d | 21 | const char* Game::SCORE = "Score: "; |
john_ghielec | 1:cd8a3926f263 | 22 | |
loop | 7:c0f12f624832 | 23 | Game::Game(): left(P0_14, PullUp), |
loop | 7:c0f12f624832 | 24 | right(P0_11, PullUp), |
loop | 7:c0f12f624832 | 25 | down(P0_12, PullUp), |
loop | 7:c0f12f624832 | 26 | up(P0_13, PullUp), |
loop | 7:c0f12f624832 | 27 | square(P0_16, PullUp), |
loop | 7:c0f12f624832 | 28 | circle(P0_1, PullUp), |
loop | 7:c0f12f624832 | 29 | led1(P0_9), |
loop | 7:c0f12f624832 | 30 | led2(P0_8), |
loop | 7:c0f12f624832 | 31 | pwm(P0_18), |
loop | 7:c0f12f624832 | 32 | ain(P0_15), |
loop | 7:c0f12f624832 | 33 | i2c(P0_5, P0_4), |
loop | 7:c0f12f624832 | 34 | disp(P0_19, P0_20, P0_7, P0_21, P0_22, P1_15, P0_2, LCD_ST7735::RGB) { |
john_ghielec | 1:cd8a3926f263 | 35 | srand(this->ain.read_u16()); |
john_ghielec | 1:cd8a3926f263 | 36 | |
john_ghielec | 2:6ab46f2e851a | 37 | this->lastUp = false; |
john_ghielec | 2:6ab46f2e851a | 38 | this->lastDown = false; |
john_ghielec | 2:6ab46f2e851a | 39 | this->mode = true; |
john_ghielec | 2:6ab46f2e851a | 40 | |
john_ghielec | 2:6ab46f2e851a | 41 | this->i2c.frequency(400); |
john_ghielec | 2:6ab46f2e851a | 42 | this->writeRegister(0x2A, 0x01); |
john_ghielec | 2:6ab46f2e851a | 43 | |
loop | 7:c0f12f624832 | 44 | this->colors[0] = Color565::Red; |
loop | 7:c0f12f624832 | 45 | this->colors[1] = Color565::Green; |
loop | 7:c0f12f624832 | 46 | this->colors[2] = Color565::Blue; |
john_ghielec | 2:6ab46f2e851a | 47 | |
john_ghielec | 1:cd8a3926f263 | 48 | this->initialize(); |
john_ghielec | 1:cd8a3926f263 | 49 | } |
john_ghielec | 1:cd8a3926f263 | 50 | |
john_ghielec | 2:6ab46f2e851a | 51 | void Game::readRegisters(char address, char* buffer, int len) { |
john_ghielec | 2:6ab46f2e851a | 52 | this->i2c.write(Game::I2C_ADDR, &address, 1, true); |
john_ghielec | 2:6ab46f2e851a | 53 | this->i2c.read(Game::I2C_ADDR | 1, buffer, len); |
john_ghielec | 2:6ab46f2e851a | 54 | } |
john_ghielec | 2:6ab46f2e851a | 55 | |
john_ghielec | 2:6ab46f2e851a | 56 | int Game::writeRegister(char address, char value) { |
john_ghielec | 2:6ab46f2e851a | 57 | char buffer[2] = { address, value }; |
john_ghielec | 2:6ab46f2e851a | 58 | |
john_ghielec | 2:6ab46f2e851a | 59 | return this->i2c.write(Game::I2C_ADDR, buffer, 2); |
john_ghielec | 2:6ab46f2e851a | 60 | } |
john_ghielec | 2:6ab46f2e851a | 61 | |
john_ghielec | 2:6ab46f2e851a | 62 | double Game::convert(char* buffer) { |
john_ghielec | 2:6ab46f2e851a | 63 | double val = ((buffer[0] << 2) | (buffer[1] >> 6)); |
john_ghielec | 2:6ab46f2e851a | 64 | |
john_ghielec | 2:6ab46f2e851a | 65 | if (val > 511.0) |
john_ghielec | 2:6ab46f2e851a | 66 | val -= 1024.0; |
john_ghielec | 2:6ab46f2e851a | 67 | |
john_ghielec | 2:6ab46f2e851a | 68 | return val / 512.0; |
john_ghielec | 2:6ab46f2e851a | 69 | } |
john_ghielec | 2:6ab46f2e851a | 70 | |
loop | 7:c0f12f624832 | 71 | void Game::getXY(double& x, double& y) { |
loop | 7:c0f12f624832 | 72 | char buffer[4]; |
loop | 7:c0f12f624832 | 73 | this->readRegisters(0x01, buffer, 4); |
loop | 7:c0f12f624832 | 74 | x = this->convert(buffer); |
loop | 7:c0f12f624832 | 75 | y = this->convert(buffer + 2); |
loop | 7:c0f12f624832 | 76 | } |
loop | 7:c0f12f624832 | 77 | |
john_ghielec | 2:6ab46f2e851a | 78 | void Game::getXYZ(double& x, double& y, double& z) { |
john_ghielec | 2:6ab46f2e851a | 79 | char buffer[6]; |
loop | 7:c0f12f624832 | 80 | this->readRegisters(0x01, buffer, 4); |
john_ghielec | 2:6ab46f2e851a | 81 | x = this->convert(buffer); |
john_ghielec | 2:6ab46f2e851a | 82 | y = this->convert(buffer + 2); |
john_ghielec | 2:6ab46f2e851a | 83 | z = this->convert(buffer + 4); |
loop | 7:c0f12f624832 | 84 | |
john_ghielec | 2:6ab46f2e851a | 85 | } |
john_ghielec | 2:6ab46f2e851a | 86 | |
loop | 7:c0f12f624832 | 87 | |
john_ghielec | 2:6ab46f2e851a | 88 | void Game::printDouble(double value, int x, int y) { |
john_ghielec | 2:6ab46f2e851a | 89 | char buffer[10]; |
john_ghielec | 2:6ab46f2e851a | 90 | int len = sprintf(buffer, "%.1f", value); |
john_ghielec | 2:6ab46f2e851a | 91 | |
loop | 7:c0f12f624832 | 92 | this->disp.drawString(font_ibm, x, y, buffer); |
john_ghielec | 2:6ab46f2e851a | 93 | } |
john_ghielec | 2:6ab46f2e851a | 94 | |
john_ghielec | 2:6ab46f2e851a | 95 | void Game::drawAxes() { |
john_ghielec | 2:6ab46f2e851a | 96 | for (int i = 0; i < 3; i++) { |
loop | 7:c0f12f624832 | 97 | this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING), 0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT, Color565::White); |
loop | 7:c0f12f624832 | 98 | this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, disp.getWidth(), i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT / 2, Color565::White); |
john_ghielec | 2:6ab46f2e851a | 99 | } |
john_ghielec | 2:6ab46f2e851a | 100 | } |
john_ghielec | 2:6ab46f2e851a | 101 | |
john_ghielec | 2:6ab46f2e851a | 102 | void Game::drawPoint(int axis, double value) { |
john_ghielec | 2:6ab46f2e851a | 103 | if (value < -1.0) |
john_ghielec | 2:6ab46f2e851a | 104 | value = -1.0; |
john_ghielec | 2:6ab46f2e851a | 105 | |
john_ghielec | 2:6ab46f2e851a | 106 | if (value > 1.0) |
john_ghielec | 2:6ab46f2e851a | 107 | value = 1.0; |
john_ghielec | 2:6ab46f2e851a | 108 | |
john_ghielec | 2:6ab46f2e851a | 109 | value += 1.0; |
john_ghielec | 2:6ab46f2e851a | 110 | value /= 2.0; |
john_ghielec | 2:6ab46f2e851a | 111 | value = 1.0 - value; |
john_ghielec | 2:6ab46f2e851a | 112 | value *= Game::GRAPH_HEIGHT; |
john_ghielec | 2:6ab46f2e851a | 113 | |
john_ghielec | 2:6ab46f2e851a | 114 | this->disp.setPixel(this->graphX, axis * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + (int)value, this->colors[axis]); |
john_ghielec | 2:6ab46f2e851a | 115 | } |
john_ghielec | 2:6ab46f2e851a | 116 | |
john_ghielec | 2:6ab46f2e851a | 117 | void Game::checkGraphReset() { |
loop | 7:c0f12f624832 | 118 | if (this->graphX > disp.getWidth()) { |
john_ghielec | 2:6ab46f2e851a | 119 | this->graphX = 0; |
loop | 7:c0f12f624832 | 120 | this->disp.clearScreen(); |
john_ghielec | 2:6ab46f2e851a | 121 | this->drawAxes(); |
john_ghielec | 2:6ab46f2e851a | 122 | } |
john_ghielec | 2:6ab46f2e851a | 123 | } |
john_ghielec | 2:6ab46f2e851a | 124 | |
john_ghielec | 1:cd8a3926f263 | 125 | void Game::initialize() { |
john_ghielec | 1:cd8a3926f263 | 126 | this->initializeBall(); |
john_ghielec | 1:cd8a3926f263 | 127 | this->pwmTicksLeft = 0; |
john_ghielec | 1:cd8a3926f263 | 128 | this->lives = 4; |
devhammer | 3:2f09c90a732d | 129 | this->score = 0; |
loop | 7:c0f12f624832 | 130 | this->muted = false; |
john_ghielec | 1:cd8a3926f263 | 131 | |
devhammer | 3:2f09c90a732d | 132 | this->pwm.period(1); |
john_ghielec | 1:cd8a3926f263 | 133 | this->pwm.write(0.00); |
loop | 7:c0f12f624832 | 134 | this->disp.setOrientation(LCD_ST7735::Rotate270, false); |
loop | 7:c0f12f624832 | 135 | this->disp.clearScreen(); |
john_ghielec | 1:cd8a3926f263 | 136 | } |
john_ghielec | 1:cd8a3926f263 | 137 | |
john_ghielec | 1:cd8a3926f263 | 138 | void Game::initializeBall() { |
loop | 7:c0f12f624832 | 139 | this->ballX = disp.getWidth() / 2 - Game::BALL_RADIUS; |
loop | 7:c0f12f624832 | 140 | this->ballY = disp.getHeight() / 4 - Game::BALL_RADIUS; |
loop | 6:20788dfdb7b8 | 141 | this->ballSpeedX = 0; |
loop | 6:20788dfdb7b8 | 142 | this->ballSpeedY = 0; |
loop | 4:84be90860d7c | 143 | this->ballAccelX = 0; |
loop | 4:84be90860d7c | 144 | this->ballAccelY = 0; |
john_ghielec | 2:6ab46f2e851a | 145 | } |
john_ghielec | 2:6ab46f2e851a | 146 | |
loop | 5:8a26ad9d9ea1 | 147 | void Game::readAccel() { |
loop | 7:c0f12f624832 | 148 | double x, y; |
loop | 7:c0f12f624832 | 149 | this->getXY(x, y); |
loop | 6:20788dfdb7b8 | 150 | x = x * 8; |
loop | 6:20788dfdb7b8 | 151 | y = y * 8; |
loop | 6:20788dfdb7b8 | 152 | this->ballAccelX = (int)x; |
loop | 6:20788dfdb7b8 | 153 | this->ballAccelY = (int)y; |
loop | 6:20788dfdb7b8 | 154 | // this->printDouble(x, 20, 15); |
loop | 6:20788dfdb7b8 | 155 | // this->printDouble(y, 20, 25); |
loop | 5:8a26ad9d9ea1 | 156 | } |
loop | 5:8a26ad9d9ea1 | 157 | |
loop | 6:20788dfdb7b8 | 158 | void Game::tick() { |
loop | 6:20788dfdb7b8 | 159 | int tcount = 0; |
john_ghielec | 2:6ab46f2e851a | 160 | this->checkButtons(); |
john_ghielec | 1:cd8a3926f263 | 161 | |
john_ghielec | 2:6ab46f2e851a | 162 | if (this->mode) { |
loop | 7:c0f12f624832 | 163 | if ((tcount++ % 10) == 0) { |
loop | 6:20788dfdb7b8 | 164 | this->readAccel(); |
loop | 6:20788dfdb7b8 | 165 | }; |
john_ghielec | 2:6ab46f2e851a | 166 | this->clearBall(); |
john_ghielec | 2:6ab46f2e851a | 167 | this->updateBall(); |
john_ghielec | 2:6ab46f2e851a | 168 | this->checkCollision(); |
john_ghielec | 2:6ab46f2e851a | 169 | this->drawBall(); |
loop | 7:c0f12f624832 | 170 | // this->checkPwm(); |
loop | 7:c0f12f624832 | 171 | //this->checkLives(); |
john_ghielec | 2:6ab46f2e851a | 172 | } |
john_ghielec | 2:6ab46f2e851a | 173 | else { |
john_ghielec | 2:6ab46f2e851a | 174 | double x, y, z; |
john_ghielec | 2:6ab46f2e851a | 175 | |
john_ghielec | 2:6ab46f2e851a | 176 | this->getXYZ(x, y, z); |
john_ghielec | 2:6ab46f2e851a | 177 | |
john_ghielec | 2:6ab46f2e851a | 178 | this->checkGraphReset(); |
john_ghielec | 2:6ab46f2e851a | 179 | this->drawPoint(0, x); |
john_ghielec | 2:6ab46f2e851a | 180 | this->drawPoint(1, y); |
john_ghielec | 2:6ab46f2e851a | 181 | this->drawPoint(2, z); |
john_ghielec | 2:6ab46f2e851a | 182 | this->graphX++; |
john_ghielec | 2:6ab46f2e851a | 183 | } |
john_ghielec | 1:cd8a3926f263 | 184 | } |
john_ghielec | 1:cd8a3926f263 | 185 | |
john_ghielec | 2:6ab46f2e851a | 186 | void Game::checkButtons() { |
john_ghielec | 2:6ab46f2e851a | 187 | if (!this->square.read()) { |
devhammer | 3:2f09c90a732d | 188 | //this->muted = !this->muted; |
john_ghielec | 2:6ab46f2e851a | 189 | this->mode = !this->mode; |
john_ghielec | 2:6ab46f2e851a | 190 | |
loop | 7:c0f12f624832 | 191 | this->disp.clearScreen(); |
john_ghielec | 2:6ab46f2e851a | 192 | |
john_ghielec | 2:6ab46f2e851a | 193 | if (!this->mode) { |
john_ghielec | 2:6ab46f2e851a | 194 | this->graphX = 0; |
john_ghielec | 2:6ab46f2e851a | 195 | |
john_ghielec | 2:6ab46f2e851a | 196 | this->drawAxes(); |
john_ghielec | 2:6ab46f2e851a | 197 | } |
john_ghielec | 2:6ab46f2e851a | 198 | |
devhammer | 3:2f09c90a732d | 199 | //this->led1.write(this->muted); |
devhammer | 3:2f09c90a732d | 200 | //this->led2.write(!this->muted); |
john_ghielec | 2:6ab46f2e851a | 201 | this->led1.write(this->mode); |
john_ghielec | 2:6ab46f2e851a | 202 | this->led2.write(!this->mode); |
john_ghielec | 2:6ab46f2e851a | 203 | } |
john_ghielec | 2:6ab46f2e851a | 204 | |
john_ghielec | 2:6ab46f2e851a | 205 | bool xDir = this->ballSpeedX > 0; |
john_ghielec | 2:6ab46f2e851a | 206 | bool yDir = this->ballSpeedY > 0; |
john_ghielec | 2:6ab46f2e851a | 207 | bool isUp = !this->up.read(); |
john_ghielec | 2:6ab46f2e851a | 208 | bool isDown = !this->down.read(); |
john_ghielec | 1:cd8a3926f263 | 209 | |
john_ghielec | 2:6ab46f2e851a | 210 | if (isUp && isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 211 | if (!isUp && !isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 212 | |
john_ghielec | 2:6ab46f2e851a | 213 | if (isUp && this->lastUp) goto end; |
john_ghielec | 2:6ab46f2e851a | 214 | if (isDown && this->lastDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 215 | |
john_ghielec | 2:6ab46f2e851a | 216 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 217 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 218 | |
john_ghielec | 2:6ab46f2e851a | 219 | if (isUp) { |
john_ghielec | 2:6ab46f2e851a | 220 | if (++this->ballSpeedX > 5) this->ballSpeedX = 5; |
john_ghielec | 2:6ab46f2e851a | 221 | if (++this->ballSpeedY > 5) this->ballSpeedY = 5; |
john_ghielec | 2:6ab46f2e851a | 222 | } |
john_ghielec | 2:6ab46f2e851a | 223 | else if (isDown) { |
john_ghielec | 2:6ab46f2e851a | 224 | if (--this->ballSpeedX == 0) this->ballSpeedX = 1; |
john_ghielec | 2:6ab46f2e851a | 225 | if (--this->ballSpeedY == 0) this->ballSpeedY = 1; |
john_ghielec | 2:6ab46f2e851a | 226 | } |
john_ghielec | 1:cd8a3926f263 | 227 | |
john_ghielec | 2:6ab46f2e851a | 228 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 229 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 2:6ab46f2e851a | 230 | |
john_ghielec | 2:6ab46f2e851a | 231 | end: |
john_ghielec | 2:6ab46f2e851a | 232 | this->lastUp = isUp; |
john_ghielec | 2:6ab46f2e851a | 233 | this->lastDown = isDown; |
john_ghielec | 1:cd8a3926f263 | 234 | } |
john_ghielec | 1:cd8a3926f263 | 235 | |
john_ghielec | 1:cd8a3926f263 | 236 | void Game::drawString(const char* str, int y) { |
loop | 7:c0f12f624832 | 237 | this->disp.drawString(font_ibm, |
loop | 7:c0f12f624832 | 238 | this->disp.getWidth() / 2 - (CHAR_WIDTH + CHAR_SPACING) * strlen(str) / 2, |
loop | 7:c0f12f624832 | 239 | y, str); |
john_ghielec | 1:cd8a3926f263 | 240 | } |
john_ghielec | 1:cd8a3926f263 | 241 | void Game::showSplashScreen() { |
loop | 7:c0f12f624832 | 242 | this->drawString(Game::SPLASH_1, this->disp.getHeight() / 2 - CHAR_HEIGHT / 2); |
loop | 7:c0f12f624832 | 243 | this->drawString(Game::SPLASH_2, this->disp.getHeight() / 2 + CHAR_HEIGHT / 2); |
john_ghielec | 1:cd8a3926f263 | 244 | |
john_ghielec | 1:cd8a3926f263 | 245 | while (this->circle.read()) |
loop | 7:c0f12f624832 | 246 | wait_ms(5); |
john_ghielec | 1:cd8a3926f263 | 247 | |
loop | 7:c0f12f624832 | 248 | this->disp.clearScreen(); |
john_ghielec | 1:cd8a3926f263 | 249 | } |
john_ghielec | 1:cd8a3926f263 | 250 | |
john_ghielec | 1:cd8a3926f263 | 251 | void Game::clearBall() { |
loop | 7:c0f12f624832 | 252 | this->disp.fillRect(ballX - BALL_RADIUS, |
loop | 7:c0f12f624832 | 253 | ballY - BALL_RADIUS, |
loop | 7:c0f12f624832 | 254 | ballX + BALL_RADIUS, |
loop | 7:c0f12f624832 | 255 | ballY + BALL_RADIUS, |
loop | 7:c0f12f624832 | 256 | Color565::Black); |
john_ghielec | 1:cd8a3926f263 | 257 | } |
john_ghielec | 1:cd8a3926f263 | 258 | |
john_ghielec | 1:cd8a3926f263 | 259 | void Game::drawBall() { |
loop | 7:c0f12f624832 | 260 | this->disp.fillRect(ballX - BALL_RADIUS, |
loop | 7:c0f12f624832 | 261 | ballY - BALL_RADIUS, |
loop | 7:c0f12f624832 | 262 | ballX + BALL_RADIUS, |
loop | 7:c0f12f624832 | 263 | ballY + BALL_RADIUS, |
loop | 7:c0f12f624832 | 264 | Color565::Red); |
john_ghielec | 1:cd8a3926f263 | 265 | } |
john_ghielec | 1:cd8a3926f263 | 266 | |
john_ghielec | 1:cd8a3926f263 | 267 | void Game::updateBall() { |
loop | 5:8a26ad9d9ea1 | 268 | this->ballSpeedX += this->ballAccelX; |
loop | 5:8a26ad9d9ea1 | 269 | this->ballSpeedY += this->ballAccelY; |
john_ghielec | 1:cd8a3926f263 | 270 | this->ballX += this->ballSpeedX; |
john_ghielec | 1:cd8a3926f263 | 271 | this->ballY += this->ballSpeedY; |
john_ghielec | 1:cd8a3926f263 | 272 | } |
john_ghielec | 1:cd8a3926f263 | 273 | |
loop | 4:84be90860d7c | 274 | void Game::checkCollision() { |
loop | 4:84be90860d7c | 275 | // Does bounds checking first.. |
loop | 4:84be90860d7c | 276 | if ((this->ballX - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedX < 0) || |
loop | 7:c0f12f624832 | 277 | (this->ballX + Game::BALL_RADIUS * 2 >= disp.getWidth() && this->ballSpeedX > 0)) { |
john_ghielec | 1:cd8a3926f263 | 278 | this->ballSpeedX *= -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 | } |
loop | 4:84be90860d7c | 284 | if ((this->ballY - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedY < 0) || |
loop | 7:c0f12f624832 | 285 | (this->ballY + Game::BALL_RADIUS * 2 >= disp.getHeight() && this->ballSpeedY > 0)){ |
john_ghielec | 1:cd8a3926f263 | 286 | this->ballSpeedY *= -1; |
devhammer | 3:2f09c90a732d | 287 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 288 | this->pwm.period_ms(2); |
devhammer | 3:2f09c90a732d | 289 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 290 | } |
devhammer | 3:2f09c90a732d | 291 | } |
loop | 4:84be90860d7c | 292 | /* |
john_ghielec | 1:cd8a3926f263 | 293 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT - Game::PADDLE_HEIGHT && this->ballSpeedY > 0) { |
john_ghielec | 1:cd8a3926f263 | 294 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT) { |
john_ghielec | 1:cd8a3926f263 | 295 | this->initializeBall(); |
john_ghielec | 1:cd8a3926f263 | 296 | |
john_ghielec | 1:cd8a3926f263 | 297 | this->lives--; |
devhammer | 3:2f09c90a732d | 298 | |
devhammer | 3:2f09c90a732d | 299 | if(this->lives > 0) { |
devhammer | 3:2f09c90a732d | 300 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 301 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 302 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 303 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 304 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 305 | } |
devhammer | 3:2f09c90a732d | 306 | } |
devhammer | 3:2f09c90a732d | 307 | |
john_ghielec | 1:cd8a3926f263 | 308 | } |
john_ghielec | 1:cd8a3926f263 | 309 | else if (this->ballX > this->paddleX && this->ballX < this->paddleX + Game::PADDLE_WIDTH) { |
john_ghielec | 1:cd8a3926f263 | 310 | this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 311 | |
devhammer | 3:2f09c90a732d | 312 | if(!this->muted){ |
devhammer | 3:2f09c90a732d | 313 | this->pwm.period_ms(1); |
devhammer | 3:2f09c90a732d | 314 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 315 | } |
devhammer | 3:2f09c90a732d | 316 | this->score = this->score + 10; |
devhammer | 3:2f09c90a732d | 317 | if(this->score % 100 == 0) { |
devhammer | 3:2f09c90a732d | 318 | if(this->ballSpeedX < 0){ |
devhammer | 3:2f09c90a732d | 319 | this->ballSpeedX -= 1; |
devhammer | 3:2f09c90a732d | 320 | } |
devhammer | 3:2f09c90a732d | 321 | else { |
devhammer | 3:2f09c90a732d | 322 | this->ballSpeedX += 1; |
devhammer | 3:2f09c90a732d | 323 | } |
devhammer | 3:2f09c90a732d | 324 | this->ballSpeedY -= 1; |
devhammer | 3:2f09c90a732d | 325 | } |
john_ghielec | 1:cd8a3926f263 | 326 | } |
john_ghielec | 1:cd8a3926f263 | 327 | } |
devhammer | 3:2f09c90a732d | 328 | char buf[10]; |
devhammer | 3:2f09c90a732d | 329 | int a = this->score; |
devhammer | 3:2f09c90a732d | 330 | sprintf(buf, "%d", a); |
loop | 7:c0f12f624832 | 331 | this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, Color565::White, DisplayN18::BLACK); |
loop | 7:c0f12f624832 | 332 | this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 4), 0, buf, Color565::White, DisplayN18::BLACK); |
loop | 7:c0f12f624832 | 333 | */ |
loop | 7:c0f12f624832 | 334 | |
john_ghielec | 1:cd8a3926f263 | 335 | } |
john_ghielec | 1:cd8a3926f263 | 336 | |
john_ghielec | 1:cd8a3926f263 | 337 | void Game::checkPwm() { |
john_ghielec | 1:cd8a3926f263 | 338 | if (this->pwmTicksLeft == 0) { |
devhammer | 3:2f09c90a732d | 339 | this->pwm.write(0.0); |
john_ghielec | 1:cd8a3926f263 | 340 | } |
john_ghielec | 1:cd8a3926f263 | 341 | else { |
john_ghielec | 1:cd8a3926f263 | 342 | this->pwmTicksLeft--; |
john_ghielec | 1:cd8a3926f263 | 343 | this->pwm.write(0.5); |
john_ghielec | 1:cd8a3926f263 | 344 | } |
john_ghielec | 1:cd8a3926f263 | 345 | } |
john_ghielec | 1:cd8a3926f263 | 346 | |
john_ghielec | 1:cd8a3926f263 | 347 | void Game::checkLives() { |
john_ghielec | 1:cd8a3926f263 | 348 | if (this->lives == 0) { |
loop | 7:c0f12f624832 | 349 | this->disp.clearScreen(); |
devhammer | 3:2f09c90a732d | 350 | |
loop | 7:c0f12f624832 | 351 | this->drawString(Game::LOSE_1, disp.getHeight() / 2 - CHAR_HEIGHT); |
loop | 7:c0f12f624832 | 352 | this->drawString(Game::LOSE_2, disp.getHeight() / 2); |
john_ghielec | 1:cd8a3926f263 | 353 | |
devhammer | 3:2f09c90a732d | 354 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 355 | this->pwm.period(1.0/220); |
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 | this->pwm.period(1.0/196); |
devhammer | 3:2f09c90a732d | 361 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 362 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 363 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 364 | |
devhammer | 3:2f09c90a732d | 365 | this->pwm.period(1.0/164.81); |
devhammer | 3:2f09c90a732d | 366 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 367 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 368 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 369 | } |
devhammer | 3:2f09c90a732d | 370 | |
john_ghielec | 1:cd8a3926f263 | 371 | while (this->circle.read()) |
john_ghielec | 1:cd8a3926f263 | 372 | wait_ms(1); |
john_ghielec | 1:cd8a3926f263 | 373 | |
john_ghielec | 1:cd8a3926f263 | 374 | this->initialize(); |
john_ghielec | 1:cd8a3926f263 | 375 | } |
john_ghielec | 1:cd8a3926f263 | 376 | else { |
loop | 7:c0f12f624832 | 377 | this->disp.drawString(font_ibm, 0, 0, Game::LIVES); |
loop | 7:c0f12f624832 | 378 | //this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), Color565::White, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 379 | } |
john_ghielec | 1:cd8a3926f263 | 380 | } |