multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Sun Nov 29 22:40:24 2020 +0000
Revision:
37:8a0fc62a0512
Parent:
36:46bb54b669bc
finalizing code before writing report

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsoltan 16:7fd48cda0773 1
vsoltan 16:7fd48cda0773 2 #include "graphics.h"
vsoltan 16:7fd48cda0773 3
vsoltan 16:7fd48cda0773 4 Graphics::Graphics() {
vsoltan 17:32ae1f106002 5 printf("\n\rInitializing Graphics: initializing TFT\n\r");
vsoltan 16:7fd48cda0773 6 this->tft = new Adafruit_ST7735(P_MOSI, P_MISO, P_SOCK, P_CS, P_RS, P_DC);
vsoltan 17:32ae1f106002 7 printf("Initializing Graphics: optimizing display for resolution\n\r");
vsoltan 17:32ae1f106002 8 tft->initR(INITR_144GREENTAB);
vsoltan 17:32ae1f106002 9 tft->setTextColor(ST7735_WHITE);
vsoltan 16:7fd48cda0773 10 }
vsoltan 16:7fd48cda0773 11
vsoltan 16:7fd48cda0773 12 void Graphics::renderLaunchScreen() {
vsoltan 22:1c49e1fae846 13 tft->fillScreen(LAUNCH_SCREEN_COLOR);
vsoltan 29:4708bfb863cb 14 tft->setTextCursor(18, 20);
vsoltan 17:32ae1f106002 15 tft->printf("%s", "Multiplayer Pong");
vsoltan 29:4708bfb863cb 16 tft->setTextCursor(15, 60);
vsoltan 22:1c49e1fae846 17 tft->printf("%s", "press any button");
vsoltan 29:4708bfb863cb 18 tft->setTextCursor(15, 70);
vsoltan 22:1c49e1fae846 19 tft->printf("%s", "to continue");
vsoltan 17:32ae1f106002 20 }
vsoltan 17:32ae1f106002 21
vsoltan 18:32fce82690a1 22 void Graphics::renderWaitingRoom() {
vsoltan 22:1c49e1fae846 23 tft->fillScreen(WAITING_SCREEN_COLOR);
vsoltan 29:4708bfb863cb 24 tft->setTextCursor(18, 20);
vsoltan 29:4708bfb863cb 25 tft->printf("%s", "Waiting For");
vsoltan 29:4708bfb863cb 26 tft->setTextCursor(18, 30);
vsoltan 29:4708bfb863cb 27 tft->printf("%s", "Player...");
vsoltan 18:32fce82690a1 28 }
vsoltan 18:32fce82690a1 29
vsoltan 37:8a0fc62a0512 30 void Graphics::renderCountdown(GameState *gs) {
vsoltan 37:8a0fc62a0512 31 tft->setTextColor(DEFAULT_TEXT_COLOR);
vsoltan 37:8a0fc62a0512 32 tft->setTextCursor(63, 63);
vsoltan 37:8a0fc62a0512 33 tft->printf("%i", gs->getCountdown());
vsoltan 37:8a0fc62a0512 34 }
vsoltan 37:8a0fc62a0512 35
vsoltan 37:8a0fc62a0512 36 void Graphics::renderGameState(GameState *gs) {
vsoltan 37:8a0fc62a0512 37 renderBall(gs);
vsoltan 37:8a0fc62a0512 38 renderPlayers(gs);
vsoltan 37:8a0fc62a0512 39 renderScore(gs);
vsoltan 37:8a0fc62a0512 40 }
vsoltan 37:8a0fc62a0512 41
vsoltan 17:32ae1f106002 42 void Graphics::renderBall(GameState *gs) {
vsoltan 19:58cc5465f647 43 Coord ball_loc = gs->getBallLocation();
vsoltan 19:58cc5465f647 44 tft->drawPixel(ball_loc.x, ball_loc.y, ST7735_WHITE);
vsoltan 17:32ae1f106002 45 }
vsoltan 17:32ae1f106002 46
vsoltan 17:32ae1f106002 47 void Graphics::renderPlayers(GameState *gs) {
vsoltan 24:05eb0b0ab554 48 int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;
vsoltan 24:05eb0b0ab554 49 int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
vsoltan 24:05eb0b0ab554 50
vsoltan 24:05eb0b0ab554 51 uint16_t top_color = YOUR_PADDLE_COLOR;
vsoltan 24:05eb0b0ab554 52 uint16_t bottom_color = OTHER_PADDLE_COLOR;
vsoltan 23:c38680c32552 53
vsoltan 24:05eb0b0ab554 54 if (gs->getLocalPlayerNum() == 1) {
vsoltan 24:05eb0b0ab554 55 top_color = OTHER_PADDLE_COLOR;
vsoltan 24:05eb0b0ab554 56 bottom_color = YOUR_PADDLE_COLOR;
vsoltan 24:05eb0b0ab554 57 }
vsoltan 24:05eb0b0ab554 58
vsoltan 24:05eb0b0ab554 59 // draw top paddle
vsoltan 26:ebadab157abe 60 tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 24:05eb0b0ab554 61 ELEVATION, PADDLE_WIDTH, top_color);
vsoltan 23:c38680c32552 62
vsoltan 23:c38680c32552 63 // draw bottom paddle
vsoltan 26:ebadab157abe 64 tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 24:05eb0b0ab554 65 127 - ELEVATION, PADDLE_WIDTH, bottom_color);
vsoltan 22:1c49e1fae846 66 }
vsoltan 22:1c49e1fae846 67
vsoltan 29:4708bfb863cb 68 void Graphics::renderScore(GameState *gs) {
vsoltan 29:4708bfb863cb 69 tft->setTextColor(ST7735_WHITE);
vsoltan 29:4708bfb863cb 70 tft->setTextCursor(120, 50);
vsoltan 29:4708bfb863cb 71 tft->printf("%i", gs->getPlayerOneScore());
vsoltan 29:4708bfb863cb 72 tft->setTextCursor(120, 77);
vsoltan 29:4708bfb863cb 73 tft->printf("%i", gs->getPlayerTwoScore());
vsoltan 29:4708bfb863cb 74 }
vsoltan 29:4708bfb863cb 75
vsoltan 27:fcc5fee18a24 76 void Graphics::renderGameOver(GameState *gs) {
vsoltan 27:fcc5fee18a24 77 reset();
vsoltan 33:98ba7b0b8c2b 78 tft->setTextColor(DEFAULT_TEXT_COLOR);
vsoltan 27:fcc5fee18a24 79 tft->setTextCursor(15, 20);
vsoltan 27:fcc5fee18a24 80 tft->printf("Game Over");
vsoltan 27:fcc5fee18a24 81 tft->setTextCursor(15, 40);
vsoltan 33:98ba7b0b8c2b 82
vsoltan 33:98ba7b0b8c2b 83 bool winner = gs->getPlayerOneScore() > gs->getPlayerTwoScore() ? 0 : 1;
vsoltan 33:98ba7b0b8c2b 84
vsoltan 33:98ba7b0b8c2b 85 if (winner == gs->getLocalPlayerNum()) {
vsoltan 36:46bb54b669bc 86 tft->printf("You won, %i:%i",
vsoltan 36:46bb54b669bc 87 gs->getPlayerScore(winner), gs->getPlayerScore(1 - winner));
vsoltan 33:98ba7b0b8c2b 88 } else {
vsoltan 36:46bb54b669bc 89 tft->printf("You lost, %i:%i",
vsoltan 36:46bb54b669bc 90 gs->getPlayerScore(1 - winner), gs->getPlayerScore(winner));
vsoltan 33:98ba7b0b8c2b 91 }
vsoltan 33:98ba7b0b8c2b 92
vsoltan 27:fcc5fee18a24 93 tft->setTextCursor(15, 60);
vsoltan 36:46bb54b669bc 94 tft->printf("Press any button");
vsoltan 36:46bb54b669bc 95 tft->setTextCursor(15, 70);
vsoltan 36:46bb54b669bc 96 tft->printf("to play again!");
vsoltan 27:fcc5fee18a24 97 }
vsoltan 27:fcc5fee18a24 98
vsoltan 29:4708bfb863cb 99 void Graphics::eraseCountdown(GameState *gs) {
vsoltan 29:4708bfb863cb 100 tft->setTextColor(BACKGROUND_COLOR);
vsoltan 29:4708bfb863cb 101 tft->setTextCursor(63, 63);
vsoltan 29:4708bfb863cb 102 tft->printf("%i", gs->getCountdown());
vsoltan 29:4708bfb863cb 103 }
vsoltan 29:4708bfb863cb 104
vsoltan 37:8a0fc62a0512 105 void Graphics::eraseBall(GameState *gs) {
vsoltan 37:8a0fc62a0512 106 Coord ball_loc = gs->getBallLocation();
vsoltan 37:8a0fc62a0512 107 tft->drawPixel(ball_loc.x, ball_loc.y, BACKGROUND_COLOR);
vsoltan 37:8a0fc62a0512 108 }
vsoltan 37:8a0fc62a0512 109
vsoltan 37:8a0fc62a0512 110 void Graphics::erasePlayers(GameState *gs) {
vsoltan 37:8a0fc62a0512 111 int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;
vsoltan 37:8a0fc62a0512 112 int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
vsoltan 37:8a0fc62a0512 113
vsoltan 37:8a0fc62a0512 114 // erase top paddle
vsoltan 37:8a0fc62a0512 115 tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 37:8a0fc62a0512 116 ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
vsoltan 37:8a0fc62a0512 117
vsoltan 37:8a0fc62a0512 118 // erase bottom paddle
vsoltan 37:8a0fc62a0512 119 tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 37:8a0fc62a0512 120 127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
vsoltan 37:8a0fc62a0512 121 }
vsoltan 37:8a0fc62a0512 122
vsoltan 37:8a0fc62a0512 123 void Graphics::eraseScore(GameState *gs) {
vsoltan 37:8a0fc62a0512 124 tft->setTextColor(BACKGROUND_COLOR);
vsoltan 37:8a0fc62a0512 125 tft->setTextCursor(120, 50);
vsoltan 37:8a0fc62a0512 126 tft->printf("%i", gs->getPlayerOneScore());
vsoltan 37:8a0fc62a0512 127 tft->setTextCursor(120, 77);
vsoltan 37:8a0fc62a0512 128 tft->printf("%i", gs->getPlayerTwoScore());
vsoltan 23:c38680c32552 129 }
vsoltan 23:c38680c32552 130
vsoltan 23:c38680c32552 131 void Graphics::eraseGameState(GameState *gs) {
vsoltan 23:c38680c32552 132 eraseBall(gs);
vsoltan 23:c38680c32552 133 erasePlayers(gs);
vsoltan 27:fcc5fee18a24 134 eraseScore(gs);
vsoltan 23:c38680c32552 135 }
vsoltan 23:c38680c32552 136
vsoltan 23:c38680c32552 137 void Graphics::reset() {
vsoltan 23:c38680c32552 138 tft->fillScreen(BACKGROUND_COLOR);
vsoltan 16:7fd48cda0773 139 }