Rishi Bhargava / Mbed 2 deprecated 4180FinalLab

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

Fork of ECE2036Lab2StarterCode by Joseph Lind

main.cpp

Committer:
rishibhargava1
Date:
2016-04-24
Revision:
3:591086e44bf9
Parent:
2:6163865f5ce3
Child:
4:7da18e3c590b

File content as of revision 3:591086e44bf9:

#include "mbed.h"
#include "PinDetect.h"
#include "Speaker.h"
#include "paddle.h"
#include "ball.h"

// Pushbuttons
AnalogIn xMove(p15); 
PinDetect select(p13);
//Speaker
//Speaker mySpeaker(p20);
Serial pc(USBTX, USBRX);
 
// State machine definitions
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
 * GAME_SETUP -- Sets up one time things (like boarders, initializes beginning velocity
 * GAME -- When the user actually gets to play
 * LOSE -- clears the screen, prints you lose, waits, then goes back to start
 */
 
// Global state machine variable (So that the select can modify it)
gameStateType gameState = START;
bool ready = false;

// Pushbutton callbacks for selecting to start game
void select_hit_callback (void)
{
    switch (gameState)
    {
    case WAIT:
        ready = true;
        break;
    }
}
 
int main() 
{   
    // This is setting up the joystick select as a pushbutton
    select.mode(PullUp);
    wait(0.1);
    select.attach_deasserted(&select_hit_callback);
    select.setSampleFrequency();
    
    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;

    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:
        if (pc.writeable()){
            pc.printf("%c%c%c%c%c\n", 0, 0, 0, 0, 0);
            wait(.5);
            gameState = WAIT;
        }
            break;
        case GAME_SETUP:
            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);
            //if (rand()%2){
//                ball.setBaseVy(-1);
//            }
//            else{
//                ball.setBaseVy(1);
//            }
            gameState = GAME;
        }
            break;
        case GAME:
        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();
            }
            else if (ball.getFutureX()+size>=gameHighX){
                ball.reverseXDirection();
            }
            
            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));
            }
            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;
            }
            
            ball.update();
            botMove = xMove;
            if (!botMove == .5){
                botPaddle.move(.5-botMove);
            }
            //GET OTHER PADDLE SPOT AND UPDATE topPaddle
        }
            break;
        case LOSE:
        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:
        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);

    } 
}