Watch the ScreencastRead the Source Code
Play the game Standalone
Coder X here,
Today we will build a multiplayer snake game. Players connect to a central server and eat green food dots to grow. We will use websockets to communicate between the browser and server.
One concern when using websockets is browser support. Sente is a library which addresses this concern by falling back to ajax when websockets are not available.
We will use Sente to collect player direction changes, and push out the new state of the world every time it updates. Let’s get started with a new ClojureScript project.
First we create the game client. We set up three namespaces:
- Main - the entry point that initializes our game
- Model - to store and manipulate the data for our game
- View - for components to be rendered on the page
Our game board will be a 20 by 20 matrix. We store our board and players in app-state. Players will have a current position, direction, length, and tail path. We need a ticker that updates the game world at regular intervals. Each update will be calculated by the step function.
Let’s see how we might draw the game board. For each position in the matrix, we may draw a snake body segment. We start a ticker that updates player positions, and some code to trim the tails of snakes as they move. Snakes are slithering.
Let’s adjust the look of the segments to taste. We need to handle keyboard input, so let’s add a listener that sets the next direction the snake should travel in. Great, we can steer the snake around now. Let’s add some eyes so it is obvious where the head is. We put two circle elements inside a group element that is translated to the player location.
We are now ready to work on the server side of our game. Let’s copy the example code from the Sente website to set up our websockets. On the server side we have compojure routes to accept the socket. We’ll host these routes in a HTTP-kit server because HTTP-kit supports websockets.
Ring reload middleware is essential for development. It will watch the filesystem for changes in our code, and load them when we call the server. When we make changes, we don’t need to restart or send to our REPL integration. We just save our changes, and see the effect immediately.
The server is receiving events. The events are identified with a unique keyword. We dispatch handling of events with a multimethod. Our first custom handler will be for the :snakelake/dir event.
We’ll start by just printing the event we receive.
Now we will move the model code we built up in our client code over to the server. Isn’t it convenient that we are using Clojure in both places? Instead of an atom, the server should use a ref to model world state. Refs provide transactional guarantees, and we want to handle concurrent user access.
When players connect, we’ll assign them a random colors as their identifier. The ticker will be a thread instead of an interval callback. We’ll broadcast the entire world every tick. On the client side, our model now gets replaced with whatever we hear from the server. Our client sends directions to the server, which updates the player state in the server model. Every tick, the world is updated and broadcast to all connected clients. When we run two browsers, we can see them both updating together.
At this point we have a multiplayer game. The server and client message handling code is very similar. Communication is expressed as event handling.
As this is a server process, we have the option to enable ahead of time compilation. Ahead of time compiled code starts faster, but it brings some weirdness. We need to pay careful attention to the Sente Lifecycle. Top level forms are executed during ahead of time compilation. It doesn’t make any sense to establish a socket server while compiling. So we need to reorganize our code a little bit to have a start function. Alternatively we could have chosen to avoid AOT completely..
We have our deployment unit, so let’s try running it locally. Making adjustments locally is faster than a full round trip deploy. Ok, things are looking good, so let’s deploy to the hosting provider.
Push the repository to Heroku, and our game is now live.
Setting up bidirectional communication is easy! The communication model is simple and concise.
Clojure lends itself to separation of concerns:
- Communication is event handling oriented.
- The model is data and transformations.
- The interface is a function of the data.
I hope you enjoy playing Snake Lake, and that you will build something even better.
Until next time,