// create a new scene named “Game”
let gameScene = new Phaser.Scene(‘Game’);
// some parameters for our scene
gameScene.init = function() {
this.playerSpeed = 10;
this.enemySpeed = 5;
this.enemyMaxY = 700;
this.enemyMinY = 20;
}
// load asset files for our game
gameScene.preload = function() {
// afbeeldingen laden
this.load.image(‘background’, ‘assets/background.png’);
this.load.image(‘player’, ‘assets/player.png’);
this.load.image(‘dragon’, ‘assets/dragon.png’);
this.load.image(‘treasure’, ‘assets/treasure.png’);
//audio
this.load.audio(‘anthem’, ‘assets/anthem.mp3’);
};
// executed once, after assets were loaded
gameScene.create = function() {
//audio
let achtergrondGeluid = this.sound.add(‘anthem’)
achtergrondGeluid.play();
//background
let bg = this.add.sprite(0, 0, ‘background’);
// change origin to the top-left of the sprite
bg.setOrigin(0, 0);
// player
this.player = this.add.sprite(40, this.sys.game.config.height / 1.8, ‘player’);
this.player.health = 120;
// Pijltjes besturing
cursors = game.input.keyboard.createCursorKeys();
// scale down
this.player.setScale(0.5);
// goal
this.treasure = this.add.sprite(this.sys.game.config.width - 52, this.sys.game.config.height / 1.8, ‘treasure’);
this.treasure.setScale(0.6);
// group of enemies
this.enemies = this.add.group({
key: 'dragon',
repeat: 5,
setXY: {
x: 320,
y: 110,
stepX: 300,
stepY: 0
}
});
// scale enemies
Phaser.Actions.ScaleXY(this.enemies.getChildren(), -0.5, -0.5);
// set speeds
Phaser.Actions.Call(this.enemies.getChildren(), function(enemy) {
enemy.speed = Math.random() * 2 + 1;
}, this);
// player is alive
this.isPlayerAlive = true;
// reset camera
this.cameras.main.resetFX();
};
// executed on every frame (60 times per second)
gameScene.update = function() {
// only if the player is alive
if (!this.isPlayerAlive) {
return;
}
// check for active input
if (cursors.right.isDown) {
// player walks
this.player.x += this.playerSpeed;
}
else if (cursors.left.isDown) {
this.player.x -= this.playerSpeed;
}
// treasure collision
if (Phaser.Geom.Intersects.RectangleToRectangle(this.player.getBounds(), this.treasure.getBounds())) {
this.gameOver();
}
// enemy movement and collision
let enemies = this.enemies.getChildren();
let numEnemies = enemies.length;
for (let i = 0; i < numEnemies; i++) {
// move enemies
enemies[i].y += enemies[i].speed;
// reverse movement if reached the edges
if (enemies[i].y >= this.enemyMaxY && enemies[i].speed > 0) {
enemies[i].speed *= -1;
} else if (enemies[i].y <= this.enemyMinY && enemies[i].speed < 0) {
enemies[i].speed *= -1;
}
// enemy collision
if (Phaser.Geom.Intersects.RectangleToRectangle(this.player.getBounds(), enemies[i].getBounds())) {
this.player.health -= 2;
if (this.player.health <= 0) {
gameScene.gameOver();
break;
}
}
}
};
gameScene.gameOver = function() {
// flag to set player is dead
this.isPlayerAlive = false;
// shake the camera
this.cameras.main.shake(500);
// fade camera
this.time.delayedCall(250, function() {
this.cameras.main.fade(250);
}, [], this);
// restart game
this.time.delayedCall(500, function() {
this.scene.restart();
}, [], this);
};
// our game’s configuration
let config = {
type: Phaser.AUTO,
width: 1080,
height: 720,
scene: gameScene
};
// create the game, and pass it the configuration
let game = new Phaser.Game(config);