Some random attempts at programming the retro console

Dependencies:   LCD_ST7735 mbed

Fork of RETRO_Pong_Mod by G. Andrew Duthie

Revision:
9:5c4a3e89a713
Parent:
8:c63981a45c95
Child:
10:ba2dea5fffd1
diff -r c63981a45c95 -r 5c4a3e89a713 Game.cpp
--- a/Game.cpp	Sat Feb 28 00:04:52 2015 +0000
+++ b/Game.cpp	Sat Feb 28 14:46:09 2015 +0000
@@ -31,13 +31,13 @@
                 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),
-                music(P0_18) {
+                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;
     this->lastDown = false;
     this->mode = true;
+    this->cCol = 0;
     
     this->i2c.frequency(400);
     this->writeRegister(0x2A, 0x01); 
@@ -60,15 +60,15 @@
 }
 
 double Game::convert(char* buffer) {
-    double val = ((buffer[0] << 2) | (buffer[1] >> 6));
+    int val = ((buffer[0] << 2) | (buffer[1] >> 6));
             
-    if (val > 511.0) 
-        val -= 1024.0;
+    if (val > 511) 
+        val -= 1024;
     
     return val / 512.0;
 }
 
-void Game::getXY(double& x, double& y) {
+void Game::getXY(float& x, float& y) {
     char buffer[4];
     this->readRegisters(0x01, buffer, 4);
     x = this->convert(buffer);
@@ -84,11 +84,9 @@
 
 }
 
-
 void Game::printDouble(double value, int x, int y) {
     char buffer[10];
     int len = sprintf(buffer, "%.1f", value);
-    
     this->disp.drawString(font_ibm, x, y, buffer);
 }
 
@@ -122,45 +120,35 @@
     }
 }
 
-void playSong() {
-    MusicEngine music(P0_18);
-    music.play("t160o3l4V12@1ed8ce8gg8er8aa8>c<a8g2raa8ga8>cc8d<r8ee8de8c2rdd8dd8dd8dr8ed8ef8g2raa8ga8>cc8<ar8>dc8de8d2<r>ee8dc8<ab8>cc8<gg8ea8g2r>cc8<ge8cd8ea8gg8de8");
-}
-
 void Game::initialize() {
-    this->music.setCompletionCallback(&playSong);
-    playSong();
+    this->disp.setOrientation(LCD_ST7735::Rotate270, false);    
+    this->disp.clearScreen();
     this->initializeBall();
     this->pwmTicksLeft = 0;
     this->lives = 4;
     this->score = 0;
     this->muted = false;
-    
-    //this->pwm.period(1);
-    //this->pwm.write(0.00);
-    this->disp.setOrientation(LCD_ST7735::Rotate270, false);    
-    this->disp.clearScreen();
+    this->pwm.period(0.3);
+    this->pwm.write(0.00);
 }
     
 void Game::initializeBall() {
     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;
-    this->ballAccelY = 0;
+    this->ballSpeedX = 1.0f;
+    this->ballSpeedY = 1.0f;
+    this->ballAccelX = 0.0f;
+    this->ballAccelY = 0.0f;
 }
 
 void Game::readAccel() {
-    double x, y;
+    float x, y;
     this->getXY(x, y);
     x = x * 8;
     y = y * 8;
-    this->ballAccelX = (int)x;
-    this->ballAccelY = (int)y;
-//    this->printDouble(x, 20, 15);
-//    this->printDouble(y, 20, 25);
-}
+    this->ballAccelX = x;
+    this->ballAccelY = y;
+ }
 
 void Game::tick() {
     int tcount = 0;
@@ -209,8 +197,8 @@
         //this->led2.write(!this->mode);
     }  
     
-    bool xDir = this->ballSpeedX > 0;
-    bool yDir = this->ballSpeedY > 0;
+    bool xDir = this->ballSpeedX > 0.0;
+    bool yDir = this->ballSpeedY > 0.0;
     bool isUp = !this->up.read();
     bool isDown = !this->down.read();
     
@@ -220,8 +208,8 @@
     if (isUp && this->lastUp) goto end;
     if (isDown && this->lastDown) goto end;
     
-    if (!xDir) this->ballSpeedX *= -1;
-    if (!yDir) this->ballSpeedY *= -1;
+    if (!xDir) this->ballSpeedX *= -1.0;
+    if (!yDir) this->ballSpeedY *= -1.0;
     
     if (isUp) {
         if (++this->ballSpeedX > 5) this->ballSpeedX = 5;
@@ -248,10 +236,8 @@
 void Game::showSplashScreen() {
     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(5);
-        
     this->disp.clearScreen();
 }
 
@@ -268,34 +254,53 @@
                         ballY - BALL_RADIUS,
                         ballX + BALL_RADIUS,
                         ballY + BALL_RADIUS,
-                        Color565::Red);
+                        this->colors[this->cCol]);
 }
 
 void Game::updateBall() {
     this->ballSpeedX += this->ballAccelX;
-    this->ballSpeedY += this->ballAccelY;
+    if(this->ballSpeedX > MAX_SPEED)
+      this->ballSpeedX = MAX_SPEED;
+    if (this->ballSpeedX < -MAX_SPEED)
+      this->ballSpeedX = -MAX_SPEED;
     this->ballX += this->ballSpeedX;
+
+    this->ballSpeedY += this->ballAccelY;
+    if (this->ballSpeedY > MAX_SPEED)
+      this->ballSpeedY = MAX_SPEED;
+    if (this->ballSpeedY < -MAX_SPEED)
+      this->ballSpeedY = -MAX_SPEED;
     this->ballY += this->ballSpeedY;
+
+    // Does bounds checking..   
+    if ((this->ballX - Game::BALL_RADIUS <= 0 && this->ballSpeedX < 0.0) || 
+        (this->ballX + Game::BALL_RADIUS >= disp.getWidth() && this->ballSpeedX > 0.0)) {
+        this->ballSpeedX *= -1.0;
+        this->bounce();
+    }
+    if ((this->ballY - Game::BALL_RADIUS <= 0 && this->ballSpeedY < 0.0) ||
+        (this->ballY + Game::BALL_RADIUS >= disp.getHeight() && this->ballSpeedY > 0.0)){
+        this->ballSpeedY *= -1.0;
+        this->bounce();
+    }
+    // Sanity
+    this->printDouble(this->ballSpeedX, 1, 0);
+    this->printDouble(this->ballSpeedY, 1, 8);
+    this->printDouble(this->ballAccelX, 120, 0);
+    this->printDouble(this->ballAccelY, 120, 8);
+}
+
+void Game::bounce() {
+    this->cCol++;
+    if (this->cCol == 3)
+        this->cCol = 0;
+    if (!this->muted) {
+        this->pwm.period_ms(2);
+        this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS;
+    }
 }
 
 void Game::checkCollision() {
-    // Does bounds checking first..   
-    if ((this->ballX - Game::BALL_RADIUS * 2 <= 0 && 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);
-            this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS;
-        }
-    }
-    if ((this->ballY - Game::BALL_RADIUS * 2 <= 0 && 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);
-            this->pwmTicksLeft = Game::BOUNCE_SOUND_TICKS;
-        }
-    }
 /*    
     if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT - Game::PADDLE_HEIGHT && this->ballSpeedY > 0) {
         if (this->ballY + Game::BALL_RADIUS >= DisplayN18::HEIGHT) {
@@ -352,7 +357,10 @@
 }
 
 void Game::checkLives() {
-    if (this->lives == 0) {
+    if (this->lives != 0) {
+        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);   
+    } else {
         this->disp.clearScreen();
                 
         this->drawString(Game::LOSE_1, disp.getHeight() / 2 - CHAR_HEIGHT); 
@@ -376,12 +384,6 @@
         }
         
         while (this->circle.read())
-            wait_ms(1);
-            
         this->initialize();
     }
-    else {
-        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