
player 1
Dependencies: 4DGL-uLCD-SE PinDetect SparkfunAnalogJoystick mbed-rtos mbed SDFileSystem
Fork of 4180FinalLab by
Wireless 2 Player Pong game
main.cpp@9:aa967c554d10, 2016-04-28 (annotated)
- Committer:
- fepie3
- Date:
- Thu Apr 28 20:08:23 2016 +0000
- Revision:
- 9:aa967c554d10
- Parent:
- 8:8cc2aa78348c
- Child:
- 10:b57b3fbf8266
Wifi added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jlind6 | 0:356124c0bafc | 1 | #include "mbed.h" |
Mpmart08 | 4:7da18e3c590b | 2 | #include "rtos.h" |
jlind6 | 0:356124c0bafc | 3 | #include "PinDetect.h" |
jlind6 | 0:356124c0bafc | 4 | #include "Speaker.h" |
Mpmart08 | 4:7da18e3c590b | 5 | #include "soundBuilder.h" |
Mpmart08 | 4:7da18e3c590b | 6 | #include "SparkfunAnalogJoystick.h" |
rishibhargava1 | 3:591086e44bf9 | 7 | #include "paddle.h" |
rishibhargava1 | 3:591086e44bf9 | 8 | #include "ball.h" |
jlind6 | 0:356124c0bafc | 9 | |
jlind6 | 0:356124c0bafc | 10 | // Pushbuttons |
Mpmart08 | 4:7da18e3c590b | 11 | SparkfunAnalogJoystick joystick(p16, p15, p14); |
Mpmart08 | 4:7da18e3c590b | 12 | //PinDetect select(p13); |
jlind6 | 0:356124c0bafc | 13 | //Speaker |
Mpmart08 | 4:7da18e3c590b | 14 | Speaker mySpeaker(p25); |
rishibhargava1 | 3:591086e44bf9 | 15 | Serial pc(USBTX, USBRX); |
Mpmart08 | 7:cadf69604b45 | 16 | Serial xbee(p9, p10); |
fepie3 | 9:aa967c554d10 | 17 | |
fepie3 | 9:aa967c554d10 | 18 | //Wifi |
fepie3 | 9:aa967c554d10 | 19 | Huzzah huzzah("fese","12345678", &esp, &pc); |
fepie3 | 9:aa967c554d10 | 20 | Serial esp(p28,p27); |
fepie3 | 9:aa967c554d10 | 21 | DigitalOut reset(p26); |
fepie3 | 9:aa967c554d10 | 22 | int player1score,player2score, highscore1,highscore2; |
fepie3 | 9:aa967c554d10 | 23 | |
fepie3 | 9:aa967c554d10 | 24 | //SD Card Storage |
fepie3 | 9:aa967c554d10 | 25 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
jlind6 | 0:356124c0bafc | 26 | |
jlind6 | 0:356124c0bafc | 27 | // State machine definitions |
rishibhargava1 | 3:591086e44bf9 | 28 | enum gameStateType {START, WAIT, GAME_SETUP, GAME, WIN, LOSE}; |
jlind6 | 0:356124c0bafc | 29 | /* State Definitions: |
jlind6 | 0:356124c0bafc | 30 | * START -- Creates the start screen |
jlind6 | 0:356124c0bafc | 31 | * WAIT -- After the start screen, goes into wait where mbed spins and does nothing |
jlind6 | 0:356124c0bafc | 32 | * GAME_SETUP -- Sets up one time things (like boarders, initializes beginning velocity |
jlind6 | 0:356124c0bafc | 33 | * GAME -- When the user actually gets to play |
jlind6 | 0:356124c0bafc | 34 | * LOSE -- clears the screen, prints you lose, waits, then goes back to start |
jlind6 | 0:356124c0bafc | 35 | */ |
jlind6 | 0:356124c0bafc | 36 | |
rishibhargava1 | 3:591086e44bf9 | 37 | // Global state machine variable (So that the select can modify it) |
jlind6 | 0:356124c0bafc | 38 | gameStateType gameState = START; |
rishibhargava1 | 3:591086e44bf9 | 39 | bool ready = false; |
jlind6 | 0:356124c0bafc | 40 | |
Mpmart08 | 4:7da18e3c590b | 41 | Paddle botPaddle(0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 42 | Paddle topPaddle(0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 43 | Ball ball(0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 44 | |
Mpmart08 | 8:8cc2aa78348c | 45 | uint8_t gameLowX = 10, gameHighX = 190, gameLowY = 5, gameHighY = 245, gameX = 200, gameY = 250; |
Mpmart08 | 8:8cc2aa78348c | 46 | uint8_t gameCenterX = gameX/2, gameCenterY = gameY/2; |
Mpmart08 | 8:8cc2aa78348c | 47 | uint8_t ballSize=10; |
Mpmart08 | 8:8cc2aa78348c | 48 | uint8_t paddleWidth = 5, paddleLength = 40; |
Mpmart08 | 8:8cc2aa78348c | 49 | float botMove = joystick.yAxis(); |
Mpmart08 | 8:8cc2aa78348c | 50 | uint8_t botScore = 0; |
Mpmart08 | 8:8cc2aa78348c | 51 | uint8_t topScore = 0; |
Mpmart08 | 8:8cc2aa78348c | 52 | int i = 0; |
Mpmart08 | 8:8cc2aa78348c | 53 | |
Mpmart08 | 4:7da18e3c590b | 54 | // thread that plays game sounds through the speaker |
Mpmart08 | 4:7da18e3c590b | 55 | void speaker_thread(void const *argument) { |
Mpmart08 | 4:7da18e3c590b | 56 | |
Mpmart08 | 4:7da18e3c590b | 57 | Speaker *player = &mySpeaker; |
Mpmart08 | 4:7da18e3c590b | 58 | |
Mpmart08 | 4:7da18e3c590b | 59 | // Start Song |
Mpmart08 | 4:7da18e3c590b | 60 | float sFreq[] = {550,750,550,750}; |
Mpmart08 | 4:7da18e3c590b | 61 | float sDur[] = {.3,.3,.3,.3}; |
Mpmart08 | 4:7da18e3c590b | 62 | float sVol[] = {.01,.01,.01,.01}; |
Mpmart08 | 4:7da18e3c590b | 63 | SoundBuilder startSong(sFreq, sDur, sVol, sizeof(sFreq)/sizeof(*sFreq), player); |
Mpmart08 | 4:7da18e3c590b | 64 | |
Mpmart08 | 4:7da18e3c590b | 65 | // End Song |
Mpmart08 | 4:7da18e3c590b | 66 | float eFreq[] = {300,200,250,225,200,150,150,100}; |
Mpmart08 | 4:7da18e3c590b | 67 | float eDur[] = {.3,.3,.3,.3,.3,.3,.3,.3}; |
Mpmart08 | 4:7da18e3c590b | 68 | float eVol[] = {.01,.01,.01,.01,.01,.01,.01,.01}; |
Mpmart08 | 4:7da18e3c590b | 69 | SoundBuilder endSong(eFreq, eDur, eVol, sizeof(eFreq)/sizeof(*eFreq), player); |
Mpmart08 | 4:7da18e3c590b | 70 | |
Mpmart08 | 4:7da18e3c590b | 71 | while (true) { |
Mpmart08 | 4:7da18e3c590b | 72 | switch (gameState) { |
Mpmart08 | 4:7da18e3c590b | 73 | case GAME: // if game is running and user dodges a pipe, play a note |
Mpmart08 | 4:7da18e3c590b | 74 | if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), ball.getSize()) |
Mpmart08 | 4:7da18e3c590b | 75 | || botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), ball.getSize()) |
Mpmart08 | 4:7da18e3c590b | 76 | || ball.getFutureX() <= 10 |
Mpmart08 | 4:7da18e3c590b | 77 | || ball.getFutureX() + ball.getSize() >= 190) { |
Mpmart08 | 4:7da18e3c590b | 78 | mySpeaker.PlayNote(440, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 79 | } |
Mpmart08 | 4:7da18e3c590b | 80 | |
Mpmart08 | 4:7da18e3c590b | 81 | if (ball.getY() < 5){ |
Mpmart08 | 4:7da18e3c590b | 82 | mySpeaker.PlayNote(440, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 83 | mySpeaker.PlayNote(880, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 84 | } |
Mpmart08 | 4:7da18e3c590b | 85 | else if (ball.getY() + ball.getSize() > 245){ |
Mpmart08 | 4:7da18e3c590b | 86 | mySpeaker.PlayNote(880, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 87 | mySpeaker.PlayNote(440, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 88 | } |
Mpmart08 | 4:7da18e3c590b | 89 | |
Mpmart08 | 4:7da18e3c590b | 90 | break; |
Mpmart08 | 4:7da18e3c590b | 91 | case START: // play a song at the start of the game |
Mpmart08 | 4:7da18e3c590b | 92 | startSong.playSong(); |
Mpmart08 | 4:7da18e3c590b | 93 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 94 | break; |
Mpmart08 | 4:7da18e3c590b | 95 | case WIN: // play a song when the player wins the game |
Mpmart08 | 4:7da18e3c590b | 96 | case LOSE: // play a song when the player loses the game |
Mpmart08 | 4:7da18e3c590b | 97 | endSong.playSong(); |
Mpmart08 | 4:7da18e3c590b | 98 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 99 | break; |
Mpmart08 | 4:7da18e3c590b | 100 | } |
jlind6 | 0:356124c0bafc | 101 | } |
jlind6 | 0:356124c0bafc | 102 | } |
Mpmart08 | 7:cadf69604b45 | 103 | |
fepie3 | 9:aa967c554d10 | 104 | // thread that writes to the sd card and loads highscores to website |
Mpmart08 | 7:cadf69604b45 | 105 | void sd_card_thread(void const *argument) { |
Mpmart08 | 7:cadf69604b45 | 106 | |
Mpmart08 | 7:cadf69604b45 | 107 | while (true) { |
fepie3 | 9:aa967c554d10 | 108 | FILE *fp = fopen("/sd/highscore.txt", "r"); |
fepie3 | 9:aa967c554d10 | 109 | if(fp == NULL) { |
fepie3 | 9:aa967c554d10 | 110 | error("Could not open file for read\n"); |
fepie3 | 9:aa967c554d10 | 111 | } |
fepie3 | 9:aa967c554d10 | 112 | fscanf(fp, "%i%i", &player1score, &player2score); |
fepie3 | 9:aa967c554d10 | 113 | fclose(fp); |
fepie3 | 9:aa967c554d10 | 114 | huzzah.sendwebpage(player1score, player2score); |
Mpmart08 | 7:cadf69604b45 | 115 | switch (gameState) { |
Mpmart08 | 7:cadf69604b45 | 116 | case WIN: |
fepie3 | 9:aa967c554d10 | 117 | highscore2=0; //reset player 2 highscore |
fepie3 | 9:aa967c554d10 | 118 | if (player1score < highscore1){ |
fepie3 | 9:aa967c554d10 | 119 | // overwrite previous high score |
fepie3 | 9:aa967c554d10 | 120 | fp = fopen("/sd/highscore.txt", "w"); |
fepie3 | 9:aa967c554d10 | 121 | fprintf(fp, "%i%i", highscore1, player2score); |
fepie3 | 9:aa967c554d10 | 122 | fclose(fp); |
fepie3 | 9:aa967c554d10 | 123 | huzzah.sendwebpage(player1score, player2score); |
fepie3 | 9:aa967c554d10 | 124 | } |
fepie3 | 9:aa967c554d10 | 125 | highscore1++; |
Mpmart08 | 7:cadf69604b45 | 126 | case LOSE: |
fepie3 | 9:aa967c554d10 | 127 | highscore1=0; //reset player 1 highscore |
fepie3 | 9:aa967c554d10 | 128 | if (player2score < highscore2){ |
fepie3 | 9:aa967c554d10 | 129 | // overwrite previous high score |
fepie3 | 9:aa967c554d10 | 130 | fp = fopen("/sd/highscore.txt", "w"); |
fepie3 | 9:aa967c554d10 | 131 | fprintf(fp, "%i%i", player1score, highscore2); |
fepie3 | 9:aa967c554d10 | 132 | fclose(fp); |
fepie3 | 9:aa967c554d10 | 133 | huzzah.sendwebpage(player1score, player2score); |
fepie3 | 9:aa967c554d10 | 134 | } |
fepie3 | 9:aa967c554d10 | 135 | highscore2++; |
Mpmart08 | 7:cadf69604b45 | 136 | break; |
Mpmart08 | 7:cadf69604b45 | 137 | } |
Mpmart08 | 7:cadf69604b45 | 138 | } |
fepie3 | 9:aa967c554d10 | 139 | } |
Mpmart08 | 8:8cc2aa78348c | 140 | |
Mpmart08 | 8:8cc2aa78348c | 141 | void xbee_thread(void const *argument) { |
Mpmart08 | 8:8cc2aa78348c | 142 | |
Mpmart08 | 8:8cc2aa78348c | 143 | while (true) { |
Mpmart08 | 8:8cc2aa78348c | 144 | switch (gameState) { |
Mpmart08 | 8:8cc2aa78348c | 145 | case WAIT: |
Mpmart08 | 8:8cc2aa78348c | 146 | if (ready && xbee.writeable()) { |
Mpmart08 | 8:8cc2aa78348c | 147 | xbee.putc(1); |
Mpmart08 | 8:8cc2aa78348c | 148 | } |
Mpmart08 | 8:8cc2aa78348c | 149 | if (ready && xbee.readable()) { |
Mpmart08 | 8:8cc2aa78348c | 150 | char c = xbee.getc(); |
Mpmart08 | 8:8cc2aa78348c | 151 | if (c == 1) { |
Mpmart08 | 8:8cc2aa78348c | 152 | gameState = GAME_SETUP; |
Mpmart08 | 8:8cc2aa78348c | 153 | } |
Mpmart08 | 8:8cc2aa78348c | 154 | } |
Mpmart08 | 8:8cc2aa78348c | 155 | break; |
Mpmart08 | 8:8cc2aa78348c | 156 | case GAME: |
Mpmart08 | 8:8cc2aa78348c | 157 | if (xbee.writeable()) { |
Mpmart08 | 8:8cc2aa78348c | 158 | xbee.putc(botPaddle.getX()); |
Mpmart08 | 8:8cc2aa78348c | 159 | } |
Mpmart08 | 8:8cc2aa78348c | 160 | if (xbee.readable()) { |
Mpmart08 | 8:8cc2aa78348c | 161 | char c = xbee.getc(); |
Mpmart08 | 8:8cc2aa78348c | 162 | uint8_t top = 200 - paddleLength - ((uint8_t) c); |
Mpmart08 | 8:8cc2aa78348c | 163 | topPaddle.setX(top); |
Mpmart08 | 8:8cc2aa78348c | 164 | } |
Mpmart08 | 8:8cc2aa78348c | 165 | break; |
Mpmart08 | 8:8cc2aa78348c | 166 | } |
Mpmart08 | 8:8cc2aa78348c | 167 | } |
Mpmart08 | 8:8cc2aa78348c | 168 | } |
jlind6 | 0:356124c0bafc | 169 | |
jlind6 | 0:356124c0bafc | 170 | int main() |
jlind6 | 0:356124c0bafc | 171 | { |
fepie3 | 9:aa967c554d10 | 172 | //WifiServer Configuration and set-up |
fepie3 | 9:aa967c554d10 | 173 | highscore1 = 0; // variable to store high score |
fepie3 | 9:aa967c554d10 | 174 | highscore2=0; |
fepie3 | 9:aa967c554d10 | 175 | reset=0; //hardware reset for 8266 |
fepie3 | 9:aa967c554d10 | 176 | Thread::wait(500); |
fepie3 | 9:aa967c554d10 | 177 | reset=1; |
fepie3 | 9:aa967c554d10 | 178 | esp.baud(9600); |
fepie3 | 9:aa967c554d10 | 179 | huzzah.config(); |
fepie3 | 9:aa967c554d10 | 180 | huzzah.sendwebpage(highscore1,highscore2); |
fepie3 | 9:aa967c554d10 | 181 | |
rishibhargava1 | 3:591086e44bf9 | 182 | // This is setting up the joystick select as a pushbutton |
Mpmart08 | 4:7da18e3c590b | 183 | //joystick.set_callback(&select_hit_callback); |
jlind6 | 0:356124c0bafc | 184 | |
Mpmart08 | 4:7da18e3c590b | 185 | pc.format(8, SerialBase::None, 1); |
Mpmart08 | 4:7da18e3c590b | 186 | pc.baud(115200); |
rishibhargava1 | 3:591086e44bf9 | 187 | |
Mpmart08 | 4:7da18e3c590b | 188 | botPaddle = Paddle(gameCenterX-(paddleLength/2), gameHighY, paddleLength, paddleWidth); |
Mpmart08 | 8:8cc2aa78348c | 189 | botPaddle.setLimits(gameLowX, gameHighX); |
Mpmart08 | 8:8cc2aa78348c | 190 | botPaddle.setMaxMove(3); |
Mpmart08 | 4:7da18e3c590b | 191 | topPaddle = Paddle(gameCenterX-(paddleLength/2), gameLowY, paddleLength, paddleWidth); |
Mpmart08 | 8:8cc2aa78348c | 192 | topPaddle.setLimits(gameLowX, gameHighX); |
Mpmart08 | 8:8cc2aa78348c | 193 | topPaddle.setMaxMove(3); |
Mpmart08 | 4:7da18e3c590b | 194 | ball = Ball(gameCenterX, gameCenterY, ballSize); |
rishibhargava1 | 3:591086e44bf9 | 195 | |
Mpmart08 | 4:7da18e3c590b | 196 | ball.setVxDir(true); |
rishibhargava1 | 3:591086e44bf9 | 197 | ball.setVyDir(true); |
rishibhargava1 | 3:591086e44bf9 | 198 | |
Mpmart08 | 6:9cdde66d7502 | 199 | while (!pc.readable()){ |
rishibhargava1 | 3:591086e44bf9 | 200 | |
rishibhargava1 | 3:591086e44bf9 | 201 | } |
Mpmart08 | 4:7da18e3c590b | 202 | |
Mpmart08 | 4:7da18e3c590b | 203 | Thread thread1(speaker_thread); |
Mpmart08 | 7:cadf69604b45 | 204 | Thread thread2(sd_card_thread); |
Mpmart08 | 8:8cc2aa78348c | 205 | Thread thread3(xbee_thread); |
Mpmart08 | 4:7da18e3c590b | 206 | |
jlind6 | 0:356124c0bafc | 207 | while (1) |
Mpmart08 | 6:9cdde66d7502 | 208 | { |
jlind6 | 0:356124c0bafc | 209 | switch (gameState) |
jlind6 | 0:356124c0bafc | 210 | { |
jlind6 | 0:356124c0bafc | 211 | case START: |
Mpmart08 | 4:7da18e3c590b | 212 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 213 | pc.printf("%c%c%c%c%c", 0, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 214 | ball.setVxDir(true); |
Mpmart08 | 4:7da18e3c590b | 215 | ball.setVyDir(true); |
Mpmart08 | 4:7da18e3c590b | 216 | gameState = WAIT; |
Mpmart08 | 4:7da18e3c590b | 217 | } |
jlind6 | 0:356124c0bafc | 218 | break; |
jlind6 | 0:356124c0bafc | 219 | case GAME_SETUP: |
Mpmart08 | 8:8cc2aa78348c | 220 | ball.reset(gameCenterX-(ballSize/2), gameCenterY-(ballSize/2), 0, 6); |
rishibhargava1 | 3:591086e44bf9 | 221 | botPaddle.reset(gameCenterX-(paddleLength/2), gameHighY); |
rishibhargava1 | 3:591086e44bf9 | 222 | topPaddle.reset(gameCenterX-(paddleLength/2), gameLowY); |
Mpmart08 | 4:7da18e3c590b | 223 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 224 | pc.printf("%c%c%c%c%c", 3, botScore, topScore, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 225 | ready = false; |
Mpmart08 | 4:7da18e3c590b | 226 | srand(i); |
Mpmart08 | 4:7da18e3c590b | 227 | gameState = GAME; |
Mpmart08 | 4:7da18e3c590b | 228 | Thread::wait(2000); |
Mpmart08 | 4:7da18e3c590b | 229 | } |
jlind6 | 0:356124c0bafc | 230 | break; |
jlind6 | 0:356124c0bafc | 231 | case GAME: |
Mpmart08 | 4:7da18e3c590b | 232 | if (pc.writeable()) { |
Mpmart08 | 4:7da18e3c590b | 233 | pc.printf("%c%c%c%c%c", 4, botPaddle.getX(), topPaddle.getX(), ball.getX(), ball.getY()); |
Mpmart08 | 4:7da18e3c590b | 234 | |
Mpmart08 | 4:7da18e3c590b | 235 | uint8_t size = ball.getSize(); //stored in a temp variable because used a lot |
Mpmart08 | 4:7da18e3c590b | 236 | |
Mpmart08 | 4:7da18e3c590b | 237 | if (ball.getFutureX() <= gameLowX){ |
Mpmart08 | 4:7da18e3c590b | 238 | ball.reverseXDirection(); |
Mpmart08 | 4:7da18e3c590b | 239 | } |
Mpmart08 | 4:7da18e3c590b | 240 | else if (ball.getFutureX() + size >= gameHighX){ |
Mpmart08 | 4:7da18e3c590b | 241 | ball.reverseXDirection(); |
Mpmart08 | 4:7da18e3c590b | 242 | } |
Mpmart08 | 4:7da18e3c590b | 243 | |
Mpmart08 | 4:7da18e3c590b | 244 | if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)) { |
Mpmart08 | 4:7da18e3c590b | 245 | ball.reverseYDirection(); |
Mpmart08 | 4:7da18e3c590b | 246 | uint8_t fx = ball.getFutureX(); |
Mpmart08 | 8:8cc2aa78348c | 247 | uint8_t newVx = topPaddle.returnAngle(fx, size); |
Mpmart08 | 8:8cc2aa78348c | 248 | uint8_t newVy = 6 - newVx; |
Mpmart08 | 4:7da18e3c590b | 249 | ball.setVx(newVx); |
Mpmart08 | 4:7da18e3c590b | 250 | ball.setVy(newVy); |
Mpmart08 | 4:7da18e3c590b | 251 | ball.setVxDir(topPaddle.returnDir(fx, size)); |
Mpmart08 | 4:7da18e3c590b | 252 | } |
Mpmart08 | 4:7da18e3c590b | 253 | else if (botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)) { |
Mpmart08 | 4:7da18e3c590b | 254 | ball.reverseYDirection(); |
Mpmart08 | 4:7da18e3c590b | 255 | uint8_t fx = ball.getFutureX(); |
Mpmart08 | 8:8cc2aa78348c | 256 | uint8_t newVx = botPaddle.returnAngle(fx, size); |
Mpmart08 | 8:8cc2aa78348c | 257 | uint8_t newVy = 6 - newVx; |
Mpmart08 | 4:7da18e3c590b | 258 | ball.setVx(newVx); |
Mpmart08 | 4:7da18e3c590b | 259 | ball.setVy(newVy); |
Mpmart08 | 4:7da18e3c590b | 260 | ball.setVxDir(botPaddle.returnDir(fx, size)); |
Mpmart08 | 4:7da18e3c590b | 261 | } |
Mpmart08 | 4:7da18e3c590b | 262 | |
Mpmart08 | 4:7da18e3c590b | 263 | if (ball.getY() < gameLowY){ |
Mpmart08 | 4:7da18e3c590b | 264 | botScore++; |
Mpmart08 | 8:8cc2aa78348c | 265 | ball.setVyDir(false); |
Mpmart08 | 4:7da18e3c590b | 266 | gameState = GAME_SETUP; |
Mpmart08 | 4:7da18e3c590b | 267 | } |
Mpmart08 | 4:7da18e3c590b | 268 | else if (ball.getY() + size > gameHighY){ |
Mpmart08 | 4:7da18e3c590b | 269 | topScore++; |
Mpmart08 | 8:8cc2aa78348c | 270 | ball.setVyDir(true); |
Mpmart08 | 4:7da18e3c590b | 271 | gameState = GAME_SETUP; |
Mpmart08 | 4:7da18e3c590b | 272 | } |
Mpmart08 | 4:7da18e3c590b | 273 | |
Mpmart08 | 4:7da18e3c590b | 274 | if (botScore >= 5) { |
Mpmart08 | 4:7da18e3c590b | 275 | gameState = WIN; |
Mpmart08 | 4:7da18e3c590b | 276 | } |
Mpmart08 | 4:7da18e3c590b | 277 | else if (topScore >= 5) { |
Mpmart08 | 4:7da18e3c590b | 278 | gameState = LOSE; |
Mpmart08 | 4:7da18e3c590b | 279 | } |
Mpmart08 | 4:7da18e3c590b | 280 | |
Mpmart08 | 4:7da18e3c590b | 281 | ball.update(); |
Mpmart08 | 4:7da18e3c590b | 282 | botMove = -joystick.yAxis(); |
Mpmart08 | 8:8cc2aa78348c | 283 | if ((botMove < -0.1 || botMove > 0.1) && ball.getY() < gameHighY - 50) { |
Mpmart08 | 4:7da18e3c590b | 284 | botPaddle.move(botMove); |
Mpmart08 | 4:7da18e3c590b | 285 | } |
jlind6 | 0:356124c0bafc | 286 | } |
jlind6 | 0:356124c0bafc | 287 | break; |
jlind6 | 0:356124c0bafc | 288 | case LOSE: |
Mpmart08 | 4:7da18e3c590b | 289 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 290 | pc.printf("%c%c%c%c%c", 5, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 291 | botScore = 0; |
Mpmart08 | 4:7da18e3c590b | 292 | topScore = 0; |
Mpmart08 | 4:7da18e3c590b | 293 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 294 | gameState = START; |
Mpmart08 | 4:7da18e3c590b | 295 | } |
rishibhargava1 | 3:591086e44bf9 | 296 | break; |
rishibhargava1 | 3:591086e44bf9 | 297 | case WIN: |
Mpmart08 | 4:7da18e3c590b | 298 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 299 | pc.printf("%c%c%c%c%c", 6, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 300 | botScore = 0; |
Mpmart08 | 4:7da18e3c590b | 301 | topScore = 0; |
Mpmart08 | 4:7da18e3c590b | 302 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 303 | gameState = START; |
Mpmart08 | 4:7da18e3c590b | 304 | } |
jlind6 | 0:356124c0bafc | 305 | break; |
jlind6 | 0:356124c0bafc | 306 | case WAIT: |
Mpmart08 | 4:7da18e3c590b | 307 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 308 | // Used to seed the rand() function so the ball has a random starting direction |
Mpmart08 | 4:7da18e3c590b | 309 | i++; |
Mpmart08 | 4:7da18e3c590b | 310 | if (joystick.button()) |
Mpmart08 | 4:7da18e3c590b | 311 | ready = true; |
Mpmart08 | 4:7da18e3c590b | 312 | if (ready){ |
Mpmart08 | 4:7da18e3c590b | 313 | pc.printf("%c%c%c%c%c", 2, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 314 | } |
Mpmart08 | 4:7da18e3c590b | 315 | else { |
Mpmart08 | 4:7da18e3c590b | 316 | pc.printf("%c%c%c%c%c", 1, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 317 | } |
rishibhargava1 | 3:591086e44bf9 | 318 | } |
jlind6 | 0:356124c0bafc | 319 | break; |
jlind6 | 0:356124c0bafc | 320 | } |
Mpmart08 | 4:7da18e3c590b | 321 | Thread::wait(20); |
jlind6 | 0:356124c0bafc | 322 | } |
jlind6 | 0:356124c0bafc | 323 | } |