How to make with JavaScript: Part 5 - Snake Eyes


This is the fifth part of our tutorial series on creating with JavaScript and Phaser! Take a look at Part 1 if you are just starting with this series, or jump back to Part 4 if you need to.

Look at the example and also look at the source code for this part.

This game clones all the core features of, including mouse-following controls, snake collisions, food, snake growth, eyes, and more.

Eye Concepts

In this part of our tutorial series we will be adding eyes to the snake heads. Now let's go over what we want the eyes to do.

Firstly, we need white circles for the back parts of the eyes. These white circles will be locked to the snake heads using a lock constraint. Next, we need black circles for the front parts of the eyes. These black circles will need to stay within the white circles, just so that they can move around like eye pupils. We will keep these black circles near the centers of the white circles using distance constraints, meaning that they can only move a certain distance away from their centers. To make these black circles look towards the mouse pointer, we will apply small forces to them in the direction of the mouse from the head.


As far as organization, we will make two new classes, Eye and EyePair. The Eye class will handle constraints for a single eye to the head, and the EyePair class will handle positioning of two eyes.

As with other parts of the snakes, the eyes will have to be scaled and destroyed. There will need to be methods for this in the eye classes that can be called from the snake class controlling the eyes.


Let's start coding. Take a look at eye.js. First we create the Eye function:

Eye = function(game, head, scale) { = game;
    this.head = head;
    this.scale = scale;
    this.eyeGroup =;
    this.collisionGroup =;
    this.debug = false;

    //constraints that will hold the circles in place
    //the lock will hold the white circle on the head, and the distance
    //constraint (dist) will keep the black circle within the white one
    this.lock = null;
    this.dist = null;

    //initialize the circle sprites
    this.whiteCircle =
        this.head.body.x, this.head.body.y, "eye-white"
    this.whiteCircle = this.initCircle(this.whiteCircle);

    this.blackCircle =
        this.whiteCircle.body.x, this.whiteCircle.body.y, "eye-black"
    this.blackCircle = this.initCircle(this.blackCircle);
    this.blackCircle.body.mass = 0.01;

We create some groups, then we create properties for the lock and distance constraints, though we do not actually create the constraints yet. We create the black and white circles, then call initCircle on them to add some basic physics properties to these circle sprites. If you look in the asset folder, you will see the new circle images that we are using.

Now let's look at initCircle in the Eye prototype:

initCircle: function(circle) {
    circle.scale.setTo(this.scale);, this.debug);
    //give the circle a circular physics body
    return circle;

Here we simply give the circles circular physics bodies, remove collisions for these bodies, and add the circles to a group. Now we need a way to add the constraints.


Let's look at the updateConstraints method, which should be called at least once to add the constraints:

updateConstraints: function(offset) {
    //change where the lock constraint of the white circle
    //is if it already exists
    if (this.lock) {
        this.lock.localOffsetB = [
    //create a lock constraint if it doesn't already exist
    else {
        this.lock =
            this.whiteCircle.body, this.head.body, offset, 0

    //change the distance of the distance constraint for
    //the black circle if it exists already
    if (this.dist) {
        this.dist.distance =*0.25);
    //create a distance constraint if it doesn't exist already
    else {
        this.dist =
            this.blackCircle.body, this.whiteCircle.body, this.whiteCircle.width*0.25

This method take's a parameter offset which is an Array in the form [x,y] telling where to offset the eye from the center of the head, placing a lock constraint in that position. Why do we check whether constraints exist or not? If they already exist, we are probably changing the scale of the eye, so we need to simply change where the eye is offset and how far the black circle can go within the white circle.


Next we have the scale method:

setScale: function(scale) {
    this.scale = scale;
    for (var i = 0 ; i < this.eyeGroup.children.length ; i++) {
        var circle = this.eyeGroup.children[i];
        //change the radii of the circle bodies using pure p2 physics[0].radius =*0.5);


We iterate through the circles, scaling up their sprites, then scaling up their physics bodies.


We need an update method to move the black part of the eyes based on mouse position. Let's take a look:

update: function() {
    var mousePosX =;
    var mousePosY =;
    var headX = this.head.body.x;
    var headY = this.head.body.y;
    var angle = Math.atan2(mousePosY-headY, mousePosX-headX);
    var force = 300;
    //move the black circle of the eye towards the mouse

Here we find the angle of the line formed by the center of the snake head and the mouse position. Then we apply a force on the black circle towards the mouse. I ensured a specific force because a force that is too small would make the eyes lazy and a force that is too large could cause physics glitches.


Finally, we have a destroy method:

destroy: function() {

We must make sure to destroy the constraints!


Now take a look at eyePair.js. This class will create two eyes, then act as a driver for them that calls their methods:

EyePair = function(game, head, scale) { = game;
    this.head = head;
    this.scale = scale;
    this.eyes = [];

    this.debug = false;

    //create two eyes
    var offset = this.getOffset();
    this.leftEye = new Eye(, this.head, this.scale);
    this.leftEye.updateConstraints([-offset.x, -offset.y]);

    this.rightEye = new Eye(, this.head, this.scale);
    this.rightEye.updateConstraints([offset.x, -offset.y]);

We create two eyes, then call their updateConstraints methods using offsets obtained from a method called getOffset. Let's take a look at it now in the EyePair prototype:

getOffset: function() {
    var xDim = this.head.width*0.25;
    var yDim = this.head.width*.125;
    return {x: xDim, y: yDim};

We position the eyes relative to the width of the snake head. So if the head is scaled up, the width of the head is larger, and therefore the offset will be different.

Finally, we have methods that call the eyes' methods for scaling, updating, and destruction:

 * Set the scale of the eyes
 * @param  {Number} scale new scale
setScale: function(scale) {
    //update constraints to place them at the right offset
    var offset = this.getOffset();
    this.leftEye.updateConstraints([-offset.x, -offset.y]);
    this.rightEye.updateConstraints([offset.x, -offset.y]);
 * Call from snake update loop
update: function() {
    for (var i = 0 ; i < this.eyes.length ; i++) {
 * Destroy this eye pair
destroy: function() {


Of course, we need to still implement this EyePair within the Snake class. Open snake.js. First we initialize an EyePair object:

this.eyes = new EyePair(, this.head, this.scale);

In the snake's update method, we need to call the eyes update method:


We scale the eyes in the snake scale method:


and we destroy the eyes in the snake destroy method:


And that's what it takes to give a snake eyes! In Part 6 we will add shadows under the snakes.

Up Next