multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Files at this revision

API Documentation at this revision

Comitter:
vsoltan
Date:
Sun Nov 29 22:40:24 2020 +0000
Parent:
36:46bb54b669bc
Commit message:
finalizing code before writing report

Changed in this revision

gamestate.cpp Show annotated file Show diff for this revision Revisions of this file
gamestate.h Show annotated file Show diff for this revision Revisions of this file
graphics.cpp Show annotated file Show diff for this revision Revisions of this file
graphics.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/gamestate.cpp	Sat Nov 28 04:21:08 2020 +0000
+++ b/gamestate.cpp	Sun Nov 29 22:40:24 2020 +0000
@@ -24,10 +24,56 @@
     return this->p2_loc; 
 } 
 
+Coord GameState::getPlayerLocation(char player) {
+    if (player == 0) {
+        return getPlayerOneLocation(); 
+    }
+    return getPlayerTwoLocation(); 
+}
+
 Coord GameState::getBallLocation() {
     return this->ball_loc; 
 }
 
+char *GameState::getLobbyHash() {
+    return this->lobbyHash; 
+}
+
+char GameState::getLocalPlayerNum() {
+    return this->localPlayerNum; 
+}
+
+char GameState::hasStarted() {
+    return this->has_started; 
+}
+
+char GameState::done() {
+    return this->is_done; 
+}
+
+int GameState::getCountdown() {
+    return this->countdown; 
+}
+
+int GameState::getPlayerOneScore() {
+    return this->score[0];
+}
+
+int GameState::getPlayerTwoScore() {
+    return this->score[1];
+}
+
+int GameState::getPlayerScore(int player) {
+    if (player == 0) {
+        return getPlayerOneScore(); 
+    }
+    return getPlayerTwoScore(); 
+}
+
+void GameState::setCountdown(int val) {
+    this->countdown = val; 
+}
+
 void GameState::updateAndRender(MbedJSONValue *serverResponse, Graphics *gfx) {
     string typeResponse = (*serverResponse)["type"].get<std::string>(); 
     if (typeResponse == "connected") { 
@@ -73,7 +119,6 @@
             this->score[0] = (serverGameState)["score"][0].get<int>(); 
             this->score[1] = (serverGameState)["score"][1].get<int>();
         }
-        // TODO: check if hash has already been set
         if (serverGameState.hasMember("hash")) {
             strcpy(this->lobbyHash, 
                 (serverGameState)["hash"].get<std::string>().c_str());
@@ -83,48 +128,4 @@
     }
 }
 
-Coord GameState::getPlayerLocation(char player) {
-    if (player == 0) {
-        return getPlayerOneLocation(); 
-    }
-    return getPlayerTwoLocation(); 
-}
 
-char *GameState::getLobbyHash() {
-    return this->lobbyHash; 
-}
-
-char GameState::getLocalPlayerNum() {
-    return this->localPlayerNum; 
-}
-
-int GameState::getCountdown() {
-    return this->countdown; 
-}
-
-void GameState::setCountdown(int val) {
-    this->countdown = val; 
-}
-
-int GameState::getPlayerOneScore() {
-    return this->score[0];
-}
-
-int GameState::getPlayerTwoScore() {
-    return this->score[1];
-}
-
-int GameState::getPlayerScore(int player) {
-    if (player == 0) {
-        return getPlayerOneScore(); 
-    }
-    return getPlayerTwoScore(); 
-}
-
-char GameState::hasStarted() {
-    return this->has_started; 
-}
-
-char GameState::done() {
-    return this->is_done; 
-}
--- a/gamestate.h	Sat Nov 28 04:21:08 2020 +0000
+++ b/gamestate.h	Sun Nov 29 22:40:24 2020 +0000
@@ -16,33 +16,32 @@
 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; 
-        int countdown; 
         char localPlayerNum; 
         char lobbyHash[21]; 
+        int countdown; 
         int score[2]; 
     public:
         GameState();
-        Coord getPlayerLocation(char player);   
         Coord getPlayerOneLocation(); 
         Coord getPlayerTwoLocation(); 
+        Coord getPlayerLocation(char player);   
         Coord getBallLocation();
         char *getLobbyHash();
         char getLocalPlayerNum(); 
-        int getCountdown(); 
         char hasStarted();
-        void setCountdown(int val); 
+        char done();   
+        int getCountdown();
         int getPlayerOneScore();
         int getPlayerTwoScore();
         int getPlayerScore(int player); 
+        void setCountdown(int val);
         void updateAndRender(MbedJSONValue *serverResponse, Graphics *gfx);
-        char done();   
 };
 
 #endif // GAME_STATE_H
\ No newline at end of file
--- a/graphics.cpp	Sat Nov 28 04:21:08 2020 +0000
+++ b/graphics.cpp	Sun Nov 29 22:40:24 2020 +0000
@@ -27,16 +27,23 @@
     tft->printf("%s", "Player..."); 
 }
 
+void Graphics::renderCountdown(GameState *gs) {
+    tft->setTextColor(DEFAULT_TEXT_COLOR); 
+    tft->setTextCursor(63, 63); 
+    tft->printf("%i", gs->getCountdown()); 
+}
+
+void Graphics::renderGameState(GameState *gs) {
+    renderBall(gs); 
+    renderPlayers(gs);
+    renderScore(gs); 
+}
+
 void Graphics::renderBall(GameState *gs) {
     Coord ball_loc = gs->getBallLocation(); 
     tft->drawPixel(ball_loc.x, ball_loc.y, ST7735_WHITE); 
 }
 
-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 topPaddleRenderPos = gs->getPlayerLocation(0).x;    
     int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
@@ -58,19 +65,6 @@
         127 - ELEVATION, PADDLE_WIDTH, bottom_color);
 }
 
-void Graphics::erasePlayers(GameState *gs) {
-    int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;    
-    int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
-    
-    // erase top paddle
-    tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
-        ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
-    
-    // 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); 
@@ -79,14 +73,6 @@
     tft->printf("%i", gs->getPlayerTwoScore());
 }
 
-void Graphics::eraseScore(GameState *gs) {
-    tft->setTextColor(BACKGROUND_COLOR);
-    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(DEFAULT_TEXT_COLOR);
@@ -110,22 +96,36 @@
     tft->printf("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);
-    renderScore(gs); 
+void Graphics::eraseBall(GameState *gs) {
+    Coord ball_loc = gs->getBallLocation(); 
+    tft->drawPixel(ball_loc.x, ball_loc.y, BACKGROUND_COLOR); 
+}
+
+void Graphics::erasePlayers(GameState *gs) {
+    int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;    
+    int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
+    
+    // erase top paddle
+    tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
+        ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
+    
+    // erase bottom paddle
+    tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
+        127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
+}
+
+void Graphics::eraseScore(GameState *gs) {
+    tft->setTextColor(BACKGROUND_COLOR);
+    tft->setTextCursor(120, 50); 
+    tft->printf("%i", gs->getPlayerOneScore());
+    tft->setTextCursor(120, 77); 
+    tft->printf("%i", gs->getPlayerTwoScore());
 }
 
 void Graphics::eraseGameState(GameState *gs) {
--- a/graphics.h	Sat Nov 28 04:21:08 2020 +0000
+++ b/graphics.h	Sun Nov 29 22:40:24 2020 +0000
@@ -33,17 +33,17 @@
         Graphics(); 
         void renderLaunchScreen(); 
         void renderWaitingRoom(); 
+        void renderCountdown(GameState *gs);  
         void renderGameState(GameState *gs); 
         void renderBall(GameState *gs); 
         void renderPlayers(GameState *gs); 
         void renderScore(GameState *gs);
         void renderGameOver(GameState *gs); 
-        void renderCountdown(GameState *gs);  
+        void eraseCountdown(GameState *gs);
         void eraseBall(GameState *gs);
         void erasePlayers(GameState *gs); 
+        void eraseScore(GameState *gs); 
         void eraseGameState(GameState *gs);
-        void eraseScore(GameState *gs); 
-        void eraseCountdown(GameState *gs);
         void reset();
 };
 
--- a/main.cpp	Sat Nov 28 04:21:08 2020 +0000
+++ b/main.cpp	Sun Nov 29 22:40:24 2020 +0000
@@ -109,7 +109,7 @@
             // 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); 
+                printf("flushing bytes: %i\n\r", flushBytes); 
                 printf("tmp_buffer %s\n\r", tmp_buffer); 
             }