multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Mon Nov 23 01:19:17 2020 +0000
Revision:
27:fcc5fee18a24
Parent:
26:ebadab157abe
Child:
29:4708bfb863cb
fixed game over logic

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 22:1c49e1fae846 14 tft->setTextCursor(15, 20);
vsoltan 17:32ae1f106002 15 tft->printf("%s", "Multiplayer Pong");
vsoltan 22:1c49e1fae846 16 tft->setTextCursor(15, 80);
vsoltan 22:1c49e1fae846 17 tft->printf("%s", "press any button");
vsoltan 22:1c49e1fae846 18 tft->setTextCursor(15, 90);
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 22:1c49e1fae846 24 tft->setTextCursor(5, 20);
vsoltan 18:32fce82690a1 25 tft->printf("%s", "Waiting For Player");
vsoltan 18:32fce82690a1 26 }
vsoltan 18:32fce82690a1 27
vsoltan 17:32ae1f106002 28 void Graphics::renderBall(GameState *gs) {
vsoltan 19:58cc5465f647 29 Coord ball_loc = gs->getBallLocation();
vsoltan 19:58cc5465f647 30 tft->drawPixel(ball_loc.x, ball_loc.y, ST7735_WHITE);
vsoltan 17:32ae1f106002 31 }
vsoltan 17:32ae1f106002 32
vsoltan 23:c38680c32552 33 void Graphics::eraseBall(GameState *gs) {
vsoltan 22:1c49e1fae846 34 Coord ball_loc = gs->getBallLocation();
vsoltan 22:1c49e1fae846 35 tft->drawPixel(ball_loc.x, ball_loc.y, BACKGROUND_COLOR);
vsoltan 22:1c49e1fae846 36 }
vsoltan 22:1c49e1fae846 37
vsoltan 17:32ae1f106002 38 void Graphics::renderPlayers(GameState *gs) {
vsoltan 24:05eb0b0ab554 39 int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;
vsoltan 24:05eb0b0ab554 40 int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
vsoltan 24:05eb0b0ab554 41
vsoltan 24:05eb0b0ab554 42 uint16_t top_color = YOUR_PADDLE_COLOR;
vsoltan 24:05eb0b0ab554 43 uint16_t bottom_color = OTHER_PADDLE_COLOR;
vsoltan 23:c38680c32552 44
vsoltan 24:05eb0b0ab554 45 if (gs->getLocalPlayerNum() == 1) {
vsoltan 24:05eb0b0ab554 46 top_color = OTHER_PADDLE_COLOR;
vsoltan 24:05eb0b0ab554 47 bottom_color = YOUR_PADDLE_COLOR;
vsoltan 24:05eb0b0ab554 48 }
vsoltan 24:05eb0b0ab554 49
vsoltan 24:05eb0b0ab554 50 // draw top paddle
vsoltan 26:ebadab157abe 51 tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 24:05eb0b0ab554 52 ELEVATION, PADDLE_WIDTH, top_color);
vsoltan 23:c38680c32552 53
vsoltan 23:c38680c32552 54 // draw bottom paddle
vsoltan 26:ebadab157abe 55 tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 24:05eb0b0ab554 56 127 - ELEVATION, PADDLE_WIDTH, bottom_color);
vsoltan 22:1c49e1fae846 57 }
vsoltan 22:1c49e1fae846 58
vsoltan 23:c38680c32552 59 void Graphics::erasePlayers(GameState *gs) {
vsoltan 24:05eb0b0ab554 60 int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;
vsoltan 24:05eb0b0ab554 61 int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
vsoltan 23:c38680c32552 62
vsoltan 24:05eb0b0ab554 63 // draw top paddle
vsoltan 26:ebadab157abe 64 tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 23:c38680c32552 65 ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
vsoltan 23:c38680c32552 66
vsoltan 24:05eb0b0ab554 67 // draw bottom paddle
vsoltan 26:ebadab157abe 68 tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 23:c38680c32552 69 127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
vsoltan 17:32ae1f106002 70 }
vsoltan 16:7fd48cda0773 71
vsoltan 27:fcc5fee18a24 72 void Graphics::eraseScore(GameState *gs) {
vsoltan 27:fcc5fee18a24 73 tft->setTextColor(BACKGROUND_COLOR);
vsoltan 27:fcc5fee18a24 74 tft->setTextCursor(120, 50);
vsoltan 27:fcc5fee18a24 75 tft->printf("%i", gs->getPlayerOneScore());
vsoltan 27:fcc5fee18a24 76 tft->setTextCursor(120, 77);
vsoltan 27:fcc5fee18a24 77 tft->printf("%i", gs->getPlayerTwoScore());
vsoltan 27:fcc5fee18a24 78 }
vsoltan 27:fcc5fee18a24 79
vsoltan 27:fcc5fee18a24 80 void Graphics::renderScore(GameState *gs) {
vsoltan 27:fcc5fee18a24 81 tft->setTextColor(ST7735_WHITE);
vsoltan 27:fcc5fee18a24 82 tft->setTextCursor(120, 50);
vsoltan 27:fcc5fee18a24 83 tft->printf("%i", gs->getPlayerOneScore());
vsoltan 27:fcc5fee18a24 84 tft->setTextCursor(120, 77);
vsoltan 27:fcc5fee18a24 85 tft->printf("%i", gs->getPlayerTwoScore());
vsoltan 27:fcc5fee18a24 86 }
vsoltan 27:fcc5fee18a24 87
vsoltan 27:fcc5fee18a24 88 void Graphics::renderGameOver(GameState *gs) {
vsoltan 27:fcc5fee18a24 89 reset();
vsoltan 27:fcc5fee18a24 90 tft->setTextColor(ST7735_WHITE);
vsoltan 27:fcc5fee18a24 91 tft->setTextCursor(15, 20);
vsoltan 27:fcc5fee18a24 92 tft->printf("Game Over");
vsoltan 27:fcc5fee18a24 93 tft->setTextCursor(15, 40);
vsoltan 27:fcc5fee18a24 94 tft->printf("Final Score %i : %i",
vsoltan 27:fcc5fee18a24 95 gs->getPlayerOneScore(), gs->getPlayerTwoScore());
vsoltan 27:fcc5fee18a24 96 tft->setTextCursor(15, 60);
vsoltan 27:fcc5fee18a24 97 tft->printf("Press any button to play again!");
vsoltan 27:fcc5fee18a24 98 }
vsoltan 27:fcc5fee18a24 99
vsoltan 16:7fd48cda0773 100 void Graphics::renderGameState(GameState *gs) {
vsoltan 19:58cc5465f647 101 renderBall(gs);
vsoltan 23:c38680c32552 102 renderPlayers(gs);
vsoltan 27:fcc5fee18a24 103 renderScore(gs);
vsoltan 23:c38680c32552 104 }
vsoltan 23:c38680c32552 105
vsoltan 23:c38680c32552 106 void Graphics::eraseGameState(GameState *gs) {
vsoltan 23:c38680c32552 107 eraseBall(gs);
vsoltan 23:c38680c32552 108 erasePlayers(gs);
vsoltan 27:fcc5fee18a24 109 eraseScore(gs);
vsoltan 23:c38680c32552 110 }
vsoltan 23:c38680c32552 111
vsoltan 23:c38680c32552 112 void Graphics::reset() {
vsoltan 23:c38680c32552 113 tft->fillScreen(BACKGROUND_COLOR);
vsoltan 16:7fd48cda0773 114 }