multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
Revision 29:4708bfb863cb, committed 2020-11-28
- Comitter:
- vsoltan
- Date:
- Sat Nov 28 01:05:42 2020 +0000
- Parent:
- 28:a26a43cdaea8
- Child:
- 30:59e9a5409e65
- Commit message:
- polishing code and added functionality for game countdown
Changed in this revision
--- a/gamestate.cpp Mon Nov 23 01:26:24 2020 +0000 +++ b/gamestate.cpp Sat Nov 28 01:05:42 2020 +0000 @@ -10,6 +10,7 @@ this->p2_loc = playerTwoLocation; this->ball_loc = ballLocation; this->has_started = 0; + this->countdown = 0; this->is_done = 0; this->score[0] = 0; this->score[1] = 0; @@ -35,13 +36,20 @@ } gfx->renderWaitingRoom(); } else if (typeResponse == "gameState") { - if (!has_started) { - has_started = 1; - printf("reseting display\n\r"); - gfx->reset(); + MbedJSONValue &serverGameState = (*serverResponse)["gameState"]; + if (!has_started && serverGameState.hasMember("countdown")) { + int countdownValue = (serverGameState)["countdown"].get<int>(); + if (countdownValue > 0) { + countdown = countdownValue; + return; + } else { + has_started = 1; + printf("reseting display\n\r"); + gfx->reset(); + } } gfx->eraseGameState(this); - MbedJSONValue &serverGameState = (*serverResponse)["gameState"]; + if (serverGameState.hasMember("playerOnePos")) { this->p1_loc.x = (serverGameState)["playerOnePos"].get<int>(); } @@ -87,6 +95,14 @@ return this->localPlayerNum; } +char GameState::getCountdown() { + return this->countdown; +} + +void GameState::setCountdown(int val) { + this->countdown = val; +} + int GameState::getPlayerOneScore() { return this->score[0]; }
--- a/gamestate.h Mon Nov 23 01:26:24 2020 +0000 +++ b/gamestate.h Sat Nov 28 01:05:42 2020 +0000 @@ -23,6 +23,7 @@ Coord ball_loc; char is_done; char has_started; + char countdown; char localPlayerNum; char lobbyHash[21]; int score[2]; @@ -34,6 +35,8 @@ Coord getBallLocation(); char *getLobbyHash(); char getLocalPlayerNum(); + char getCountdown(); + void setCountdown(int val); int getPlayerOneScore(); int getPlayerTwoScore(); void updateAndRender(MbedJSONValue *serverResponse, Graphics *gfx);
--- a/graphics.cpp Mon Nov 23 01:26:24 2020 +0000 +++ b/graphics.cpp Sat Nov 28 01:05:42 2020 +0000 @@ -11,18 +11,20 @@ void Graphics::renderLaunchScreen() { tft->fillScreen(LAUNCH_SCREEN_COLOR); - tft->setTextCursor(15, 20); + tft->setTextCursor(18, 20); tft->printf("%s", "Multiplayer Pong"); - tft->setTextCursor(15, 80); + tft->setTextCursor(15, 60); tft->printf("%s", "press any button"); - tft->setTextCursor(15, 90); + tft->setTextCursor(15, 70); tft->printf("%s", "to continue"); } void Graphics::renderWaitingRoom() { tft->fillScreen(WAITING_SCREEN_COLOR); - tft->setTextCursor(5, 20); - tft->printf("%s", "Waiting For Player"); + tft->setTextCursor(18, 20); + tft->printf("%s", "Waiting For"); + tft->setTextCursor(18, 30); + tft->printf("%s", "Player..."); } void Graphics::renderBall(GameState *gs) { @@ -60,15 +62,23 @@ int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x; int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x; - // draw top paddle + // erase top paddle tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2, ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR); - // draw bottom paddle + // erase bottom paddle tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2, 127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR); } +void Graphics::renderScore(GameState *gs) { + tft->setTextColor(ST7735_WHITE); + tft->setTextCursor(120, 50); + tft->printf("%i", gs->getPlayerOneScore()); + tft->setTextCursor(120, 77); + tft->printf("%i", gs->getPlayerTwoScore()); +} + void Graphics::eraseScore(GameState *gs) { tft->setTextColor(BACKGROUND_COLOR); tft->setTextCursor(120, 50); @@ -77,17 +87,9 @@ tft->printf("%i", gs->getPlayerTwoScore()); } -void Graphics::renderScore(GameState *gs) { - tft->setTextColor(ST7735_WHITE); - tft->setTextCursor(120, 50); - tft->printf("%i", gs->getPlayerOneScore()); - tft->setTextCursor(120, 77); - tft->printf("%i", gs->getPlayerTwoScore()); -} - void Graphics::renderGameOver(GameState *gs) { reset(); - tft->setTextColor(ST7735_WHITE); + tft->setTextColor(DEFAULT_TEXT_COLOR); tft->setTextCursor(15, 20); tft->printf("Game Over"); tft->setTextCursor(15, 40); @@ -97,6 +99,18 @@ tft->printf("Press any button to play again!"); } +void Graphics::renderCountdown(GameState *gs) { + tft->setTextColor(DEFAULT_TEXT_COLOR); + tft->setTextCursor(63, 63); + tft->printf("%i", gs->getCountdown()); +} + +void Graphics::eraseCountdown(GameState *gs) { + tft->setTextColor(BACKGROUND_COLOR); + tft->setTextCursor(63, 63); + tft->printf("%i", gs->getCountdown()); +} + void Graphics::renderGameState(GameState *gs) { renderBall(gs); renderPlayers(gs);
--- a/graphics.h Mon Nov 23 01:26:24 2020 +0000 +++ b/graphics.h Sat Nov 28 01:05:42 2020 +0000 @@ -37,11 +37,13 @@ void renderBall(GameState *gs); void renderPlayers(GameState *gs); void renderScore(GameState *gs); - void renderGameOver(GameState *gs); + void renderGameOver(GameState *gs); + void renderCountdown(GameState *gs); void eraseBall(GameState *gs); void erasePlayers(GameState *gs); void eraseGameState(GameState *gs); void eraseScore(GameState *gs); + void eraseCountdown(GameState *gs); void reset(); };
--- a/main.cpp Mon Nov 23 01:26:24 2020 +0000 +++ b/main.cpp Sat Nov 28 01:05:42 2020 +0000 @@ -1,11 +1,9 @@ #include "mbed.h" -#include "EthernetInterface.h" #include "network.h" #include "gamestate.h" #include "graphics.h" #include "DebouncedInterrupt.h" -#include "MbedJSONValue.h" #define DEBOUNCE 25 @@ -17,7 +15,6 @@ UDPSocket sock; Endpoint nist; -volatile int count = 0; volatile int sendFlag = 0; volatile int moveData = 0; volatile int menuPress = 0; @@ -51,7 +48,11 @@ MbedJSONValue serverRequest; char connectionRequest[] = "{\"type\": \"connected\"}"; - char readTo[256]; + char tmp_buffer[256]; + + string serverRequestContent = ""; + int requestLength = 0; + char *serverRequestPayload = NULL; gfx->renderLaunchScreen(); @@ -60,7 +61,7 @@ GameState *gs = new GameState(); // request an open lobby from the server - sock.sendTo(nist, connectionRequest, sizeof(connectionRequest) - 1); + sock.sendTo(nist, connectionRequest, strlen(connectionRequest)); // change ISRs to game controls leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE); @@ -74,36 +75,37 @@ serverRequest["player"] = gs->getLocalPlayerNum(); serverRequest["delta"] = (int)moveData; - string requestContent = serverRequest.serialize(); - int len = requestContent.size(); - char *toSend = (char *)requestContent.c_str(); + serverRequestContent = serverRequest.serialize(); + requestLength = serverRequestContent.size(); + serverRequestPayload = (char *)serverRequestContent.c_str(); sendFlag = 0; moveData = 0; - sock.sendTo(nist, toSend, len); + sock.sendTo(nist, serverRequestPayload, requestLength); } - int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); - readTo[bytesRead] = 0; + int bytesRead = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer)); + tmp_buffer[bytesRead] = 0; if (bytesRead > 0) { - parse(serverResponse, readTo); + parse(serverResponse, tmp_buffer); gs->updateAndRender(&serverResponse, gfx); } wait(.1); } - MbedJSONValue dcReq; - dcReq["hash"] = "jaredyeagersflipflop"; - dcReq["type"] = "disconnect"; + MbedJSONValue disconnectRequest; + disconnectRequest["hash"] = "jaredyeagersflipflop"; + disconnectRequest["type"] = "disconnect"; - string dcReqContent = dcReq.serialize(); - int len = dcReqContent.size(); - char *dcPayload = (char *)dcReqContent.c_str(); + string disconnectRequestContent = disconnectRequest.serialize(); + int len = disconnectRequestContent.size(); + char *disconnectRequestPayload = (char *)disconnectRequestContent.c_str(); - sock.sendTo(nist, dcPayload, len); - gfx->renderGameOver(gs); + sock.sendTo(nist, disconnectRequestPayload, len); + gfx->renderGameOver(gs); + + // reset the game leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); - // reset the game menuPress = 0; delete gs; }