The client application for the 2-player snake game (server repo found here). SnakeClient is a thin client that processes server messages and updates the game state accordingly. The client handles user I/O and holds the game tick and some game state. Graphics are handled by the cross-platform library: RayLib.
The simple architecture is managed by the System Manager, which is the owner of the incoming and outgoing message (thread safe) queues. To keep architecture compact and maintain only two threads, the game loop is contained within the System Manager.
The game Architecture is straightforward, making use of custom Vec2's to keep track of positions within the play grid. The snake is a linked List of the vectors that can efficiently be modified with push and pop functions for movement. The Vec2's are overloaded to easily allow comparisons between other Vec2's such as the position of the fruit, the opposing snake, and the walls. The majority of the client game class logic is comparing the Vec2's to see when they overlap, and updating the game according.
Messages are sent as JSON code and payload pairs. All server-client
communication is handled by one of these messages.
NOTES:
- Payloads are sent as strings, and coordinates are sent as
x,ypairs, with no spaces. - NULL payloads do not matter—usually sent as 0.
| Code | Payload | Description |
|---|---|---|
| SEAT | — | — |
| START | player |
Both users connected, start app (before set, first message) |
| COLLISION | player |
Snake collided |
| APPLE | x,y |
Sends new apple coordinates |
| GROW | player |
Tells client that a player should grow |
| SCORE | s1,s2 |
Sends updated scores for both players |
| SET | — | Both players ready, start game |
| DISCONNECT | player |
Notifies that a player disconnected |
| Code | Payload | Description |
|---|---|---|
| MOVE | x,y |
Movement update (player → server, server → client) |
| Code | Payload | Description |
|---|---|---|
| READY | — | Client indicates it is ready to start game |
| RESET | — | Client requests a full game reset |
The client is built using cmake and uses two dependencies.
- vcpkg to install
raylibandixwebsocket- (alternate option) install manually raylib and ixwebsocket
- CMake >3.31
- C++23 Compiler
- MacOS or Windows
- Install prerequisites
- Generate build using CMake (be sure to set your paths in the script below):
mkdir -p build
cd build
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake- Compile:
cmake --build . -j$- Run:
Unix
./SnakeServerWindows
SnakeServer.exe