Please enable JavaScript to view the comments powered by Disqus.

Part 4: Phaser Multiplayer Game Tutorial: generating food in your game for upgrades

Generate food pickup


Repository available here:

In this part 4 of multiplayer game tutorial series, we'll generate food pickups for the players to eat and grow like /

In the last tutorial, we implemented collision in Phaser client, and notified the server when any collision occurs. The server checks this collision and determines who got eaten by comparing their sizes.

Clarification for part 3:

Shouldn’t we generate collision in the server and send that data to the client to make it more secure?: Yes that’s possible, and that’s probably what other physics-intense multiplayer games do in their server. However, that means a lot of calculation will be required in the server, resulting in latency problem. In our game, we’re only going to verify if the collision is valid in the server side in the future.

For this tutorial, we need a node package called node-uuid for generating unique id in the server. Type in npm install node-uuid -save in your project directory in node command prompt.

Back to coding!!

New Client Side Script: item.js

//the food list
var food_pickup = [];

// search through food list to find the food object
function finditembyid (id) {
	for (var i = 0; i < food_pickup.length; i++) {

		if (food_pickup[i].id == id) {
			return food_pickup[i]; 
	return false; 

// function called when new food is added in the server.
function onitemUpdate (data) {
	food_pickup.push(new food_object(, data.type, data.x, data.y)); 

// function called when food needs to be removed in the client. 
function onitemremove (data) {
	var removeItem; 
	removeItem = finditembyid(;
	food_pickup.splice(food_pickup.indexOf(removeItem), 1); 
	//destroy the phaser object 

// the food class
var food_object = function (id, type, startx, starty, value) {
	// unique id for the food.
	//generated in the server with node-uuid = id; 
	//positinon of the food
	this.posx = startx;  
	this.posy = starty; 
	this.powerup = value;
	//create a circulr phaser object for food
	this.item =, this.posy);
	this.item.lineStyle(2, 0xFF0000, 1);
	this.item.drawCircle(0, 0, 20);

	this.item.type = 'food_body'; = id;
	game.physics.p2.enableBody(this.item, true);
	this.item.body_size = 10; 
	this.item.body.addCircle(this.item.body_size, 0, 0); = 0;[0].sensor = true;


Make sure to include item.js in index.html as <script src="client/item.js"></script> before main.js

The logic of creating food is simple. We listen for the create food message and remove food message from the server.

When the server tells us to create food, we initiate onitemUpdate callback. We create an instance of food object in the client using food_object “class”.

When the server tells us to remove an item, we use the onitemremove function, look for the id of the item and, remove it in the game.

Client Side: colide.js

function player_coll (body, bodyB, shapeA, shapeB, equation) {
	//the id of the collided body that player made contact with 
	var key =; 
	//the type of the body the player made contact with 
	var type = body.sprite.type; 
	if (type == "player_body") {
		//send the player collision
		socket.emit('player_collision', {id: key}); 
	} else if (type == "food_body") {
		console.log("items food");
		socket.emit('item_picked', {id: key}); 

We add a new condition to check for a phaser game object of type “food_body”. When we make a collision with this game object, we notify the server with socket.emit('item_picked', {id: key}).

Client Side: Main.js

main.prototype = {
	preload: function() {
		game.stage.disableVisibilityChange = true;
		game.scale.scaleMode = Phaser.ScaleManager.RESIZE;, 0, gameProperties.gameWidth, gameProperties.gameHeight, false, false, false, false);
		game.physics.p2.setBoundsToWorld(false, false, false, false, false)
		game.physics.p2.gravity.y = 0;
		game.physics.p2.applyGravity = false; 
		game.physics.p2.enableBody(game.physics.p2.walls, false); 
		// physics start system

	create: function () {
		game.stage.backgroundColor = 0xE1A193;;
		console.log("client started");
		socket.on("connect", onsocketConnected); 
		//listen for main player creation
		socket.on("create_player", createPlayer);
		//listen to new enemy connections
		socket.on("new_enemyPlayer", onNewPlayer);
		//listen to enemy movement 
		socket.on("enemy_move", onEnemyMove);
		//when received remove_player, remove the player passed; 
		socket.on('remove_player', onRemovePlayer); 
		//when the player receives the new input
		socket.on('input_recieved', onInputRecieved);
		//when the player gets killed
		socket.on('killed', onKilled);
		//when the player gains in size
		socket.on('gained', onGained);
		// check for item removal
		socket.on ('itemremove', onitemremove); 
		// check for item update
		socket.on('item_update', onitemUpdate); 
	update: function () {
		// emit the player input
		//move the player when the player is made 
		if (gameProperties.in_game) {
			//we're making a new mouse pointer and sending this input to 
			//the server.
			var pointer = game.input.mousePointer;
			//Send a new position data to the server 
			socket.emit('input_fired', {
				pointer_x: pointer.x, 
				pointer_y: pointer.y, 
				pointer_worldx: pointer.worldX, 
				pointer_worldy: pointer.worldY, 

Only thing that changed is the create method in main.prototype. Listen for item messages from the server and initiate a function callback

Moving on to Server side.

Server: App.js:

var express = require('express');
//require p2 physics library in the server.
var p2 = require('p2'); 
//get the node-uuid package for creating unique id
var unique = require('node-uuid')

var app = express();
var serv = require('http').Server(app);
//get the functions required to move players in the server.
var physicsPlayer = require('./server/physics/playermovement.js');

app.get('/',function(req, res) {
	res.sendFile(__dirname + '/client/index.html');
app.use('/client',express.static(__dirname + '/client'));

serv.listen(process.env.PORT || 2000);
console.log("Server started.");

var player_lst = [];

//needed for physics update 
var startTime = (new Date).getTime();
var lastTime;
var timeStep= 1/70; 

//the physics world in the server. This is where all the physics happens. 
//we set gravity to 0 since we are just following mouse pointers.
var world = new p2.World({
  gravity : [0,0]

//create a game class to store basic game data
var game_setup = function() {
	//The constant number of foods in the game
	this.food_num = 100; 
	//food object list
	this.food_pickup = [];
	//game size height
	this.canvas_height = 4000;
	//game size width
	this.canvas_width = 4000; 

// createa a new game instance
var game_instance = new game_setup();

//a player class in the server
var Player = function (startX, startY, startAngle) {
  this.x = startX
  this.y = startY
  this.angle = startAngle
  this.speed = 500;
  //We need to intilaize with true.
  this.sendData = true;
  this.size = getRndInteger(40, 100); 
  this.dead = false;

var foodpickup = function (max_x, max_y, type, id) {
	this.x = getRndInteger(10, max_x - 10) ;
	this.y = getRndInteger(10, max_y - 10);
	this.type = type; = id; 

//We call physics handler 60fps. The physics is calculated here. 
setInterval(heartbeat, 1000/60);

//Steps the physics world. 
function physics_hanlder() {
	var currentTime = (new Date).getTime();
	timeElapsed = currentTime - startTime;
	var dt = lastTime ? (timeElapsed - lastTime) / 1000 : 0;
    dt = Math.min(1 / 10, dt);

function heartbeat () {
	//the number of food that needs to be generated 
	//in this demo, we keep the food always at 100
	var food_generatenum = game_instance.food_num - game_instance.food_pickup.length; 
	//add the food 
	//physics stepping. We moved this into heartbeat

function addfood(n) {
	//return if it is not required to create food 
	if (n <= 0) {
	//create n number of foods to the game
	for (var i = 0; i < n; i++) {
		//create the unique id using node-uuid
		var unique_id = unique.v4(); 
		var foodentity = new foodpickup(game_instance.canvas_width, game_instance.canvas_height, 'food', unique_id);
		//set the food data back to client
		io.emit("item_update", foodentity); 

// when a new player connects, we make a new instance of the player object,
// and send a new player message to the client. 
function onNewplayer (data) {
	//new player instance
	var newPlayer = new Player(data.x, data.y, data.angle);
	//create an instance of player body 
	playerBody = new p2.Body ({
		mass: 0,
		position: [0,0],
		fixedRotation: true
	//add the playerbody into the player object 
	newPlayer.playerBody = playerBody;
	console.log("created new player with id " +; =; 	
	this.emit('create_player', {size: newPlayer.size});
	//information to be sent to all clients except sender
	var current_info = {
		x: newPlayer.x,
		y: newPlayer.y,
		angle: newPlayer.angle,
		size: newPlayer.size
	//send to the new player about everyone who is already connected. 	
	for (i = 0; i < player_lst.length; i++) {
		existingPlayer = player_lst[i];
		var player_info = {
			x: existingPlayer.x,
			y: existingPlayer.y, 
			angle: existingPlayer.angle,	
			size: existingPlayer.size
		console.log("pushing player");
		//send message to the sender-client only
		this.emit("new_enemyPlayer", player_info);
	//Tell the client to make foods that are exisiting
	for (j = 0; j < game_instance.food_pickup.length; j++) {
		var food_pick = game_instance.food_pickup[j];
		this.emit('item_update', food_pick); 
	//send message to every connected client except the sender
	this.broadcast.emit('new_enemyPlayer', current_info);


//instead of listening to player positions, we listen to user inputs 
function onInputFired (data) {
	var movePlayer = find_playerid(,; 
	if (!movePlayer || movePlayer.dead) {
		console.log('no player'); 

	//when sendData is true, we send the data back to client. 
	if (!movePlayer.sendData) {
	//every 50ms, we send the data. 
	setTimeout(function() {movePlayer.sendData = true}, 50);
	//we set sendData to false when we send the data. 
	movePlayer.sendData = false;
	//Make a new pointer with the new inputs from the client. 
	//contains player positions in server
	var serverPointer = {
		x: data.pointer_x,
		y: data.pointer_y,
		worldX: data.pointer_worldx, 		
		worldY: data.pointer_worldy
	//moving the player to the new inputs from the player
	if (physicsPlayer.distanceToPointer(movePlayer, serverPointer) <= 30) {
		movePlayer.playerBody.angle = physicsPlayer.movetoPointer(movePlayer, 0, serverPointer, 1000);
	} else {
		movePlayer.playerBody.angle = physicsPlayer.movetoPointer(movePlayer, movePlayer.speed, serverPointer);	
	movePlayer.x = movePlayer.playerBody.position[0]; 
	movePlayer.y = movePlayer.playerBody.position[1];
	//new player position to be sent back to client. 
	var info = {
		x: movePlayer.playerBody.position[0],
		y: movePlayer.playerBody.position[1],
		angle: movePlayer.playerBody.angle

	//send to sender (not to every clients). 
	this.emit('input_recieved', info);
	//data to be sent back to everyone except sender 
	var moveplayerData = {
		x: movePlayer.playerBody.position[0],
		y: movePlayer.playerBody.position[1],
		angle: movePlayer.playerBody.angle,
		size: movePlayer.size
	//send to everyone except sender 
	this.broadcast.emit('enemy_move', moveplayerData);

function onPlayerCollision (data) {
	var movePlayer = find_playerid(; 
	var enemyPlayer = find_playerid(; 
	if (movePlayer.dead || enemyPlayer.dead)
	if (!movePlayer || !enemyPlayer)

	if (movePlayer.size == enemyPlayer)
	//the main player size is less than the enemy size
	else if (movePlayer.size < enemyPlayer.size) {
		var gained_size = movePlayer.size / 2;
		enemyPlayer.size += gained_size; 
		//provide the new size the enemy will become
		this.broadcast.emit('remove_player', {id:});"gained", {new_size: enemyPlayer.size}); 
	} else {
		var gained_size = enemyPlayer.size / 2;
		movePlayer.size += gained_size;
		this.emit('remove_player', {id:}); 
		this.emit("gained", {new_size: movePlayer.size});"killed"); 
		//send to everyone except sender.
		this.broadcast.emit('remove_player', {id:});
	console.log("someone ate someone!!!");

function find_food (id) {
	for (var i = 0; i < game_instance.food_pickup.length; i++) {
		if (game_instance.food_pickup[i].id == id) {
			return game_instance.food_pickup[i]; 
	return false;

function onitemPicked (data) {
	var movePlayer = find_playerid(; 

	var object = find_food(;	
	if (!object) {
		console.log("could not find object");
	//increase player size
	movePlayer.size += 3; 
	//broadcast the new size
	this.emit("gained", {new_size: movePlayer.size}); 
	game_instance.food_pickup.splice(game_instance.food_pickup.indexOf(object), 1);

	io.emit('itemremove', object); 

function playerKilled (player) {
	player.dead = true; 

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;

//call when a client disconnects and tell the clients except sender to remove the disconnected player
function onClientdisconnect() {

	var removePlayer = find_playerid(; 
	if (removePlayer) {
		player_lst.splice(player_lst.indexOf(removePlayer), 1);
	console.log("removing player " +;
	//send message to every connected client except the sender
	this.broadcast.emit('remove_player', {id:});

// find player by the the unique socket id 
function find_playerid(id) {

	for (var i = 0; i < player_lst.length; i++) {

		if (player_lst[i].id == id) {
			return player_lst[i]; 
	return false; 

 // io connection 
var io = require('')(serv,{});

io.sockets.on('connection', function(socket){
	console.log("socket connected"); 
	// listen for disconnection; 
	socket.on('disconnect', onClientdisconnect); 
	// listen for new player
	socket.on("new_player", onNewplayer);
	//we dont need this anymore
	socket.on("move_player", onMovePlayer);
	//listen for new player inputs. 
	socket.on("input_fired", onInputFired);
	socket.on("player_collision", onPlayerCollision);
	//listen if player got items 
	socket.on('item_picked', onitemPicked);

A major change in the server code is the heartbeat function. We check the game state every interval and update the server. We’re always keeping our food amount to 100, so we check the number of food and update the food list. We created a new instance of a game object. The reason we did this is because we might be creating different rooms later.

onItemPicked function is added to be called when player makes collision with a food object. We modify the game state (increase player size, remove food from the server) and update the client.

Notice the new “io.emit” function. It means broadcast to all connected clients including sender.

We successfully added an eating mechanics and generated food in the game. In the next tutorial, we’re going to keep track of player score and display it in the leader board.

<< Part 3