How to code multiplayer games with p5!

Coding games using p5 is very fun, coding multiplayer games that you can get your mates to join in with, even better. I’ve made a few multiplayer games and a couple using p5, so I thought I’d share with you beautiful people how to do it yourself!

Where to begin?

Okay, so with setting up multiplayer games there is quite a lot of boilerplate, so I’ve created a kick-starter repository so you can avoid all the boring bits. p5-multiplayer-game-starter is this beautiful repository!

What is p5-multiplayer-game-starter?

It’s a very simple way to kick-start your multiplayer game development using express, socket.io and p5.js. It represents an opinionated approach to multiplayer game development which should allow you to skip the laborious beginning boiler-plate steps.

How to use this magical repository?

  1. Clone the project
  2. npm install in the root directory
  3. npm start to get it running!

Details

The project is very simple, it sets up a client Player class and a server Player. Every 16ms the server emits the current state of the game to the client. If a new client joins the server a new Player will be added to the game and displayed on all clients, it also automatically removes players when they leave the game. These are the standard features I frequently have to reproduce when creating a new multiplayer game, so I hope you find this as useful as I do!

Each client will be a player of your game, they all communicate with the server. The server will control all the game state, such as the player position. The position of the player will be emitted to the all of the clients so that each client can render (using p5) the player in the game!

Image result for client server

A demo of your game so far!

A player joins the game by going to localhost:8080

Player one joins

Your friend then joins!

Player two joins


Your friend sees how bloody good you are at the game and quickly leaves..

Player two joins

So basically, in its simplest form the repository handles clients joining and leaving the game.

So there we go, what I recommend you do is having a play around with the repository, add some basic physics, get some interaction with the clients. This should give you the very basic building blocks of your next IO hit!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s