Some random attempts at programming the retro console
Fork of RETRO_Pong_Mod by
Diff: Game.cpp
- Revision:
- 7:c0f12f624832
- Parent:
- 6:20788dfdb7b8
- Child:
- 8:c63981a45c95
--- a/Game.cpp Thu Feb 26 20:29:02 2015 +0000 +++ b/Game.cpp Fri Feb 27 20:21:32 2015 +0000 @@ -6,6 +6,10 @@ // - Adjusted collision detection to add ball speed every 10 paddle hits // - Added scoring // - Added mute function (not fully implemented...needs to be set up on a button). +// Further mods buy loop (loop23) +// Changes: +// - Removed useless stuff +// - Read accelerometer in game #include "Game.h" @@ -16,7 +20,18 @@ const char* Game::LIVES = "Lives: "; const char* Game::SCORE = "Score: "; -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) { +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), + disp(P0_19, P0_20, P0_7, P0_21, P0_22, P1_15, P0_2, LCD_ST7735::RGB) { srand(this->ain.read_u16()); this->lastUp = false; @@ -26,9 +41,9 @@ this->i2c.frequency(400); this->writeRegister(0x2A, 0x01); - this->colors[0] = DisplayN18::RED; - this->colors[1] = DisplayN18::GREEN; - this->colors[2] = DisplayN18::BLUE; + this->colors[0] = Color565::Red; + this->colors[1] = Color565::Green; + this->colors[2] = Color565::Blue; this->initialize(); } @@ -53,27 +68,34 @@ return val / 512.0; } +void Game::getXY(double& x, double& y) { + char buffer[4]; + this->readRegisters(0x01, buffer, 4); + x = this->convert(buffer); + y = this->convert(buffer + 2); +} + void Game::getXYZ(double& x, double& y, double& z) { char buffer[6]; - - this->readRegisters(0x01, buffer, 6); - + this->readRegisters(0x01, buffer, 4); x = this->convert(buffer); y = this->convert(buffer + 2); z = this->convert(buffer + 4); + } + void Game::printDouble(double value, int x, int y) { char buffer[10]; int len = sprintf(buffer, "%.1f", value); - this->disp.drawString(x, y, buffer, DisplayN18::WHITE, DisplayN18::BLACK); + this->disp.drawString(font_ibm, x, y, buffer); } void Game::drawAxes() { for (int i = 0; i < 3; i++) { - this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING), 0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT, DisplayN18::WHITE); - 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); + this->disp.drawLine(0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING), 0, i * (Game::GRAPH_HEIGHT + Game::GRAPH_SPACING) + Game::GRAPH_HEIGHT, Color565::White); + 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); } } @@ -93,9 +115,9 @@ } void Game::checkGraphReset() { - if (this->graphX > DisplayN18::WIDTH) { + if (this->graphX > disp.getWidth()) { this->graphX = 0; - this->disp.clear(); + this->disp.clearScreen(); this->drawAxes(); } } @@ -105,17 +127,17 @@ this->pwmTicksLeft = 0; this->lives = 4; this->score = 0; - this->muted = true; + this->muted = false; this->pwm.period(1); this->pwm.write(0.00); - - this->disp.clear(); + this->disp.setOrientation(LCD_ST7735::Rotate270, false); + this->disp.clearScreen(); } void Game::initializeBall() { - this->ballX = DisplayN18::WIDTH / 2 - Game::BALL_RADIUS; - this->ballY = DisplayN18::HEIGHT / 4 - Game::BALL_RADIUS; + this->ballX = disp.getWidth() / 2 - Game::BALL_RADIUS; + this->ballY = disp.getHeight() / 4 - Game::BALL_RADIUS; this->ballSpeedX = 0; this->ballSpeedY = 0; this->ballAccelX = 0; @@ -123,8 +145,8 @@ } void Game::readAccel() { - double x, y, z; - this->getXYZ(x, y, z); + double x, y; + this->getXY(x, y); x = x * 8; y = y * 8; this->ballAccelX = (int)x; @@ -138,18 +160,15 @@ this->checkButtons(); if (this->mode) { - if ((tcount % 10) == 0) { + if ((tcount++ % 10) == 0) { this->readAccel(); }; this->clearBall(); this->updateBall(); this->checkCollision(); this->drawBall(); - this->checkPwm(); - this->checkLives(); - tcount++; - //wait_ms(1); - + // this->checkPwm(); + //this->checkLives(); } else { double x, y, z; @@ -169,7 +188,7 @@ //this->muted = !this->muted; this->mode = !this->mode; - this->disp.clear(); + this->disp.clearScreen(); if (!this->mode) { this->graphX = 0; @@ -215,31 +234,34 @@ } void Game::drawString(const char* str, int y) { - this->disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) * strlen(str) / 2, y, str, DisplayN18::WHITE, DisplayN18::BLACK); + this->disp.drawString(font_ibm, + this->disp.getWidth() / 2 - (CHAR_WIDTH + CHAR_SPACING) * strlen(str) / 2, + y, str); } - void Game::showSplashScreen() { - this->drawString(Game::SPLASH_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT / 2); - this->drawString(Game::SPLASH_2, DisplayN18::HEIGHT / 2 + DisplayN18::CHAR_HEIGHT / 2); + this->drawString(Game::SPLASH_1, this->disp.getHeight() / 2 - CHAR_HEIGHT / 2); + this->drawString(Game::SPLASH_2, this->disp.getHeight() / 2 + CHAR_HEIGHT / 2); while (this->circle.read()) - wait_ms(1); + wait_ms(5); - this->disp.clear(); + this->disp.clearScreen(); } void Game::clearBall() { - this->disp.fillRect(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS * 2, Game::BALL_RADIUS * 2, DisplayN18::GREY); - //this->disp.fillCircle(this->ballX - Game::BALL_RADIUS, this->ballY - Game::BALL_RADIUS, Game::BALL_RADIUS, DisplayN18::BLACK); - //this->disp.fillCircle(this->ballX, this->ballY, Game::BALL_RADIUS, DisplayN18::GREY); - //this->disp.setPixel(this->ballX, this->ballY, DisplayN18::GREY); + this->disp.fillRect(ballX - BALL_RADIUS, + ballY - BALL_RADIUS, + ballX + BALL_RADIUS, + ballY + BALL_RADIUS, + Color565::Black); } void Game::drawBall() { - this->disp.fillRect(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS * 2, Game::BALL_RADIUS * 2, DisplayN18::RED); - //this->disp.fillCircle(this->ballX - Game::BALL_RADIUS, ballY - Game::BALL_RADIUS, Game::BALL_RADIUS, DisplayN18::RED); - //this->disp.fillCircle(this->ballX, ballY, Game::BALL_RADIUS, DisplayN18::RED); - //this->disp.setPixel(this->ballX, this->ballY, DisplayN18::GREEN); + this->disp.fillRect(ballX - BALL_RADIUS, + ballY - BALL_RADIUS, + ballX + BALL_RADIUS, + ballY + BALL_RADIUS, + Color565::Red); } void Game::updateBall() { @@ -252,7 +274,7 @@ void Game::checkCollision() { // Does bounds checking first.. if ((this->ballX - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedX < 0) || - (this->ballX + Game::BALL_RADIUS * 2 >= DisplayN18::WIDTH && this->ballSpeedX > 0)) { + (this->ballX + Game::BALL_RADIUS * 2 >= disp.getWidth() && this->ballSpeedX > 0)) { this->ballSpeedX *= -1; if(!this->muted) { this->pwm.period_ms(2); @@ -260,7 +282,7 @@ } } if ((this->ballY - Game::BALL_RADIUS * 2 <= 0 && this->ballSpeedY < 0) || - (this->ballY + Game::BALL_RADIUS * 2 >= DisplayN18::HEIGHT && this->ballSpeedY > 0)){ + (this->ballY + Game::BALL_RADIUS * 2 >= disp.getHeight() && this->ballSpeedY > 0)){ this->ballSpeedY *= -1; if(!this->muted) { this->pwm.period_ms(2); @@ -303,12 +325,13 @@ } } } -*/ char buf[10]; int a = this->score; sprintf(buf, "%d", a); - this->disp.drawString(DisplayN18::WIDTH - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, DisplayN18::WHITE, DisplayN18::BLACK); - this->disp.drawString(DisplayN18::WIDTH - (DisplayN18::CHAR_WIDTH * 4), 0, buf, DisplayN18::WHITE, DisplayN18::BLACK); + this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 12), 0, Game::SCORE, Color565::White, DisplayN18::BLACK); + this->disp.drawString(LCDST7735.getWidth() - (DisplayN18::CHAR_WIDTH * 4), 0, buf, Color565::White, DisplayN18::BLACK); +*/ + } void Game::checkPwm() { @@ -323,10 +346,10 @@ void Game::checkLives() { if (this->lives == 0) { - this->disp.clear(); + this->disp.clearScreen(); - this->drawString(Game::LOSE_1, DisplayN18::HEIGHT / 2 - DisplayN18::CHAR_HEIGHT); - this->drawString(Game::LOSE_2, DisplayN18::HEIGHT / 2); + this->drawString(Game::LOSE_1, disp.getHeight() / 2 - CHAR_HEIGHT); + this->drawString(Game::LOSE_2, disp.getHeight() / 2); if(!this->muted) { this->pwm.period(1.0/220); @@ -351,7 +374,7 @@ this->initialize(); } else { - this->disp.drawString(0, 0, Game::LIVES, DisplayN18::WHITE, DisplayN18::BLACK); - this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), DisplayN18::WHITE, DisplayN18::BLACK); + this->disp.drawString(font_ibm, 0, 0, Game::LIVES); + //this->disp.drawCharacter(DisplayN18::CHAR_WIDTH * 8, 0, static_cast<char>(this->lives + '0'), Color565::White, DisplayN18::BLACK); } } \ No newline at end of file