Talking about Pop the Lock game, Game development, HTML5, Javascript and Phaser.
Believe or not, Pop The Lock is a blockbuster. It’s part of a game trend which is gaining a lot of popularity during the last months, where you basically have to solve the same level in the same way more and more times consecutively. Anyway, the game is fun, like other games in the same genre I have already deconstructed like Perfect Square and Circle Path, so here we go with this first step where we’ll analyze the basic movement of the bar and the random position of the lock. This is what you will learn to do: Just cick or tap to make the bar move, then click or tap again to move the bar in the opposite direction, changing lock position. At the moment I do not check for a successful “unlock” as this is meant to be a basic example which will be expanded later. Here is the source code, commented line by line:
// the game itself
var game;
// a selection of colors to be randomly picked and set as background color
var bgColors = [0x62bd18, 0xff5300, 0xd21034, 0xff475c, 0x8f16b2, 0x588c7e, 0x8c4646];
// lock rotatiokn speed
var rotationSpeed = 3;
window.onload = function() {
// creation of the game
game = new Phaser.Game(640, 960, Phaser.AUTO, "");
// creation of the main (and only) game state
game.state.add("PlayGame", playGame);
// starting "PlayGame" game state
game.state.start("PlayGame");
}
var playGame = function(game){};
playGame.prototype = {
preload: function(){
// preloading the images we are going to use
// the ball
game.load.image("ball", "ball.png");
// the rotating bar
game.load.image("bar", "bar.png");
// the ring
game.load.image("ring", "ring.png");
},
create: function(){
// center the game horizontally
game.scale.pageAlignHorizontally = true;
// center the game vertically
game.scale.pageAlignVertically = true;
// setting the scale mode to cover the largest part of the screen possible while showing the entire game
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
// picking a random item in bgColors array
var tintColor = game.rnd.pick(bgColors);
// setting the document background color to tint color
document.body.style.background = "#"+tintColor.toString(16);
// setting the game background color to tint color
game.stage.backgroundColor = tintColor;
// placing the ring in the center of the canvas
var ring = game.add.sprite(game.width / 2, game.height / 2, "ring");
// setting ring anchor point to its middle
ring.anchor.set(0.5);
// setting it to half/transparent
ring.alpha = 0.5;
// placing the ball, no matter where as we will change its position later
this.ball = game.add.sprite(0, 0, "ball");
// setting ball anchor point to its middle
this.ball.anchor.set(0.5);
// this function will place the ball in a random spot around the ring
this.placeBall();
// placing the bar in the middle of the canvas
this.bar = game.add.sprite(game.width / 2, game.height / 2, "bar");
// setting bar anchor point
this.bar.anchor.set(0.5, 1);
// setting bar rotation direction
this.bar.rotationDirection = 0;
// waiting for a game input then call startMoving function
game.input.onDown.add(this.startMoving, this);
},
placeBall: function(){
// choosing a random angle between -180 and 180 then turning it into radians
this.ball.ballAngle = Phaser.Math.degToRad(game.rnd.angle());
// placing the ball accordingly thanks to trigonometry
this.ball.x = game.width / 2 + 175 * Math.cos(this.ball.ballAngle);
this.ball.y = game.height / 2 + 175 * Math.sin(this.ball.ballAngle);
},
startMoving: function(){
// removing the old input listener
game.input.onDown.remove(this.startMoving, this);
// adding a new input listener calling changeDirection function
game.input.onDown.add(this.changeDirection, this);
// setting rotation direction
this.bar.rotationDirection = 1;
},
changeDirection: function(){
// inverting rotation direction
this.bar.rotationDirection *= -1;
// placing the ball elsewhere
this.placeBall();
},
update: function(){
// moving the bar according to its rotation speed
this.bar.angle += rotationSpeed * this.bar.rotationDirection;
}
}
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.