multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
Revision 23:c38680c32552, committed 2020-11-16
- Comitter:
- vsoltan
- Date:
- Mon Nov 16 03:01:50 2020 +0000
- Parent:
- 22:1c49e1fae846
- Child:
- 24:05eb0b0ab554
- Commit message:
- moving paddles work in progress
Changed in this revision
--- a/gamestate.cpp Mon Nov 16 02:04:23 2020 +0000 +++ b/gamestate.cpp Mon Nov 16 03:01:50 2020 +0000 @@ -6,7 +6,6 @@ Coord playerTwoLocation = {0, 0}; Coord ballLocation = {0, 0}; this->localPlayerNum = 0; - this->lobbyHash = ""; this->p1_loc = playerOneLocation; this->p2_loc = playerTwoLocation; this->ball_loc = ballLocation; @@ -30,13 +29,17 @@ if (typeResponse == "connected") { gfx->renderWaitingRoom(); } else if (typeResponse == "gameState") { - gfx->removeGameState(this); + if (!has_started) { + has_started = 1; + gfx->reset(); + } + gfx->eraseGameState(this); MbedJSONValue &serverGameState = (*serverResponse)["gameState"]; if (serverGameState.hasMember("playerOnePos")) { - this->p1_loc.y = (serverGameState)["playerOnePos"].get<int>(); + this->p1_loc.x = (serverGameState)["playerOnePos"].get<int>(); } if (serverGameState.hasMember("playerTwoPos")) { - this->p2_loc.y = (serverGameState)["playerTwoPos"].get<int>(); + this->p2_loc.x = (serverGameState)["playerTwoPos"].get<int>(); } if (serverGameState.hasMember("ballPos")) { @@ -54,8 +57,11 @@ this->localPlayerNum = (char)(serverGameState)["player"].get<int>(); } + // TODO: check if hash has already been set if (serverGameState.hasMember("hash")) { - this->lobbyHash = (serverGameState)["hash"].get<std::string>(); + strcpy(this->lobbyHash, + (serverGameState)["hash"].get<std::string>().c_str()); + this->lobbyHash[21] = 0; } gfx->renderGameState(this); } @@ -68,6 +74,10 @@ return getPlayerTwoLocation(); } +char *GameState::getLobbyHash() { + return this->lobbyHash; +} + char GameState::getLocalPlayerNum() { return this->localPlayerNum; }
--- a/gamestate.h Mon Nov 16 02:04:23 2020 +0000 +++ b/gamestate.h Mon Nov 16 03:01:50 2020 +0000 @@ -16,19 +16,22 @@ class Graphics; class GameState { + // TODO: make the syntax consistent please private: Coord p1_loc; Coord p2_loc; Coord ball_loc; char is_done; + char has_started; char localPlayerNum; - string lobbyHash; + char lobbyHash[21]; public: GameState(); Coord getPlayerLocation(char player); Coord getPlayerOneLocation(); Coord getPlayerTwoLocation(); Coord getBallLocation(); + char *getLobbyHash(); char getLocalPlayerNum(); void update(MbedJSONValue *serverResponse, Graphics *gfx); bool done();
--- a/graphics.cpp Mon Nov 16 02:04:23 2020 +0000 +++ b/graphics.cpp Mon Nov 16 03:01:50 2020 +0000 @@ -30,22 +30,51 @@ tft->drawPixel(ball_loc.x, ball_loc.y, ST7735_WHITE); } -void Graphics::removeBall(GameState *gs) { +void Graphics::eraseBall(GameState *gs) { Coord ball_loc = gs->getBallLocation(); tft->drawPixel(ball_loc.x, ball_loc.y, BACKGROUND_COLOR); } void Graphics::renderPlayers(GameState *gs) { - int8_t bottomPaddleRenderPos = gs->getPlayerLocation(gs->getLocalPlayerNum()).y; - int8_t topPaddleRenderPos = 127 - bottomPaddleRenderPos; + int otherPlayerNum = (gs->getLocalPlayerNum() == 0) ? 1 : 0; + + int8_t bottomPaddleRenderPos = gs->getPlayerLocation(gs->getLocalPlayerNum()).x; + int8_t topPaddleRenderPos = gs->getPlayerLocation(otherPlayerNum).x; + + // draw bottom paddle + tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2, + ELEVATION, PADDLE_WIDTH, PADDLE_COLOR); + + // draw top paddle + tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2, + 127 - ELEVATION, PADDLE_WIDTH, PADDLE_COLOR); } -void Graphics::removeGameState(GameState *gs) { - removeBall(gs); - //removePlayers(gs); +void Graphics::erasePlayers(GameState *gs) { + int otherPlayerNum = (gs->getLocalPlayerNum() == 0) ? 1 : 0; + + int8_t bottomPaddleRenderPos = gs->getPlayerLocation(gs->getLocalPlayerNum()).x; + int8_t topPaddleRenderPos = gs->getPlayerLocation(otherPlayerNum).x; + + // draw bottom paddle + tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2, + ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR); + + // draw top paddle + tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2, + 127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR); } void Graphics::renderGameState(GameState *gs) { renderBall(gs); -// renderPlayers(gs); + renderPlayers(gs); +} + +void Graphics::eraseGameState(GameState *gs) { + eraseBall(gs); + erasePlayers(gs); +} + +void Graphics::reset() { + tft->fillScreen(BACKGROUND_COLOR); } \ No newline at end of file
--- a/graphics.h Mon Nov 16 02:04:23 2020 +0000 +++ b/graphics.h Mon Nov 16 03:01:50 2020 +0000 @@ -16,6 +16,12 @@ #define LAUNCH_SCREEN_COLOR ST7735_BLACK #define WAITING_SCREEN_COLOR ST7735_BLACK +#define PADDLE_COLOR ST7735_GREEN +#define BALL_COLOR ST7735_RED + +#define PADDLE_WIDTH 30 +#define ELEVATION 5 + class GameState; class Graphics { @@ -28,9 +34,10 @@ void renderGameState(GameState *gs); void renderBall(GameState *gs); void renderPlayers(GameState *gs); - void removeBall(GameState *gs); - void removePlayers(GameState *gs); - void removeGameState(GameState *gs); + void eraseBall(GameState *gs); + void erasePlayers(GameState *gs); + void eraseGameState(GameState *gs); + void reset(); }; #endif // GRAPHICS_H \ No newline at end of file
--- a/main.cpp Mon Nov 16 02:04:23 2020 +0000 +++ b/main.cpp Mon Nov 16 03:01:50 2020 +0000 @@ -50,7 +50,8 @@ MbedJSONValue serverResponse; char connectionRequest[] = "{\"type\": \"connected\"}"; - char toSend[] = "{\"type\": \"move\", \"delta\": 5}"; + char moveFormat[] = "\"type\": \"move\", \"hash\": %s, \"player\": %i, \"delta\": %i"; + char move[128]; char readTo[256]; gfx->renderLaunchScreen(); @@ -68,10 +69,12 @@ while (!gs->done()) { if (sendFlag != 0) { + sprintf(move, moveFormat, gs->getLobbyHash(), + gs->getLocalPlayerNum(), moveData); printf("Move value: %i\n\r", moveData); sendFlag = 0; moveData = 0; - sock.sendTo(nist, toSend, sizeof(toSend) - 1); + sock.sendTo(nist, move, sizeof(move) - 1); } int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); readTo[bytesRead] = 0;