Skip to content

Latest commit

 

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Online 2 Player Snake

Client

Authors: Tyler Black, Blaine Morton

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.

General Architecture

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.

Client Architecture Diagram

Game Architecture

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.

Message Protocol

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,y pairs, with no spaces.
  • NULL payloads do not matter—usually sent as 0.

To Client

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

Both Directions

Code Payload Description
MOVE x,y Movement update (player → server, server → client)

To Server

Code Payload Description
READY Client indicates it is ready to start game
RESET Client requests a full game reset

Build

The client is built using cmake and uses two dependencies.

Prerequisites

  • vcpkg to install raylib and ixwebsocket
  • CMake >3.31
  • C++23 Compiler
  • MacOS or Windows
  1. Install prerequisites
  2. 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
  1. Compile:
  cmake --build . -j$
  1. Run:

Unix

  ./SnakeServer

Windows

  SnakeServer.exe

About

client for the SnakeGame Multiplayer server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages