
player 1
Dependencies: 4DGL-uLCD-SE PinDetect SparkfunAnalogJoystick mbed-rtos mbed SDFileSystem
Fork of 4180FinalLab by
Wireless 2 Player Pong game
Diff: main.cpp
- Revision:
- 8:8cc2aa78348c
- Parent:
- 7:cadf69604b45
- Child:
- 9:aa967c554d10
diff -r cadf69604b45 -r 8cc2aa78348c main.cpp --- a/main.cpp Wed Apr 27 15:23:41 2016 +0000 +++ b/main.cpp Thu Apr 28 19:58:48 2016 +0000 @@ -33,6 +33,15 @@ Paddle topPaddle(0, 0, 0, 0); Ball ball(0, 0, 0); +uint8_t gameLowX = 10, gameHighX = 190, gameLowY = 5, gameHighY = 245, gameX = 200, gameY = 250; +uint8_t gameCenterX = gameX/2, gameCenterY = gameY/2; +uint8_t ballSize=10; +uint8_t paddleWidth = 5, paddleLength = 40; +float botMove = joystick.yAxis(); +uint8_t botScore = 0; +uint8_t topScore = 0; +int i = 0; + // thread that plays game sounds through the speaker void speaker_thread(void const *argument) { @@ -95,6 +104,35 @@ } } } + +void xbee_thread(void const *argument) { + + while (true) { + switch (gameState) { + case WAIT: + if (ready && xbee.writeable()) { + xbee.putc(1); + } + if (ready && xbee.readable()) { + char c = xbee.getc(); + if (c == 1) { + gameState = GAME_SETUP; + } + } + break; + case GAME: + if (xbee.writeable()) { + xbee.putc(botPaddle.getX()); + } + if (xbee.readable()) { + char c = xbee.getc(); + uint8_t top = 200 - paddleLength - ((uint8_t) c); + topPaddle.setX(top); + } + break; + } + } +} int main() { @@ -104,21 +142,12 @@ pc.format(8, SerialBase::None, 1); pc.baud(115200); - 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 = joystick.yAxis(); - uint8_t botScore = 0; - uint8_t topScore = 0; - int i = 0; - botPaddle = Paddle(gameCenterX-(paddleLength/2), gameHighY, paddleLength, paddleWidth); - botPaddle.setLimits(gameLowX, gameHighX - paddleWidth); - botPaddle.setMaxMove(2); + botPaddle.setLimits(gameLowX, gameHighX); + botPaddle.setMaxMove(3); topPaddle = Paddle(gameCenterX-(paddleLength/2), gameLowY, paddleLength, paddleWidth); - topPaddle.setLimits(gameLowX, gameHighX - paddleWidth); - topPaddle.setMaxMove(2); + topPaddle.setLimits(gameLowX, gameHighX); + topPaddle.setMaxMove(3); ball = Ball(gameCenterX, gameCenterY, ballSize); ball.setVxDir(true); @@ -130,6 +159,7 @@ Thread thread1(speaker_thread); Thread thread2(sd_card_thread); + Thread thread3(xbee_thread); while (1) { @@ -144,7 +174,7 @@ } break; case GAME_SETUP: - ball.reset(gameCenterX, gameCenterY, 0, sqrt(5.0)); + ball.reset(gameCenterX-(ballSize/2), gameCenterY-(ballSize/2), 0, 6); botPaddle.reset(gameCenterX-(paddleLength/2), gameHighY); topPaddle.reset(gameCenterX-(paddleLength/2), gameLowY); if (pc.writeable()){ @@ -171,8 +201,8 @@ if (topPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)) { ball.reverseYDirection(); uint8_t fx = ball.getFutureX(); - double newVx = topPaddle.returnAngle(fx, size); - double newVy = sqrt(5.0 - (newVx * newVx)); + uint8_t newVx = topPaddle.returnAngle(fx, size); + uint8_t newVy = 6 - newVx; ball.setVx(newVx); ball.setVy(newVy); ball.setVxDir(topPaddle.returnDir(fx, size)); @@ -180,20 +210,21 @@ else if (botPaddle.checkHit(ball.getFutureX(), ball.getFutureY(), size)) { ball.reverseYDirection(); uint8_t fx = ball.getFutureX(); - double newVx = botPaddle.returnAngle(fx, size); - double newVy = sqrt(5.0 - (newVx * newVx)); + uint8_t newVx = botPaddle.returnAngle(fx, size); + uint8_t newVy = 6 - newVx; ball.setVx(newVx); ball.setVy(newVy); - ball.setVx(botPaddle.returnAngle(fx, size)); ball.setVxDir(botPaddle.returnDir(fx, size)); } if (ball.getY() < gameLowY){ botScore++; + ball.setVyDir(false); gameState = GAME_SETUP; } else if (ball.getY() + size > gameHighY){ topScore++; + ball.setVyDir(true); gameState = GAME_SETUP; } @@ -206,10 +237,9 @@ ball.update(); botMove = -joystick.yAxis(); - if (botMove < -0.1 || botMove > 0.1) { + if ((botMove < -0.1 || botMove > 0.1) && ball.getY() < gameHighY - 50) { botPaddle.move(botMove); } - //GET OTHER PADDLE SPOT AND UPDATE topPaddle } break; case LOSE: @@ -238,8 +268,6 @@ ready = true; if (ready){ pc.printf("%c%c%c%c%c", 2, 0, 0, 0, 0); - Thread::wait(2000); - gameState = GAME_SETUP; } else { pc.printf("%c%c%c%c%c", 1, 0, 0, 0, 0);