
player 1
Dependencies: 4DGL-uLCD-SE PinDetect SparkfunAnalogJoystick mbed-rtos mbed SDFileSystem
Fork of 4180FinalLab by
Wireless 2 Player Pong game
Revision 8:8cc2aa78348c, committed 2016-04-28
- Comitter:
- Mpmart08
- Date:
- Thu Apr 28 19:58:48 2016 +0000
- Parent:
- 7:cadf69604b45
- Child:
- 9:aa967c554d10
- Commit message:
- stuff
Changed in this revision
--- a/ball.cpp Wed Apr 27 15:23:41 2016 +0000 +++ b/ball.cpp Thu Apr 28 19:58:48 2016 +0000 @@ -6,7 +6,7 @@ diameter = size; } -void Ball::setVx(double newvx){ +void Ball::setVx(uint8_t newvx){ vx = newvx; } @@ -14,7 +14,7 @@ vxDir = dir; } -void Ball::setVy(double newVy){ +void Ball::setVy(uint8_t newVy){ vy = newVy; }
--- a/ball.h Wed Apr 27 15:23:41 2016 +0000 +++ b/ball.h Thu Apr 28 19:58:48 2016 +0000 @@ -5,9 +5,9 @@ public: Ball (uint8_t, uint8_t, uint8_t); // Set Functions - void setVx(double); // This sets the velocity + void setVx(uint8_t); // This sets the velocity void setVxDir(bool); - void setVy(double); // This sets the velocity + void setVy(uint8_t); // This sets the velocity void setVyDir(bool); // Get Functions uint8_t getSize(); @@ -22,11 +22,11 @@ void update(); // moves the ball on the screen one vx and vy private: - double vx; + uint8_t vx; bool vxDir; //false is left (-x), true is right (+x) - double vy; + uint8_t vy; bool vyDir; //false is up (-y), true is down (+y) - double x; - double y; + uint8_t x; + uint8_t y; uint8_t diameter; };
--- 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);
--- a/paddle.cpp Wed Apr 27 15:23:41 2016 +0000 +++ b/paddle.cpp Thu Apr 28 19:58:48 2016 +0000 @@ -51,8 +51,16 @@ return false; } -double Paddle::returnAngle(double ballX, double size){ - return abs((ballX + size/2) - (x + length/2)) / 35.0 * sqrt(5.0); +uint8_t Paddle::returnAngle(uint8_t ballX, uint8_t size){ + uint8_t dist = abs((ballX + size/2) - (x + length/2)); + if (dist < 5) + return 0; + else if (dist < 10) + return 1; + else if (dist < 15) + return 2; + else + return 3; } bool Paddle::returnDir(uint8_t ballX, uint8_t size){
--- a/paddle.h Wed Apr 27 15:23:41 2016 +0000 +++ b/paddle.h Thu Apr 28 19:58:48 2016 +0000 @@ -14,7 +14,7 @@ // Member Functions void move(float); // false means move left, true means move right bool checkHit(uint8_t, uint8_t, uint8_t); // Using a position and size, checks to see if something has hit the paddle - double returnAngle(double, double); // Using a ball x position, gives a return vx and vxDir + uint8_t returnAngle(uint8_t, uint8_t); // Using a ball x position, gives a return vx and vxDir bool returnDir(uint8_t, uint8_t); void reset(uint8_t, uint8_t);