Phaser. Unit 8. Dino game clone (part II)

Introduction

In this unit we will build a clone of Google Chrome “secret” Dino game (the original game can be revealed by entering “chrome://dino” in the address bar). We will use simple web technologies such as HTML, CSS, JavaScript and Phaser 3, so that any user can play on any device just using any browser (not only Chrome).

The result is a game which looks almost the same as the original one using less than 10% lines of code. As it can be seen from the Chromium source code, the original game is made of more than 4000 lines of code, whereas the one in this unit has been developed using around 300 lines of code. This means that Phaser could be a much better option than simple JavaScript when thinking about game development.

Some features

This version of the game clones almost every feature of the original one:

  • The speed of the game is increased gradually.
  • Both cactuses and birds will appear randomly to hurt you.
  • Two scores are shown: current and highest.
  • Each time the user reaches 100 points, the score blinks and a sound is played.
  • It can be played on desktop computers using both the mouse and the keyboard:
    • By clicking on the screen to jump or to restart the game.
    • Using the up arrow key or the space bar to jump, the down arrow key to duck under the enemy, and the enter key to restart the game.
  • It can be played on mobile devices by touching the screen, the same way as any single button game.
  • The game is automatically adjusted to fit the screen size, even if it changes after the page is loaded (for example, in case the user rotates the device).
  • Vibration is activated on mobile devices when the game over screen appears.

CSS code (styles.css)

We are going to insert some CSS code to remove any margin and padding, and also to remove the scrollbars, so that the whole window size is used when the game is started:

html, body {
  margin: 0px;
  padding: 0px;
  overflow: hidden;
  height: 100%;
}

HTML code (index.html)

We are going to use a single HTML file to link all the files in the project. This is going to be the “index.html” file:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="description" content="Dino game clone using Phaser">
  <meta name="keywords" content="HTML, CSS, JavaScript, Phaser">
  <meta name="author" content="Fernando Ruiz Rico">
  
  <title>Dino Phaser</title>

  <link href="styles.css" rel="stylesheet">
</head>

<body>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
  <script src="preload.js"></script>
  <script src="create.js"></script>
  <script src="update.js"></script>
  <script src="config.js"></script>
</body>

</html>

Constants and Phaser configuration (config.js)

We are going to define some constants (i.e. screen width and height and text styles) so that we can use them inside every JavaScript file. We will also define a function to set the initial values of some variables before the game is started, together with the usual Phaser configuration, which must be performed as usual.

Moreover we are adding a piece of code to check whether the screen is being resized (i.e. when the device is rotated or the window size is adjusted) so that the whole game is reloaded to fit the new values of the screen width and height.

const WIDTH = window.innerWidth;
const HEIGHT = window.innerHeight;

const TEXT_STYLE = { fill: "#535353", font: '900 35px Courier', resolution: 10 };

function initVariables() {
  this.isGameRunning = false;
  this.gameSpeed = 10;
  this.respawnTime = 0;
  this.score = 0;
}

const config = {
  type: Phaser.AUTO,
  width: WIDTH,
  height: HEIGHT,
  pixelArt: true,
  transparent: true,
  physics: {
    default: 'arcade',
    arcade: {
      debug: false
    }
  },
  scene: { preload: preload, create: create, update: update }
};

let game = new Phaser.Game(config);

let resizing = false;
window.addEventListener('resize', () => {
  if (resizing) clearTimeout(resizing);
  resizing = setTimeout(() => window.location.reload(), 500);
});

The assets

We will need some images, sprites and sounds. You may use any assets you like, but we are providing the original ones so that you can easily develop the same game as the one available in Google Chrome. Some examples are shown below, and also a zip file containing all the assets can be downloaded here.

Images

Cloud
Cactuses
Game over
Restart

Sprites

Dino waiting and running
Dino down
Bird enemy

Sounds

Hit
Jump
Reach

Loading all the assets (preload.js)

So that we can use all those images, sprites and sounds, they have to be loaded previously. That is the purpose of the “preload()” function.

Inside the “initSounds()” and “createAnims()” we will set the volume of the sounds and the frames of the animations respectively.

function preload() {
  this.load.audio('jump', 'assets/jump.m4a');
  this.load.audio('hit', 'assets/hit.m4a');
  this.load.audio('reach', 'assets/reach.m4a');

  this.load.image('ground', 'assets/ground.png');
  this.load.image('dino-idle', 'assets/dino-idle.png');
  this.load.image('dino-hurt', 'assets/dino-hurt.png');
  this.load.image('restart', 'assets/restart.png');
  this.load.image('game-over', 'assets/game-over.png');
  this.load.image('cloud', 'assets/cloud.png');

  this.load.image('obsticle-1', 'assets/cactuses_small_1.png');
  this.load.image('obsticle-2', 'assets/cactuses_small_2.png');
  this.load.image('obsticle-3', 'assets/cactuses_small_3.png');
  this.load.image('obsticle-4', 'assets/cactuses_big_1.png');
  this.load.image('obsticle-5', 'assets/cactuses_big_2.png');
  this.load.image('obsticle-6', 'assets/cactuses_big_3.png');

  this.load.spritesheet('dino', 'assets/dino-run.png', { frameWidth: 88, frameHeight: 94 });
  this.load.spritesheet('dino-down', 'assets/dino-down.png', { frameWidth: 118, frameHeight: 94 });
  this.load.spritesheet('enemy-bird', 'assets/enemy-bird.png', { frameWidth: 92, frameHeight: 77 });
}

function initSounds() {
  this.jumpSound = this.sound.add('jump', { volume: 0.75 });
  this.hitSound = this.sound.add('hit', { volume: 0.75 });
  this.reachSound = this.sound.add('reach', { volume: 0.75 });
}

function createAnims() {
  this.anims.create({
    key: 'dino-waiting',
    frames: this.anims.generateFrameNumbers('dino', { start: 0, end: 1 }),
    frameRate: 1,
    repeat: -1
  })

  this.anims.create({
    key: 'dino-run',
    frames: this.anims.generateFrameNumbers('dino', { start: 2, end: 3 }),
    frameRate: 10,
    repeat: -1
  })

  this.anims.create({
    key: 'dino-down-anim',
    frames: this.anims.generateFrameNumbers('dino-down', { start: 0, end: 1 }),
    frameRate: 10,
    repeat: -1
  })

  this.anims.create({
    key: 'enemy-bird-anim',
    frames: this.anims.generateFrameNumbers('enemy-bird', { start: 0, end: 1 }),
    frameRate: 6,
    repeat: -1
  })
}

Creating the scores, the clouds, the dino, and the game over message (create.js)

  • Scores: Two boxes are used to print both the current and highest scores on the right hand side of the screen.
  • Clouds: A loop will be used to create 3 clouds at once printing the same image on random positions.
  • Dino: The dino will collide with the ground below, and it will jump using the keys defined here (in our case we will use both the up cursor key and the space bar). The mouse functionality is already available by default.
  • Game over: A message will be shown when the user collides with either the birds or the cactuses. After the user clicks on it, this message will be hidden using the “setAlpha(0)” function call.
function initColliders() {
  this.physics.add.collider(this.dino, this.obsticles, () => {
    initVariables.call(this);
    updateHighScore.call(this);
    this.hitSound.play();
    this.physics.pause();
    this.anims.pauseAll();
    this.dino.setTexture('dino-hurt');
    this.gameOverScreen.setAlpha(1);
    if (window.navigator.vibrate) window.navigator.vibrate(50);
  }, null, this);
}

function createScore() {
  this.scoreText = this.add.text(WIDTH - 25, HEIGHT / 3, "", TEXT_STYLE).setOrigin(1, 1);
  this.highScoreText = this.add.text(0, HEIGHT / 3, "", TEXT_STYLE).setOrigin(1, 1).setAlpha(0.75);
}

function createClouds() {
  this.clouds = this.add.group();
  for (let i = 0; i < 3; i++) {
    const x = Phaser.Math.Between(0, WIDTH);
    const y = Phaser.Math.Between(HEIGHT / 3, HEIGHT / 2);
    this.clouds.add(this.add.image(x, y, 'cloud'));
  }
  this.clouds.setAlpha(0);
}

function createDino() {
  this.dino = this.physics.add.sprite(0, HEIGHT / 1.5, 'dino-idle').setSize(50).setGravityY(5000).setOrigin(0, 1);
  this.dino.play('dino-waiting', true);
  this.physics.add.collider(this.dino, this.belowGround);
  this.cursors = this.input.keyboard.createCursorKeys();
  this.spaceBar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);

  this.startText = this.add.text(WIDTH - 15, HEIGHT / 1.5, "Press to play", TEXT_STYLE).setOrigin(1);
}

function createGameOverScreen() {
  this.gameOverText = this.add.image(0, 0, 'game-over');
  this.restart = this.add.image(0, 50, 'restart').setInteractive();
  this.gameOverScreen = this.add.container(WIDTH / 2, HEIGHT / 2 - 50).setAlpha(0);
  this.gameOverScreen.add([this.gameOverText, this.restart]);
  this.enter = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);

  this.restart.on('pointerdown', () => restartGame.call(this));
}

function createWorld() {
  this.ground = this.add.tileSprite(0, HEIGHT / 1.5, 88, 26, 'ground').setOrigin(0, 1);
  this.belowGround = this.add.rectangle(0, HEIGHT / 1.5, WIDTH, HEIGHT / 3).setOrigin(0, 0);
  this.physics.add.existing(this.belowGround);
  this.belowGround.body.setImmovable();
  this.obsticles = this.physics.add.group();
}

function create() {
  initVariables.call(this);

  initSounds.call(this);
  createAnims.call(this);
  createWorld.call(this);
  createClouds.call(this);
  createScore.call(this);
  createDino.call(this);
  initColliders.call(this);
  createGameOverScreen.call(this);

  setInterval(() => updateScore.call(this), 100);
}

Controlling the player and displaying the birds and the cactuses (update.js)

  • Starting the game and building the ground: The “startGame()” method will build the ground progresively, just once, when the game is started the very first time.
  • Restarting the game: After the game over message appears, the user may click on the screen to start a new game. This is done inside the “restartGame()” method, which hides the message using the “setAlpha(0)” method call, restarts the score and resumes all the physics.
  • Updating the scores: The function “updateScore()” will not only update the current score, but it will also increment the game speed, and it will play a sound each time the user reaches 100 points, making the numbers blink at the same time. The “updateHighScore()” function will keep a record of the highest score each time a game is over.
  • Placing and moving the birds and the cactuses: The “placeObsticle()” function will create either birds or cactuses randomly, and the “update()” function will move everything to the left except the dino, which will always remain in the same position.
function startGame() {
  this.startText.destroy();
  this.dino.setVelocityX(80);
  this.dino.play('dino-run', 1);

  function buildGround() {
    this.ground.width += (WIDTH / 100);
    if (this.ground.width > WIDTH) {
      clearInterval(interval);
      this.isGameRunning = true;
      this.ground.width = WIDTH;
      this.dino.setVelocityX(0);
      this.scoreText.setAlpha(1);
      this.clouds.setAlpha(1);
    }
  }
  const interval = setInterval(() => buildGround.call(this), 15);
}

function restartGame() {
  this.dino.setVelocityY(0);
  this.dino.body.height = 92;
  this.dino.body.offset.y = 0;
  this.physics.resume();
  this.anims.resumeAll();
  this.obsticles.clear(true, true);
  this.gameOverScreen.setAlpha(0);
  this.isGameRunning = true;
}

function updateScore() {
  if (!this.isGameRunning) return;

  this.score++;
  this.gameSpeed += 0.01;

  if (this.score % 100 == 0) {
    this.reachSound.play();

    this.tweens.add({
      targets: this.scoreText,
      duration: 100,
      repeat: 3,
      alpha: 0,
      yoyo: true
    })
  }

  this.scoreText.setText(("0000" + this.score).slice(-5));
}

function updateHighScore() {
  this.highScoreText.x = this.scoreText.x - this.scoreText.width - 30;

  const highScore = this.highScoreText.text.substr(this.highScoreText.text.length - 5);
  const newScore = Number(this.scoreText.text) > Number(highScore) ? this.scoreText.text : highScore;

  this.highScoreText.setText(`HI ${newScore}`);
}

function placeObsticle() {
  let obsticle;
  const obsticleNum = Phaser.Math.Between(1, 7);

  if (obsticleNum > 6) {
    obsticle = this.obsticles.create(WIDTH, HEIGHT / 1.5 - Phaser.Math.Between(20, 50), 'enemy-bird');
    obsticle.body.height /= 1.5
    obsticle.play('enemy-bird-anim', 1);
  } else {
    obsticle = this.obsticles.create(WIDTH, HEIGHT / 1.5, `obsticle-${obsticleNum}`)
  }

  obsticle.setOrigin(0, 1).setImmovable();
}

function update(time, delta) {
  if (this.isGameRunning) {
    this.ground.tilePositionX += this.gameSpeed;

    this.respawnTime += delta * this.gameSpeed * 0.08;
    if (this.respawnTime >= 1500) {
      placeObsticle.call(this);
      this.respawnTime = 0;
    }

    this.obsticles.getChildren().forEach(obsticle => {
      obsticle.x -= this.gameSpeed;
      if (obsticle.getBounds().right < 0) {
        this.obsticles.killAndHide(obsticle);
      }
    })

    this.clouds.getChildren().forEach(cloud => {
      cloud.x -= 0.5;
      if (cloud.getBounds().right < 0) {
        cloud.x = WIDTH;
      }
    })
  }
  else if (this.gameOverScreen.alpha && this.enter.isDown) {
    restartGame.call(this);
  }

  if (this.dino.body.onFloor() && !this.dino.body.velocity.x && !this.gameOverScreen.alpha &&
    (this.cursors.up.isDown || this.spaceBar.isDown || this.input.activePointer.isDown)) {
    this.jumpSound.play();
    this.dino.anims.stop();
    this.dino.setVelocityY(-1600);
    this.dino.setTexture('dino', 0);

    if (!this.isGameRunning) startGame.call(this);
  }
  else if (this.isGameRunning) {
    if (this.cursors.down.isDown) {
      this.dino.play('dino-down-anim', true);
      this.dino.body.height = 58;
      this.dino.body.offset.y = 34;
    }
    else {
      this.dino.play('dino-run', true);
      this.dino.body.height = 92;
      this.dino.body.offset.y = 0;
    }
  }
}

Proposed exercise: Using your own assets

Create a new version of the Dino game using your own assets (images, sprites and sounds). You can also change any other things you like.

Do not forget to create all the files required to run the code in this unit:

  • styles.css
  • index.html
  • preload.js
  • create.js
  • update.js
  • config.js
  • assets folder

You may find many free assets in OpenGameArt, and some new versions of the Dino game in github:

Enjoy the game!

You can enjoy playing this wonderful game online here.

Phaser 3 game examples written in TypeScript

Play the games

Alpha adjust

Alpha adjust

Asteroid

Asteroid

Blockade

Blockade

Blocks

Blocks

Breakout

Breakout

Candy Crush

Candy Crush

Clocks

Clocks

Coin runner

Coin runner

Endless runner

Endless runner

Flappy Bird

Flappy Bird

Snake

Snake

Space Invaders

Space Invaders

Super Mario Land

Super Mario Land

Tank

Tank

Run the games in your computer

Since all the games are open source, you can run all the games in your own computer. Just follow these steps:

  1. Install Nodejs (in case you don’t have it yet).
  2. Download the zip file containing the source code of the game you like.
  3. Open a terminal inside the project folder.
  4. Execute “npm install”.
  5. Execute “npm run dev”.
  6. Open the address “http://localhost:8080/” on your browser.

You can download the source code from the following links:

More information

You will find more information on github.

Phaser. Unit 9. Drawing maps from plain text

Introduction

In this unit we will define a simple Phaser game that can be used as a template to define an unlimited number of levels, enemies and objects. The map will be drawn from a simple array of tiles where several characters will be used to choose the positions of each item.

We will also use npm and webpack to provide a friendly development environment, so that the application will be automatically compiled and restarted each time a file is changed. A web server is also provided by webpack to test the application, and a production version of the code can be also obtained to upload it to any public server.

Some features

These are some of the features to be implemented:

  • Show several types of animated objects that can be collected individually.
  • Keep several counters with the number of the items collected.
  • Show several types of animated enemies that can be killed just by jumping over them.
  • Move automatically the enemies from left to right and viceversa using spritesheets that contain only a single direction.
  • Draw the map using plain characters that are matched againt tiles, objects, enemies, player, and goal.
  • Easily define an unlimited number of levels and let the player jump from one level to the other just by reaching a goal in the map.
  • Display a minimap automatically generated from the real sized map.
  • Play a different background music for each level, and a different sound when either objects are collected or enemies are killed.
  • Allow the user to play the game on mobile phones and tablets with touch controls to jump and also to activate both left and right movements.
  • Implement PWA features to let the users install the application in mobile devices and also to lock the screen in landscape mode.
  • Adjust the game automatically to the size of the window where the game is displayed, and also after the window is resized on desktop computers.

Loading the assets (src/scripts/scenes/preloadScene.ts)

Inside this file we will load all the assets (both audio and images) so that they are ready to be played or displayed after the game is started:

import Levels from "../components/levels/levels"

export default class PreloadScene extends Phaser.Scene {
  constructor() {
    super({ key: 'PreloadScene' })
  }

  preload() {
    const images = ['tile-left', 'tile-middle', 'tile-right', 'tile-single', 'controls', 'background', 'goal']
    images.forEach(img => {
      this.load.image(img, `assets/img/${img}.png`)
    })

    this.load.spritesheet('player', 'assets/img/player.png', { frameHeight: 165, frameWidth: 120 })
    this.load.spritesheet('coin', 'assets/img/coin.png', { frameHeight: 42, frameWidth: 42 })
    this.load.spritesheet('key', 'assets/img/key.png', { frameHeight: 176, frameWidth: 176 })
    this.load.spritesheet('bee', 'assets/img/bee.png', { frameHeight: 100, frameWidth: 128 })
    this.load.spritesheet('slime', 'assets/img/slime.png', { frameHeight: 68, frameWidth: 112 })
    // @ts-ignore
    this.load.spine('boy', 'assets/spine/boy.json', 'assets/spine/boy.atlas')

    const audios = ['bee', 'slime', 'coin', 'key', 'door', 'hurt', 'music']
    audios.forEach(audio => {
      this.load.audio(audio, `assets/audio/${audio}.mp3`)
    })

    for(let i=0; i<Levels.length; i++) {
      this.load.audio(`level${i}`, `assets/audio/level${i}.mp3`)
    }
  }

  create() {
    this.scene.start('MainScene')
  }
}

Drawing the map and placing everything on it

We will use plain characters to represent the player, the goal, the objects, the enemies and the ground:

  • P: Player
  • G: Goal (the door to jump to the next level)
  • Objects:
    • C: Coin
    • K: Key (at least 1 key is required to open the door to the next level)
  • Enemies:
    • B: Bee (a flying enemy)
    • S: Slime (an enemy crawling on the ground)
  • [//////]: The ground

Defining the levels (src/scripts/components/levels/levels.ts)

As it can be seen in the “levels.ts” file, the map of each level can be easily defined using simple characters. We can also place all the enemies, objects, player and goal at any position in the map:

type Level = string[]

// prettier-ignore
const level0: Level = [
  '   K    S       ',
  '  [/////]       ',
  '                ',
  '   P         G  ',
  '[/////]   [////]',
]

// prettier-ignore
const level1: Level = [
  '               B  K                   ',
  '               [/////]                ',
  '              C     C                 ',
  '   S        B            S    S    G  ',
  '[//]       [////]   [////]   [//////] ',
  '    C   C                             ',
  ' P        B                           ',
  '[///]  [//]                           ',
]

// prettier-ignore
const level2: Level = [
  ' P                     [//]       G   ',
  '[/]    C   B        C           [/]   ',
  '         [//]     [//]                ',
  '      S                     B  B      ',
  ' C  [//]      B            [///]      ',
  '            [///]     []  C        C  ',
  '[]        K                           ',
  '         [/]            [/]      [/]  '
]

// prettier-ignore
const level3: Level = [
  '       C                                    ',
  '                      C                 C   ',
  '      [///]                 C      [////]   ',
  '           [///]       S  [//]  K           ',
  '                   [///]      [///]         ',
  '             S                              ',
  '         [///]           C              G   ',
  '      C            C               [////]   ',
  '  P                     S  B                ',
  '[//////]       [///]  [////]                '
]

// prettier-ignore
const level4: Level = [
  '                        C  C         S                                  ',
  '                                 [//////]      K     B               G  ',
  '                  C    [/////]                [///////]          [////] ',
  '                                                                        ',
  '  P      S     B    B              C       S     S         S            ',
  '[////////]  [///////]             [/]    [////////////////////]         '
]

const Levels: Level[] = [level0, level1, level2, level3, level4]
export default Levels

Matching characters to textures (src/scripts/components/map/map.ts)

In case we want to add new enemies or objects, we will have to match a new character against the new texture:

import Levels from '../levels/levels'
export class Map {
  info: TilesConfig[]
  size: MapSize

  public static calcCurrentLevel(currentLevel: number) {
    const MAX_LEVELS = Levels.length
    return currentLevel % MAX_LEVELS
  }

  constructor(currentLevel: number) {
    const TILE_SIZE = 96
    const config: any = {
      '[': {
        type: 'tile',
        texture: 'tile-left'
      },
      '/': {
        type: 'tile',
        texture: 'tile-middle'
      },
      ']': {
        type: 'tile',
        texture: 'tile-right'
      },
      'G': {
        type: 'goal',
        texture: 'goal'
      },
      'C': {
        type: 'object',
        texture: 'coin'
      },
      'K': {
        type: 'object',
        texture: 'key'
      },      
      'S': {
        type: 'enemy',
        texture: 'slime'
      },
      'B': {
        type: 'enemy',
        texture: 'bee'
      },
      'P': {
        type: 'player',
        texture: 'player'
      }
    }

    const map = Levels[Map.calcCurrentLevel(currentLevel)]

    // the player can jump a bit higher than the map's height
    const paddingTop = 4 * TILE_SIZE

    this.size = {
      x: 0,
      y: 0,
      width: map[0].length * TILE_SIZE,
      height: map.length * TILE_SIZE + paddingTop
    }
    this.info = []

    map.forEach((row, y) => {
      for (let i = 0; i < row.length; i++) {
        const tile = row.charAt(i)
        const x = i
        if (tile !== ' ') {
          let info = { ...config[tile.toString()], x: x * TILE_SIZE, y: y * TILE_SIZE + paddingTop }
          this.info.push(info)
        }
      }
    })
  }
}

Enemies (src/scripts/components/enemies/enemiesGroup.ts)

All the tiles in the map are read and the enemies are created depending on the texture field:

import BeeSprite from './bee'
import SlimeSprite from './slime'

export default class EnemiesGroup extends Phaser.GameObjects.Group {
  tiles: TilesConfig[]
  TILE_SIZE = 96
  constructor(scene: Phaser.Scene, tilesConfig: TilesConfig[]) {
    super(scene)

    this.tiles = tilesConfig.filter(tile => tile.type === 'tile')
    let enemyTypes = tilesConfig.filter(tile => tile.type === 'enemy')

    let enemies: Array<BeeSprite | SlimeSprite> = []
    enemyTypes.forEach(enemy => {
      switch (enemy.texture) {
        case 'bee':
          enemies.push(new BeeSprite(scene, enemy.x, enemy.y))
          break
        case 'slime':
          enemies.push(new SlimeSprite(scene, enemy.x, enemy.y))
          break
      }
    })
    this.addMultiple(enemies)
  }

  update() {
    // check if the enemy should change its direction
    // @ts-ignore
    this.children.iterate((enemy: BeeSprite | SlimeSprite) => {
      if (enemy.dead) return

      let enemyIsMovingRight = enemy.body.velocity.x >= 0

      let hasGroundDetection = this.tiles.filter(tile => {
        let enemyPositionX = enemyIsMovingRight ? enemy.body.right : enemy.body.left
        let x = enemyPositionX + 32 > tile.x && enemyPositionX - 32 < tile.x + this.TILE_SIZE
        let y =
          enemy.body.bottom + this.TILE_SIZE / 2 > tile.y &&
          enemy.body.bottom + this.TILE_SIZE / 2 < tile.y + this.TILE_SIZE
        return x && y
      })

      if (hasGroundDetection.length === 0) {
        //@ts-ignore
        enemy.body.setVelocityX(enemy.body.velocity.x * -1)
        enemy.setFlipX(!enemyIsMovingRight)
      }
    }, null)
  }
}

The bee (src/scripts/components/enemies/bee.ts)

Frames 0 and 1 represent the usual movement, while frame number 2 is used when the bee is dead:

Bee enemy (assets/img/bee.png)
Dead bee (assets/audio/bee.mp3)
import EnemyClass from './enemyClass'

export default class BeeSprite extends EnemyClass {

  constructor(scene: Phaser.Scene, x: number, y: number) {
    super(scene, x, y, 'bee')
    scene.add.existing(this)
    scene.physics.add.existing(this)

    scene.anims.create({
      key: 'fly',
      frames: scene.anims.generateFrameNumbers('bee', { start: 0, end: 1 }),
      frameRate: 8,
      repeat: -1
    })
    this.play('fly')

    //@ts-ignore
    this.body.setVelocityX(-120)
    this.setOrigin(0.5, 1)
    this.body.setSize(80, 135)
    this.body.setOffset((this.width - 80) / 2, 30)

    this.audio = scene.sound.add('bee', { volume: 1.0 })
  }

  update() { }

  kill() {
    if (this.dead) return

    this.body.setSize(80, 40)

    this.removeEnemy(2) // Frame number 2 is used when the bee is dead
  }
}

The slime (src/scripts/components/enemies/slime.ts)

Frames from 0 to 4 represent the usual movement, while frame number 5 is used when the slime is dead:

Slime enemy (assets/img/slime.png)
Dead slime (assets/audio/slime.mp3)
import EnemyClass from './enemyClass'

export default class SlimeSprite extends EnemyClass {

  constructor(scene: Phaser.Scene, x: number, y: number) {
    super(scene, x, y, 'slime')
    scene.add.existing(this)
    scene.physics.add.existing(this)

    scene.anims.create({
      key: 'crawl',
      frames: scene.anims.generateFrameNumbers('slime', { start: 0, end: 4 }),
      frameRate: 6,
      yoyo: true,
      repeat: -1
    })
    this.play('crawl')

    //@ts-ignore
    this.body.setVelocityX(-60)
    this.setOrigin(0.5, 1)
    this.setScale(1)
    this.body.setSize(this.width - 40, this.height - 20)
    this.body.setOffset(20, 20)

    this.audio = scene.sound.add('slime', { volume: 1.0 })
  }

  update() { }

  kill() {
    if (this.dead) return

    this.removeEnemy(5) // Frame number 5 is used when the bee is dead
  }
}

Killing the enemies (src/scripts/scenes/mainScene.ts)

The player kills an enemy when it jumps over it. As a reward, it will get 5 points:

... 
   this.physics.add.overlap(this.player, this.enemiesGroup, (player: Player, enemy: EnemySprite) => {
      if (enemy.dead) return
      if (enemy.body.touching.up && player.body.touching.down) {
        this.score += 5
        scoreText.update(this.score, this.keys)
        player.killEnemy()
        enemy.kill()
      } else {
        player.kill()
      }
    })
...

Objects (src/scripts/components/objects/objectsGroup.ts)

All the tiles in the map are read and the objects are created depending on the texture field:

import CoinSprite from './coin'
import KeySprite from './key'

export default class ObjectsGroup extends Phaser.GameObjects.Group {
  constructor(scene: Phaser.Scene, tilesConfig: TilesConfig[]) {
    super(scene)

    let objectTypes = tilesConfig.filter(tile => tile.type === 'object')

    let objects: Array<CoinSprite | KeySprite> = []
    objectTypes.forEach(object => {
      switch (object.texture) {
        case 'coin':
          objects.push(new CoinSprite(scene, object.x, object.y))
          break
        case 'key':
          objects.push(new KeySprite(scene, object.x, object.y))
          break
      }
    })

    this.addMultiple(objects)
  }
}

The coin (src/scripts/components/objects/coin.ts)

All the frames are selected to create the animation:

The coin (assets/img/coin.png)
Collecting coin (assets/audio/coin.mp3)
import ObjectClass from "../objects/objectClass";

export default class CoinSprite extends ObjectClass {

  constructor(scene: Phaser.Scene, x: number, y: number) {
    super(scene, x, y, 'coin')
    scene.add.existing(this)
    scene.physics.add.existing(this)

    this.setImmovable()
    this.setScale(1.5)
    // @ts-ignore
    this.body.setAllowGravity(false)

    scene.anims.create({
      key: 'spincoin',
      frames: scene.anims.generateFrameNames('coin'),
      frameRate: 16,
      repeat: -1
    })
    this.play('spincoin')

    this.audio = scene.sound.add('coin', { volume: 1.0 })
  }
}

The key (src/scripts/components/objects/key.ts)

All the frames are selected to create the animation:

The key (assets/img/key.png)
Collecting key (assets/audio/key.mp3)
import ObjectClass from "./objectClass";

export default class CoinSprite extends ObjectClass {

  constructor(scene: Phaser.Scene, x: number, y: number) {
    super(scene, x, y, 'key')
    scene.add.existing(this)
    scene.physics.add.existing(this)

    this.setImmovable()
    this.setScale(0.7)
    // @ts-ignore
    this.body.setAllowGravity(false)

    scene.anims.create({
      key: 'spinkey',
      frames: scene.anims.generateFrameNames('key'),
      frameRate: 10,
      repeat: -1
    })
    this.play('spinkey')

    this.audio = scene.sound.add('key', { volume: 1.0 })
  }
}

Collecting objects (src/scripts/scenes/mainScene.ts)

The counters of coins and keys are incremented when any of these objects is collected:

...
    this.physics.add.overlap(this.player, objectsGroup, (player: Player, object: ObjectClass) => {
      if (object.collecting) return
      if (object instanceof CoinSprite) this.score++
      else if (object instanceof KeySprite) this.keys++
      scoreText.update(this.score, this.keys)
      object.collect()
    })
...

The player (src/scripts/components/player/player.ts)

The get a more realistic movement, the player is animated using the “spine” plugin. The full documentation about this plugin can be found here, and some examples can be found here.

Player animation using spine plugin (assets/spine/boy.png)
Player killed (assets/audio/hurt.mp3)
import Controls from '../controls/controls'
import PlayerSpine from './playerSpine'

export default class Player extends Phaser.Physics.Arcade.Sprite {
  private _dead: boolean = false
  private _halt: boolean = false
  private mapSize: MapSize
  playerSpine: PlayerSpine
  audioHurt: any

  constructor(scene: Phaser.Scene, player: TilesConfig, mapSize: MapSize, level: number) {
    super(scene, player.x, player.y, player.texture)
    scene.add.existing(this)
    scene.physics.add.existing(this)

    this.scene = scene
    this.mapSize = mapSize

    this.setVisible(false)

    this.setOrigin(0, 1)
    this.setDragX(1500)
    this.body.setSize(70, 132)
    this.body.setOffset(25, 24)

    let theSkin = level % 2 == 0 ? 'blue' : 'green'
    this.playerSpine = new PlayerSpine(scene, this.body.center.x, this.body.bottom)
    this.playerSpine.setSkin(theSkin)

    this.audioHurt = scene.sound.add('hurt', { volume: 1.0 })
  }

  kill() {
    this._dead = true

    this.audioHurt.play()

    // animate the camera if the player dies
    this.scene.cameras.main.shake(500, 0.025)
    this.scene.time.addEvent({
      delay: 500,
      callback: () => this.scene.scene.restart()
    })
  }

  killEnemy() {
    this.playerSpine.spine.customParams.isKilling = true
    this.setVelocityY(-600)
  }

  halt() {
    this.body.enable = false
    this._halt = true
  }

  update(cursors: any, controls: Controls) {
    if (this._halt || this._dead) return

    // check if out of camera and kill
    if (this.body.right < this.mapSize.x || this.body.left > this.mapSize.width || this.body.top > this.mapSize.height)
      this.kill()

    // controls left & right
    if (cursors.left.isDown || controls.leftIsDown) {
      this.setVelocityX(-500)
      this.playerSpine.spine.setScale(-1, 1)
    } else if (cursors.right.isDown || controls.rightIsDown) {
      this.setVelocityX(550)
      this.playerSpine.spine.setScale(1, 1)
    }
    // controls up
    if ((cursors.up.isDown || cursors.space.isDown || controls.upIsDown) && this.body.blocked.down) {
      this.setVelocityY(-1250)
    }

    // update spine animation
    this.playerSpine.update(this)
  }
}

The goal (src/scripts/components/goal/goalSprite.ts)

When the user collides with the door, an achievement sound will be played, and it will jump to the next level.

The goal (assets/img/goal.png)
Next level (assets/audio/door.mp3)
export default class GoalSprite extends Phaser.Physics.Arcade.Sprite {
  private _loadNextLevel: boolean = false;
  private audio: any;

  constructor(scene: Phaser.Scene, tilesConfig: TilesConfig) {
    super(scene, tilesConfig.x, tilesConfig.y + 14, 'goal')
    scene.add.existing(this)
    scene.physics.add.existing(this)

    this.setImmovable(true)
    // @ts-ignore
    this.body.setAllowGravity(false)
    this.setOrigin(0, 0.5)

    this.audio = scene.sound.add('door', { volume: 1.0 })
  }

  get loadNextLevel() {
    return this._loadNextLevel
  }

  nextLevel(scene: Phaser.Scene, level: number, score: number, keys: number) {
    if (this._loadNextLevel) return
    this._loadNextLevel = true

    this.audio.play()

    scene.cameras.main.fadeOut()
    scene.time.addEvent({
      delay: 2000,
      callback: () => {
        scene.scene.restart({ level: level += 1, score: score, keys: keys })
      }
    })
  }
}

Jumping to the next level (src/scripts/scenes/mainScene.ts)

After colliding with the door, the user will only jump to the next level in case it collected previously at least one key:

...
    this.physics.add.overlap(this.player, this.goal, (player: Player, goal: GoalSprite) => {
      if (!this.keys) return
      player.halt()
      this.keys--
      goal.nextLevel(this, this.level, this.score, this.keys)
    })
...

Progressive Web App (PWA)

This game is 100% PWA ready, so that it can be installed and executed in your mobile device as it was an app from the Play Store. You can easily personalize its settings by following these steps:

  • Replace both icons in /pwa/icons with your own.
    • One is 512×512 the other 192×192.
  • Add your own favicon.ico to /src.
  • Adjust these parameters in the manifest.json file in /pwa:
    • short_name: Max. 12 characters.
    • name: The full game name.
    • orientation: “landscape” or “portrait”.
    • background_color: color of the splash screen.
    • theme_color: color of the navbar – has to match the theme-color in the index.html file.
  • You can leave the sw.js (serviceWorker) file in /pwa as it is.
  • Change the gameName in /webpack/webpack.common.js

Read more about PWA on developers.google.com.

Running the game

You can get the game up and running just following these steps:

  1. Install nodejs (in case you do not have installed it yet). Select the version recommended for most users and just click next on each option to use the default settings.
  2. Download the code. You can download from this link a single zip file containing the full game template and all the assets.
  3. Uncompress the zip file, and open a terminal from inside the project folder.
  4. Execute “npm install” to install all the dependencies requested by the project.
  5. Execute “npm run start” to compile and execute the code. After this command is completed, a new tab will be opened in your default browser. The game will also be refreshed automatically when the source code changes.
  6. Execute “npm run build” to get a production version. After executing this command, all the html, css and js files, and also all the assets required to run the game will be put inside the “dist” folder, ready to be uploaded to any public web server. For example, you could make your game available to anyone just by creating a new project in https://replit.com and uploading all the files inside “dist” to the root folder of the replit project.

Play the game on your browser

You have also a version ready to be played online here.

Install the game in your phone

Since all the PWA functionality is included in this project, you can install your application in any mobile device (Chrome browser and Android operating system are the preferred options for this purpose). You will get the same behaviour as the apps in the play store: an icon will be created after carrying out the installation from your browser, a loading screen will be shown when the game is started, and the game will run in fullscreen and landscape mode.

When you open the address of the game in your browser, a message to install the application is displayed after a few seconds:

Installing the app

Or you can also install the game manually just by selecting the option “Instalar aplicación” from the browser menu:

Installing the app

Bibliography

More information about the template used to build this game can be found on github.

Phaser. Unit 7. Dino game clone (part I)

Introduction

In this unit we will build a clone of Google Chrome “secret” Dino game (the original game can be revealed by entering “chrome://dino” in the address bar). We will use simple web technologies such as HTML, CSS, JavaScript and Phaser 3, so that any user can play on any device just using any browser (not only Chrome).

The result is a game which looks almost the same as the original one using less than 10% lines of code. As it can be seen from the Chromium source code, the original game is made of more than 4000 lines of code, whereas the one in this unit has been developed using around 300 lines of code. This means that Phaser could be a much better option than simple JavaScript when thinking about game development.

Some features

This version of the game clones almost every feature of the original one:

  • The speed of the game is increased gradually.
  • Both cactuses and birds will appear randomly to hurt you.
  • Two scores are shown: current and highest.
  • Each time the user reaches 100 points, the score blinks and a sound is played.
  • It can be played on desktop computers using both the mouse and the keyboard:
    • By clicking on the screen to jump or to restart the game.
    • Using the up arrow key or the space bar to jump, the down arrow key to duck under the enemy, and the enter key to restart the game.
  • It can be played on mobile devices by touching the screen, the same way as any single button game.
  • The game is automatically adjusted to fit the screen size, even if it changes after the page is loaded (for example, in case the user rotates the device).
  • Vibration is activated on mobile devices when the game over screen appears.

CSS code (styles.css)

We are going to insert some CSS code to remove any margin and padding, and also to remove the scrollbars, so that the whole window size is used when the game is started:

html, body {
  margin: 0px;
  padding: 0px;
  overflow: hidden;
}

HTML code (index.html)

We are going to use a single HTML file to link all the files in the project. This is going to be the “index.html” file:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="description" content="Dino game clone using Phaser">
  <meta name="keywords" content="HTML, CSS, JavaScript, Phaser">
  <meta name="author" content="Fernando Ruiz Rico">
  
  <title>Dino Phaser</title>

  <link href="styles.css" rel="stylesheet">
</head>

<body>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
  <script src="preload.js"></script>
  <script src="create.js"></script>
  <script src="update.js"></script>
  <script src="config.js"></script>
</body>

</html>

Constants and Phaser configuration (config.js)

We are going to define some constants (i.e. screen width and height and text styles) so that we can use them inside every JavaScript file. We will also define a function to set the initial values of some variables before the game is started, together with the usual Phaser configuration, which must be performed as usual.

Moreover we are adding a piece of code to check whether the screen is being resized (i.e. when the device is rotated or the window size is adjusted) so that the whole game is reloaded to fit the new values of the screen width and height.

// Fill the whole width and height of the screen
const WIDTH = window.innerWidth;
const HEIGHT = window.innerHeight;

// Time in milliseconds for each obsticle (lower = faster)
const OBSTICLE_TIME = 750;

// Text style to be used when printing texts (scores, etc.)
const TEXT_STYLE = { fill: "#535353", font: '900 35px Courier', resolution: 10 };

// Variables to be initialized each time the game is restarted
function initVariables() {
  this.isGameRunning = false;
  this.gameSpeed = 10;
  this.respawnTime = 0;
  this.score = 0;
}

// Phaser initialization
let game = new Phaser.Game({
  type: Phaser.AUTO,
  width: WIDTH,
  height: HEIGHT,
  pixelArt: true,
  transparent: true,
  physics: {
    default: 'arcade',
    arcade: {
      debug: false
    }
  },
  scene: { preload: preload, create: create, update: update }
});

// Reload the game when the device orientation changes or the user resizes the window
let resizing = false;
window.addEventListener('resize', () => {
  if (resizing) clearTimeout(resizing);
  resizing = setTimeout(() => window.location.reload(), 500);
});

The assets

We will need some images, sprites and sounds. You may use any assets you like, but we are providing the original ones so that you can easily develop the same game as the one available in Google Chrome. Some examples are shown below, and also a zip file containing all the assets can be downloaded here.

Images

Cloud
Cactuses
Game over
Restart

Sprites

Dino waiting and running
Bird enemy

Sounds

Hit
Jump
Reach

Loading all the assets (preload.js)

So that we can use all those images, sprites and sounds, they have to be loaded previously. That is the purpose of the “preload()” function.

Inside the “initSounds()” and “createAnims()” we will set the volume of the sounds and the frames of the animations respectively.

// Load all the audios, images and sprites
function preload() {
  this.load.audio('jump', 'assets/jump.m4a');
  this.load.audio('hit', 'assets/hit.m4a');
  this.load.audio('reach', 'assets/reach.m4a');

  this.load.image('ground', 'assets/ground.png');
  this.load.image('dino-idle', 'assets/dino-idle.png');
  this.load.image('dino-hurt', 'assets/dino-hurt.png');
  this.load.image('restart', 'assets/restart.png');
  this.load.image('game-over', 'assets/game-over.png');
  this.load.image('cloud', 'assets/cloud.png');

  this.load.image('obsticle-1', 'assets/cactuses_small_1.png');
  this.load.image('obsticle-2', 'assets/cactuses_small_2.png');
  this.load.image('obsticle-3', 'assets/cactuses_small_3.png');
  this.load.image('obsticle-4', 'assets/cactuses_big_1.png');
  this.load.image('obsticle-5', 'assets/cactuses_big_2.png');
  this.load.image('obsticle-6', 'assets/cactuses_big_3.png');

  this.load.spritesheet('dino', 'assets/dino-run.png', { frameWidth: 88, frameHeight: 94 });
  this.load.spritesheet('enemy-bird', 'assets/enemy-bird.png', { frameWidth: 92, frameHeight: 77 });
}

// Set the volume of each audio
function initSounds() {
  this.jumpSound = this.sound.add('jump', { volume: 0.75 });
  this.hitSound = this.sound.add('hit', { volume: 0.75 });
  this.reachSound = this.sound.add('reach', { volume: 0.75 });
}

// Define all the animations from the previously loaded spritesheets
function createAnims() {
  this.anims.create({
    key: 'dino-waiting',
    frames: this.anims.generateFrameNumbers('dino', { start: 0, end: 1 }),
    frameRate: 1,
    repeat: -1
  })

  this.anims.create({
    key: 'dino-run',
    frames: this.anims.generateFrameNumbers('dino', { start: 2, end: 3 }),
    frameRate: 10,
    repeat: -1
  })

  this.anims.create({
    key: 'enemy-bird-anim',
    frames: this.anims.generateFrameNumbers('enemy-bird', { start: 0, end: 1 }),
    frameRate: 6,
    repeat: -1
  })
}

Creating the scores, the clouds, the dino, and the game over message (create.js)

  • Scores: Two boxes are used to print both the current and highest scores on the right hand side of the screen.
  • Clouds: A loop will be used to create 3 clouds at once printing the same image on random positions.
  • Dino: The dino will collide with the ground below, and it will jump using the keys defined here (in our case we will use both the up cursor key and the space bar). The mouse functionality is already available by default.
  • Game over: A message will be shown when the user collides with either the birds or the cactuses. After the user clicks on it, this message will be hidden using the “setAlpha(0)” function call.
// Score text on the right side of the screen
function createScore() {
  this.scoreText = this.add.text(WIDTH - 25, HEIGHT / 3, "", TEXT_STYLE).setOrigin(1, 1);
  this.highScoreText = this.add.text(0, HEIGHT / 3, "", TEXT_STYLE).setOrigin(1, 1).setAlpha(0.75);
}

// Three clouds hidden at the beginning
function createClouds() {
  this.clouds = this.add.group();
  for (let i = 0; i < 3; i++) {
    const x = Phaser.Math.Between(0, WIDTH);
    const y = Phaser.Math.Between(HEIGHT / 3, HEIGHT / 2);
    this.clouds.add(this.add.image(x, y, 'cloud'));
  }
  this.clouds.setAlpha(0);
}

// Dino starts running and the the ground is created
function createGround() {
  this.startText.destroy();
  this.dino.setVelocityX(100);
  this.dino.play('dino-run', 1);

  const interval = setInterval(() => {
    this.ground.width += (WIDTH / 100);
    if (this.ground.width >= WIDTH) {
      clearInterval(interval);
      this.dino.setVelocityX(0);
      this.scoreText.setAlpha(1);
      this.clouds.setAlpha(1);
      this.isGameRunning = true;
    }
  }, 50);
}

// Dino waiting animation, keyboard initilization and starting text
function createDino() {
  this.dino = this.physics.add.sprite(0, HEIGHT / 1.5, 'dino-idle').setSize(50).setGravityY(5000).setOrigin(0, 1);
  this.dino.play('dino-waiting', true);

  this.physics.add.collider(this.dino, this.belowGround);
  this.physics.add.collider(this.dino, this.obsticles, () => stopGame.call(this));

  this.cursors = this.input.keyboard.createCursorKeys();
  this.spaceBar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);

  this.startText = this.add.text(WIDTH - 15, HEIGHT / 1.5, "Press to play", TEXT_STYLE).setOrigin(1);
}

// Game over screen and pause until player starts the game again
function createGameOverScreen() {
  this.gameOverText = this.add.image(0, 0, 'game-over');
  this.restart = this.add.image(0, 50, 'restart').setInteractive();
  this.gameOverScreen = this.add.container(WIDTH / 2, HEIGHT / 2 - 50);
  this.gameOverScreen.add([this.gameOverText, this.restart]);

  this.input.keyboard.on('keydown_ENTER', () => startGame.call(this));
  this.restart.on('pointerdown', () => startGame.call(this));
}

// Define the physical ground and obsticles group
function createWorld() {
  this.ground = this.add.tileSprite(0, HEIGHT / 1.5, 88, 26, 'ground').setOrigin(0, 1);
  this.belowGround = this.add.rectangle(0, HEIGHT / 1.5, WIDTH, HEIGHT / 3).setOrigin(0, 0);
  this.physics.add.existing(this.belowGround);
  this.belowGround.body.setImmovable();
  this.obsticles = this.physics.add.group();
}

// Create all the elements in the game and initilize the score
function create() {
  initVariables.call(this);
  initSounds.call(this);
  createAnims.call(this);
  createWorld.call(this);
  createClouds.call(this);
  createScore.call(this);
  createDino.call(this);

  setInterval(() => updateScore.call(this), 100);
}

Controlling the player and displaying the birds and the cactuses (update.js)

  • Starting the game and building the ground: The “createGround()” method will build the ground progresively, just once, when the game is started the very first time (after the user taps the screen or presses either the up arrow key or the space bar).
  • Restarting the game: After the game over message appears, the user may click on the screen to start a new game. This is done inside the “startGame()” method, which clears the message using the “destroy()” method call, restarts the score and resumes all the physics.
  • Updating the scores: The function “updateScore()” will not only update the current score, but it will also increment the game speed, and it will play a sound each time the user reaches 100 points, making the numbers blink at the same time. The “updateHighScore()” function will keep a record of the highest score each time a game is over.
  • Placing and moving the birds and the cactuses: The “placeObsticle()” function will create either birds or cactuses randomly, and the “update()” function will move everything to the left except the dino, which will always remain in the same position.
// Restart the game after dino is hurt
function startGame() {
  this.dino.setVelocityY(0);
  this.physics.resume();
  this.anims.resumeAll();
  this.obsticles.clear(true, true);
  this.gameOverScreen.destroy();
  this.isGameRunning = true;
}

// On game over a sound is played and all physics are paused
// The game over screen will be shown and mobile devices will vibrate
function stopGame() {
  initVariables.call(this);
  updateHighScore.call(this);
  this.hitSound.play();
  this.physics.pause();
  this.anims.pauseAll();
  this.dino.setTexture('dino-hurt');
  createGameOverScreen.call(this);
  if (window.navigator.vibrate) window.navigator.vibrate(50);
}

// Update the score and increase the game speed
function updateScore() {
  if (!this.isGameRunning) return;

  this.score++;
  this.gameSpeed += 0.01;

  if (this.score % 100 == 0) {
    this.reachSound.play();

    this.tweens.add({
      targets: this.scoreText,
      duration: 100,
      repeat: 3,
      alpha: 0,
      yoyo: true
    })
  }

  this.scoreText.setText(("0000" + this.score).slice(-5));
}

// Update the high score in case is higher than the previous one
function updateHighScore() {
  this.highScoreText.x = this.scoreText.x - this.scoreText.width - 30;

  const highScore = this.highScoreText.text.substr(this.highScoreText.text.length - 5);
  const newScore = Number(this.scoreText.text) > Number(highScore) ? this.scoreText.text : highScore;

  this.highScoreText.setText(`HI ${newScore}`);
}

// Place a new obsticle on the screen (either cactuses or birds)
function placeObsticle() {
  let obsticle;
  const obsticleNum = Phaser.Math.Between(1, 7);

  if (obsticleNum > 6) {
    obsticle = this.obsticles.create(WIDTH, HEIGHT / 1.5 - Phaser.Math.Between(20, 50), 'enemy-bird');
    obsticle.body.height /= 1.5
    obsticle.play('enemy-bird-anim', 1);
  } else {
    obsticle = this.obsticles.create(WIDTH, HEIGHT / 1.5, `obsticle-${obsticleNum}`)
  }

  obsticle.setOrigin(0, 1).setImmovable();
}

// Update the screen (obsticles, ground, clouds and dino) and check the keyboard and mouse (or touch screen) continuously
// Dino will start running and jump if the user taps the screen or presses either the arrow up key or the spacebar  
function update() {
  let jump = this.cursors.up.isDown || this.spaceBar.isDown || this.input.activePointer.isDown;

  if (this.isGameRunning) {
    this.ground.tilePositionX += this.gameSpeed;

    this.respawnTime += this.gameSpeed;
    if (this.respawnTime >= OBSTICLE_TIME) {
      this.respawnTime = 0;
      placeObsticle.call(this);
    }

    this.obsticles.getChildren().forEach(obsticle => {
      obsticle.x -= this.gameSpeed;
      if (obsticle.getBounds().right < 0) {
        obsticle.destroy();
      }
    })

    this.clouds.getChildren().forEach(cloud => {
      cloud.x -= 0.5;
      if (cloud.getBounds().right < 0) {
        cloud.x = WIDTH;
      }
    })

    if (this.dino.body.onFloor()) {
      if (jump) {
        this.jumpSound.play();
        this.dino.anims.stop();
        this.dino.setVelocityY(-1600);
        this.dino.setTexture('dino', 0);
      }
      else {
        this.dino.play('dino-run', true);
      }
    }
  }
  else if (jump && (this.ground.width < WIDTH)) {
    createGround.call(this);
  }
}

Proposed exercise: Using your own assets

Create a new version of the Dino game using your own assets (images, sprites and sounds). You can also change any other things you like.

Do not forget to create all the files required to run the code in this unit:

  • styles.css
  • index.html
  • preload.js
  • create.js
  • update.js
  • config.js
  • assets folder

You may find many free assets in OpenGameArt, and some new versions of the Dino game in github:

Enjoy the game!

You can enjoy playing this wonderful game online here.

Phaser. Unit 6. Using tilemaps to build a space shooter game

The source code and the assets

You can download this zip file to get the source code and all the assets.

Updating the map

As done previously, you can use the tile map editor to update the map. In case you have not done it yet, you can download the editor from this link and install it on your computer.

To update the map you have to go the assets folder and open the file “town.tmx” which contains the map of the example in this unit. Select a layer (on the right corner, at the top) and change anything you like (by adding or removing some objects to or from the map). When you finish, export the map to JSON format using the option “File” -> “Export as…”, and overwrite the file “town.json”, located inside the “assets” folder.

Finally check the results using your browser and move the player to go to each part of the map. You will notice that the camera will follow you on each movement. Do not forget to fully refresh the contents of the browser by pressing Ctrl+F5, since the map and some other contents are usually cached by the browser. Also move the player and check that the map has been updated with the changes you have made.

Frames per second (FPS)

You can update the number of frames per second to choose the best renderization performance for your computer:

const MAX_FPS = 25;

Using the keyboard and the mouse

In this game we are using the keys A (left), S (down), D (right), W (up) to move the player on desktop computers and the joystick for mobile devices:

  let vX = 0, vY = 0;

  // Horizontal movement
  if (wsadKeys.A.isDown || joyStick.left) vX = -speed;
  else if (wsadKeys.D.isDown || joyStick.right) vX = speed;

  // Vertical movement
  if (wsadKeys.W.isDown || joyStick.up) vY = -speed;
  else if (wsadKeys.S.isDown || joyStick.down) vY = speed;

The mouse is used for shooting on desktop computers:

  let pointer = this.input.activePointer;
  if (pointer.isDown && this.sys.game.device.os.desktop) createBullet.call(this, pointer.worldX - player.x, pointer.worldY - player.y);

In case you do not have any mouse, you may use the cursor keys on desktop computers, or the second joystick on mobile devices, to shoot the bullets and to choose the shooting direction:

  let vX = 0, vY = 0;

  // Horizontal bullets
  if (cursors.left.isDown || joyStick2.left) vX = -speed;
  else if (cursors.right.isDown || joyStick2.right) vX = speed;

  // Vertical bullets
  if (cursors.up.isDown || joyStick2.up) vY = -speed;
  else if (cursors.down.isDown || joyStick2.down) vY = speed;

  if (vX || vY) createBullet.call(this, vX, vY);

Enjoy the game!

You may enjoy playing this wonderful game online here.

Phaser. Unit 5. Using tilemaps to build a shooter game in a huge world

A preview of the map

You can have a look at this preview of the whole map.

The source code and the assets

You can download this zip file to get the source code and all the assets.

Updating the map

As done previously, you can use the tile map editor to update the map. In case you have not done it yet, you can download the editor from this link and install it on your computer.

To update the map you have to go the assets folder and open the file “town.tmx” which contains the map of the example in this unit. Select a layer (on the right corner, at the top) and change anything you like (by adding or removing some objects to or from the map). When you finish, export the map to JSON format using the option “File” -> “Export as…”, and overwrite the file “town.json”, located inside the “assets” folder.

Since we are working with a lot of tilesets, in this case we are setting the collisions with all the objects in some of the layers: “Terrain_3”, “Buildings_1” and “Buildings_2”. The player and the enemies will collide with all the objects that you drawn in these three layers.

Finally check the results using your browser and move the player to go to each part of the map. You will notice that the camera will follow you on each movement. Do not forget to fully refresh the contents of the browser by pressing Ctrl+F5, since the map and some other contents are usually cached by the browser. Also move the player and check that the map has been updated with the changes you have made.

Renderization problems

Due to the large amount of resources this game is going to use from your computer, you might need to adjust a couple of parameters. It seems it works fine on Chrome browser, but if you are using another browser, or in case you find any problems, you can try updating the following parameters.

Phaser renderer

I have selected “Phaser.CANVAS” inside the phaser configuration settings, which seems to work much better on Chrome browser, but you may try “Phaser.AUTO” instead:

const config = {
  type: Phaser.AUTO
  ...
}

Frames per second (FPS)

You can also change the number of frames per second to choose the right renderization performance:

const MAX_FPS = 25;

Using the keyboard and the mouse

In this game we are using the keys A (left), S (down), D (right), W (up) to move the player on desktop computers and the joystick for mobile devices:

  let vX = 0, vY = 0;

  // Horizontal movement
  if (wsadKeys.A.isDown || joyStick.left) vX = -speed;
  else if (wsadKeys.D.isDown || joyStick.right) vX = speed;

  // Vertical movement
  if (wsadKeys.W.isDown || joyStick.up) vY = -speed;
  else if (wsadKeys.S.isDown || joyStick.down) vY = speed;

The mouse is used for shooting on desktop computers:

  let pointer = this.input.activePointer;
  if (pointer.isDown && this.sys.game.device.os.desktop) createBullet.call(this, pointer.worldX - player.x, pointer.worldY - player.y);

In case you do not have any mouse, you may use the cursor keys on desktop computers, or the second joystick on mobile devices, to shoot the bullets and to choose the shooting direction:

  let vX = 0, vY = 0;

  // Horizontal bullets
  if (cursors.left.isDown || joyStick2.left) vX = -speed;
  else if (cursors.right.isDown || joyStick2.right) vX = speed;

  // Vertical bullets
  if (cursors.up.isDown || joyStick2.up) vY = -speed;
  else if (cursors.down.isDown || joyStick2.down) vY = speed;

  if (vX || vY) createBullet.call(this, vX, vY);

Enjoy the game!

You may enjoy playing this wonderful game online here.

Phaser. Unit 4. Using tilemaps to build a pacman game

Introduction

In this unit we are going to use Phaser together with a tilemap editor to build a maze. You may find more information at the official page of the Tiled editor and you may also have a look at some interesting demonstrations at the Phaser’s examples page, and also performing some search on the latest examples.

HTML and CSS code

The first thing we should do is linking the Phaser library. We will use the last version at the time this unit has been written:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>

After that we are going to insert some CSS code to remove any margin and padding, and also to remove the scrollbars, so that all the window size is used when the game is started:

html, body {
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    height: 100%;
} 

And finally we are going to use a single file to put all the code inside. This is going to be the basic structure of the “.html” file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html, body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>
<body>
    <script type="text/javascript">
        ...
    </script>
</body>
</html>

Constants

We are going to define some constants at the beginning of the code so that we can easily change the number of coins, fruits and ghosts that will be created:

const numCoins = 10;
const numFruits = 5;
const numGhosts = 5;

Phaser configuration and other variables

Following the constants we are going to create some variables so keep all the information about the game and also to start the initialization of Phaser:

const config = {
    type: Phaser.AUTO,
    pixelArt: true,
    scale: {
        mode: Phaser.Scale.ENVELOP,
        width: window.innerWidth,
        height: window.innerHeight
    },
    physics: {
        default: "arcade",
        arcade: {
            gravity: { y: 0 }
        }
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

const game = new Phaser.Game(config);
let speed = 60, vX = 0, vY = 0, prevX = 0, prevY = 0, prevTime = 0;
let belowLayer, worldLayer, aboveLayer, tileset, emptyTiles;
let map, player, cursors, bell, bell2, dead, hurt, timeout;
let joyStick = joyStick2 = { up: false, down: false, left: false, right: false };
let score = 0, lives = 5, scoreText = null, gameOver = false;

The assets (images, sprites and sounds)

We will need some images, sprites and sounds. You may use any assets you like, but we are also providing some examples so that you can easily start testing (they can also be downloaded here):

Images

Tileset containing almost every image needed to build the game
Restart button

Sounds and music

When the player gets a coin
When the user collects a fruit
When the user gets hurt by a ghost
When the user kills a ghost
Background music always playing

Loading all the assets

So that we can use all those images, sprites and music, they have to be loaded previously. That is the purpose of the “preload()” function. We will also include here the initialization of the joystick so that we can use it in another section of this unit.

Proposed exercise: Game initialization

Create a specific folder for your game in your domain to put all the code and assets inside. After that, create an “index.html” inside that folder with the code below, and also create an “assets” folder to put all the images and sounds, which can be downloaded here. After uploading everything to your domain, test the game using the url of that folder in your browser, and you should get a full black screen. Click on it and the background music should start playing.

You can see the result here (you can also look at the source code by pressing Ctrl+U).
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html,
        body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>

<body>
    <div id="game-container"></div>
    <script type="text/javascript">
        const numCoins = 10;
        const numFruits = 5;
        const numGhosts = 5;

        const config = {
            type: Phaser.AUTO,
            pixelArt: true,
            scale: {
                mode: Phaser.Scale.ENVELOP,
                width: window.innerWidth,
                height: window.innerHeight
            },
            physics: {
                default: "arcade",
                arcade: {
                    gravity: { y: 0 }
                }
            },
            scene: {
                preload: preload,
                create: create,
                update: update
            }
        };

        const game = new Phaser.Game(config);
        let speed = 60, vX = 0, vY = 0, prevX = 0, prevY = 0, prevTime = 0;
        let belowLayer, worldLayer, aboveLayer, tileset, emptyTiles;
        let map, player, cursors, bell, bell2, dead, hurt, timeout;
        let joyStick = joyStick2 = { up: false, down: false, left: false, right: false };
        let score = 0, lives = 5, scoreText = null, gameOver = false;

        function preload() {
            this.load.image("restart", "assets/restart.png");
            this.load.image("tiles", "assets/tileset.png");
            this.load.tilemapTiledJSON("map", "assets/map.json");
            this.load.spritesheet("sprites", "assets/tileset.png", { frameWidth: 20, frameHeight: 20, margin: 1, spacing: 1 });

            this.load.audio("bell", "assets/ding.mp3");
            this.load.audio("bell2", "assets/ding2.mp3");
            this.load.audio("dead", "assets/dead.mp3");
            this.load.audio("hurt", "assets/hurt.mp3");
            this.load.audio("music", "assets/music.mp3");

            this.load.plugin('rexvirtualjoystickplugin', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/rexvirtualjoystickplugin.min.js', true);
        }

        function initSounds() {
            bell = this.sound.add('bell', { volume: 0.2 });
            bell2 = this.sound.add('bell2', { volume: 0.8 });
            dead = this.sound.add('dead', { volume: 0.7 });
            hurt = this.sound.add('hurt', { volume: 0.9 });
            this.sound.add('music', { volume: 0.4 }).play({ loop: -1 });
        }

        function create() {
            initSounds.call(this);
        }

        function update(time, delta) {
        }
    </script>
</body>

</html>

Proposed exercise: Using your own music

Download any other music file you like to use it as a background music and change the JavaScript code inside the “preload()” function to set the file name accordingly. Also in the “initSounds()” function you will find that you may change the volume of the music (volume:0.4). Try changing that value from 0.1 to 1 and you will notice that the volume of the music will also change.

You may find in the following sites some free images and sounds which you can download and use for your own game:

The maze

Once we have initialized Phaser and after loading all the assets, we can go ahead and display the maze on the screen.

Proposed exercise: Displaying the maze

Add the function “createWorld()” (provided below) to your code, and modify the calling function “create()” so that it is called when the game is started (also shown below). Finally test everything in your browser from your domain.

About the music, do not forget to click on the screen after loading the game (the music will not start playing unless the browser gets the focus). Also, you can see the result here (you can look at the source code by pressing Ctrl+U).
  1. This is the new function you have to insert in your code to create the world:
function createWorld() {
    map = this.make.tilemap({ key: "map" });

    // Parameters are the name you gave the tileset in Tiled and then the key of the tileset image in
    // Phaser's cache (i.e. the name you used in preload)
    tileset = map.addTilesetImage("tileset", "tiles");

    // Parameters: layer name (or index) from Tiled, tileset, x, y
    belowLayer = map.createLayer("Below Player", tileset, 0, 0).setDepth(1);
    worldLayer = map.createLayer("World", tileset, 0, 0).setDepth(2);
    aboveLayer = map.createLayer("Above Player", tileset, 0, 0).setDepth(3);

    worldLayer.setCollisionByProperty({ collides: true });

    this.physics.world.setBounds(0, 0, map.widthInPixels, map.heightInPixels);

    // Find empty tiles where new zombies, coins or health can be created
    emptyTiles = worldLayer.filterTiles(tile => (tile.index === -1));

    this.scale.resize(map.widthInPixels, map.heightInPixels).refresh();
}
  1. Do not forget to update the “create()” function to create the world. This is the only line you should add to that function:
function create() {
    ...
    createWorld.call(this);            
}

Proposed exercises: Modifying the maze

Download the tile map editor from this link and install it on your computer (if you don’t have installed it yet). Go to the assets folder and open the file “map.tmx” which contains the map of the example explained in this unit. Select the layer “World” (on the right corner, at the top) and change anything you like (by adding or removing some objects to or from the map). When you finish, export the map to JSON format using the option “File” -> “Export as…”, and overwrite the file “map.json”, located inside the “assets” folder. Finally check the results using your browser.

Do not forget to fully refresh the contents of the browser by pressing Ctrl+F5, since the map and some other contents are usually cached by the browser. Then you can check that the map has been updated with the changes you have made.

The animations (sprites)

We will use sprites to create the player and the animated objects. Let’s have a look at the the picture containing all the sprites:

Tileset containing all the sprites

We may appreciate that we have several groups of images. This way, we may easily create the animations:

  • The coin: Pictures from 0 to 7
  • The point: Picture 8
  • The ghosts:
    • Blue: Pictures from 17 to 20
    • Pink: Pictures from 21 to 24
    • Yellow: Pictures from 25 to 28
    • Green: Pictures from 34 to 37
    • Orange: Pictures from 38 to 41
    • Red: Pictures from 42 to 45
  • The player:
    • Left: Pictures from 51 to 58
    • Right: Pictures from 68 to 75
    • Down: Pictures from 85 to 92
    • Up: Pictures from 102 to 109
  • The fruits: Pictures 46, 47, 114, 143

Controlling the player’s animations

So that this game can be used in both desktop and mobile devices, we have to initialize both the cursor keys and the joystick. Once this is done, we must change the player’s velocity and the current animation when the user presses the keys or moves the joystick. In the next exercise we will first define the animations inside the “createAnimations()” function, and after that we will go ahead with the “createPlayer()” function, where we will display the player. We will finally include in the “update()” function some conditions to adjust the player’s speed and current animation.

Proposed exercise: Adding the player

Add the code below to your game and check both the player animations and movements. After that, look for another sprite and change the code accordingly to use your own sprite. Finally, change the values of the constant “speed” (at the top of your code) and check the results.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose other sprites you like.
  1. These are the new functions you have to insert in your code to create the animations and initialize everything related to the player:
function newAnimation(name, rate, sprites, context) {
    context.anims.create({
        key: name, frameRate: rate, repeat: -1,
        frames: context.anims.generateFrameNumbers('sprites', sprites),
    });
}

function createAnimations() {
    // Player
    newAnimation("left", 10, { start: 51, end: 58 }, this);
    newAnimation("right", 10, { start: 68, end: 75 }, this);
    newAnimation("down", 10, { start: 85, end: 92 }, this);
    newAnimation("up", 10, { start: 102, end: 109 }, this);
    // Point
    newAnimation("point", 1, { start: 8, end: 8 }, this);
    // Coin
    newAnimation("spinning", 10, { start: 0, end: 7 }, this);
    // Fruits
    newAnimation("fruits", 2, { frames: [46, 47, 114, 143] }, this);
    // Ghosts
    newAnimation("blue", 5, { start: 17, end: 20 }, this);
    newAnimation("pink", 5, { start: 21, end: 24 }, this);
    newAnimation("yellow", 5, { start: 25, end: 28 }, this);
    newAnimation("green", 5, { start: 34, end: 37 }, this);
    newAnimation("orange", 5, { start: 38, end: 41 }, this);
    newAnimation("red", 5, { start: 42, end: 45 }, this);
}

function createPlayer() {
    // Object layers in Tiled let you embed extra info into a map - like a spawn point or custom
    // collision shapes. In the tmx file, there's an object layer with a point named "Spawn Point"
    const spawnPoint = map.findObject("Objects", obj => obj.name === "Spawn Point");

    // Create a sprite with physics enabled via the physics system
    player = this.physics.add.sprite(spawnPoint.x, spawnPoint.y, "sprites").setDepth(2);

    // Watch the player and worldLayer for collisions, for the duration of the scene
    this.physics.add.collider(player, worldLayer);

    // Show points on empty tiles
    emptyTiles.forEach(tile => {
        let point = this.physics.add.sprite(tile.pixelX, tile.pixelY, 'sprites').setOrigin(0).anims.play('point', true);
        this.physics.add.overlap(player, point, collectPoint, null, this);
    });

    cursors = this.input.keyboard.createCursorKeys();

    // Show the joysticks only in mobile devices
    if (!this.sys.game.device.os.desktop) {
        joyStick = this.plugins.get('rexvirtualjoystickplugin').add(this, {
            x: 380, y: 420, radius: 20,
            base: this.add.circle(0, 0, 20, 0x888888).setAlpha(0.5).setDepth(4),
            thumb: this.add.circle(0, 0, 20, 0xcccccc).setAlpha(0.5).setDepth(4)
        }).on('update', update, this);

        joyStick2 = this.plugins.get('rexvirtualjoystickplugin').add(this, {
            x: 40, y: 420, radius: 20,
            base: this.add.circle(0, 0, 20, 0x888888).setAlpha(0.5).setDepth(4),
            thumb: this.add.circle(0, 0, 20, 0xcccccc).setAlpha(0.5).setDepth(4)
        }).on('update', update, this);
    }
}

function showScore() {
    if (!scoreText) scoreText = this.add.text(map.widthInPixels/2, 4, '', { fontSize: (18) + 'px', fill: '#FFF' }).setOrigin(0.5, 0).setScrollFactor(0).setDepth(4);
    scoreText.setText('Score:' + score + ' / Lives:' + lives);
}

function collectPoint(player, point) {
    bell.play();
    point.destroy();

    score += 5;
    showScore();
}
  1. Also you have to insert some code inside the “update()” function to respond to the cursor keys and the joystick:
function update(time, delta) {
    if (gameOver) return;

    // Choose the right animation depending on the player's direction
    if (player.x > prevX) player.anims.play('right', true);
    else if (player.x < prevX) player.anims.play('left', true);
    else if (player.y > prevY) player.anims.play('down', true);
    else if (player.y < prevY) player.anims.play('up', true);

    // If the player goes outside the map
    if (player.x < 0) player.x = map.widthInPixels - 20;
    else if (player.x > map.widthInPixels) player.x = 0;

    let key = player.anims.currentAnim.key;
    let blocked = player.body.blocked;

    // Reset the velocity when the player touches a wall
    if (key == 'right' && blocked.right || key == 'left' && blocked.left) vX = 0;
    if (key == 'up' && blocked.up || key == 'down' && blocked.down) vY = 0;

    // Horizontal movement
    if (cursors.left.isDown || joyStick.left || joyStick2.left) vX = -speed;
    else if (cursors.right.isDown || joyStick.right || joyStick2.right) vX = speed;

    // Vertical movement
    if (cursors.up.isDown || joyStick.up || joyStick2.up) vY = -speed;
    else if (cursors.down.isDown || joyStick.down || joyStick2.down) vY = speed;

    player.setVelocity(vX, vY);

    if ((time - prevTime) > 100) {
        prevX = player.x;
        prevY = player.y;
        prevTime = time;
    }
}
  1. And finally do not forget to update the “create()” function to create the animations, the player and the score. You just have to insert some new lines:
function create() {
    ...
    createAnimations.call(this); 
    createPlayer.call(this);
    showScore.call(this);
}

Coins, fruits, and ghosts

We will create many different objects at once using the images from the same tileset. We will just use a loop to print as many objects as we like.

Proposed exercises: Adding the coins

Add the code below to your file and check the results. After that, change the value of the constant “numCoins” (at the top of your code) to check how easily you can customize your game. Refresh the page several times and you will notice that the generated coins appear at random positions. Finally, look for another sprite sheet (it may be a coin or any other object you like) and change the code accordingly to use your own animated object.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose the images you like.
function newObject(animation, context) {
    const tile = Phaser.Utils.Array.GetRandom(emptyTiles);
    return context.physics.add.sprite(tile.pixelX, tile.pixelY, 'sprites').setOrigin(0).anims.play(animation, true);
}

function createCoin() {
    let coin = newObject('spinning', this);
    coin.body.setAllowGravity(false);
    this.physics.add.overlap(player, coin, collectCoin, null, this);
}

function protect(color) {
    player.setTint(color);
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => { timeout = false; player.clearTint() }, 5000);
}

function collectCoin(player, coin) {
    bell2.play();
    coin.destroy();
    createCoin.call(this);

    score += 10;
    showScore();

    protect(0xFFFF00);
}
function create() {
    ...
    for (i = 0; i < numCoins; i++) setTimeout(() => createCoin.call(this), Phaser.Math.Between(0, 5000));
}

Proposed exercises: Adding the fruits

Add the code below to your game and check the results. After that, change the value of the constants “numFruits” (at the top of your code) to check how easily you can customize your game. Finally, look for another sprite and change the code accordingly to use your own object to provide the player with extra lives and points.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createFruits() {
    let fruits = newObject('fruits', this);
    fruits.body.setAllowGravity(false);
    this.physics.add.overlap(player, fruits, collectFruits, null, this);
}

function collectFruits(player, fruits) {
    bell2.play();
    fruits.destroy();
    createFruits.call(this);

    score += 15;
    showScore();
}
function create() {
    ...
    for (i = 0; i < numFruits; i++) setTimeout(() => createFruits.call(this), Phaser.Math.Between(0, 5000));
}

Proposed exercises: Adding the ghosts

Add the code below to your game and check the results. After that, change the value of the constants “numGhosts” (at the top of your code) to check how easily you can customize your game. Finally, look for another sprite and change the code accordingly to display your own ghosts.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function newGhost(animation, context) {
    const spawnPoint = map.findObject("Objects", obj => obj.name === "Spawn Point 2");
    return context.physics.add.sprite(spawnPoint.x, spawnPoint.y, 'sprites').setOrigin(0).anims.play(animation, true).setDepth(2);
}

function createGhost() {
    const colors = ['blue', 'pink', 'yellow', 'green', 'orange', 'red'];
    let ghost = newGhost(colors[Phaser.Math.Between(0, colors.length - 1)], this).setCollideWorldBounds(true).setBounce(1);
    ghost.setVelocity(Phaser.Math.Between(speed / 3, speed / 2), Phaser.Math.Between(-speed / 2, -speed / 3)).body.setAllowGravity(false);
    this.physics.add.collider(ghost, worldLayer);
    this.physics.add.overlap(player, ghost, hitGhost, null, this);
}

function hitGhost(player, ghost) {
    // If the player is already hurt, it cannot be hurt again for a while
    if (player.tintTopLeft == 0xFF00FF) return;

    if (timeout) {
        score += 15;
        dead.play();
    }
    else {
        lives--;
        hurt.play();
        protect(0xFF00FF);
    }

    showScore();

    if (lives == 0) {
        this.physics.pause();
        gameOver = true;
        this.add.image(210, 230, 'restart').setScale(2).setScrollFactor(0).setDepth(4)
            .setInteractive().on('pointerdown', () => location.reload());
    }
    else {
        ghost.destroy();
        createGhost.call(this);
    }
}  
function create() {
    ...
    for (i = 0; i < numGhosts; i++) setTimeout(() => createGhost.call(this), Phaser.Math.Between(0, 15000)); 
}

Enjoy the game!

You may enjoy playing this wonderful game online here.

Phaser. Unit 3. Using tilemaps

Introduction

In this unit we are going to use Phaser together with a tilemap editor to build our world. You may find more information at the official page of the Tiled editor and you may also have a look at some interesting demonstrations at the Phaser’s examples page, and also performing some search on the latest examples.

HTML and CSS code

The first thing we should do is linking the Phaser library. We will use the last version at the time this unit has been written:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>

After that we are going to insert some CSS code to remove any margin and padding, and also to remove the scrollbars, so that all the window size is used when the game is started:

html, body {
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    height: 100%;
} 

And finally we are going to use a single file to put all the code inside. This is going to be the basic structure of the “.html” file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html, body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>
<body>
    <script type="text/javascript">
        ...
    </script>
</body>
</html>

Constants

We are going to define some constants at the beginning of the code so that we can easily change the game appearance, difficulty, and some other parameters:

const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const numCoins = 15;
const numHealth = 5;
const numPower = 5;
const numZombies = 10;

Phaser configuration and other variables

Following the constants we are going to create some variables so keep all the information about the game and also to start the initialization of Phaser:

const config = {
    type: Phaser.AUTO,
    width: screenWidth,
    height: screenHeight,
    physics: {
        default: "arcade",
        arcade: {
            debug: false,
            gravity: { y: 0 }
        }
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

const game = new Phaser.Game(config);
let worldWidth, worldHeight, joystickSize = 40, scale = 1.75, speed = 175;
let belowLayer, worldLayer, aboveLayer, tileset, emptyTiles;
let map, player, cursors, bell, bell2, dead, timeout;
let joyStick = joyStick2 = { up: false, down: false, left: false, right: false };
let score = 0, lives = 5, scoreText = null, gameOver = false;

The assets (images, sprites and sounds)

We will need some images, sprites and sounds. You may use any assets you like, but we are also providing some examples so that you can easily start testing (they can also be downloaded here):

Images

Tileset to be used to build the map
Restart button

Sprites (player and animated objects)

Player’s sprite sheet
Enemy’s sprite sheet
Sprite sheet to animate the coins
Sprite sheet to animate the health objects
Sprite sheet to animate the power object

Sounds and music

When the player gets a coin
When the user collects a health object
When the user gets hurt by a zombie
Background music always playing

Loading all the assets

So that we can use all those images, sprites and music, they have to be loaded previously. That is the purpose of the “preload()” function. We will also include here the initialization of the joystick so that we can use it in another section of this unit.

Proposed exercise: Game initialization

Create a specific folder for your game in your domain to put all the code and assets inside. After that, create an “index.html” inside that folder with the code below, and also create an “assets” folder to put all the images and sounds, which can be downloaded here. After uploading everything to your domain, test the game using the url of that folder in your browser, and you should get a full black screen. Click on it and the background music should start playing.

You can see the result here (you can also look at the source code by pressing Ctrl+U).
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html,
        body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>
<body>
    <div id="game-container"></div>
    <script type="text/javascript">
        const screenWidth = window.innerWidth;
        const screenHeight = window.innerHeight;
        const numCoins = 15;
        const numHealth = 5;
        const numPower = 5;
        const numZombies = 10;

        const config = {
            type: Phaser.AUTO,
            width: screenWidth,
            height: screenHeight,
            physics: {
                default: "arcade",
                arcade: {
                    debug: false,
                    gravity: { y: 0 }
                }
            },
            scene: {
                preload: preload,
                create: create,
                update: update
            }
        };

        const game = new Phaser.Game(config);
        let worldWidth, worldHeight, joystickSize = 40, scale = 1.75, speed = 175;
        let belowLayer, worldLayer, aboveLayer, tileset, emptyTiles;
        let map, player, cursors, bell, bell2, dead, timeout;
        let joyStick = joyStick2 = { up: false, down: false, left: false, right: false };
        let score = 0, lives = 5, scoreText = null, gameOver = false;

        function preload() {
            this.load.image("restart", "assets/restart.png");
            this.load.image("tiles", "assets/tileset.png");
            this.load.tilemapTiledJSON("map", "assets/town.json");
            this.load.spritesheet("player", "assets/lidia.png", { frameWidth: 64, frameHeight: 64 });
            this.load.spritesheet("coin", "assets/coin.png", { frameWidth: 32, frameHeight: 32 });
            this.load.spritesheet("health", "assets/health.png", { frameWidth: 32, frameHeight: 32 });
            this.load.spritesheet("power", "assets/power.png", { frameWidth: 64, frameHeight: 64 });
            this.load.spritesheet("zombie", "assets/zombie.png", { frameWidth: 48, frameHeight: 64 });

            this.load.audio("bell", "assets/ding.mp3");
            this.load.audio("bell2", "assets/ding2.mp3");
            this.load.audio("dead", "assets/dead.mp3");
            this.load.audio("music", "assets/music.mp3");

            this.load.plugin('rexvirtualjoystickplugin', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/rexvirtualjoystickplugin.min.js', true);
        }

        function initSounds() {
            bell = this.sound.add('bell', { volume: 0.2 });
            bell2 = this.sound.add('bell2', { volume: 0.2 });
            dead = this.sound.add('dead', { volume: 0.7 });
            this.sound.add('music', { volume: 0.4 }).play({ loop: -1 });
        }

        function create() {
            initSounds.call(this);
        }

        function update(time, delta) {
        }
    </script>
</body>
</html>

Proposed exercise: Using your own music

Download any other music file you like to use it as a background music and change the JavaScript code inside the “preload()” function to set the file name accordingly. Also in the “initSounds()” function you will find that you may change the volume of the music (volume:0.4). Try changing that value from 0.1 to 1 and you will notice that the volume of the music will also change.

You may find in the following sites some free images and sounds which you can download and use for your own game:

The map

Once we have initialized Phaser and after loading all the assets, we can go ahead and display the map on the screen.

Proposed exercise: Displaying the map

Add the function “createWorld()” (provided below) to your code, and modify the calling function “create()” so that it is called when the game is started (also shown below). Finally test everything in your browser from your domain.

About the music, do not forget to click on the screen after loading the game (the music will not start playing unless the browser gets the focus). Also, you can see the result here (you can look at the source code by pressing Ctrl+U).
  1. This is the new function you have to insert in your code to create the world:
function createWorld() {
    map = this.make.tilemap({ key: "map" });

    scale *= Math.max(screenWidth/map.widthInPixels, screenHeight/map.heightInPixels);

    worldWidth = map.widthInPixels * scale;
    worldHeight = map.heightInPixels * scale;
    speed *= scale;
    joystickSize *= scale;

    // Parameters are the name you gave the tileset in Tiled and then the key of the tileset image in
    // Phaser's cache (i.e. the name you used in preload)
    tileset = map.addTilesetImage("tileset", "tiles");

    // Parameters: layer name (or index) from Tiled, tileset, x, y
    belowLayer = map.createLayer("Below Player", tileset, 0, 0).setScale(scale).setDepth(1);
    worldLayer = map.createLayer("World", tileset, 0, 0).setScale(scale).setDepth(2);
    aboveLayer = map.createLayer("Above Player", tileset, 0, 0).setScale(scale).setDepth(3);

    worldLayer.setCollisionByProperty({ collides: true });

    // Find empty tiles where new zombies, coins or health can be created
    emptyTiles = worldLayer.filterTiles(tile => (tile.index === -1 || !tile.collides));
}
  1. Do not forget to update the “create()” function to create the world. This is the only line you should add to that function:
function create() {
    ...
    createWorld.call(this);            
}

Proposed exercise: Scaling the map

Change the value of the “scale” variable (at the top of your code) and refresh the map in your browser to check that it is scaled accordingly. You can set values such as “scale = 0.5” and you will notice that the map just fits half the screen. Set also higher values and check the results again.

The animations (sprites)

We will use sprites to create the player and the animated objects. For example, let’s have a look at the player:

Player’s sprite sheet

We may appreciate that we have 36 pictures divided into 4 different groups. This way, we may easily create all four animations:

  • Up: Pictures from 0 to 8
  • Left: Pictures from 9 to 17
  • Down: Pictures from 18 to 26
  • Right: Pictures from 27 to 35

Controlling the player’s animations

So that this game can be used in both desktop and mobile devices, we have to initialize both the cursor keys and the joystick. Once this is done, we must change the player’s velocity and the current animation when the user presses the keys or moves the joystick. In the next exercise we will first define the animations inside the “createAnimations()” function, and after that we will go ahead with the “createPlayer()” function, where we will display the player. We will finally include in the “update()” function some conditions to adjust the player’s speed and current animation.

Proposed exercise: Adding the player

Add the code below to your game and check both the player animations and movements. After that, look for another sprite and change the code accordingly to use your own sprite. Finally, change the values of the constant “speed” (at the top of your code) and check the results. You will also notice that the camera will follow you on each player’s movement.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose other sprites you like.
  1. These are the new functions you have to insert in your code to create the animations and initialize everything related to the player:
function createAnimations() {
    const anims = this.anims;
    // Player
    anims.create({
        key: "left", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 9, end: 17 }),
    });
    anims.create({
        key: "right", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 27, end: 35 }),
    });
    anims.create({
        key: "up", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 0, end: 8 }),
    });
    anims.create({
        key: "down", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 18, end: 26 }),
    });
    anims.create({
        key: "waiting", frameRate: 3, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 19, end: 21 }),
    });
    // Coin
    anims.create({
        key: "flying", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('coin', { start: 0, end: 7 }),
    });
    // Health
    anims.create({
        key: "floating", frameRate: 5, repeat: -1,
        frames: this.anims.generateFrameNumbers('health', { start: 0, end: 3 }),
    });
    // Power
    anims.create({
        key: "growing", frameRate: 5, repeat: -1,
        frames: this.anims.generateFrameNumbers('power', { start: 0, end: 3 }),
    });
    // Zombie
    anims.create({
        key: "moving", frameRate: 5, repeat: -1,
        frames: this.anims.generateFrameNumbers('zombie', { start: 0, end: 11 }),
    });
}

function createPlayer() {
    // Object layers in Tiled let you embed extra info into a map - like a spawn point or custom
    // collision shapes. In the tmx file, there's an object layer with a point named "Spawn Point"
    const spawnPoint = map.findObject("Objects", obj => obj.name === "Spawn Point");

    // Create a sprite with physics enabled via the physics system. The image used for the sprite has
    // a bit of whitespace, so I'm using setSize & setOffset to control the size of the player's body
    player = this.physics.add
        .sprite(spawnPoint.x * scale, spawnPoint.y * scale, "player")
        .setSize(16, 32).setOffset(24, 32).setScale(scale)
        .setCollideWorldBounds(true).setDepth(2);

    // Watch the player and worldLayer for collisions, for the duration of the scene
    this.physics.add.collider(player, worldLayer);

    const camera = this.cameras.main;
    const world = this.physics.world;
    camera.startFollow(player);
    camera.setBounds(0, 0, worldWidth, worldHeight);
    world.setBounds(0, 0, worldWidth, worldHeight);

    cursors = this.input.keyboard.createCursorKeys();

    // Do not show the joysticks on desktop devices
    if (!this.sys.game.device.os.desktop) {
        joyStick = this.plugins.get('rexvirtualjoystickplugin').add(this, {
            x: screenWidth - joystickSize * 2,
            y: screenHeight - joystickSize * 2,
            radius: joystickSize,
            base: this.add.circle(0, 0, joystickSize, 0x888888).setAlpha(0.5).setDepth(4),
            thumb: this.add.circle(0, 0, joystickSize, 0xcccccc).setAlpha(0.5).setDepth(4)
        }).on('update', update, this);

        joyStick2 = this.plugins.get('rexvirtualjoystickplugin').add(this, {
            x: joystickSize * 2,
            y: screenHeight - joystickSize * 2,
            radius: joystickSize,
            base: this.add.circle(0, 0, joystickSize, 0x888888).setAlpha(0.5).setDepth(4),
            thumb: this.add.circle(0, 0, joystickSize, 0xcccccc).setAlpha(0.5).setDepth(4)
        }).on('update', update, this);
    }          
}
  1. Also you have to insert some code inside the “update()” function to respond to the cursor keys and the joystick:
function update(time, delta) {
    if (gameOver) return;

    let vX = 0, vY = 0;

    // Horizontal movement
    if (cursors.left.isDown || joyStick.left || joyStick2.left) vX = -speed;
    else if (cursors.right.isDown || joyStick.right || joyStick2.right) vX = speed;

    // Vertical movement
    if (cursors.up.isDown || joyStick.up || joyStick2.up) vY = -speed;
    else if (cursors.down.isDown || joyStick.down || joyStick2.down) vY = speed;

    // Set and normalize the velocity so that player can't move faster along a diagonal
    player.setVelocity(vX, vY).body.velocity.normalize().scale(speed);

    // Choose the right player's animation
    if (vX < 0) player.anims.play('left', true);
    else if (vX > 0) player.anims.play('right', true);
    else if (vY < 0) player.anims.play('up', true);
    else if (vY > 0) player.anims.play('down', true);
    else player.anims.play('waiting', true);            
}
  1. And finally do not forget to update the “create()” function to create the animations and the player. You just have to insert a couple of new lines:
function create() {
    ...
    createAnimations.call(this); 
    createPlayer.call(this);
}

Proposed exercises: Modifying the map

Download the tile map editor from this link and install it on your computer. Go to the assets folder and open the file “town.tmx” which contains the map of the example explained in this unit. Select the layer “World” (on the right corner, at the top) and change anything you like (by adding or removing some objects to or from the map). When you finish, export the map to JSON format using the option “File” -> “Export as…”, and overwrite the file “town.json”, located inside the “assets” folder. Finally check the results using your browser and move the player to go to each part of the map. You will notice again that the camera will follow you on each movement.

Do not forget to fully refresh the contents of the browser by pressing Ctrl+F5, since the map and some other contents are usually cached by the browser. Also move the player and check that the map has been updated with the changes you have made.

Score, coins, power, health, and enemies

We will create many different objects at once using the same images. We will just change the size of the objects by scaling them, and we will use a loop to print as many objects as we like.

Proposed exercise: Adding the score

Using the global variables, we may easily show both the score and number of remaining lives. Add the code below to your game and check the results. After that, change the value of the variable “lives” (at the top of your code) to check how easily you can customize that text.

You can see the result here (you can look at the source code by pressing Ctrl+U).
function showScore() {
    if (!scoreText) {
        scoreText = this.add.text(screenWidth/2, 16, '', { fontSize: (32 * scale) + 'px', fill: '#FFF' });
        scoreText.setShadow(3, 3, 'rgba(0,0,0,1)', 3).setOrigin(0.5, 0).setScrollFactor(0).setDepth(4);
    }
    scoreText.setText('Score:' + score + ' / Lives:' + lives);
}
function create() {
    ...
    showScore.call(this);
}

Proposed exercises: Adding the coins

Add the code below to your file and check the results. After that, change the value of the constant “numCoins” (at the top of your code) to check how easily you can customize your game. Refresh the page several times and you will notice that the generated coins have random sizes and they appear at random positions. Finally, look for another sprite sheet (it may be a coin or any other object you like) and change the code accordingly to use your own animated object.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose the images you like.
function newObject(type, animation, context) {
    let tile = Phaser.Utils.Array.GetRandom(emptyTiles);
    return context.physics.add.sprite(tile.pixelX * scale, tile.pixelY * scale, type).anims.play(animation, true).setDepth(2);
}

function createCoin() {
    const v = Phaser.Math.Between(speed / 10, speed / 5);
    let coin = newObject('coin', 'flying', this).setSize(16, 32).setScale(1.5 * scale).setBounce(1).setVelocity(v, 0);
    coin.body.setAllowGravity(false).setCollideWorldBounds(true);
    this.physics.add.collider(coin, worldLayer);
    this.physics.add.overlap(player, coin, collectCoin, null, this);
} 

function collectCoin(player, coin) {
    bell.play();
    coin.destroy();
    createCoin.call(this);

    score += 10;
    showScore();
}
function create() {
    ...
    for (i = 0; i < numCoins; i++) setTimeout(() => createCoin.call(this), Phaser.Math.Between(0, 5000));
}

Proposed exercises: Adding the health

Add the code below to your game and check the results. After that, change the value of the constants “numHealth” (at the top of your code) to check how easily you can customize your game. Finally, look for another sprite and change the code accordingly to use your own object to provide the player with extra lives.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createHealth() {
    let health = newObject('health', 'floating', this).setSize(16, 32).setScale(1.2 * scale);
    health.body.setAllowGravity(false);
    this.physics.add.overlap(player, health, collectHealth, null, this);
}

function collectHealth(player, health) {
    bell2.play();
    health.destroy();
    createHealth.call(this);

    lives++;
    showScore();
}
function create() {
    ...
    for (i = 0; i < numHealth; i++) setTimeout(() => createHealth.call(this), Phaser.Math.Between(0, 5000));
}

Proposed exercises: Adding the power

Add the code below to your game and check the results. After that, change the value of the constants “numPower” (at the top of your code) to check how easily you can customize your game. Finally, look for another sprite and change the code accordingly to use your own power object.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createPower() {
    let power = newObject('power', 'growing', this).setSize(32, 64).setScale(scale/2);
    power.body.setAllowGravity(false);
    this.physics.add.overlap(player, power, collectPower, null, this);
}

function protect(color) {
    player.setTint(color);
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => { timeout = false; player.clearTint() }, 5000);
}

function collectPower(player, power) {
    bell2.play();
    power.destroy();
    createPower.call(this);

    protect(0xFFFF00);
}
function create() {
    ...
    for (i = 0; i < numPower; i++) setTimeout(() => createPower.call(this), Phaser.Math.Between(0, 5000)); 
}

Proposed exercises: Adding the enemies

Add the code below to your game and check the results. After that, change the value of the constants “numZombies” (at the top of your code) to check how easily you can customize your game. Finally, look for another sprite and change the code accordingly to display your own enemies.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createZombie() {
    const v = Phaser.Math.Between(-speed / 4, -speed / 3);
    let zombie = newObject('zombie', 'moving', this).setSize(16, 32).setOffset(16, 32).setScale(1.2 * scale).setBounce(1).setVelocity(0, v);
    zombie.body.setAllowGravity(false).setCollideWorldBounds(true);
    this.physics.add.collider(zombie, worldLayer);
    this.physics.add.overlap(player, zombie, hitZombie, null, this);
}

function hitZombie(player, zombie) {
    // If the player is already hurt, it cannot be hurt again for a while
    if (player.tintTopLeft == 0xFF0000) return;

    if (timeout) {
        score += 15;
        showScore();
    }
    else {
        protect(0xFF0000);
        lives--;
    }

    dead.play();
    showScore();

    if (lives == 0) {
        this.physics.pause();
        gameOver = true;
        this.add.image(screenWidth / 2, screenHeight / 2, 'restart')
            .setScale(4).setScrollFactor(0).setDepth(4)
            .setInteractive().on('pointerdown', () => location.reload());
    }
    else {
        zombie.destroy();
        createZombie.call(this);
    }
} 
function create() {
    ...
    for (i = 0; i < numZombies; i++) setTimeout(() => createZombie.call(this), Phaser.Math.Between(0, 5000)); 
}

Enjoy the game!

You may enjoy playing this wonderful game online here.

Phaser. Unit 2. A bigger world

Introduction

In this unit we are going to expand our world with a whole different scene. We will use the game developed in the previous unit so that by modifying or adding some code, we will have double the entertainment than before 🙂

The new assets

These are the new assets that we will use to build the next scene (you can download all the assets here):

An arrow to jump to the next scene
Flying stars that will increase the score
Dead trees
Flying dragons trying to kill the player
Sound to be played after collecting a star

Expanding the world

The first thing we are going to do is creating a wider sky and platform, using new colors for the new scene.

Proposed exercise: Expanding sky and platform

First check that you have a working game on your domain. After that, follow the instructions below, and upload the new code and the new assets to your domain (you can get all the assets from this link). Finally, check the results in your browser.

Now you should have an arrow on the right corner, at the top of the screen, so that you can easily jump to the next scene. Your new game should look now like this one.
  1. Insert at the top of your code a new constant with the new size of the world (double than before):
const maxWorldWidth = worldWidth * 2;
  1. Modify the function “createWorld()” so that both the camera and world bounds use this new constant, and also insert the new sky and the new platform:
function createWorld() {
    this.cameras.main.setBounds(0, 0, maxWorldWidth, screenHeight);
    this.physics.world.setBounds(0, 0, maxWorldWidth, screenHeight);

    ...

    // New sky
    this.add.rectangle(worldWidth, 0, worldWidth, screenHeight, 0xff6600).setOrigin(0);

    // New platform
    platform2 = this.add.rectangle(worldWidth, screenHeight, worldWidth, platformHeight, 0x006600).setOrigin(0, 1);
    this.physics.add.existing(platform2);
    platform2.body.setCollideWorldBounds(true);
}
  1. Activate the collision between the player and the new platform. This can be done with a single line inside the “createPlayer()” function:
function createPlayer() {
    ...
    this.physics.add.collider(player, platform2);
}
  1. Load the new assets (the button to jump to the next scene, the dead tree, the stars to be collected, the sound to be played when collecting the stars, and the sprite sheet of the flying dragon) inside the “preload()” function:
function preload() {
    ...
    this.load.image('arrow', 'assets/arrow.png');
    this.load.image('tree2', 'assets/tree2.png');
    this.load.image('star', 'assets/star.png');
    this.load.audio('bell2', 'assets/ding2.mp3');
    this.load.spritesheet('dragon', 'assets/dragon.png', { frameWidth: 144, frameHeight: 128 });
}
  1. Insert a new function to display the arrow to jump to the next scene:
function showArrow() {
    this.add.image(screenWidth, 0, 'arrow').setOrigin(1, 0).setScale(2).setScrollFactor(0).setInteractive().on('pointerdown', () => player.x = (player.x + worldWidth) % maxWorldWidth);
}
  1. And finally you must execute that code, just by adding a new line to the “create()” function:
function create() {
    ...            
    showArrow.call(this);
}

Proposed exercise: A big tree between the two scenes

To achieve a better transition between one scene to the other, we will put a dead tree in the middle. You just need to insert one line of code at the end of the “createWorld()” function:

You can see the expected result here.
function createWorld() {
    ...
    this.add.image(worldWidth, 0, 'tree2').setOrigin(0.5, 0).setDisplaySize(screenWidth/5, screenHeight);
}

Proposed exercise: Changing the new scene’s colors

Choose any colors you like for the new scene, and change the colors of both the sky and the platform. You only have to change a couple of values inside the function “createWorld()”.

You can use a color picker to get the values of the new colors in hexadecimal notation.

More trees, more fires, more clouds…

Now we have to show the fires and the clouds in the new scene. And to change even more the appearance of the new scene, we will not change only the colors but also the background images, by using new trees.

Proposed exercise: More trees

Update the constant with the number of trees to be displayed, and also, add a new line to the “createTree()” function so that lots of new trees are shown inside the new scene.

You can see the expected result here.
  1. Update the constant to show as many trees as you like:
const numTrees = 100;
  1. Create new trees in the new scene (you will notice that these trees are shown at ‘x+worldWidth’ position, and also the ‘scale’ is adjusted for the new image):
function createTree() {
    ...
    this.add.image(x + worldWidth, y, 'tree2').setOrigin(1).setScale(scale * 2);
}

Proposed exercise: More fires

Update the constant with the number of fires to be displayed, and also, update the “createFire()” function to use the constant ‘maxWorldWidth’ as the limit to generate the fires.

You can see the expected result here.
  1. Update the constant to show as many fires as you like:
const numFires = 50;
  1. Update the following line to use ‘maxWorldWidth’ constant instead:
function createFire() {
    const x = Phaser.Math.Between(screenWidth/2, maxWorldWidth);
    ...
}

Proposed exercise: More clouds

Update the constant with the number of clouds to be displayed, and also, update the “createClouds()” function to use the constant “maxWorldWidth” as the limit to generate the clouds.

You can see the expected result here.
  1. Update the constant to show as many clouds as you like:
const numClouds = 100;
  1. Update the following line to use “maxWorldWidth” constant instead:
function createCloud() {
    const x = Phaser.Math.Between(0, maxWorldWidth);
    ...
}

The new moving objects (stars and dragons)

Now we will add to the new scene a couple of moving objects: stars and dragons. After collecting a star, a new sound will be played, and as a new feature, we will be protected for several seconds so that in case we hit a killing object, we remain unaffected. About the dragons, we may get the same behaviour as if we hit the bombs or the fires.

Proposed exercise: Adding the stars

Add or modify all the required code that will be in charge of creating the stars in the new scene and that will also handle the events related to the new objects (playing sound, increasing lives, etc.). You will have to follow the steps below.

You can see the expected result here.
  1. Create the new variables (the number of stars to be displayed, and the timeout to be protected when you are hit by the bombs, the fires, or the dragons):
const numStars = 50;
var timeout = false;
  1. Initialize the sound to be played when collecting the stars, just by adding a single line inside the existing “initSounds()” function. You might also need to adjust the volume by using the right value from 0.1 to 1:
function initSounds() {
    bell2 = this.sound.add('bell2', { volume: 0.2 });
    ...
}
  1. Add the new functions to your code. The “collectStar()” function will print each star, the “protect()” function will activate a player’s protection to avoid being killed for a while, and the “collectStar()” function will contain the code be executed each time you collect a star:
function createStar() {
    const x = Phaser.Math.Between(worldWidth, worldWidth * 2);
    const vX = Phaser.Math.Between(-velocityX, velocityX);
    const vY = Phaser.Math.Between(velocityY/2, velocityY);
    let star = this.physics.add.image(x, 0, 'star').setScale(0.5).setBounce(1).setCollideWorldBounds(true).setVelocity(vX, vY);
    star.body.setAllowGravity(false);
    this.physics.add.collider(star, platform);
    this.physics.add.collider(star, platform2);
    this.physics.add.collider(player, star, collectStar, null, this);
}

function protect(color) {
    player.setTint(color);
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => { timeout=false; player.clearTint() }, 3000);
}

function collectStar(player, star) {
    bell2.play();
    star.destroy();
    createStar.call(this);

    protect(0xFFFF00);

    score += 10;
    if (score % 100 == 0) lives++;
    showScore();
}
  1. And finally insert the loop to create all the stars at once inside the “create()” function:
funtion create() {
    ...
    for (i = 0; i < numStars; i++) createStar.call(this);
}

Proposed exercise: Adding the dragons

Add or modify all the required code that will be in charge of creating the dragons in the new scene and that will also handle the events related to them (playing sound, decreasing lives, etc.). You will have to follow the steps below.

You can see the expected result here.
  1. Insert at the top of your code a new constat to set the number of dragons you would like to print:
const numDragons = 25;
  1. Add the “flying” animation to the function “createAnimations()”:
function createAnimations() {
    ...
    // Dragon
    this.anims.create({
        key: 'flying',
        frames: this.anims.generateFrameNumbers('dragon', { start: 0, end: 11 }),
        frameRate: 5,
        repeat: -1
    });
}
  1. Add the new functions “createDragon()” and “hitDragon()” to your code, to display each dragon and to perform any action we want to be done after being hit:
function createDragon() {
    const x = Phaser.Math.Between(worldWidth, worldWidth * 2);
    const y = Phaser.Math.Between(0, screenHeight - platformHeight);
    const v = Phaser.Math.Between(velocityY/2, velocityY);
    let dragon = this.physics.add.sprite(x, y, 'dragon').setOrigin(1).setSize(72, 64).setScale(2).anims.play('flying', true).setBounce(1).setVelocity(0, v);
    dragon.body.setAllowGravity(false).setCollideWorldBounds(true);
    this.physics.add.collider(dragon, platform2);
    this.physics.add.collider(player, dragon, hitDragon, null, this);
}

function hitDragon(player, dragon) {
    dead.play();
    lives--;
    showScore();

    protect(0xFF0000);

    if (lives == 0) {
        this.physics.pause();
        gameOver = true;
        this.add.image(screenWidth/2, screenHeight/2, 'restart').setScale(5).setScrollFactor(0).setInteractive().on('pointerdown', () => location.reload());
    }
    else {
        dragon.destroy();
    }
}
  1. And finally, add a new loop to the “create()” function to print all the dragons at once:
function create() {
    ...
    for (i = 0; i < numDragons; i++) createDragon.call(this);
}

Enjoy the game!

You may enjoy playing this wonderful game online here.

Phaser. Unit 1. Your first game

Introduction

Phaser is one of the best frameworks to develop Desktop and mobile HTML games. You will be able to create 2D games and make them available to everyone with a simple domain with less than 10MB storage capacity. Also, it is fast, free, and opensource!

You may find more information at the Phaser official page and you may also have a look at some interesting demonstrations at the examples page.

HTML and CSS code

The first thing we should do is linking the Phaser library. We will use the last version at the time this unit has been written:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>

After that we are going to insert some CSS code to remove any margin and padding, and also to remove the scrollbars, so that all the window size is used when the game is started:

html, body {
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    height: 100%;
} 

And finally we are going to use a single file to put all the code inside. This is going to be the basic structure of the “.html” file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html, body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>
<body>
    <script type="text/javascript">
        ...
    </script>
</body>
</html>

Constants

We are going to define some constants at the beginning of the code so that we can easily change the game appearance, difficulty, and some other parameters:

// The game will use the whole window
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;

// The world width will be 10 times the size of the screen
const worldWidth = screenWidth*10;

// Height of platform and joystick size (at the bottom)
const platformHeight = screenHeight/6;
const joystickSize = platformHeight/3;

// Both x and y velocity (depending on the screen size) 
const velocityX = screenWidth/4;
const velocityY = screenHeight/2;

// Number of clouds, tries, fires and coins
const numClouds = 50;
const numTrees = 100;
const numFires = 25;
const numCoins = 50;

Phaser configuration and other variables

Following the constants we are going to create some variables so keep all the information about the game and also to start the initialization of Phaser:

var config = {
    type: Phaser.AUTO,
    width: screenWidth,
    height: screenHeight,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: velocityY }
        }
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var platform, player, bell, dead;
var cursors, joyStick;
var score = 0, lives = 5, scoreText = null;
var gameOver = false;

// Init Phaser
var game = new Phaser.Game(config);

The assets (images, sprites and sounds)

We will need some images, sprites and sounds. You may use any assets you like, but we are also providing some examples so that you can easily start testing (they can also be downloaded here):

Images

Cloud

Tree
Bomb
Restart button

Sprites (player and animated objects)

Player
Fire
Coin

Sounds and music

When the player gets a coin
When the user gets hurt by a bomb or a fire
Background music always playing

Loading all the assets

So that we can use all those images, sprites and music, they have to be loaded previously. That is the purpose of the “preload()” function. We will also include here the initialization of the joystick so that we can use it in another section of this unit.

Proposed exercise: Game initialization

Create a specific folder for your game in your domain to put all the code and assets inside. After that, create an “index.html” inside that folder with the code below, and also create an “assets” folder to put all the images and sounds, which can be downloaded here. After uploading everything to your domain, test the game using the url of that folder in your browser, and you should get a full black screen. Click on it and the background music should start playing.

You can see the result here (you can also look at the source code by pressing Ctrl+U).
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html, body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>

<body>
    <script type="text/javascript">
        const screenWidth = window.innerWidth;
        const screenHeight = window.innerHeight;

        const worldWidth = screenWidth * 10;

        const platformHeight = screenHeight / 6;
        const joystickSize = platformHeight / 3;

        const velocityX = screenWidth / 4;
        const velocityY = screenHeight / 2;

        const numTrees = 100;
        const numClouds = 50;
        const numFires = 25;
        const numCoins = 50;

        var config = {
            type: Phaser.AUTO,
            width: screenWidth,
            height: screenHeight,
            physics: {
                default: 'arcade',
                arcade: {
                    gravity: { y: velocityY }
                }
            },
            scene: {
                preload: preload,
                create: create,
                update: update
            }
        };

        var platform, player, bell, dead;
        var cursors, joyStick;
        var score = 0, lives = 5, scoreText = null;
        var gameOver = false;

        var game = new Phaser.Game(config);

        function preload() {
            // Load joyStick
            this.load.plugin('rexvirtualjoystickplugin', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/rexvirtualjoystickplugin.min.js', true);
       
            // Load images
            this.load.image('cloud', 'assets/cloud.png');
            this.load.image('tree', 'assets/tree.png');
            this.load.image('bomb', 'assets/bomb.png');
            this.load.image('restart', 'assets/restart.png');
            this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 });
            this.load.spritesheet('fire', 'assets/fire.png', { frameWidth: 32, frameHeight: 32 });
            this.load.spritesheet('coin', 'assets/coin.png', { frameWidth: 32, frameHeight: 32 });

            // Load sounds and music
            this.load.audio('bell', 'assets/ding.mp3');
            this.load.audio('dead', 'assets/dead.mp3');
            this.load.audio('music', 'assets/music.mp3');
        }

        function initSounds() {
            bell = this.sound.add('bell');
            dead = this.sound.add('dead');
            this.sound.add('music', {volume:0.5}).play({ loop: -1 });
        }

        function create() {
            initSounds.call(this);
        }

        function update() {
        }
    </script>
</body>
</html>

Proposed exercise: Using your own music

Download any other music file you like to use it as a background music and change the JavaScript code inside the “preload()” function to set the file name accordingly. Also in the “initSounds()” function you will find that you may change the volume of the music (volume:0.5). Try changing that value from 0.1 to 1 and you will notice that the volume of the music will also change.

You may find in the following sites some free images and sounds which you can download and use for your own game:

Sky and platform

Once we have initialized Phaser and after loading all the assets, we can go ahead and paint something on the screen. We are going the create a couple of rectangles filled with any color we like, and this way we will create the sky and the platform.

We will use the following colors (you may find more information about colors in hexadecimal notation here):

  • Sky (light blue): 0x87CEEB
  • Platform (brown): 0xB76743

Proposed exercise: Adding sky and platform

Add the function “createWorld()” (provided below) to your code, and modify the calling function “create()” so that it is called when the game is started (also shown below). Finally test everything in your browser from your domain. You may also change the colors of both the platform and the sky.

You may easily get the hexadecimal code of any color you like with a simple color picker. About the music, do not forget to click on the screen after loading the game (the music will not start playing unless the browser gets the focus). Also, you can see the result here (you can look at the source code by pressing Ctrl+U).
  1. This is the new function you have to insert in your code to create the world:
function createWorld() {
    this.cameras.main.setBounds(0, 0, worldWidth, screenHeight);
    this.physics.world.setBounds(0, 0, worldWidth, screenHeight);

    // Sky
    sky = this.add.rectangle(0, 0, worldWidth, screenHeight, 0x87CEEB).setOrigin(0);

    // Platform
    platform = this.add.rectangle(0, screenHeight, worldWidth, platformHeight, 0xB76743).setOrigin(1);
    this.physics.add.existing(platform);
    platform.body.setCollideWorldBounds(true);
}
  1. Do not forget to update the “create()” function to create the world (sky, platform, camera, and world boundaries). This is the only line you should add to that function:
function create() {
    ...
    createWorld.call(this);            
}

Objects in background (trees)

We will first create the trees so that they remain in the background. We may create all of them using the same image. We will just change the size of the trees by scaling them randomly, and we will also choose a random position for each of them. Finally we will use a loop to print as many trees as we like.

Proposed exercises: Adding the trees

Add the code below to your file and check the results. After that, change the value of the constant “numTrees” (at the top of your code) and also the value of the “scale” variable to check how easily you can customize your game. Refresh the page several times and you will notice that the generated trees have random sizes and they also appear in random positions. Finally, look for another image (a tree or any other object) and change the code accordingly to use your own image.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt.
  1. This is the new function you have to insert in your code to create each tree:
function createTree() {
    const x = Phaser.Math.Between(0, worldWidth);
    const y = screenHeight-platformHeight;
    const scale = Phaser.Math.FloatBetween(0.5, 2);
    this.add.image(x, y, 'tree').setOrigin(1).setScale(scale);
}
  1. Do not forget to update the “create()” function to create all the trees. You only need one single line to create all trees at once using a loop:
function create() {
    ...
    for(i=0; i<numTrees; i++) createTree.call(this);          
}

The animations (sprites)

We will use sprites to create the player and the animated objects. For example, let’s have a look at the player:

Sprite with three player animations

We may appreciate that we have 9 pictures divided into 3 different groups. This way, we may easily create all three animations:

  • Left: Pictures from 0 to 3
  • Turn: Picture number 4
  • Right: Pictures from 5 to 8

Controlling the player’s animations

So that this game can be used in both desktop and mobile devices, we have to initialize both the cursor keys and the joystick. Once this is done, we must change the player’s velocity and the current animation when the user presses the keys or moves the joystick. In the next exercise we will first define the animations inside the “createAnimations()” function, and after that we will go ahead with the “createPlayer()” function, where we will display the player. We will finally include in the “update()” function some conditions to adjust the player’s speed and current animation.

Proposed exercise: Adding the player

Add the code below to your game and check both the player animations and movements. After that, look for another sprite and change the code accordingly to use your own sprite. Finally, change the values of the constants “velocityX” and “velocityY” (at the top of your code) and check the results.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose other sprites you like.
  1. These are the new functions you have to insert in your code to create the animations and initialize everything related to the player:
function createAnimations() {
    // Player
    this.anims.create({
        key: 'left',
        frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
        frameRate: 10,
        repeat: -1
    });
    this.anims.create({
        key: 'turn',
        frames: [{ key: 'dude', frame: 4 }]
    });
    this.anims.create({
        key: 'right',
        frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
        frameRate: 10,
        repeat: -1
    });
    // Fire
    this.anims.create({
        key: 'burning',
        frames: this.anims.generateFrameNumbers('fire', { start: 0, end: 4 }),
        frameRate: 10,
        repeat: -1
    });
    // Coin
    this.anims.create({
        key: 'rotate',
        frames: this.anims.generateFrameNumbers('coin', { start: 0, end: 7 }),
        frameRate: 10,
        repeat: -1
    });            
}

function createPlayer() {
    player = this.physics.add.sprite(0, screenHeight-platformHeight, 'dude').setOrigin(1).setScale(3).setBounce(0.2).setCollideWorldBounds(true);
    this.physics.add.collider(player, platform);
    this.cameras.main.startFollow(player, true, 0.05, 0.05);
    // Cursor keys and joystick
    cursors = this.input.keyboard.createCursorKeys();
    joyStick = this.plugins.get('rexvirtualjoystickplugin').add(this, {
        x: screenWidth / 2,
        y: screenHeight - joystickSize*1.5,
        radius: joystickSize
    }).on('update', update, this);
}
  1. Also you have to insert some code inside the “update()” function to respond to the cursor keys and the joystick:
function update() {
    if (gameOver) return;

    if (cursors.left.isDown || joyStick.left) {
        player.setVelocityX(-velocityX).anims.play('left', true);
    }
    else if (cursors.right.isDown || joyStick.right) {
        player.setVelocityX(velocityX).anims.play('right', true);
    }
    else {
        player.setVelocityX(0).anims.play('turn');
    }

    if ((cursors.up.isDown || joyStick.up) && player.body.touching.down) {
        player.setVelocityY(-velocityY);
    }
}
  1. And finally do not forget to update the “create()” function to create the animations and the player. You just have to insert a couple of new lines:
function create() {
    ...
    createAnimations.call(this); 
    createPlayer.call(this);
}

The clouds, the score, the fires and the coins

We will create many different objects (clouds, fires and coins) at once using the same images. We will just change the size of the objects by scaling them randomly, and we will use a loop to print as many objects as we like choosing random positions.

Proposed exercises: Adding the clouds

Add the code below to your file and check the results. After that, change the value of the constant “numClouds” (at the top of your code) and also the value of the “scale” variable to check how easily you can customize your game. Refresh the page several times and you will notice that the generated clouds and also the trees have random sizes and they appear in random positions. Finally, look for another image (a cloud or any other object) and change the code accordingly to use your own image.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose the images you like.
function createCloud() {
    const x = Phaser.Math.Between(0, worldWidth);
    const y = Phaser.Math.Between(0, screenHeight-platformHeight*2);
    const scale = Phaser.Math.FloatBetween(0.5, 1.5);
    this.add.image(x, y, 'cloud').setScale(scale);
}
function create() {
    ...
    for(i=0; i<numClouds; i++) createCloud.call(this);
}

Proposed exercise: Adding the score

Using the global variables, we may easily show both the score and number of remaining lives. Add the code below to your game and check the results. After that, change the value of the constant “numLives” (at the top of your code) and also the value of the “scale” variable to check how easily you can customize that text.

You can see the result here (you can look at the source code by pressing Ctrl+U).
function showScore() {
    if (!scoreText) scoreText = this.add.text(16, 16, '', { fontSize:(screenWidth/20)+'px', fill:'#000' }).setScrollFactor(0);
    scoreText.setText('Score:' + score + ' / Lives:' + lives);
}
function create() {
    ...
    showScore.call(this);
}

Proposed exercises: Adding the fires and the coins

Add the code below to your game and check the results. After that, change the value of the constants “numFires” and “numCoins” (at the top of your code) and also the value of the “scale” variables to check how easily you can customize your game. Finally, look for another sprites (fires, coins, or any other objects) and change the code accordingly to use your own sprites.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createFire() {
    const x = Phaser.Math.Between(screenWidth/2, worldWidth);
    const y = Phaser.Math.Between(screenHeight-platformHeight, screenHeight);
    let fire = this.physics.add.sprite(x, y, 'fire').setOrigin(1).setScale(3).setImmovable(true).anims.play('burning', true)
    fire.body.setAllowGravity(false);
    this.physics.add.collider(player, fire, hitBombOrFire, null, this);   
}

function createCoin() {     
    const x = Phaser.Math.Between(screenWidth/2, worldWidth);
    const bounce = Phaser.Math.FloatBetween(0.1, 0.5);
    let coin = this.physics.add.sprite(x, 0, 'coin').setOrigin(1).setScale(3).setBounce(bounce).anims.play('rotate');
    coin.body.setOffset(0, -10);
    this.physics.add.collider(coin, platform);
    this.physics.add.overlap(player, coin, collectCoin, null, this);
}

function createBomb() {
    const x = Phaser.Math.Between(0, worldWidth);
    const v = Phaser.Math.Between(-velocityX, velocityX);
    let bomb = this.physics.add.image(x, 0, 'bomb').setScale(2).setBounce(1).setCollideWorldBounds(true).setVelocity(v, velocityY);
    bomb.body.setAllowGravity(false);
    this.physics.add.collider(bomb, platform);     
    this.physics.add.collider(player, bomb, hitBombOrFire, null, this);   
}

function collectCoin(player, coin) { 
    bell.play();
    coin.destroy();
    createCoin.call(this);
    createBomb.call(this);    

    score += 10;
    if (score % 100 == 0) lives++;
    showScore();
}

function hitBombOrFire(player, thing) {
    dead.play();
    lives--;
    showScore();
    player.setTint(0xff0000).anims.play('turn');

    if (lives == 0) {
        this.physics.pause();
        gameOver = true;
        this.add.image(screenWidth/2, screenHeight/2, 'restart').setScale(5).setScrollFactor(0).setInteractive().on('pointerdown', ()=>location.reload());                
    }
    else {
        thing.destroy();
        setTimeout(()=>player.clearTint(), 3000);
    }
}
function create() {
    ...
    for(i=0; i<numCoins; i++) createFire.call(this);
    for(i=0; i<numCoins; i++) createCoin.call(this);
}

Enjoy the game!

You may enjoy playing this wonderful game online here.