A multiplayer cell eating game in Python: eat food and smaller cells to grow, and avoid the bigger ones. The server and the network protocol are written from scratch with raw sockets. Coded by hand before AI, inspired by Tech With Tim's video on agar.io.
Players are black, bots are green.
Server and clients. The server owns the whole game state (cells and food). A client only sends where its mouse points and draws what the server sends back. The server runs one thread per client to receive messages, and one loop at 30 updates per second which moves the bots, checks who eats what, and sends the new state to everyone.
Protocol. Everything goes through TCP. Since TCP is a stream, each message starts with a 15 byte header containing its length, followed by the message as text, TYPE;field;field:
| Message | Direction | Content |
|---|---|---|
UPDATE;dx;dy |
client to server | the move of the player for this frame |
ID / CREATE |
server to client | the id of the new player, and the existing cells |
UPDATE |
server to all | id,x,y,radius of every cell |
FOOD_UPDATE |
server to all | state and position of every food |
INACTIVE;id |
server to all | a cell was eaten or left |
Rules. A cell moves towards the mouse with a speed proportional to
Food makes a cell grow by
Needs pygame and Python 3.11 or older.
python Server.py # on the server
python Controller.py # on each player's computerTo play on several computers, set SERVER to the server's IP address in Constants.py (localhost by default).