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:
Mon Nov 16 03:34:34 2020 +0000
Parent:
23:c38680c32552
Child:
25:4a965eccb922
Commit message:
can send move input and fixed graphics bugs;

Changed in this revision

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/graphics.cpp	Mon Nov 16 03:01:50 2020 +0000
+++ b/graphics.cpp	Mon Nov 16 03:34:34 2020 +0000
@@ -36,31 +36,35 @@
 }
 
 void Graphics::renderPlayers(GameState *gs) {
-    int otherPlayerNum = (gs->getLocalPlayerNum() == 0) ? 1 : 0; 
+    int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;    
+    int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
+    
+    uint16_t top_color = YOUR_PADDLE_COLOR;
+    uint16_t bottom_color = OTHER_PADDLE_COLOR; 
     
-    int8_t bottomPaddleRenderPos = gs->getPlayerLocation(gs->getLocalPlayerNum()).x;
-    int8_t topPaddleRenderPos = gs->getPlayerLocation(otherPlayerNum).x;    
+    if (gs->getLocalPlayerNum() == 1) {
+        top_color = OTHER_PADDLE_COLOR; 
+        bottom_color = YOUR_PADDLE_COLOR; 
+    }
+    
+    // draw top paddle
+    tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
+        ELEVATION, PADDLE_WIDTH, top_color);
     
     // 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);
+        127 - ELEVATION, PADDLE_WIDTH, bottom_color);
 }
 
 void Graphics::erasePlayers(GameState *gs) {
-    int otherPlayerNum = (gs->getLocalPlayerNum() == 0) ? 1 : 0; 
+    int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;    
+    int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
     
-    int8_t bottomPaddleRenderPos = gs->getPlayerLocation(gs->getLocalPlayerNum()).x;
-    int8_t topPaddleRenderPos = gs->getPlayerLocation(otherPlayerNum).x;    
-    
-    // draw bottom paddle
+    // draw top paddle
     tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
         ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
     
-    // draw top paddle
+    // draw bottom paddle
     tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
         127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
 }
--- a/graphics.h	Mon Nov 16 03:01:50 2020 +0000
+++ b/graphics.h	Mon Nov 16 03:34:34 2020 +0000
@@ -16,8 +16,9 @@
 #define LAUNCH_SCREEN_COLOR     ST7735_BLACK  
 #define WAITING_SCREEN_COLOR    ST7735_BLACK  
 
-#define PADDLE_COLOR    ST7735_GREEN
-#define BALL_COLOR      ST7735_RED
+#define OTHER_PADDLE_COLOR      ST7735_WHITE
+#define YOUR_PADDLE_COLOR       ST7735_GREEN
+#define BALL_COLOR              ST7735_RED
 
 #define PADDLE_WIDTH 30 
 #define ELEVATION    5  
--- a/main.cpp	Mon Nov 16 03:01:50 2020 +0000
+++ b/main.cpp	Mon Nov 16 03:34:34 2020 +0000
@@ -48,10 +48,9 @@
     rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
     
     MbedJSONValue serverResponse; 
+    MbedJSONValue serverRequest; 
     
     char connectionRequest[] = "{\"type\": \"connected\"}";
-    char moveFormat[] = "\"type\": \"move\", \"hash\": %s, \"player\": %i, \"delta\": %i";
-    char move[128];
     char readTo[256]; 
     
     gfx->renderLaunchScreen(); 
@@ -69,12 +68,17 @@
             
             while (!gs->done()) {
                 if (sendFlag != 0) {
-                    sprintf(move, moveFormat, gs->getLobbyHash(), 
-                        gs->getLocalPlayerNum(), moveData);
-                    printf("Move value: %i\n\r", moveData); 
+                    serverRequest["type"] = "move"; 
+                    serverRequest["hash"] = "jaredyeagersflipflop"; 
+                    serverRequest["player"] = gs->getLocalPlayerNum(); 
+                    serverRequest["delta"] = (int)moveData; 
+                    
+                    string requestContent = serverRequest.serialize(); 
+                    int len = requestContent.size(); 
+                    char *toSend = (char *)requestContent.c_str(); 
                     sendFlag = 0; 
                     moveData = 0; 
-                    sock.sendTo(nist, move, sizeof(move) - 1);
+                    sock.sendTo(nist, toSend, len);
                 }
                 int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo));
                 readTo[bytesRead] = 0;