Rishi Bhargava / Mbed 2 deprecated 4180FinalLab

Dependencies:   4DGL-uLCD-SE PinDetect mbed SparkfunAnalogJoystick mbed-rtos

Fork of ECE2036Lab2StarterCode by Joseph Lind

Revision:
3:591086e44bf9
Parent:
2:6163865f5ce3
Child:
4:7da18e3c590b
--- a/main.cpp	Fri Jun 20 15:22:28 2014 +0000
+++ b/main.cpp	Sun Apr 24 01:23:28 2016 +0000
@@ -1,25 +1,18 @@
 #include "mbed.h"
 #include "PinDetect.h"
-#include "uLCD_4DGL.h"
 #include "Speaker.h"
+#include "paddle.h"
+#include "ball.h"
 
 // Pushbuttons
-PinDetect pbUp(p15); 
-PinDetect pbDown(p16);
-// uLCD
-uLCD_4DGL uLCD(p28, p27, p29);
+AnalogIn xMove(p15); 
+PinDetect select(p13);
 //Speaker
-Speaker mySpeaker(p21);
- 
-// Global variables needed for the push button interrupts
-int cornerX = 118, cornerY = 1;
-int oldCornerY = 1;
-int paddleMove = 8;
-int length = 40;
-int width = 3;
+//Speaker mySpeaker(p20);
+Serial pc(USBTX, USBRX);
  
 // State machine definitions
-enum gameStateType {START, WAIT, GAME_SETUP, GAME, LOSE};
+enum gameStateType {START, WAIT, GAME_SETUP, GAME, WIN, LOSE};
 /* State Definitions:
  * START -- Creates the start screen
  * WAIT -- After the start screen, goes into wait where mbed spins and does nothing
@@ -28,158 +21,151 @@
  * LOSE -- clears the screen, prints you lose, waits, then goes back to start
  */
  
-// Global state machine variable (So that the pushbuttons can modify it)
+// Global state machine variable (So that the select can modify it)
 gameStateType gameState = START;
+bool ready = false;
 
-// Pushbutton callbacks
-// WARNING: Do not call to draw anything to the uLCD in these
-// as this will cause the uLCD to crash sometimes. Update positions
-// and draw elsewhere (like it's done here).
-// Only modify the logic inside the callback functions.
-void pbUp_hit_callback (void)
+// Pushbutton callbacks for selecting to start game
+void select_hit_callback (void)
 {
     switch (gameState)
     {
     case WAIT:
-        gameState = GAME_SETUP;
-        break;
-    case GAME:  
-        if(cornerY > paddleMove) {
-            cornerY -= paddleMove;
-        }
-        break;
-    }
-}
- 
-void pbDown_hit_callback (void)
-{
-    switch (gameState)
-    {
-    case WAIT:
-        gameState = GAME_SETUP;
-        break;
-    case GAME:
-        if(cornerY < 127 - paddleMove - length){
-            cornerY += paddleMove;
-        }
+        ready = true;
         break;
     }
 }
  
 int main() 
 {   
-    // This is setting up the pushbuttons
-    // Don't modify this code.
-    pbUp.mode(PullUp);
-    pbDown.mode(PullUp);
+    // This is setting up the joystick select as a pushbutton
+    select.mode(PullUp);
     wait(0.1);
-    pbUp.attach_deasserted(&pbUp_hit_callback);
-    pbDown.attach_deasserted(&pbDown_hit_callback);
-    pbUp.setSampleFrequency();
-    pbDown.setSampleFrequency();
-    // Don't modify this code.
+    select.attach_deasserted(&select_hit_callback);
+    select.setSampleFrequency();
     
-    uLCD.display_control(PORTRAIT);
-    uLCD.cls();
-    uLCD.baudrate(BAUD_3000000);
-    uLCD.background_color(BLACK);
+    pc.baud(9600);
+
+    uint8_t gameLowX = 10, gameHighX = 190, gameLowY = 5, gameHighY = 245;
+    uint8_t gameCenterX = (gameHighX-gameLowX)/2, gameCenterY = (gameHighY-gameLowY)/2;
+    uint8_t ballSize=10;
+    uint8_t paddleWidth = 5, paddleLength = 35;
+    float botMove = xMove;
+    uint8_t score = 0;
+    int i = 0;
 
-    // Initialize all your variables outside the while/switch statement
-    // to avoid compiler warning/errors
-    int vxSign = 1, vySign = 1;
-    float fx=50.0,fy=21.0,vx=1.6,vy=1.2;
-    int x=50, y=21, radius=5;
-    int score = 0;
-    int i = 0;
-    int random;
- 
+    Paddle botPaddle(gameCenterX-(paddleLength/2), gameHighY, paddleLength, paddleWidth);
+    botPaddle.setLimits(gameLowX, gameHighX);
+    botPaddle.setMaxMove(2);
+    Paddle topPaddle(gameCenterX-(paddleLength/2), gameLowY, paddleLength, paddleWidth);
+    topPaddle.setLimits(gameLowX, gameHighX);
+    topPaddle.setMaxMove(2);
+    Ball ball(gameCenterX, gameCenterY, ballSize);
+    
+    ball.setVyDir(true);
+    
+    while (!pc.writeable() && !pc.readable()){
+    
+    }
     while (1) 
     {   
         switch (gameState)
         {
         case START:
-            uLCD.cls();
-            uLCD.locate(0,0);
-            uLCD.printf("Pong!!!\n\n");
-            uLCD.printf("Press Key to Start");
+        if (pc.writeable()){
+            pc.printf("%c%c%c%c%c\n", 0, 0, 0, 0, 0);
+            wait(.5);
             gameState = WAIT;
+        }
             break;
         case GAME_SETUP:
-            uLCD.cls();
-            uLCD.line(0, 0, 127, 0, 0xCFB53B);
-            uLCD.line(127, 0, 127, 127, 0xCFB53B);
-            uLCD.line(127, 127, 0, 127, 0xCFB53B);
-            uLCD.line(0, 127, 0, 0, 0xCFB53B);
-            vx = 1.6;
-            vy = 1.2;
+            ball.reset(gameCenterX, gameCenterY, 0, 1);
+            botPaddle.reset(gameCenterX-(paddleLength/2), gameHighY);
+            topPaddle.reset(gameCenterX-(paddleLength/2), gameLowY);
+            ready = false;
+        if (pc.writeable()){
+            pc.printf("%c%c%c%c%c\n", 3, botPaddle.getX(), topPaddle.getX(), ball.getX(), ball.getY());
+            ball.setVx(0);
             srand(i);
-            random = (rand() % (118 - 2*radius)) + radius; 
-            fx = random;
-            random = (rand() % (127 - 2*radius)) + radius;
-            fy = random;
-            x=(int)fx; y=(int)fy;
-            random = rand() % 1;
-            vxSign=-1; vySign=((float)random - 0.5)*2;
-            uLCD.filled_rectangle(cornerX, cornerY, cornerX+width, cornerY+length, BLUE);
+            //if (rand()%2){
+//                ball.setBaseVy(-1);
+//            }
+//            else{
+//                ball.setBaseVy(1);
+//            }
             gameState = GAME;
+        }
             break;
         case GAME:
-            if ((fx+vxSign*vx<=radius+1)) 
-            {
-                vxSign = -vxSign;
+        if (pc.writeable()){
+            pc.printf("%c%c%c%c%c\n", 4, botPaddle.getX(), topPaddle.getX(), ball.getX(), ball.getY());
+            uint8_t size = ball.getSize(); //stored in a temp variable because used a lot
+            if (ball.getFutureX()<=gameLowX){
+                ball.reverseXDirection();
             }
-            if ((fy+vySign*vy<=radius+1) || (fy+vySign*vy>=126-radius)) 
-            {
-                vySign = -vySign;
+            else if (ball.getFutureX()+size>=gameHighX){
+                ball.reverseXDirection();
             }
-            if (((fx+vxSign*vx >= cornerX) && (fx+vxSign*vx <= cornerX+3)) && 
-                ((fy+vySign*vy>=cornerY) && (fy+vySign*vy<=cornerY+length))) 
-            {
-                vySign = -vySign;
+            
+            if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)){
+                ball.reverseYDirection();
+                uint8_t fx = ball.getFutureX();
+                ball.setVx(topPaddle.returnAngle(fx, size));
+                ball.setVxDir(topPaddle.returnDir(fx, size));
             }
-            if ((fx+vxSign*vx>=126-radius)) 
-            {
-                vx = 0;
-                vy = 0;
+            else if (botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)){
+                ball.reverseYDirection();
+                uint8_t fx = ball.getFutureX();
+                ball.setVx(botPaddle.returnAngle(fx, size));
+                ball.setVxDir(botPaddle.returnDir(fx, size));
+            }
+            
+            if (ball.getY() < gameLowY){
+                gameState = WIN;
+            }
+            else if (ball.getY() > gameHighY){
                 gameState = LOSE;
             }
-            if ((fx+vxSign*vx>=cornerX-radius) && (fy+vySign*vy<=cornerY+length) && (fy+vySign*vy>=cornerY)) 
-            {
-                vxSign = -vxSign;
-                score++;
-                uLCD.locate(1,1);
-                uLCD.printf("%d", score);
+            
+            ball.update();
+            botMove = xMove;
+            if (!botMove == .5){
+                botPaddle.move(.5-botMove);
             }
-            uLCD.circle(x, y, radius, BLACK);
-            fx=fx+(vxSign*vx);
-            fy=fy+(vySign*vy);
-            x=(int)fx;
-            y=(int)fy;
-            uLCD.circle(x, y, radius, WHITE);
-            // We can assume that these for loops are quick enough that the paddle will move only one interval.
-            // These movements of the paddle have been optimized. Feel free to draw it out to see how it's been done.
-            if(oldCornerY > cornerY) {
-                uLCD.filled_rectangle(cornerX, oldCornerY-paddleMove+1, cornerX+width, oldCornerY, BLUE);
-                uLCD.filled_rectangle(cornerX, oldCornerY+length-paddleMove+1, cornerX+width, oldCornerY+length, BLACK);
-                oldCornerY = cornerY;
-            }
-            else if(oldCornerY < cornerY) {
-                uLCD.filled_rectangle(cornerX, oldCornerY, cornerX+width, oldCornerY+paddleMove, BLACK);
-                uLCD.filled_rectangle(cornerX, oldCornerY+length, cornerX+width, oldCornerY+length+paddleMove, BLUE);
-                oldCornerY = cornerY;
-            }
+            //GET OTHER PADDLE SPOT AND UPDATE topPaddle
+        }
             break;
         case LOSE:
-            uLCD.cls();
-            uLCD.printf("YOU LOSE D:");
+        if (pc.writeable()){
+            pc.printf("%c%c%c%c%c\n", 5, 0, 0, 0, 0);
             score = 0;
             wait(5.0);
             gameState = START;
+        }
+            break;
+        case WIN:
+        if (pc.writeable()){
+            pc.printf("%c%c%c%c%c\n", 6, 0, 0, 0, 0);
+            wait(5.0);
+            gameState = START;
+        }
             break;
         case WAIT:
-            // Used to seed the rand() function so we don't get the same starting position every time.
-            i++; 
+        if (pc.writeable()){
+            // Used to seed the rand() function so the ball has a random starting direction
+            i++;
+            if (ready){
+                pc.printf("%c%c%c%c%c\n", 2, 0, 0, 0, 0);
+                wait(2.0);
+                gameState = GAME_SETUP;
+            }
+            else {
+                pc.printf("%c%c%c%c%c\n", 1, 0, 0, 0, 0);
+            }
+        }
             break;
         }
+        wait_ms(20);
+
     } 
 }
\ No newline at end of file