I am working on the game system overview. Without going into details and technologies I sketched out how I imagine the game should work.

Client

My Snake will be a browser based game. I am not planning to use fancy frameworks works, for now I can start with a simple canvas.

From the client perspective situation looks simple. On the one hand player decides which directions he want to move next. On the other hand player wants to see the game map with snake position updated accordingly.

Player want’s to see real-time updates. To achieve this I am planning to use WebSockets.

Backend

Thinks become more interesting on the backend.

First, we have Game State. It’s keeping track of snakes and crumbs positions, crumbs at least. If I decide that the game map isn’t static it will need to accommodate that as well. I thought it would be cool if snakes leave a trail behind them. It’s not clear yet how the state will be persisted. It has to offer fast write and read this means I either keep it in memory or use a in-memory database like redis.

Second – the most exciting part – the Game Loop. It takes current game state and applies snakes moves, solves collisions and resolves events like ‘snake eating a crumb’. Then updates the game state.

Players will send the moves asynchronously. I can’t feed them directly to the game loop. I need some kind of Moves Queue (buffer) that collects player moves and when new game loop starts the buffers is closed for changes. At this point game loop evaluates the sate based moves in the buffer. If new moves are posted by players in this time they will be saved in new buffer for next game loop to pick up.

Now what is left is to send the updated state back to client. There is one problem tho. I want the game to accommodate a looooooot of players, possibly 1e6 snakes. This will require the game map to be huge! I don’t suppose I can send entire map to every player after every game loop. Therefore I am planning to divide the map into areas the same way a map (like googlemaps) is divided into tiles. Each player will receive only a chunk of game map – one that is located in proximity of players snake. This means I need some kind of Tile Server and TileUpdates that will updated relevant tiles after game state changes

Endnote

This is very high level overview of my plan how to implement this game. There is still a lot of questions to be answered and I will tackle them in future logs. My biggest concern is at the moment with latency – if WebSockets will prove fast enough to provide smooth game experience.

To discover this I am planning to create a mini snake game – a game with a single player and tiny map.

More about that in next log!