multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
Revision 15:9d90f68e53da, committed 2020-11-12
- Comitter:
- vsoltan
- Date:
- Thu Nov 12 05:51:00 2020 +0000
- Parent:
- 14:6b20a930e1cb
- Child:
- 16:7fd48cda0773
- Commit message:
- added game state object
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamestate.cpp Thu Nov 12 05:51:00 2020 +0000 @@ -0,0 +1,31 @@ +#include "gamestate.h" + +GameState::GameState() { + Coord playerOneLocation = {0, 0}; + Coord playerTwoLocation = {0, 0}; + Coord ballLocation = {0, 0}; + this->p1_loc = playerOneLocation; + this->p2_loc = playerTwoLocation; + this->ball_loc = ballLocation; + this->is_done = false; +} + +Coord GameState::getPlayerOneLocation() { + return this->p1_loc; +} + +Coord GameState::getPlayerTwoLocation() { + return this->p2_loc; +} + +Coord GameState::getBallLocation() { + return this->ball_loc; +} + +void GameState::update(char *server_data) { + +} + +bool GameState::done() { + return this->is_done; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamestate.h Thu Nov 12 05:51:00 2020 +0000 @@ -0,0 +1,28 @@ + +#ifndef GAME_STATE_H +#define GAME_STATE_H + +#include "mbed.h" +#include "stdint.h" + +struct Coord { + int8_t x; + int8_t y; +}; + +class GameState { + private: + Coord p1_loc; + Coord p2_loc; + Coord ball_loc; + bool is_done; + public: + GameState(); + Coord getPlayerOneLocation(); + Coord getPlayerTwoLocation(); + Coord getBallLocation(); + void update(char *server_data); + bool done(); +}; + +#endif // GAME_STATE_H \ No newline at end of file
--- a/main.cpp Thu Nov 12 04:44:05 2020 +0000 +++ b/main.cpp Thu Nov 12 05:51:00 2020 +0000 @@ -1,6 +1,7 @@ #include "mbed.h" #include "EthernetInterface.h" #include "network.h" +#include "gamestate.h" #include "DebouncedInterrupt.h" #define DEBOUNCE 50 @@ -40,20 +41,32 @@ middleButton.attach(&pressMiddle, IRQ_RISE, DEBOUNCE); rightButton.attach(&pressRight, IRQ_RISE, DEBOUNCE); + GameState *gs = new GameState(); + char toSend[] = "{\"type\": \"connected\", \"data\": \"Ay whats good\"}"; char readTo[256]; - while (1) { - if (sendFlag != 0) { - printf("Move value: %i\n\r", moveData); - sendFlag = 0; - moveData = 0; - sock.sendTo(nist, toSend, sizeof(toSend) - 1); + while (1) { // keep program running + + /* TODO: poll for navigation within menu + * press any button to start + */ + + while (!gs->done()) { + if (sendFlag != 0) { + printf("Move value: %i\n\r", moveData); + sendFlag = 0; + moveData = 0; + sock.sendTo(nist, toSend, sizeof(toSend) - 1); + } + int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); + if (bytesRead > 0) { + printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo); + // TODO: update game state with json string + } + wait(.1); } - int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); - printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo); - wait(.1); + wait(0.5); } - - cleanupEthernet(ð, &sock); + // cleanupEthernet(ð, &sock); }
--- a/network.cpp Thu Nov 12 04:44:05 2020 +0000 +++ b/network.cpp Thu Nov 12 05:51:00 2020 +0000 @@ -6,6 +6,7 @@ sock->init(); nist->set_address(AWS_IP, PORT); sock->set_blocking(false, 0); + printf("Connected to %s, port: %i\n\r", AWS_IP, PORT); } void cleanupEthernet(EthernetInterface *eth, UDPSocket *sock) {
--- a/network.h Thu Nov 12 04:44:05 2020 +0000 +++ b/network.h Thu Nov 12 05:51:00 2020 +0000 @@ -1,8 +1,14 @@ + +#ifndef NETWORK_H +#define NETWORK_H #include "EthernetInterface.h" +#include "mbed.h" #define AWS_IP "3.128.153.185" #define PORT 3030 void initEthernet(EthernetInterface *eth, UDPSocket *sock, Endpoint *nist); void cleanupEthernet(EthernetInterface *eth, UDPSocket *sock); + +#endif // NETWORK_H