multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
Revision 36:46bb54b669bc, committed 2020-11-28
- Comitter:
- vsoltan
- Date:
- Sat Nov 28 04:21:08 2020 +0000
- Parent:
- 35:2c5e582a6ee2
- Child:
- 37:8a0fc62a0512
- Commit message:
- starting to refactor
Changed in this revision
--- a/gamestate.cpp Sat Nov 28 03:52:53 2020 +0000 +++ b/gamestate.cpp Sat Nov 28 04:21:08 2020 +0000 @@ -114,6 +114,13 @@ return this->score[1]; } +int GameState::getPlayerScore(int player) { + if (player == 0) { + return getPlayerOneScore(); + } + return getPlayerTwoScore(); +} + char GameState::hasStarted() { return this->has_started; }
--- a/gamestate.h Sat Nov 28 03:52:53 2020 +0000 +++ b/gamestate.h Sat Nov 28 04:21:08 2020 +0000 @@ -40,6 +40,7 @@ void setCountdown(int val); int getPlayerOneScore(); int getPlayerTwoScore(); + int getPlayerScore(int player); void updateAndRender(MbedJSONValue *serverResponse, Graphics *gfx); char done(); };
--- a/graphics.cpp Sat Nov 28 03:52:53 2020 +0000 +++ b/graphics.cpp Sat Nov 28 04:21:08 2020 +0000 @@ -97,13 +97,17 @@ bool winner = gs->getPlayerOneScore() > gs->getPlayerTwoScore() ? 0 : 1; if (winner == gs->getLocalPlayerNum()) { - tft->printf("You won :)"); + tft->printf("You won, %i:%i", + gs->getPlayerScore(winner), gs->getPlayerScore(1 - winner)); } else { - printf("You lost :("); + tft->printf("You lost, %i:%i", + gs->getPlayerScore(1 - winner), gs->getPlayerScore(winner)); } tft->setTextCursor(15, 60); - tft->printf("Press any button to play again!"); + tft->printf("Press any button"); + tft->setTextCursor(15, 70); + tft->printf("to play again!"); } void Graphics::renderCountdown(GameState *gs) {
--- a/main.cpp Sat Nov 28 03:52:53 2020 +0000 +++ b/main.cpp Sat Nov 28 04:21:08 2020 +0000 @@ -1,9 +1,10 @@ #include "mbed.h" -#include "network.h" +#include "DebouncedInterrupt.h" + #include "gamestate.h" #include "graphics.h" -#include "DebouncedInterrupt.h" +#include "network.h" #define DEBOUNCE 25 @@ -16,10 +17,8 @@ Endpoint nist; volatile int sendFlag = 0; -volatile int moveData = 0; -volatile int menuPress = 0; - -int numGamesPlayed = 0; +volatile int moveDelta = 0; +volatile int menuPress = 0; // interrupts service routines @@ -29,12 +28,12 @@ void pressLeftGame( void ) { sendFlag = 1; - moveData -= 5; + moveDelta -= 5; } void pressRightGame() { sendFlag = 1; - moveData += 5; + moveDelta += 5; } int main() { @@ -49,11 +48,10 @@ MbedJSONValue serverResponse; MbedJSONValue serverRequest; - char connectionRequest[] = "{\"type\": \"connected\"}"; char tmp_buffer[256]; string serverRequestContent = ""; - int requestLength = 0; + int serverRequestLength = 0; char *serverRequestPayload = NULL; gfx->renderLaunchScreen(); @@ -61,7 +59,6 @@ while (1) { // keep program running if (menuPress) { GameState *gs = new GameState(); - numGamesPlayed++; // change ISRs to game controls leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE); @@ -69,45 +66,47 @@ rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE); // request an open lobby from the server - sock.sendTo(nist, connectionRequest, strlen(connectionRequest)); - - printf("hello\n\r"); + sock.sendTo(nist, CONNECTION_REQ, strlen(CONNECTION_REQ)); + + // play an individual game to completion while (!gs->done()) { - if (sendFlag != 0) { + if (sendFlag) { serverRequest["type"] = "move"; - serverRequest["hash"] = "jaredyeagersflipflop"; + serverRequest["hash"] = LOBBY_HASH; serverRequest["player"] = gs->getLocalPlayerNum(); - serverRequest["delta"] = (int)moveData; + serverRequest["delta"] = (int)moveDelta; + // prepare and send JSON payload serverRequestContent = serverRequest.serialize(); - requestLength = serverRequestContent.size(); + serverRequestLength = serverRequestContent.size(); serverRequestPayload = (char *)serverRequestContent.c_str(); + sock.sendTo(nist, serverRequestPayload, serverRequestLength); + + // reset sendFlag = 0; - moveData = 0; - sock.sendTo(nist, serverRequestPayload, requestLength); + moveDelta = 0; } int bytesRead = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer)); tmp_buffer[bytesRead] = 0; - if (numGamesPlayed == 2) { - printf("read: %s\n\r", tmp_buffer); - } + + // populate serverResponse JSON and update local gamestate if (bytesRead > 0) { parse(serverResponse, tmp_buffer); gs->updateAndRender(&serverResponse, gfx); } wait(.1); } + // once game ends, leave lobby and disconnect MbedJSONValue disconnectRequest; - disconnectRequest["hash"] = "jaredyeagersflipflop"; + disconnectRequest["hash"] = LOBBY_HASH; disconnectRequest["type"] = "disconnect"; string disconnectRequestContent = disconnectRequest.serialize(); int len = disconnectRequestContent.size(); char *disconnectRequestPayload = (char *)disconnectRequestContent.c_str(); - sock.sendTo(nist, disconnectRequestPayload, len); - // cleanup the socket from the previous game + // flush the socket from the previous game int flushBytes = 0; while ((flushBytes = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer))) != 0) { printf("reading bytes: %i\n\r", flushBytes); @@ -116,7 +115,7 @@ gfx->renderGameOver(gs); - // reset the game + // reset leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
--- a/network.h Sat Nov 28 03:52:53 2020 +0000 +++ b/network.h Sat Nov 28 04:21:08 2020 +0000 @@ -8,6 +8,9 @@ #define AWS_IP "3.128.153.185" #define PORT 3030 +#define CONNECTION_REQ "{\"type\": \"connected\"}" +#define LOBBY_HASH "jaredyeagersflipflop" + void initEthernet(EthernetInterface *eth, UDPSocket *sock, Endpoint *nist); void cleanupEthernet(EthernetInterface *eth, UDPSocket *sock);