
player 1
Dependencies: 4DGL-uLCD-SE PinDetect SparkfunAnalogJoystick mbed-rtos mbed SDFileSystem
Fork of 4180FinalLab by
Wireless 2 Player Pong game
main.cpp@4:7da18e3c590b, 2016-04-26 (annotated)
- Committer:
- Mpmart08
- Date:
- Tue Apr 26 18:24:46 2016 +0000
- Revision:
- 4:7da18e3c590b
- Parent:
- 3:591086e44bf9
- Child:
- 6:9cdde66d7502
stuff
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); |
jlind6 | 0:356124c0bafc | 16 | |
jlind6 | 0:356124c0bafc | 17 | // State machine definitions |
rishibhargava1 | 3:591086e44bf9 | 18 | enum gameStateType {START, WAIT, GAME_SETUP, GAME, WIN, LOSE}; |
jlind6 | 0:356124c0bafc | 19 | /* State Definitions: |
jlind6 | 0:356124c0bafc | 20 | * START -- Creates the start screen |
jlind6 | 0:356124c0bafc | 21 | * WAIT -- After the start screen, goes into wait where mbed spins and does nothing |
jlind6 | 0:356124c0bafc | 22 | * GAME_SETUP -- Sets up one time things (like boarders, initializes beginning velocity |
jlind6 | 0:356124c0bafc | 23 | * GAME -- When the user actually gets to play |
jlind6 | 0:356124c0bafc | 24 | * LOSE -- clears the screen, prints you lose, waits, then goes back to start |
jlind6 | 0:356124c0bafc | 25 | */ |
jlind6 | 0:356124c0bafc | 26 | |
rishibhargava1 | 3:591086e44bf9 | 27 | // Global state machine variable (So that the select can modify it) |
jlind6 | 0:356124c0bafc | 28 | gameStateType gameState = START; |
rishibhargava1 | 3:591086e44bf9 | 29 | bool ready = false; |
jlind6 | 0:356124c0bafc | 30 | |
Mpmart08 | 4:7da18e3c590b | 31 | Paddle botPaddle(0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 32 | Paddle topPaddle(0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 33 | Ball ball(0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 34 | |
Mpmart08 | 4:7da18e3c590b | 35 | // thread that plays game sounds through the speaker |
Mpmart08 | 4:7da18e3c590b | 36 | void speaker_thread(void const *argument) { |
Mpmart08 | 4:7da18e3c590b | 37 | |
Mpmart08 | 4:7da18e3c590b | 38 | Speaker *player = &mySpeaker; |
Mpmart08 | 4:7da18e3c590b | 39 | |
Mpmart08 | 4:7da18e3c590b | 40 | // Start Song |
Mpmart08 | 4:7da18e3c590b | 41 | float sFreq[] = {550,750,550,750}; |
Mpmart08 | 4:7da18e3c590b | 42 | float sDur[] = {.3,.3,.3,.3}; |
Mpmart08 | 4:7da18e3c590b | 43 | float sVol[] = {.01,.01,.01,.01}; |
Mpmart08 | 4:7da18e3c590b | 44 | SoundBuilder startSong(sFreq, sDur, sVol, sizeof(sFreq)/sizeof(*sFreq), player); |
Mpmart08 | 4:7da18e3c590b | 45 | |
Mpmart08 | 4:7da18e3c590b | 46 | // End Song |
Mpmart08 | 4:7da18e3c590b | 47 | float eFreq[] = {300,200,250,225,200,150,150,100}; |
Mpmart08 | 4:7da18e3c590b | 48 | float eDur[] = {.3,.3,.3,.3,.3,.3,.3,.3}; |
Mpmart08 | 4:7da18e3c590b | 49 | float eVol[] = {.01,.01,.01,.01,.01,.01,.01,.01}; |
Mpmart08 | 4:7da18e3c590b | 50 | SoundBuilder endSong(eFreq, eDur, eVol, sizeof(eFreq)/sizeof(*eFreq), player); |
Mpmart08 | 4:7da18e3c590b | 51 | |
Mpmart08 | 4:7da18e3c590b | 52 | while (true) { |
Mpmart08 | 4:7da18e3c590b | 53 | switch (gameState) { |
Mpmart08 | 4:7da18e3c590b | 54 | case GAME: // if game is running and user dodges a pipe, play a note |
Mpmart08 | 4:7da18e3c590b | 55 | if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), ball.getSize()) |
Mpmart08 | 4:7da18e3c590b | 56 | || botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), ball.getSize()) |
Mpmart08 | 4:7da18e3c590b | 57 | || ball.getFutureX() <= 10 |
Mpmart08 | 4:7da18e3c590b | 58 | || ball.getFutureX() + ball.getSize() >= 190) { |
Mpmart08 | 4:7da18e3c590b | 59 | mySpeaker.PlayNote(440, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 60 | } |
Mpmart08 | 4:7da18e3c590b | 61 | |
Mpmart08 | 4:7da18e3c590b | 62 | if (ball.getY() < 5){ |
Mpmart08 | 4:7da18e3c590b | 63 | mySpeaker.PlayNote(440, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 64 | mySpeaker.PlayNote(880, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 65 | } |
Mpmart08 | 4:7da18e3c590b | 66 | else if (ball.getY() + ball.getSize() > 245){ |
Mpmart08 | 4:7da18e3c590b | 67 | mySpeaker.PlayNote(880, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 68 | mySpeaker.PlayNote(440, 0.1, 0.01); |
Mpmart08 | 4:7da18e3c590b | 69 | } |
Mpmart08 | 4:7da18e3c590b | 70 | |
Mpmart08 | 4:7da18e3c590b | 71 | break; |
Mpmart08 | 4:7da18e3c590b | 72 | case START: // play a song at the start of the game |
Mpmart08 | 4:7da18e3c590b | 73 | startSong.playSong(); |
Mpmart08 | 4:7da18e3c590b | 74 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 75 | break; |
Mpmart08 | 4:7da18e3c590b | 76 | case WIN: // play a song when the player wins the game |
Mpmart08 | 4:7da18e3c590b | 77 | case LOSE: // play a song when the player loses the game |
Mpmart08 | 4:7da18e3c590b | 78 | endSong.playSong(); |
Mpmart08 | 4:7da18e3c590b | 79 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 80 | break; |
Mpmart08 | 4:7da18e3c590b | 81 | } |
jlind6 | 0:356124c0bafc | 82 | } |
jlind6 | 0:356124c0bafc | 83 | } |
jlind6 | 0:356124c0bafc | 84 | |
jlind6 | 0:356124c0bafc | 85 | int main() |
jlind6 | 0:356124c0bafc | 86 | { |
rishibhargava1 | 3:591086e44bf9 | 87 | // This is setting up the joystick select as a pushbutton |
Mpmart08 | 4:7da18e3c590b | 88 | //joystick.set_callback(&select_hit_callback); |
jlind6 | 0:356124c0bafc | 89 | |
Mpmart08 | 4:7da18e3c590b | 90 | pc.format(8, SerialBase::None, 1); |
Mpmart08 | 4:7da18e3c590b | 91 | pc.baud(115200); |
rishibhargava1 | 3:591086e44bf9 | 92 | |
rishibhargava1 | 3:591086e44bf9 | 93 | uint8_t gameLowX = 10, gameHighX = 190, gameLowY = 5, gameHighY = 245; |
rishibhargava1 | 3:591086e44bf9 | 94 | uint8_t gameCenterX = (gameHighX-gameLowX)/2, gameCenterY = (gameHighY-gameLowY)/2; |
rishibhargava1 | 3:591086e44bf9 | 95 | uint8_t ballSize=10; |
rishibhargava1 | 3:591086e44bf9 | 96 | uint8_t paddleWidth = 5, paddleLength = 35; |
Mpmart08 | 4:7da18e3c590b | 97 | float botMove = joystick.yAxis(); |
Mpmart08 | 4:7da18e3c590b | 98 | uint8_t botScore = 0; |
Mpmart08 | 4:7da18e3c590b | 99 | uint8_t topScore = 0; |
rishibhargava1 | 3:591086e44bf9 | 100 | int i = 0; |
jlind6 | 0:356124c0bafc | 101 | |
Mpmart08 | 4:7da18e3c590b | 102 | botPaddle = Paddle(gameCenterX-(paddleLength/2), gameHighY, paddleLength, paddleWidth); |
rishibhargava1 | 3:591086e44bf9 | 103 | botPaddle.setLimits(gameLowX, gameHighX); |
rishibhargava1 | 3:591086e44bf9 | 104 | botPaddle.setMaxMove(2); |
Mpmart08 | 4:7da18e3c590b | 105 | topPaddle = Paddle(gameCenterX-(paddleLength/2), gameLowY, paddleLength, paddleWidth); |
rishibhargava1 | 3:591086e44bf9 | 106 | topPaddle.setLimits(gameLowX, gameHighX); |
rishibhargava1 | 3:591086e44bf9 | 107 | topPaddle.setMaxMove(2); |
Mpmart08 | 4:7da18e3c590b | 108 | ball = Ball(gameCenterX, gameCenterY, ballSize); |
rishibhargava1 | 3:591086e44bf9 | 109 | |
Mpmart08 | 4:7da18e3c590b | 110 | ball.setVxDir(true); |
rishibhargava1 | 3:591086e44bf9 | 111 | ball.setVyDir(true); |
rishibhargava1 | 3:591086e44bf9 | 112 | |
Mpmart08 | 4:7da18e3c590b | 113 | while (!pc.writeable()){ |
rishibhargava1 | 3:591086e44bf9 | 114 | |
rishibhargava1 | 3:591086e44bf9 | 115 | } |
Mpmart08 | 4:7da18e3c590b | 116 | |
Mpmart08 | 4:7da18e3c590b | 117 | Thread thread1(speaker_thread); |
Mpmart08 | 4:7da18e3c590b | 118 | |
Mpmart08 | 4:7da18e3c590b | 119 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 120 | |
jlind6 | 0:356124c0bafc | 121 | while (1) |
jlind6 | 0:356124c0bafc | 122 | { |
jlind6 | 0:356124c0bafc | 123 | switch (gameState) |
jlind6 | 0:356124c0bafc | 124 | { |
jlind6 | 0:356124c0bafc | 125 | case START: |
Mpmart08 | 4:7da18e3c590b | 126 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 127 | pc.printf("%c%c%c%c%c", 0, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 128 | ball.setVxDir(true); |
Mpmart08 | 4:7da18e3c590b | 129 | ball.setVyDir(true); |
Mpmart08 | 4:7da18e3c590b | 130 | gameState = WAIT; |
Mpmart08 | 4:7da18e3c590b | 131 | } |
jlind6 | 0:356124c0bafc | 132 | break; |
jlind6 | 0:356124c0bafc | 133 | case GAME_SETUP: |
Mpmart08 | 4:7da18e3c590b | 134 | ball.reset(gameCenterX, gameCenterY, 0, sqrt(5.0)); |
rishibhargava1 | 3:591086e44bf9 | 135 | botPaddle.reset(gameCenterX-(paddleLength/2), gameHighY); |
rishibhargava1 | 3:591086e44bf9 | 136 | topPaddle.reset(gameCenterX-(paddleLength/2), gameLowY); |
Mpmart08 | 4:7da18e3c590b | 137 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 138 | pc.printf("%c%c%c%c%c", 3, botScore, topScore, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 139 | ready = false; |
Mpmart08 | 4:7da18e3c590b | 140 | srand(i); |
Mpmart08 | 4:7da18e3c590b | 141 | gameState = GAME; |
Mpmart08 | 4:7da18e3c590b | 142 | Thread::wait(2000); |
Mpmart08 | 4:7da18e3c590b | 143 | } |
jlind6 | 0:356124c0bafc | 144 | break; |
jlind6 | 0:356124c0bafc | 145 | case GAME: |
Mpmart08 | 4:7da18e3c590b | 146 | if (pc.writeable()) { |
Mpmart08 | 4:7da18e3c590b | 147 | pc.printf("%c%c%c%c%c", 4, botPaddle.getX(), topPaddle.getX(), ball.getX(), ball.getY()); |
Mpmart08 | 4:7da18e3c590b | 148 | |
Mpmart08 | 4:7da18e3c590b | 149 | uint8_t size = ball.getSize(); //stored in a temp variable because used a lot |
Mpmart08 | 4:7da18e3c590b | 150 | |
Mpmart08 | 4:7da18e3c590b | 151 | if (ball.getFutureX() <= gameLowX){ |
Mpmart08 | 4:7da18e3c590b | 152 | ball.reverseXDirection(); |
Mpmart08 | 4:7da18e3c590b | 153 | } |
Mpmart08 | 4:7da18e3c590b | 154 | else if (ball.getFutureX() + size >= gameHighX){ |
Mpmart08 | 4:7da18e3c590b | 155 | ball.reverseXDirection(); |
Mpmart08 | 4:7da18e3c590b | 156 | } |
Mpmart08 | 4:7da18e3c590b | 157 | |
Mpmart08 | 4:7da18e3c590b | 158 | if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)) { |
Mpmart08 | 4:7da18e3c590b | 159 | ball.reverseYDirection(); |
Mpmart08 | 4:7da18e3c590b | 160 | uint8_t fx = ball.getFutureX(); |
Mpmart08 | 4:7da18e3c590b | 161 | double newVx = topPaddle.returnAngle(fx, size); |
Mpmart08 | 4:7da18e3c590b | 162 | double newVy = sqrt(5.0 - (newVx * newVx)); |
Mpmart08 | 4:7da18e3c590b | 163 | ball.setVx(newVx); |
Mpmart08 | 4:7da18e3c590b | 164 | ball.setVy(newVy); |
Mpmart08 | 4:7da18e3c590b | 165 | ball.setVxDir(topPaddle.returnDir(fx, size)); |
Mpmart08 | 4:7da18e3c590b | 166 | } |
Mpmart08 | 4:7da18e3c590b | 167 | else if (botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)) { |
Mpmart08 | 4:7da18e3c590b | 168 | ball.reverseYDirection(); |
Mpmart08 | 4:7da18e3c590b | 169 | uint8_t fx = ball.getFutureX(); |
Mpmart08 | 4:7da18e3c590b | 170 | double newVx = botPaddle.returnAngle(fx, size); |
Mpmart08 | 4:7da18e3c590b | 171 | double newVy = sqrt(5.0 - (newVx * newVx)); |
Mpmart08 | 4:7da18e3c590b | 172 | ball.setVx(newVx); |
Mpmart08 | 4:7da18e3c590b | 173 | ball.setVy(newVy); |
Mpmart08 | 4:7da18e3c590b | 174 | ball.setVx(botPaddle.returnAngle(fx, size)); |
Mpmart08 | 4:7da18e3c590b | 175 | ball.setVxDir(botPaddle.returnDir(fx, size)); |
Mpmart08 | 4:7da18e3c590b | 176 | } |
Mpmart08 | 4:7da18e3c590b | 177 | |
Mpmart08 | 4:7da18e3c590b | 178 | if (ball.getY() < gameLowY){ |
Mpmart08 | 4:7da18e3c590b | 179 | botScore++; |
Mpmart08 | 4:7da18e3c590b | 180 | gameState = GAME_SETUP; |
Mpmart08 | 4:7da18e3c590b | 181 | } |
Mpmart08 | 4:7da18e3c590b | 182 | else if (ball.getY() + size > gameHighY){ |
Mpmart08 | 4:7da18e3c590b | 183 | topScore++; |
Mpmart08 | 4:7da18e3c590b | 184 | gameState = GAME_SETUP; |
Mpmart08 | 4:7da18e3c590b | 185 | } |
Mpmart08 | 4:7da18e3c590b | 186 | |
Mpmart08 | 4:7da18e3c590b | 187 | if (botScore >= 5) { |
Mpmart08 | 4:7da18e3c590b | 188 | gameState = WIN; |
Mpmart08 | 4:7da18e3c590b | 189 | } |
Mpmart08 | 4:7da18e3c590b | 190 | else if (topScore >= 5) { |
Mpmart08 | 4:7da18e3c590b | 191 | gameState = LOSE; |
Mpmart08 | 4:7da18e3c590b | 192 | } |
Mpmart08 | 4:7da18e3c590b | 193 | |
Mpmart08 | 4:7da18e3c590b | 194 | ball.update(); |
Mpmart08 | 4:7da18e3c590b | 195 | botMove = -joystick.yAxis(); |
Mpmart08 | 4:7da18e3c590b | 196 | if (botMove < -0.25 || botMove > 0.25) { |
Mpmart08 | 4:7da18e3c590b | 197 | botPaddle.move(botMove); |
Mpmart08 | 4:7da18e3c590b | 198 | } |
Mpmart08 | 4:7da18e3c590b | 199 | //GET OTHER PADDLE SPOT AND UPDATE topPaddle |
jlind6 | 0:356124c0bafc | 200 | } |
jlind6 | 0:356124c0bafc | 201 | break; |
jlind6 | 0:356124c0bafc | 202 | case LOSE: |
Mpmart08 | 4:7da18e3c590b | 203 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 204 | pc.printf("%c%c%c%c%c", 5, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 205 | botScore = 0; |
Mpmart08 | 4:7da18e3c590b | 206 | topScore = 0; |
Mpmart08 | 4:7da18e3c590b | 207 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 208 | gameState = START; |
Mpmart08 | 4:7da18e3c590b | 209 | } |
rishibhargava1 | 3:591086e44bf9 | 210 | break; |
rishibhargava1 | 3:591086e44bf9 | 211 | case WIN: |
Mpmart08 | 4:7da18e3c590b | 212 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 213 | pc.printf("%c%c%c%c%c", 6, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 214 | botScore = 0; |
Mpmart08 | 4:7da18e3c590b | 215 | topScore = 0; |
Mpmart08 | 4:7da18e3c590b | 216 | Thread::wait(5000); |
Mpmart08 | 4:7da18e3c590b | 217 | gameState = START; |
Mpmart08 | 4:7da18e3c590b | 218 | } |
jlind6 | 0:356124c0bafc | 219 | break; |
jlind6 | 0:356124c0bafc | 220 | case WAIT: |
Mpmart08 | 4:7da18e3c590b | 221 | if (pc.writeable()){ |
Mpmart08 | 4:7da18e3c590b | 222 | // Used to seed the rand() function so the ball has a random starting direction |
Mpmart08 | 4:7da18e3c590b | 223 | i++; |
Mpmart08 | 4:7da18e3c590b | 224 | if (joystick.button()) |
Mpmart08 | 4:7da18e3c590b | 225 | ready = true; |
Mpmart08 | 4:7da18e3c590b | 226 | if (ready){ |
Mpmart08 | 4:7da18e3c590b | 227 | pc.printf("%c%c%c%c%c", 2, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 228 | Thread::wait(2000); |
Mpmart08 | 4:7da18e3c590b | 229 | gameState = GAME_SETUP; |
Mpmart08 | 4:7da18e3c590b | 230 | } |
Mpmart08 | 4:7da18e3c590b | 231 | else { |
Mpmart08 | 4:7da18e3c590b | 232 | pc.printf("%c%c%c%c%c", 1, 0, 0, 0, 0); |
Mpmart08 | 4:7da18e3c590b | 233 | } |
rishibhargava1 | 3:591086e44bf9 | 234 | } |
jlind6 | 0:356124c0bafc | 235 | break; |
jlind6 | 0:356124c0bafc | 236 | } |
Mpmart08 | 4:7da18e3c590b | 237 | Thread::wait(20); |
jlind6 | 0:356124c0bafc | 238 | } |
jlind6 | 0:356124c0bafc | 239 | } |