Some random attempts at programming the retro console
Fork of RETRO_Pong_Mod by
Game.cpp@8:c63981a45c95, 2015-02-28 (annotated)
- Committer:
- loop
- Date:
- Sat Feb 28 00:04:52 2015 +0000
- Revision:
- 8:c63981a45c95
- Parent:
- 7:c0f12f624832
- Child:
- 9:5c4a3e89a713
Brought in MusicEngine
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 | 8:c63981a45c95 | 34 | disp(P0_19, P0_20, P0_7, P0_21, P0_22, P1_15, P0_2, LCD_ST7735::RGB), |
loop | 8:c63981a45c95 | 35 | music(P0_18) { |
john_ghielec | 1:cd8a3926f263 | 36 | srand(this->ain.read_u16()); |
john_ghielec | 1:cd8a3926f263 | 37 | |
john_ghielec | 2:6ab46f2e851a | 38 | this->lastUp = false; |
john_ghielec | 2:6ab46f2e851a | 39 | this->lastDown = false; |
john_ghielec | 2:6ab46f2e851a | 40 | this->mode = true; |
john_ghielec | 2:6ab46f2e851a | 41 | |
john_ghielec | 2:6ab46f2e851a | 42 | this->i2c.frequency(400); |
john_ghielec | 2:6ab46f2e851a | 43 | this->writeRegister(0x2A, 0x01); |
john_ghielec | 2:6ab46f2e851a | 44 | |
loop | 7:c0f12f624832 | 45 | this->colors[0] = Color565::Red; |
loop | 7:c0f12f624832 | 46 | this->colors[1] = Color565::Green; |
loop | 7:c0f12f624832 | 47 | this->colors[2] = Color565::Blue; |
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 | |
loop | 8:c63981a45c95 | 125 | void playSong() { |
loop | 8:c63981a45c95 | 126 | MusicEngine music(P0_18); |
loop | 8:c63981a45c95 | 127 | music.play("t160o3l4V12@1ed8ce8gg8er8aa8>c<a8g2raa8ga8>cc8d<r8ee8de8c2rdd8dd8dd8dr8ed8ef8g2raa8ga8>cc8<ar8>dc8de8d2<r>ee8dc8<ab8>cc8<gg8ea8g2r>cc8<ge8cd8ea8gg8de8"); |
loop | 8:c63981a45c95 | 128 | } |
loop | 8:c63981a45c95 | 129 | |
loop | 8:c63981a45c95 | 130 | void Game::initialize() { |
loop | 8:c63981a45c95 | 131 | this->music.setCompletionCallback(&playSong); |
loop | 8:c63981a45c95 | 132 | playSong(); |
john_ghielec | 1:cd8a3926f263 | 133 | this->initializeBall(); |
john_ghielec | 1:cd8a3926f263 | 134 | this->pwmTicksLeft = 0; |
john_ghielec | 1:cd8a3926f263 | 135 | this->lives = 4; |
devhammer | 3:2f09c90a732d | 136 | this->score = 0; |
loop | 7:c0f12f624832 | 137 | this->muted = false; |
john_ghielec | 1:cd8a3926f263 | 138 | |
loop | 8:c63981a45c95 | 139 | //this->pwm.period(1); |
loop | 8:c63981a45c95 | 140 | //this->pwm.write(0.00); |
loop | 7:c0f12f624832 | 141 | this->disp.setOrientation(LCD_ST7735::Rotate270, false); |
loop | 7:c0f12f624832 | 142 | this->disp.clearScreen(); |
john_ghielec | 1:cd8a3926f263 | 143 | } |
john_ghielec | 1:cd8a3926f263 | 144 | |
john_ghielec | 1:cd8a3926f263 | 145 | void Game::initializeBall() { |
loop | 7:c0f12f624832 | 146 | this->ballX = disp.getWidth() / 2 - Game::BALL_RADIUS; |
loop | 7:c0f12f624832 | 147 | this->ballY = disp.getHeight() / 4 - Game::BALL_RADIUS; |
loop | 6:20788dfdb7b8 | 148 | this->ballSpeedX = 0; |
loop | 6:20788dfdb7b8 | 149 | this->ballSpeedY = 0; |
loop | 4:84be90860d7c | 150 | this->ballAccelX = 0; |
loop | 4:84be90860d7c | 151 | this->ballAccelY = 0; |
john_ghielec | 2:6ab46f2e851a | 152 | } |
john_ghielec | 2:6ab46f2e851a | 153 | |
loop | 5:8a26ad9d9ea1 | 154 | void Game::readAccel() { |
loop | 7:c0f12f624832 | 155 | double x, y; |
loop | 7:c0f12f624832 | 156 | this->getXY(x, y); |
loop | 6:20788dfdb7b8 | 157 | x = x * 8; |
loop | 6:20788dfdb7b8 | 158 | y = y * 8; |
loop | 6:20788dfdb7b8 | 159 | this->ballAccelX = (int)x; |
loop | 6:20788dfdb7b8 | 160 | this->ballAccelY = (int)y; |
loop | 6:20788dfdb7b8 | 161 | // this->printDouble(x, 20, 15); |
loop | 6:20788dfdb7b8 | 162 | // this->printDouble(y, 20, 25); |
loop | 5:8a26ad9d9ea1 | 163 | } |
loop | 5:8a26ad9d9ea1 | 164 | |
loop | 6:20788dfdb7b8 | 165 | void Game::tick() { |
loop | 6:20788dfdb7b8 | 166 | int tcount = 0; |
john_ghielec | 2:6ab46f2e851a | 167 | this->checkButtons(); |
john_ghielec | 1:cd8a3926f263 | 168 | |
john_ghielec | 2:6ab46f2e851a | 169 | if (this->mode) { |
loop | 7:c0f12f624832 | 170 | if ((tcount++ % 10) == 0) { |
loop | 6:20788dfdb7b8 | 171 | this->readAccel(); |
loop | 6:20788dfdb7b8 | 172 | }; |
john_ghielec | 2:6ab46f2e851a | 173 | this->updateBall(); |
john_ghielec | 2:6ab46f2e851a | 174 | this->checkCollision(); |
loop | 8:c63981a45c95 | 175 | this->clearBall(); |
john_ghielec | 2:6ab46f2e851a | 176 | this->drawBall(); |
loop | 7:c0f12f624832 | 177 | // this->checkPwm(); |
loop | 7:c0f12f624832 | 178 | //this->checkLives(); |
john_ghielec | 2:6ab46f2e851a | 179 | } |
john_ghielec | 2:6ab46f2e851a | 180 | else { |
john_ghielec | 2:6ab46f2e851a | 181 | double x, y, z; |
john_ghielec | 2:6ab46f2e851a | 182 | |
john_ghielec | 2:6ab46f2e851a | 183 | this->getXYZ(x, y, z); |
john_ghielec | 2:6ab46f2e851a | 184 | |
john_ghielec | 2:6ab46f2e851a | 185 | this->checkGraphReset(); |
john_ghielec | 2:6ab46f2e851a | 186 | this->drawPoint(0, x); |
john_ghielec | 2:6ab46f2e851a | 187 | this->drawPoint(1, y); |
john_ghielec | 2:6ab46f2e851a | 188 | this->drawPoint(2, z); |
john_ghielec | 2:6ab46f2e851a | 189 | this->graphX++; |
john_ghielec | 2:6ab46f2e851a | 190 | } |
john_ghielec | 1:cd8a3926f263 | 191 | } |
john_ghielec | 1:cd8a3926f263 | 192 | |
john_ghielec | 2:6ab46f2e851a | 193 | void Game::checkButtons() { |
john_ghielec | 2:6ab46f2e851a | 194 | if (!this->square.read()) { |
devhammer | 3:2f09c90a732d | 195 | //this->muted = !this->muted; |
john_ghielec | 2:6ab46f2e851a | 196 | this->mode = !this->mode; |
john_ghielec | 2:6ab46f2e851a | 197 | |
loop | 7:c0f12f624832 | 198 | this->disp.clearScreen(); |
john_ghielec | 2:6ab46f2e851a | 199 | |
john_ghielec | 2:6ab46f2e851a | 200 | if (!this->mode) { |
john_ghielec | 2:6ab46f2e851a | 201 | this->graphX = 0; |
john_ghielec | 2:6ab46f2e851a | 202 | |
john_ghielec | 2:6ab46f2e851a | 203 | this->drawAxes(); |
john_ghielec | 2:6ab46f2e851a | 204 | } |
john_ghielec | 2:6ab46f2e851a | 205 | |
devhammer | 3:2f09c90a732d | 206 | //this->led1.write(this->muted); |
devhammer | 3:2f09c90a732d | 207 | //this->led2.write(!this->muted); |
loop | 8:c63981a45c95 | 208 | //this->led1.write(this->mode); |
loop | 8:c63981a45c95 | 209 | //this->led2.write(!this->mode); |
john_ghielec | 2:6ab46f2e851a | 210 | } |
john_ghielec | 2:6ab46f2e851a | 211 | |
john_ghielec | 2:6ab46f2e851a | 212 | bool xDir = this->ballSpeedX > 0; |
john_ghielec | 2:6ab46f2e851a | 213 | bool yDir = this->ballSpeedY > 0; |
john_ghielec | 2:6ab46f2e851a | 214 | bool isUp = !this->up.read(); |
john_ghielec | 2:6ab46f2e851a | 215 | bool isDown = !this->down.read(); |
john_ghielec | 1:cd8a3926f263 | 216 | |
john_ghielec | 2:6ab46f2e851a | 217 | if (isUp && isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 218 | if (!isUp && !isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 219 | |
john_ghielec | 2:6ab46f2e851a | 220 | if (isUp && this->lastUp) goto end; |
john_ghielec | 2:6ab46f2e851a | 221 | if (isDown && this->lastDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 222 | |
john_ghielec | 2:6ab46f2e851a | 223 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 224 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 225 | |
john_ghielec | 2:6ab46f2e851a | 226 | if (isUp) { |
john_ghielec | 2:6ab46f2e851a | 227 | if (++this->ballSpeedX > 5) this->ballSpeedX = 5; |
john_ghielec | 2:6ab46f2e851a | 228 | if (++this->ballSpeedY > 5) this->ballSpeedY = 5; |
john_ghielec | 2:6ab46f2e851a | 229 | } |
john_ghielec | 2:6ab46f2e851a | 230 | else if (isDown) { |
john_ghielec | 2:6ab46f2e851a | 231 | if (--this->ballSpeedX == 0) this->ballSpeedX = 1; |
john_ghielec | 2:6ab46f2e851a | 232 | if (--this->ballSpeedY == 0) this->ballSpeedY = 1; |
john_ghielec | 2:6ab46f2e851a | 233 | } |
john_ghielec | 1:cd8a3926f263 | 234 | |
john_ghielec | 2:6ab46f2e851a | 235 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 236 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 2:6ab46f2e851a | 237 | |
john_ghielec | 2:6ab46f2e851a | 238 | end: |
john_ghielec | 2:6ab46f2e851a | 239 | this->lastUp = isUp; |
john_ghielec | 2:6ab46f2e851a | 240 | this->lastDown = isDown; |
john_ghielec | 1:cd8a3926f263 | 241 | } |
john_ghielec | 1:cd8a3926f263 | 242 | |
john_ghielec | 1:cd8a3926f263 | 243 | void Game::drawString(const char* str, int y) { |
loop | 7:c0f12f624832 | 244 | this->disp.drawString(font_ibm, |
loop | 7:c0f12f624832 | 245 | this->disp.getWidth() / 2 - (CHAR_WIDTH + CHAR_SPACING) * strlen(str) / 2, |
loop | 7:c0f12f624832 | 246 | y, str); |
john_ghielec | 1:cd8a3926f263 | 247 | } |
john_ghielec | 1:cd8a3926f263 | 248 | void Game::showSplashScreen() { |
loop | 7:c0f12f624832 | 249 | this->drawString(Game::SPLASH_1, this->disp.getHeight() / 2 - CHAR_HEIGHT / 2); |
loop | 7:c0f12f624832 | 250 | this->drawString(Game::SPLASH_2, this->disp.getHeight() / 2 + CHAR_HEIGHT / 2); |
john_ghielec | 1:cd8a3926f263 | 251 | |
john_ghielec | 1:cd8a3926f263 | 252 | while (this->circle.read()) |
loop | 7:c0f12f624832 | 253 | wait_ms(5); |
john_ghielec | 1:cd8a3926f263 | 254 | |
loop | 7:c0f12f624832 | 255 | this->disp.clearScreen(); |
john_ghielec | 1:cd8a3926f263 | 256 | } |
john_ghielec | 1:cd8a3926f263 | 257 | |
john_ghielec | 1:cd8a3926f263 | 258 | void Game::clearBall() { |
loop | 7:c0f12f624832 | 259 | this->disp.fillRect(ballX - BALL_RADIUS, |
loop | 7:c0f12f624832 | 260 | ballY - BALL_RADIUS, |
loop | 7:c0f12f624832 | 261 | ballX + BALL_RADIUS, |
loop | 7:c0f12f624832 | 262 | ballY + BALL_RADIUS, |
loop | 8:c63981a45c95 | 263 | Color565::Gray); |
john_ghielec | 1:cd8a3926f263 | 264 | } |
john_ghielec | 1:cd8a3926f263 | 265 | |
john_ghielec | 1:cd8a3926f263 | 266 | void Game::drawBall() { |
loop | 7:c0f12f624832 | 267 | this->disp.fillRect(ballX - BALL_RADIUS, |
loop | 7:c0f12f624832 | 268 | ballY - BALL_RADIUS, |
loop | 7:c0f12f624832 | 269 | ballX + BALL_RADIUS, |
loop | 7:c0f12f624832 | 270 | ballY + BALL_RADIUS, |
loop | 7:c0f12f624832 | 271 | Color565::Red); |
john_ghielec | 1:cd8a3926f263 | 272 | } |
john_ghielec | 1:cd8a3926f263 | 273 | |
john_ghielec | 1:cd8a3926f263 | 274 | void Game::updateBall() { |
loop | 5:8a26ad9d9ea1 | 275 | this->ballSpeedX += this->ballAccelX; |
loop | 5:8a26ad9d9ea1 | 276 | this->ballSpeedY += this->ballAccelY; |
john_ghielec | 1:cd8a3926f263 | 277 | this->ballX += this->ballSpeedX; |
john_ghielec | 1:cd8a3926f263 | 278 | this->ballY += this->ballSpeedY; |
john_ghielec | 1:cd8a3926f263 | 279 | } |
john_ghielec | 1:cd8a3926f263 | 280 | |
loop | 4:84be90860d7c | 281 | void Game::checkCollision() { |
loop | 4:84be90860d7c | 282 | // Does bounds checking first.. |
loop | 4:84be90860d7c | 283 | if ((this->ballX - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedX < 0) || |
loop | 7:c0f12f624832 | 284 | (this->ballX + Game::BALL_RADIUS * 2 >= disp.getWidth() && this->ballSpeedX > 0)) { |
john_ghielec | 1:cd8a3926f263 | 285 | this->ballSpeedX *= -1; |
devhammer | 3:2f09c90a732d | 286 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 287 | this->pwm.period_ms(2); |
devhammer | 3:2f09c90a732d | 288 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 289 | } |
devhammer | 3:2f09c90a732d | 290 | } |
loop | 4:84be90860d7c | 291 | if ((this->ballY - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedY < 0) || |
loop | 7:c0f12f624832 | 292 | (this->ballY + Game::BALL_RADIUS * 2 >= disp.getHeight() && this->ballSpeedY > 0)){ |
john_ghielec | 1:cd8a3926f263 | 293 | this->ballSpeedY *= -1; |
devhammer | 3:2f09c90a732d | 294 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 295 | this->pwm.period_ms(2); |
devhammer | 3:2f09c90a732d | 296 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 297 | } |
devhammer | 3:2f09c90a732d | 298 | } |
loop | 4:84be90860d7c | 299 | /* |
john_ghielec | 1:cd8a3926f263 | 300 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT - Game::PADDLE_HEIGHT && this->ballSpeedY > 0) { |
john_ghielec | 1:cd8a3926f263 | 301 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT) { |
john_ghielec | 1:cd8a3926f263 | 302 | this->initializeBall(); |
john_ghielec | 1:cd8a3926f263 | 303 | |
john_ghielec | 1:cd8a3926f263 | 304 | this->lives--; |
devhammer | 3:2f09c90a732d | 305 | |
devhammer | 3:2f09c90a732d | 306 | if(this->lives > 0) { |
devhammer | 3:2f09c90a732d | 307 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 308 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 309 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 310 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 311 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 312 | } |
devhammer | 3:2f09c90a732d | 313 | } |
devhammer | 3:2f09c90a732d | 314 | |
john_ghielec | 1:cd8a3926f263 | 315 | } |
john_ghielec | 1:cd8a3926f263 | 316 | else if (this->ballX > this->paddleX && this->ballX < this->paddleX + Game::PADDLE_WIDTH) { |
john_ghielec | 1:cd8a3926f263 | 317 | this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 318 | |
devhammer | 3:2f09c90a732d | 319 | if(!this->muted){ |
devhammer | 3:2f09c90a732d | 320 | this->pwm.period_ms(1); |
devhammer | 3:2f09c90a732d | 321 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 322 | } |
devhammer | 3:2f09c90a732d | 323 | this->score = this->score + 10; |
devhammer | 3:2f09c90a732d | 324 | if(this->score % 100 == 0) { |
devhammer | 3:2f09c90a732d | 325 | if(this->ballSpeedX < 0){ |
devhammer | 3:2f09c90a732d | 326 | this->ballSpeedX -= 1; |
devhammer | 3:2f09c90a732d | 327 | } |
devhammer | 3:2f09c90a732d | 328 | else { |
devhammer | 3:2f09c90a732d | 329 | this->ballSpeedX += 1; |
devhammer | 3:2f09c90a732d | 330 | } |
devhammer | 3:2f09c90a732d | 331 | this->ballSpeedY -= 1; |
devhammer | 3:2f09c90a732d | 332 | } |
john_ghielec | 1:cd8a3926f263 | 333 | } |
john_ghielec | 1:cd8a3926f263 | 334 | } |
devhammer | 3:2f09c90a732d | 335 | char buf[10]; |
devhammer | 3:2f09c90a732d | 336 | int a = this->score; |
devhammer | 3:2f09c90a732d | 337 | sprintf(buf, "%d", a); |
loop | 7:c0f12f624832 | 338 | this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, Color565::White, DisplayN18::BLACK); |
loop | 7:c0f12f624832 | 339 | this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 4), 0, buf, Color565::White, DisplayN18::BLACK); |
loop | 7:c0f12f624832 | 340 | */ |
loop | 7:c0f12f624832 | 341 | |
john_ghielec | 1:cd8a3926f263 | 342 | } |
john_ghielec | 1:cd8a3926f263 | 343 | |
john_ghielec | 1:cd8a3926f263 | 344 | void Game::checkPwm() { |
john_ghielec | 1:cd8a3926f263 | 345 | if (this->pwmTicksLeft == 0) { |
devhammer | 3:2f09c90a732d | 346 | this->pwm.write(0.0); |
john_ghielec | 1:cd8a3926f263 | 347 | } |
john_ghielec | 1:cd8a3926f263 | 348 | else { |
john_ghielec | 1:cd8a3926f263 | 349 | this->pwmTicksLeft--; |
john_ghielec | 1:cd8a3926f263 | 350 | this->pwm.write(0.5); |
john_ghielec | 1:cd8a3926f263 | 351 | } |
john_ghielec | 1:cd8a3926f263 | 352 | } |
john_ghielec | 1:cd8a3926f263 | 353 | |
john_ghielec | 1:cd8a3926f263 | 354 | void Game::checkLives() { |
john_ghielec | 1:cd8a3926f263 | 355 | if (this->lives == 0) { |
loop | 7:c0f12f624832 | 356 | this->disp.clearScreen(); |
devhammer | 3:2f09c90a732d | 357 | |
loop | 7:c0f12f624832 | 358 | this->drawString(Game::LOSE_1, disp.getHeight() / 2 - CHAR_HEIGHT); |
loop | 7:c0f12f624832 | 359 | this->drawString(Game::LOSE_2, disp.getHeight() / 2); |
john_ghielec | 1:cd8a3926f263 | 360 | |
devhammer | 3:2f09c90a732d | 361 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 362 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 363 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 364 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 365 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 366 | |
devhammer | 3:2f09c90a732d | 367 | this->pwm.period(1.0/196); |
devhammer | 3:2f09c90a732d | 368 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 369 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 370 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 371 | |
devhammer | 3:2f09c90a732d | 372 | this->pwm.period(1.0/164.81); |
devhammer | 3:2f09c90a732d | 373 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 374 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 375 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 376 | } |
devhammer | 3:2f09c90a732d | 377 | |
john_ghielec | 1:cd8a3926f263 | 378 | while (this->circle.read()) |
john_ghielec | 1:cd8a3926f263 | 379 | wait_ms(1); |
john_ghielec | 1:cd8a3926f263 | 380 | |
john_ghielec | 1:cd8a3926f263 | 381 | this->initialize(); |
john_ghielec | 1:cd8a3926f263 | 382 | } |
john_ghielec | 1:cd8a3926f263 | 383 | else { |
loop | 7:c0f12f624832 | 384 | this->disp.drawString(font_ibm, 0, 0, Game::LIVES); |
loop | 7:c0f12f624832 | 385 | //this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), Color565::White, DisplayN18::BLACK); |
john_ghielec | 1:cd8a3926f263 | 386 | } |
john_ghielec | 1:cd8a3926f263 | 387 | } |