multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
Revision 30:59e9a5409e65, committed 2020-11-28
- Comitter:
- vsoltan
- Date:
- Sat Nov 28 02:48:31 2020 +0000
- Parent:
- 29:4708bfb863cb
- Child:
- 31:f70cf03c8ef9
- Commit message:
- potential fix to replay-ability issue
Changed in this revision
--- a/gamestate.cpp Sat Nov 28 01:05:42 2020 +0000 +++ b/gamestate.cpp Sat Nov 28 02:48:31 2020 +0000 @@ -10,6 +10,7 @@ this->p2_loc = playerTwoLocation; this->ball_loc = ballLocation; this->has_started = 0; + this->connected = 0; this->countdown = 0; this->is_done = 0; this->score[0] = 0; @@ -31,6 +32,7 @@ void GameState::updateAndRender(MbedJSONValue *serverResponse, Graphics *gfx) { string typeResponse = (*serverResponse)["type"].get<std::string>(); if (typeResponse == "connected") { + connected = 1; if (serverResponse->hasMember("player")) { this->localPlayerNum = (char)(*serverResponse)["player"].get<int>(); } @@ -38,9 +40,11 @@ } else if (typeResponse == "gameState") { MbedJSONValue &serverGameState = (*serverResponse)["gameState"]; if (!has_started && serverGameState.hasMember("countdown")) { - int countdownValue = (serverGameState)["countdown"].get<int>(); + int countdownValue = (serverGameState)["countdown"].get<int>() / 1000; if (countdownValue > 0) { - countdown = countdownValue; + gfx->eraseCountdown(this); + setCountdown(countdownValue); + gfx->renderCountdown(this); return; } else { has_started = 1; @@ -93,9 +97,13 @@ char GameState::getLocalPlayerNum() { return this->localPlayerNum; -} +} -char GameState::getCountdown() { +char GameState::isConnected() { + return this->connected; +} + +int GameState::getCountdown() { return this->countdown; } @@ -111,6 +119,10 @@ return this->score[1]; } +char GameState::hasStarted() { + return this->has_started; +} + char GameState::done() { return this->is_done; }
--- a/gamestate.h Sat Nov 28 01:05:42 2020 +0000 +++ b/gamestate.h Sat Nov 28 02:48:31 2020 +0000 @@ -23,7 +23,8 @@ Coord ball_loc; char is_done; char has_started; - char countdown; + char connected; + int countdown; char localPlayerNum; char lobbyHash[21]; int score[2]; @@ -35,7 +36,9 @@ Coord getBallLocation(); char *getLobbyHash(); char getLocalPlayerNum(); - char getCountdown(); + int getCountdown(); + char hasStarted(); + char isConnected(); void setCountdown(int val); int getPlayerOneScore(); int getPlayerTwoScore();
--- a/main.cpp Sat Nov 28 01:05:42 2020 +0000 +++ b/main.cpp Sat Nov 28 02:48:31 2020 +0000 @@ -60,6 +60,8 @@ if (menuPress) { GameState *gs = new GameState(); + printf("isDone? %i\n\r", gs->done()); + // request an open lobby from the server sock.sendTo(nist, connectionRequest, strlen(connectionRequest)); @@ -68,7 +70,8 @@ middleButton.reset(); rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE); - while (!gs->done()) { + printf("isDone? %i\n\r", gs->done()); + while (!gs->isConnected() || !gs->done()) { if (sendFlag != 0) { serverRequest["type"] = "move"; serverRequest["hash"] = "jaredyeagersflipflop"; @@ -83,6 +86,7 @@ sock.sendTo(nist, serverRequestPayload, requestLength); } int bytesRead = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer)); + printf("read: %s\n\r", tmp_buffer); tmp_buffer[bytesRead] = 0; if (bytesRead > 0) { parse(serverResponse, tmp_buffer); @@ -101,6 +105,9 @@ sock.sendTo(nist, disconnectRequestPayload, len); gfx->renderGameOver(gs); + cleanupEthernet(ð, &sock); + initEthernet(ð, &sock, &nist); + // reset the game leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);