Some random attempts at programming the retro console
Fork of RETRO_Pong_Mod by
Game.cpp@9:5c4a3e89a713, 2015-02-28 (annotated)
- Committer:
- loop
- Date:
- Sat Feb 28 14:46:09 2015 +0000
- Revision:
- 9:5c4a3e89a713
- Parent:
- 8:c63981a45c95
- Child:
- 10:ba2dea5fffd1
Fixed bounds
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 | 9:5c4a3e89a713 | 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; |
loop | 9:5c4a3e89a713 | 40 | this->cCol = 0; |
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) { |
loop | 9:5c4a3e89a713 | 63 | int val = ((buffer[0] << 2) | (buffer[1] >> 6)); |
john_ghielec | 2:6ab46f2e851a | 64 | |
loop | 9:5c4a3e89a713 | 65 | if (val > 511) |
loop | 9:5c4a3e89a713 | 66 | val -= 1024; |
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 | 9:5c4a3e89a713 | 71 | void Game::getXY(float& x, float& 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 | |
john_ghielec | 2:6ab46f2e851a | 87 | void Game::printDouble(double value, int x, int y) { |
john_ghielec | 2:6ab46f2e851a | 88 | char buffer[10]; |
john_ghielec | 2:6ab46f2e851a | 89 | int len = sprintf(buffer, "%.1f", value); |
loop | 7:c0f12f624832 | 90 | this->disp.drawString(font_ibm, x, y, buffer); |
john_ghielec | 2:6ab46f2e851a | 91 | } |
john_ghielec | 2:6ab46f2e851a | 92 | |
john_ghielec | 2:6ab46f2e851a | 93 | void Game::drawAxes() { |
john_ghielec | 2:6ab46f2e851a | 94 | for (int i = 0; i < 3; i++) { |
loop | 7:c0f12f624832 | 95 | 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 | 96 | 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 | 97 | } |
john_ghielec | 2:6ab46f2e851a | 98 | } |
john_ghielec | 2:6ab46f2e851a | 99 | |
john_ghielec | 2:6ab46f2e851a | 100 | void Game::drawPoint(int axis, double value) { |
john_ghielec | 2:6ab46f2e851a | 101 | if (value < -1.0) |
john_ghielec | 2:6ab46f2e851a | 102 | value = -1.0; |
john_ghielec | 2:6ab46f2e851a | 103 | |
john_ghielec | 2:6ab46f2e851a | 104 | if (value > 1.0) |
john_ghielec | 2:6ab46f2e851a | 105 | value = 1.0; |
john_ghielec | 2:6ab46f2e851a | 106 | |
john_ghielec | 2:6ab46f2e851a | 107 | value += 1.0; |
john_ghielec | 2:6ab46f2e851a | 108 | value /= 2.0; |
john_ghielec | 2:6ab46f2e851a | 109 | value = 1.0 - value; |
john_ghielec | 2:6ab46f2e851a | 110 | value *= Game::GRAPH_HEIGHT; |
john_ghielec | 2:6ab46f2e851a | 111 | |
john_ghielec | 2:6ab46f2e851a | 112 | this->disp.setPixel(this->graphX, axis * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + (int)value, this->colors[axis]); |
john_ghielec | 2:6ab46f2e851a | 113 | } |
john_ghielec | 2:6ab46f2e851a | 114 | |
john_ghielec | 2:6ab46f2e851a | 115 | void Game::checkGraphReset() { |
loop | 7:c0f12f624832 | 116 | if (this->graphX > disp.getWidth()) { |
john_ghielec | 2:6ab46f2e851a | 117 | this->graphX = 0; |
loop | 7:c0f12f624832 | 118 | this->disp.clearScreen(); |
john_ghielec | 2:6ab46f2e851a | 119 | this->drawAxes(); |
john_ghielec | 2:6ab46f2e851a | 120 | } |
john_ghielec | 2:6ab46f2e851a | 121 | } |
john_ghielec | 2:6ab46f2e851a | 122 | |
loop | 8:c63981a45c95 | 123 | void Game::initialize() { |
loop | 9:5c4a3e89a713 | 124 | this->disp.setOrientation(LCD_ST7735::Rotate270, false); |
loop | 9:5c4a3e89a713 | 125 | this->disp.clearScreen(); |
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; |
loop | 9:5c4a3e89a713 | 131 | this->pwm.period(0.3); |
loop | 9:5c4a3e89a713 | 132 | this->pwm.write(0.00); |
john_ghielec | 1:cd8a3926f263 | 133 | } |
john_ghielec | 1:cd8a3926f263 | 134 | |
john_ghielec | 1:cd8a3926f263 | 135 | void Game::initializeBall() { |
loop | 7:c0f12f624832 | 136 | this->ballX = disp.getWidth() / 2 - Game::BALL_RADIUS; |
loop | 7:c0f12f624832 | 137 | this->ballY = disp.getHeight() / 4 - Game::BALL_RADIUS; |
loop | 9:5c4a3e89a713 | 138 | this->ballSpeedX = 1.0f; |
loop | 9:5c4a3e89a713 | 139 | this->ballSpeedY = 1.0f; |
loop | 9:5c4a3e89a713 | 140 | this->ballAccelX = 0.0f; |
loop | 9:5c4a3e89a713 | 141 | this->ballAccelY = 0.0f; |
john_ghielec | 2:6ab46f2e851a | 142 | } |
john_ghielec | 2:6ab46f2e851a | 143 | |
loop | 5:8a26ad9d9ea1 | 144 | void Game::readAccel() { |
loop | 9:5c4a3e89a713 | 145 | float x, y; |
loop | 7:c0f12f624832 | 146 | this->getXY(x, y); |
loop | 6:20788dfdb7b8 | 147 | x = x * 8; |
loop | 6:20788dfdb7b8 | 148 | y = y * 8; |
loop | 9:5c4a3e89a713 | 149 | this->ballAccelX = x; |
loop | 9:5c4a3e89a713 | 150 | this->ballAccelY = y; |
loop | 9:5c4a3e89a713 | 151 | } |
loop | 5:8a26ad9d9ea1 | 152 | |
loop | 6:20788dfdb7b8 | 153 | void Game::tick() { |
loop | 6:20788dfdb7b8 | 154 | int tcount = 0; |
john_ghielec | 2:6ab46f2e851a | 155 | this->checkButtons(); |
john_ghielec | 1:cd8a3926f263 | 156 | |
john_ghielec | 2:6ab46f2e851a | 157 | if (this->mode) { |
loop | 7:c0f12f624832 | 158 | if ((tcount++ % 10) == 0) { |
loop | 6:20788dfdb7b8 | 159 | this->readAccel(); |
loop | 6:20788dfdb7b8 | 160 | }; |
john_ghielec | 2:6ab46f2e851a | 161 | this->updateBall(); |
john_ghielec | 2:6ab46f2e851a | 162 | this->checkCollision(); |
loop | 8:c63981a45c95 | 163 | this->clearBall(); |
john_ghielec | 2:6ab46f2e851a | 164 | this->drawBall(); |
loop | 7:c0f12f624832 | 165 | // this->checkPwm(); |
loop | 7:c0f12f624832 | 166 | //this->checkLives(); |
john_ghielec | 2:6ab46f2e851a | 167 | } |
john_ghielec | 2:6ab46f2e851a | 168 | else { |
john_ghielec | 2:6ab46f2e851a | 169 | double x, y, z; |
john_ghielec | 2:6ab46f2e851a | 170 | |
john_ghielec | 2:6ab46f2e851a | 171 | this->getXYZ(x, y, z); |
john_ghielec | 2:6ab46f2e851a | 172 | |
john_ghielec | 2:6ab46f2e851a | 173 | this->checkGraphReset(); |
john_ghielec | 2:6ab46f2e851a | 174 | this->drawPoint(0, x); |
john_ghielec | 2:6ab46f2e851a | 175 | this->drawPoint(1, y); |
john_ghielec | 2:6ab46f2e851a | 176 | this->drawPoint(2, z); |
john_ghielec | 2:6ab46f2e851a | 177 | this->graphX++; |
john_ghielec | 2:6ab46f2e851a | 178 | } |
john_ghielec | 1:cd8a3926f263 | 179 | } |
john_ghielec | 1:cd8a3926f263 | 180 | |
john_ghielec | 2:6ab46f2e851a | 181 | void Game::checkButtons() { |
john_ghielec | 2:6ab46f2e851a | 182 | if (!this->square.read()) { |
devhammer | 3:2f09c90a732d | 183 | //this->muted = !this->muted; |
john_ghielec | 2:6ab46f2e851a | 184 | this->mode = !this->mode; |
john_ghielec | 2:6ab46f2e851a | 185 | |
loop | 7:c0f12f624832 | 186 | this->disp.clearScreen(); |
john_ghielec | 2:6ab46f2e851a | 187 | |
john_ghielec | 2:6ab46f2e851a | 188 | if (!this->mode) { |
john_ghielec | 2:6ab46f2e851a | 189 | this->graphX = 0; |
john_ghielec | 2:6ab46f2e851a | 190 | |
john_ghielec | 2:6ab46f2e851a | 191 | this->drawAxes(); |
john_ghielec | 2:6ab46f2e851a | 192 | } |
john_ghielec | 2:6ab46f2e851a | 193 | |
devhammer | 3:2f09c90a732d | 194 | //this->led1.write(this->muted); |
devhammer | 3:2f09c90a732d | 195 | //this->led2.write(!this->muted); |
loop | 8:c63981a45c95 | 196 | //this->led1.write(this->mode); |
loop | 8:c63981a45c95 | 197 | //this->led2.write(!this->mode); |
john_ghielec | 2:6ab46f2e851a | 198 | } |
john_ghielec | 2:6ab46f2e851a | 199 | |
loop | 9:5c4a3e89a713 | 200 | bool xDir = this->ballSpeedX > 0.0; |
loop | 9:5c4a3e89a713 | 201 | bool yDir = this->ballSpeedY > 0.0; |
john_ghielec | 2:6ab46f2e851a | 202 | bool isUp = !this->up.read(); |
john_ghielec | 2:6ab46f2e851a | 203 | bool isDown = !this->down.read(); |
john_ghielec | 1:cd8a3926f263 | 204 | |
john_ghielec | 2:6ab46f2e851a | 205 | if (isUp && isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 206 | if (!isUp && !isDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 207 | |
john_ghielec | 2:6ab46f2e851a | 208 | if (isUp && this->lastUp) goto end; |
john_ghielec | 2:6ab46f2e851a | 209 | if (isDown && this->lastDown) goto end; |
john_ghielec | 2:6ab46f2e851a | 210 | |
loop | 9:5c4a3e89a713 | 211 | if (!xDir) this->ballSpeedX *= -1.0; |
loop | 9:5c4a3e89a713 | 212 | if (!yDir) this->ballSpeedY *= -1.0; |
john_ghielec | 1:cd8a3926f263 | 213 | |
john_ghielec | 2:6ab46f2e851a | 214 | if (isUp) { |
john_ghielec | 2:6ab46f2e851a | 215 | if (++this->ballSpeedX > 5) this->ballSpeedX = 5; |
john_ghielec | 2:6ab46f2e851a | 216 | if (++this->ballSpeedY > 5) this->ballSpeedY = 5; |
john_ghielec | 2:6ab46f2e851a | 217 | } |
john_ghielec | 2:6ab46f2e851a | 218 | else if (isDown) { |
john_ghielec | 2:6ab46f2e851a | 219 | if (--this->ballSpeedX == 0) this->ballSpeedX = 1; |
john_ghielec | 2:6ab46f2e851a | 220 | if (--this->ballSpeedY == 0) this->ballSpeedY = 1; |
john_ghielec | 2:6ab46f2e851a | 221 | } |
john_ghielec | 1:cd8a3926f263 | 222 | |
john_ghielec | 2:6ab46f2e851a | 223 | if (!xDir) this->ballSpeedX *= -1; |
john_ghielec | 2:6ab46f2e851a | 224 | if (!yDir) this->ballSpeedY *= -1; |
john_ghielec | 2:6ab46f2e851a | 225 | |
john_ghielec | 2:6ab46f2e851a | 226 | end: |
john_ghielec | 2:6ab46f2e851a | 227 | this->lastUp = isUp; |
john_ghielec | 2:6ab46f2e851a | 228 | this->lastDown = isDown; |
john_ghielec | 1:cd8a3926f263 | 229 | } |
john_ghielec | 1:cd8a3926f263 | 230 | |
john_ghielec | 1:cd8a3926f263 | 231 | void Game::drawString(const char* str, int y) { |
loop | 7:c0f12f624832 | 232 | this->disp.drawString(font_ibm, |
loop | 7:c0f12f624832 | 233 | this->disp.getWidth() / 2 - (CHAR_WIDTH + CHAR_SPACING) * strlen(str) / 2, |
loop | 7:c0f12f624832 | 234 | y, str); |
john_ghielec | 1:cd8a3926f263 | 235 | } |
john_ghielec | 1:cd8a3926f263 | 236 | void Game::showSplashScreen() { |
loop | 7:c0f12f624832 | 237 | this->drawString(Game::SPLASH_1, this->disp.getHeight() / 2 - CHAR_HEIGHT / 2); |
loop | 7:c0f12f624832 | 238 | this->drawString(Game::SPLASH_2, this->disp.getHeight() / 2 + CHAR_HEIGHT / 2); |
john_ghielec | 1:cd8a3926f263 | 239 | while (this->circle.read()) |
loop | 7:c0f12f624832 | 240 | wait_ms(5); |
loop | 7:c0f12f624832 | 241 | this->disp.clearScreen(); |
john_ghielec | 1:cd8a3926f263 | 242 | } |
john_ghielec | 1:cd8a3926f263 | 243 | |
john_ghielec | 1:cd8a3926f263 | 244 | void Game::clearBall() { |
loop | 7:c0f12f624832 | 245 | this->disp.fillRect(ballX - BALL_RADIUS, |
loop | 7:c0f12f624832 | 246 | ballY - BALL_RADIUS, |
loop | 7:c0f12f624832 | 247 | ballX + BALL_RADIUS, |
loop | 7:c0f12f624832 | 248 | ballY + BALL_RADIUS, |
loop | 8:c63981a45c95 | 249 | Color565::Gray); |
john_ghielec | 1:cd8a3926f263 | 250 | } |
john_ghielec | 1:cd8a3926f263 | 251 | |
john_ghielec | 1:cd8a3926f263 | 252 | void Game::drawBall() { |
loop | 7:c0f12f624832 | 253 | this->disp.fillRect(ballX - BALL_RADIUS, |
loop | 7:c0f12f624832 | 254 | ballY - BALL_RADIUS, |
loop | 7:c0f12f624832 | 255 | ballX + BALL_RADIUS, |
loop | 7:c0f12f624832 | 256 | ballY + BALL_RADIUS, |
loop | 9:5c4a3e89a713 | 257 | this->colors[this->cCol]); |
john_ghielec | 1:cd8a3926f263 | 258 | } |
john_ghielec | 1:cd8a3926f263 | 259 | |
john_ghielec | 1:cd8a3926f263 | 260 | void Game::updateBall() { |
loop | 5:8a26ad9d9ea1 | 261 | this->ballSpeedX += this->ballAccelX; |
loop | 9:5c4a3e89a713 | 262 | if(this->ballSpeedX > MAX_SPEED) |
loop | 9:5c4a3e89a713 | 263 | this->ballSpeedX = MAX_SPEED; |
loop | 9:5c4a3e89a713 | 264 | if (this->ballSpeedX < -MAX_SPEED) |
loop | 9:5c4a3e89a713 | 265 | this->ballSpeedX = -MAX_SPEED; |
john_ghielec | 1:cd8a3926f263 | 266 | this->ballX += this->ballSpeedX; |
loop | 9:5c4a3e89a713 | 267 | |
loop | 9:5c4a3e89a713 | 268 | this->ballSpeedY += this->ballAccelY; |
loop | 9:5c4a3e89a713 | 269 | if (this->ballSpeedY > MAX_SPEED) |
loop | 9:5c4a3e89a713 | 270 | this->ballSpeedY = MAX_SPEED; |
loop | 9:5c4a3e89a713 | 271 | if (this->ballSpeedY < -MAX_SPEED) |
loop | 9:5c4a3e89a713 | 272 | this->ballSpeedY = -MAX_SPEED; |
john_ghielec | 1:cd8a3926f263 | 273 | this->ballY += this->ballSpeedY; |
loop | 9:5c4a3e89a713 | 274 | |
loop | 9:5c4a3e89a713 | 275 | // Does bounds checking.. |
loop | 9:5c4a3e89a713 | 276 | if ((this->ballX - Game::BALL_RADIUS <= 0 && this->ballSpeedX < 0.0) || |
loop | 9:5c4a3e89a713 | 277 | (this->ballX + Game::BALL_RADIUS >= disp.getWidth() && this->ballSpeedX > 0.0)) { |
loop | 9:5c4a3e89a713 | 278 | this->ballSpeedX *= -1.0; |
loop | 9:5c4a3e89a713 | 279 | this->bounce(); |
loop | 9:5c4a3e89a713 | 280 | } |
loop | 9:5c4a3e89a713 | 281 | if ((this->ballY - Game::BALL_RADIUS <= 0 && this->ballSpeedY < 0.0) || |
loop | 9:5c4a3e89a713 | 282 | (this->ballY + Game::BALL_RADIUS >= disp.getHeight() && this->ballSpeedY > 0.0)){ |
loop | 9:5c4a3e89a713 | 283 | this->ballSpeedY *= -1.0; |
loop | 9:5c4a3e89a713 | 284 | this->bounce(); |
loop | 9:5c4a3e89a713 | 285 | } |
loop | 9:5c4a3e89a713 | 286 | // Sanity |
loop | 9:5c4a3e89a713 | 287 | this->printDouble(this->ballSpeedX, 1, 0); |
loop | 9:5c4a3e89a713 | 288 | this->printDouble(this->ballSpeedY, 1, 8); |
loop | 9:5c4a3e89a713 | 289 | this->printDouble(this->ballAccelX, 120, 0); |
loop | 9:5c4a3e89a713 | 290 | this->printDouble(this->ballAccelY, 120, 8); |
loop | 9:5c4a3e89a713 | 291 | } |
loop | 9:5c4a3e89a713 | 292 | |
loop | 9:5c4a3e89a713 | 293 | void Game::bounce() { |
loop | 9:5c4a3e89a713 | 294 | this->cCol++; |
loop | 9:5c4a3e89a713 | 295 | if (this->cCol == 3) |
loop | 9:5c4a3e89a713 | 296 | this->cCol = 0; |
loop | 9:5c4a3e89a713 | 297 | if (!this->muted) { |
loop | 9:5c4a3e89a713 | 298 | this->pwm.period_ms(2); |
loop | 9:5c4a3e89a713 | 299 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
loop | 9:5c4a3e89a713 | 300 | } |
john_ghielec | 1:cd8a3926f263 | 301 | } |
john_ghielec | 1:cd8a3926f263 | 302 | |
loop | 4:84be90860d7c | 303 | void Game::checkCollision() { |
loop | 4:84be90860d7c | 304 | /* |
john_ghielec | 1:cd8a3926f263 | 305 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT - Game::PADDLE_HEIGHT && this->ballSpeedY > 0) { |
john_ghielec | 1:cd8a3926f263 | 306 | if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT) { |
john_ghielec | 1:cd8a3926f263 | 307 | this->initializeBall(); |
john_ghielec | 1:cd8a3926f263 | 308 | |
john_ghielec | 1:cd8a3926f263 | 309 | this->lives--; |
devhammer | 3:2f09c90a732d | 310 | |
devhammer | 3:2f09c90a732d | 311 | if(this->lives > 0) { |
devhammer | 3:2f09c90a732d | 312 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 313 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 314 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 315 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 316 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 317 | } |
devhammer | 3:2f09c90a732d | 318 | } |
devhammer | 3:2f09c90a732d | 319 | |
john_ghielec | 1:cd8a3926f263 | 320 | } |
john_ghielec | 1:cd8a3926f263 | 321 | else if (this->ballX > this->paddleX && this->ballX < this->paddleX + Game::PADDLE_WIDTH) { |
john_ghielec | 1:cd8a3926f263 | 322 | this->ballSpeedY *= -1; |
john_ghielec | 1:cd8a3926f263 | 323 | |
devhammer | 3:2f09c90a732d | 324 | if(!this->muted){ |
devhammer | 3:2f09c90a732d | 325 | this->pwm.period_ms(1); |
devhammer | 3:2f09c90a732d | 326 | this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS; |
devhammer | 3:2f09c90a732d | 327 | } |
devhammer | 3:2f09c90a732d | 328 | this->score = this->score + 10; |
devhammer | 3:2f09c90a732d | 329 | if(this->score % 100 == 0) { |
devhammer | 3:2f09c90a732d | 330 | if(this->ballSpeedX < 0){ |
devhammer | 3:2f09c90a732d | 331 | this->ballSpeedX -= 1; |
devhammer | 3:2f09c90a732d | 332 | } |
devhammer | 3:2f09c90a732d | 333 | else { |
devhammer | 3:2f09c90a732d | 334 | this->ballSpeedX += 1; |
devhammer | 3:2f09c90a732d | 335 | } |
devhammer | 3:2f09c90a732d | 336 | this->ballSpeedY -= 1; |
devhammer | 3:2f09c90a732d | 337 | } |
john_ghielec | 1:cd8a3926f263 | 338 | } |
john_ghielec | 1:cd8a3926f263 | 339 | } |
devhammer | 3:2f09c90a732d | 340 | char buf[10]; |
devhammer | 3:2f09c90a732d | 341 | int a = this->score; |
devhammer | 3:2f09c90a732d | 342 | sprintf(buf, "%d", a); |
loop | 7:c0f12f624832 | 343 | this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, Color565::White, DisplayN18::BLACK); |
loop | 7:c0f12f624832 | 344 | this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 4), 0, buf, Color565::White, DisplayN18::BLACK); |
loop | 7:c0f12f624832 | 345 | */ |
loop | 7:c0f12f624832 | 346 | |
john_ghielec | 1:cd8a3926f263 | 347 | } |
john_ghielec | 1:cd8a3926f263 | 348 | |
john_ghielec | 1:cd8a3926f263 | 349 | void Game::checkPwm() { |
john_ghielec | 1:cd8a3926f263 | 350 | if (this->pwmTicksLeft == 0) { |
devhammer | 3:2f09c90a732d | 351 | this->pwm.write(0.0); |
john_ghielec | 1:cd8a3926f263 | 352 | } |
john_ghielec | 1:cd8a3926f263 | 353 | else { |
john_ghielec | 1:cd8a3926f263 | 354 | this->pwmTicksLeft--; |
john_ghielec | 1:cd8a3926f263 | 355 | this->pwm.write(0.5); |
john_ghielec | 1:cd8a3926f263 | 356 | } |
john_ghielec | 1:cd8a3926f263 | 357 | } |
john_ghielec | 1:cd8a3926f263 | 358 | |
john_ghielec | 1:cd8a3926f263 | 359 | void Game::checkLives() { |
loop | 9:5c4a3e89a713 | 360 | if (this->lives != 0) { |
loop | 9:5c4a3e89a713 | 361 | this->disp.drawString(font_ibm, 0, 0, Game::LIVES); |
loop | 9:5c4a3e89a713 | 362 | //this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), Color565::White, DisplayN18::BLACK); |
loop | 9:5c4a3e89a713 | 363 | } else { |
loop | 7:c0f12f624832 | 364 | this->disp.clearScreen(); |
devhammer | 3:2f09c90a732d | 365 | |
loop | 7:c0f12f624832 | 366 | this->drawString(Game::LOSE_1, disp.getHeight() / 2 - CHAR_HEIGHT); |
loop | 7:c0f12f624832 | 367 | this->drawString(Game::LOSE_2, disp.getHeight() / 2); |
john_ghielec | 1:cd8a3926f263 | 368 | |
devhammer | 3:2f09c90a732d | 369 | if(!this->muted) { |
devhammer | 3:2f09c90a732d | 370 | this->pwm.period(1.0/220); |
devhammer | 3:2f09c90a732d | 371 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 372 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 373 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 374 | |
devhammer | 3:2f09c90a732d | 375 | this->pwm.period(1.0/196); |
devhammer | 3:2f09c90a732d | 376 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 377 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 378 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 379 | |
devhammer | 3:2f09c90a732d | 380 | this->pwm.period(1.0/164.81); |
devhammer | 3:2f09c90a732d | 381 | this->pwm.write(0.5); |
devhammer | 3:2f09c90a732d | 382 | wait_ms(150); |
devhammer | 3:2f09c90a732d | 383 | this->pwm.write(0.0); |
devhammer | 3:2f09c90a732d | 384 | } |
devhammer | 3:2f09c90a732d | 385 | |
john_ghielec | 1:cd8a3926f263 | 386 | while (this->circle.read()) |
john_ghielec | 1:cd8a3926f263 | 387 | this->initialize(); |
john_ghielec | 1:cd8a3926f263 | 388 | } |
john_ghielec | 1:cd8a3926f263 | 389 | } |