Rishi Bhargava / Mbed 2 deprecated 4180FinalLab

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

Fork of ECE2036Lab2StarterCode by Joseph Lind

Committer:
rishibhargava1
Date:
Sun Apr 24 01:23:28 2016 +0000
Revision:
3:591086e44bf9
Parent:
2:6163865f5ce3
Child:
4:7da18e3c590b
Directions of the ball velocities are bools

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jlind6 0:356124c0bafc 1 #include "mbed.h"
jlind6 0:356124c0bafc 2 #include "PinDetect.h"
jlind6 0:356124c0bafc 3 #include "Speaker.h"
rishibhargava1 3:591086e44bf9 4 #include "paddle.h"
rishibhargava1 3:591086e44bf9 5 #include "ball.h"
jlind6 0:356124c0bafc 6
jlind6 0:356124c0bafc 7 // Pushbuttons
rishibhargava1 3:591086e44bf9 8 AnalogIn xMove(p15);
rishibhargava1 3:591086e44bf9 9 PinDetect select(p13);
jlind6 0:356124c0bafc 10 //Speaker
rishibhargava1 3:591086e44bf9 11 //Speaker mySpeaker(p20);
rishibhargava1 3:591086e44bf9 12 Serial pc(USBTX, USBRX);
jlind6 0:356124c0bafc 13
jlind6 0:356124c0bafc 14 // State machine definitions
rishibhargava1 3:591086e44bf9 15 enum gameStateType {START, WAIT, GAME_SETUP, GAME, WIN, LOSE};
jlind6 0:356124c0bafc 16 /* State Definitions:
jlind6 0:356124c0bafc 17 * START -- Creates the start screen
jlind6 0:356124c0bafc 18 * WAIT -- After the start screen, goes into wait where mbed spins and does nothing
jlind6 0:356124c0bafc 19 * GAME_SETUP -- Sets up one time things (like boarders, initializes beginning velocity
jlind6 0:356124c0bafc 20 * GAME -- When the user actually gets to play
jlind6 0:356124c0bafc 21 * LOSE -- clears the screen, prints you lose, waits, then goes back to start
jlind6 0:356124c0bafc 22 */
jlind6 0:356124c0bafc 23
rishibhargava1 3:591086e44bf9 24 // Global state machine variable (So that the select can modify it)
jlind6 0:356124c0bafc 25 gameStateType gameState = START;
rishibhargava1 3:591086e44bf9 26 bool ready = false;
jlind6 0:356124c0bafc 27
rishibhargava1 3:591086e44bf9 28 // Pushbutton callbacks for selecting to start game
rishibhargava1 3:591086e44bf9 29 void select_hit_callback (void)
jlind6 0:356124c0bafc 30 {
jlind6 0:356124c0bafc 31 switch (gameState)
jlind6 0:356124c0bafc 32 {
jlind6 0:356124c0bafc 33 case WAIT:
rishibhargava1 3:591086e44bf9 34 ready = true;
jlind6 0:356124c0bafc 35 break;
jlind6 0:356124c0bafc 36 }
jlind6 0:356124c0bafc 37 }
jlind6 0:356124c0bafc 38
jlind6 0:356124c0bafc 39 int main()
jlind6 0:356124c0bafc 40 {
rishibhargava1 3:591086e44bf9 41 // This is setting up the joystick select as a pushbutton
rishibhargava1 3:591086e44bf9 42 select.mode(PullUp);
jlind6 0:356124c0bafc 43 wait(0.1);
rishibhargava1 3:591086e44bf9 44 select.attach_deasserted(&select_hit_callback);
rishibhargava1 3:591086e44bf9 45 select.setSampleFrequency();
jlind6 0:356124c0bafc 46
rishibhargava1 3:591086e44bf9 47 pc.baud(9600);
rishibhargava1 3:591086e44bf9 48
rishibhargava1 3:591086e44bf9 49 uint8_t gameLowX = 10, gameHighX = 190, gameLowY = 5, gameHighY = 245;
rishibhargava1 3:591086e44bf9 50 uint8_t gameCenterX = (gameHighX-gameLowX)/2, gameCenterY = (gameHighY-gameLowY)/2;
rishibhargava1 3:591086e44bf9 51 uint8_t ballSize=10;
rishibhargava1 3:591086e44bf9 52 uint8_t paddleWidth = 5, paddleLength = 35;
rishibhargava1 3:591086e44bf9 53 float botMove = xMove;
rishibhargava1 3:591086e44bf9 54 uint8_t score = 0;
rishibhargava1 3:591086e44bf9 55 int i = 0;
jlind6 0:356124c0bafc 56
rishibhargava1 3:591086e44bf9 57 Paddle botPaddle(gameCenterX-(paddleLength/2), gameHighY, paddleLength, paddleWidth);
rishibhargava1 3:591086e44bf9 58 botPaddle.setLimits(gameLowX, gameHighX);
rishibhargava1 3:591086e44bf9 59 botPaddle.setMaxMove(2);
rishibhargava1 3:591086e44bf9 60 Paddle topPaddle(gameCenterX-(paddleLength/2), gameLowY, paddleLength, paddleWidth);
rishibhargava1 3:591086e44bf9 61 topPaddle.setLimits(gameLowX, gameHighX);
rishibhargava1 3:591086e44bf9 62 topPaddle.setMaxMove(2);
rishibhargava1 3:591086e44bf9 63 Ball ball(gameCenterX, gameCenterY, ballSize);
rishibhargava1 3:591086e44bf9 64
rishibhargava1 3:591086e44bf9 65 ball.setVyDir(true);
rishibhargava1 3:591086e44bf9 66
rishibhargava1 3:591086e44bf9 67 while (!pc.writeable() && !pc.readable()){
rishibhargava1 3:591086e44bf9 68
rishibhargava1 3:591086e44bf9 69 }
jlind6 0:356124c0bafc 70 while (1)
jlind6 0:356124c0bafc 71 {
jlind6 0:356124c0bafc 72 switch (gameState)
jlind6 0:356124c0bafc 73 {
jlind6 0:356124c0bafc 74 case START:
rishibhargava1 3:591086e44bf9 75 if (pc.writeable()){
rishibhargava1 3:591086e44bf9 76 pc.printf("%c%c%c%c%c\n", 0, 0, 0, 0, 0);
rishibhargava1 3:591086e44bf9 77 wait(.5);
jlind6 0:356124c0bafc 78 gameState = WAIT;
rishibhargava1 3:591086e44bf9 79 }
jlind6 0:356124c0bafc 80 break;
jlind6 0:356124c0bafc 81 case GAME_SETUP:
rishibhargava1 3:591086e44bf9 82 ball.reset(gameCenterX, gameCenterY, 0, 1);
rishibhargava1 3:591086e44bf9 83 botPaddle.reset(gameCenterX-(paddleLength/2), gameHighY);
rishibhargava1 3:591086e44bf9 84 topPaddle.reset(gameCenterX-(paddleLength/2), gameLowY);
rishibhargava1 3:591086e44bf9 85 ready = false;
rishibhargava1 3:591086e44bf9 86 if (pc.writeable()){
rishibhargava1 3:591086e44bf9 87 pc.printf("%c%c%c%c%c\n", 3, botPaddle.getX(), topPaddle.getX(), ball.getX(), ball.getY());
rishibhargava1 3:591086e44bf9 88 ball.setVx(0);
jlind6 0:356124c0bafc 89 srand(i);
rishibhargava1 3:591086e44bf9 90 //if (rand()%2){
rishibhargava1 3:591086e44bf9 91 // ball.setBaseVy(-1);
rishibhargava1 3:591086e44bf9 92 // }
rishibhargava1 3:591086e44bf9 93 // else{
rishibhargava1 3:591086e44bf9 94 // ball.setBaseVy(1);
rishibhargava1 3:591086e44bf9 95 // }
jlind6 0:356124c0bafc 96 gameState = GAME;
rishibhargava1 3:591086e44bf9 97 }
jlind6 0:356124c0bafc 98 break;
jlind6 0:356124c0bafc 99 case GAME:
rishibhargava1 3:591086e44bf9 100 if (pc.writeable()){
rishibhargava1 3:591086e44bf9 101 pc.printf("%c%c%c%c%c\n", 4, botPaddle.getX(), topPaddle.getX(), ball.getX(), ball.getY());
rishibhargava1 3:591086e44bf9 102 uint8_t size = ball.getSize(); //stored in a temp variable because used a lot
rishibhargava1 3:591086e44bf9 103 if (ball.getFutureX()<=gameLowX){
rishibhargava1 3:591086e44bf9 104 ball.reverseXDirection();
jlind6 0:356124c0bafc 105 }
rishibhargava1 3:591086e44bf9 106 else if (ball.getFutureX()+size>=gameHighX){
rishibhargava1 3:591086e44bf9 107 ball.reverseXDirection();
jlind6 0:356124c0bafc 108 }
rishibhargava1 3:591086e44bf9 109
rishibhargava1 3:591086e44bf9 110 if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)){
rishibhargava1 3:591086e44bf9 111 ball.reverseYDirection();
rishibhargava1 3:591086e44bf9 112 uint8_t fx = ball.getFutureX();
rishibhargava1 3:591086e44bf9 113 ball.setVx(topPaddle.returnAngle(fx, size));
rishibhargava1 3:591086e44bf9 114 ball.setVxDir(topPaddle.returnDir(fx, size));
jlind6 0:356124c0bafc 115 }
rishibhargava1 3:591086e44bf9 116 else if (botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)){
rishibhargava1 3:591086e44bf9 117 ball.reverseYDirection();
rishibhargava1 3:591086e44bf9 118 uint8_t fx = ball.getFutureX();
rishibhargava1 3:591086e44bf9 119 ball.setVx(botPaddle.returnAngle(fx, size));
rishibhargava1 3:591086e44bf9 120 ball.setVxDir(botPaddle.returnDir(fx, size));
rishibhargava1 3:591086e44bf9 121 }
rishibhargava1 3:591086e44bf9 122
rishibhargava1 3:591086e44bf9 123 if (ball.getY() < gameLowY){
rishibhargava1 3:591086e44bf9 124 gameState = WIN;
rishibhargava1 3:591086e44bf9 125 }
rishibhargava1 3:591086e44bf9 126 else if (ball.getY() > gameHighY){
jlind6 0:356124c0bafc 127 gameState = LOSE;
jlind6 0:356124c0bafc 128 }
rishibhargava1 3:591086e44bf9 129
rishibhargava1 3:591086e44bf9 130 ball.update();
rishibhargava1 3:591086e44bf9 131 botMove = xMove;
rishibhargava1 3:591086e44bf9 132 if (!botMove == .5){
rishibhargava1 3:591086e44bf9 133 botPaddle.move(.5-botMove);
jlind6 0:356124c0bafc 134 }
rishibhargava1 3:591086e44bf9 135 //GET OTHER PADDLE SPOT AND UPDATE topPaddle
rishibhargava1 3:591086e44bf9 136 }
jlind6 0:356124c0bafc 137 break;
jlind6 0:356124c0bafc 138 case LOSE:
rishibhargava1 3:591086e44bf9 139 if (pc.writeable()){
rishibhargava1 3:591086e44bf9 140 pc.printf("%c%c%c%c%c\n", 5, 0, 0, 0, 0);
jlind6 0:356124c0bafc 141 score = 0;
jlind6 0:356124c0bafc 142 wait(5.0);
jlind6 0:356124c0bafc 143 gameState = START;
rishibhargava1 3:591086e44bf9 144 }
rishibhargava1 3:591086e44bf9 145 break;
rishibhargava1 3:591086e44bf9 146 case WIN:
rishibhargava1 3:591086e44bf9 147 if (pc.writeable()){
rishibhargava1 3:591086e44bf9 148 pc.printf("%c%c%c%c%c\n", 6, 0, 0, 0, 0);
rishibhargava1 3:591086e44bf9 149 wait(5.0);
rishibhargava1 3:591086e44bf9 150 gameState = START;
rishibhargava1 3:591086e44bf9 151 }
jlind6 0:356124c0bafc 152 break;
jlind6 0:356124c0bafc 153 case WAIT:
rishibhargava1 3:591086e44bf9 154 if (pc.writeable()){
rishibhargava1 3:591086e44bf9 155 // Used to seed the rand() function so the ball has a random starting direction
rishibhargava1 3:591086e44bf9 156 i++;
rishibhargava1 3:591086e44bf9 157 if (ready){
rishibhargava1 3:591086e44bf9 158 pc.printf("%c%c%c%c%c\n", 2, 0, 0, 0, 0);
rishibhargava1 3:591086e44bf9 159 wait(2.0);
rishibhargava1 3:591086e44bf9 160 gameState = GAME_SETUP;
rishibhargava1 3:591086e44bf9 161 }
rishibhargava1 3:591086e44bf9 162 else {
rishibhargava1 3:591086e44bf9 163 pc.printf("%c%c%c%c%c\n", 1, 0, 0, 0, 0);
rishibhargava1 3:591086e44bf9 164 }
rishibhargava1 3:591086e44bf9 165 }
jlind6 0:356124c0bafc 166 break;
jlind6 0:356124c0bafc 167 }
rishibhargava1 3:591086e44bf9 168 wait_ms(20);
rishibhargava1 3:591086e44bf9 169
jlind6 0:356124c0bafc 170 }
jlind6 0:356124c0bafc 171 }