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
Sprites (player and animated objects)
Sounds and music
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:
- OpenGameArt.
- Itch.io.
- GameDev Market and Free character assets.
- Reddit /r/GameAssets.
- Game Art 2D.
- CraftPix.
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).
- 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); }
- 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.
- 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); }
- 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:
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.
- 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); }
- 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); } }
- 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.