HTML5 prototype of “Don’t Touch The Spikes” built with Phaser 3 and Matter physics
Talking about Don't touch the spikes game, Game development, HTML5, Javascript and Phaser.
Learn cross platform HTML5 game development
Check my Gumroad page for commented source code, games and books.

var game;
var gameOptions = {
}
window.onload = function() {
var gameConfig = {
type: Phaser.AUTO,
width: 480,
height: 640,
backgroundColor: 0x000000,
scene: playGame,
physics: {
default: "matter",
matter: {
debug: true
}
}
}
game = new Phaser.Game(gameConfig);
window.focus();
resize();
window.addEventListener("resize", resize, false);
}
class playGame extends Phaser.Scene{
constructor(){
super("PlayGame");
}
preload(){
this.load.image("wall", "wall.png");
this.load.image("ball", "ball.png");
this.load.image("leftspike", "leftspike.png");
this.load.image("rightspike", "rightspike.png");
}
create(){
this.matter.add.image(10, game.config.height / 2, "wall", null, {
isStatic: true,
label: "leftwall"
});
this.matter.add.image(game.config.width - 10, game.config.height / 2, "wall", null, {
isStatic: true,
label: "rightwall"
});
var leftSpikePath = this.matter.world.fromPath("-45 -35 45 0 -45 35");
var rightSpikePath = this.matter.world.fromPath("-45 0 45 35 45 -35");
this.leftSpike = this.matter.add.image(50, Phaser.Math.Between(50, game.config.height - 50), "leftspike", null, {
isStatic: true,
shape: {
type: "fromVerts",
verts: leftSpikePath
},
label: "spike"
});
this.rightSpike = this.matter.add.image(game.config.width - 50, Phaser.Math.Between(50, game.config.height - 50), "rightspike", null, {
isStatic: true,
shape: {
type: "fromVerts",
verts: rightSpikePath
},
label: "spike"
});
this.ball = this.matter.add.image(game.config.width / 2, game.config.height / 2, "ball");
this.ball.setCircle();
this.ball.setVelocity(7, 0);
this.ball.setBounce(1);
this.ball.setFriction(0);
this.input.on("pointerdown", this.jump, this);
this.matter.world.on("collisionstart", function (e, b1, b2) {
if(b1.label == "spike" || b2.label == "spike"){
this.scene.start("PlayGame");
}
if(b1.label == "leftwall" || b2.label == "leftwall"){
this.rightSpike.y = Phaser.Math.Between(50, game.config.height - 50);
this.ball.setVelocity(7, 0);
}
if(b1.label == "rightwall" || b2.label == "rightwall"){
this.leftSpike.y = Phaser.Math.Between(50, game.config.height - 50);
this.ball.setVelocity(-7, 0);
}
}, this);
}
jump(){
this.ball.setVelocityY(-10);
}
update(){
if(this.ball.y > game.config.height || this.ball.y < 0){
this.scene.start("PlayGame");
}
}
};
function resize() {
var canvas = document.querySelector("canvas");
var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;
var windowRatio = windowWidth / windowHeight;
var gameRatio = game.config.width / game.config.height;
if(windowRatio < gameRatio){
canvas.style.width = windowWidth + "px";
canvas.style.height = (windowWidth / gameRatio) + "px";
}
else{
canvas.style.width = (windowHeight * gameRatio) + "px";
canvas.style.height = windowHeight + "px";
}
}
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.