Create a Javascript Game

js-logo

Learn how to create a web game using Phaser 3, a popular javascript game engine. Draw a map using an editor, implement the player, make the player move, apply physics, collisions, and implement the enemies.


Introduction to Phaser

Make your first web game with Phaser 3

In this course, you will learn how to create a web game using Phaser 3 which is a game engine. You will also learn how to draw a map using a map editor, implementation of the player, make the player move, applying physics, collisions and implementation of enemies. You can preview the game here.

Downloads

To follow this course, you can download the source file, which will help you compare your progress.

What is Phaser?

Phaser1
Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers supporting Canvas and WebGL rendering. Phaser 3 is the new version of the Phaser Game Framework series. It includes a brand-new custom WebGL renderer designed specifically for the needs of modern 2D games. Phaser uses both a Canvas and WebGL renderer internally and automatically switch between them based on browser support. This allows for lightning fast rendering across Desktop and Mobile.

Why use Phaser?

Phaser is one of the best game engines for the Web using Javascript. It is a free framework that allows you to render your game across different platforms. The community is still growing, the documentation is really well explained and there's a ton of examples on their lab page to help you build your game. Phaser is powerful and easy to use if you want to start your own game.

Project

To follow this tutorial, you need a code editor of your choice. In this course, we are using Visual Studio Code. You also need the project template of Phaser 3Tiled.

Setup

We will be using the Project Template It is a template using Phaser with all the dependencies already installed for you so you can start building the game as soon as possible.
First, we will choose a location for the folder. Open your terminal. Enter this code inside the terminal to access your Desktop or the location of your choice.
cd Desktop
Then, we will clone the project template.

git clone https://github.com/photonstorm/phaser3-project-template.git


Installing dependencies

Now, you have your project. Drag and drop the folder inside Visual Studio Code. Use the shortcut CONTROL + ~ to open the internal terminal. Enter these code. You need to install the dependencies of Phaser that we need to create our game.

npm i


Phaser is already installed but, just to be sure, enter this command in your terminal to install the latest version of Phaser 3.


npm i phaser


Local environment

When you build your game, you need an environment for your code to be able to preview on your browser. This code will enable the local environment. When you close Visual Studio Code, you need to run this command to re-access it.

npm start


After running the project, it will tell you if the compilation was successful or failed. Then, in your web browser, search this link.

http://localhost:8000/


Now, you see your project. This is the Project template of Phaser.

Settings

Let's have an overview of the Project. The first file is index.html, it contains all the HTML and styling of the page.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

</head>
<body>
<script src="build/project.bundle.js" charset="utf-8"></script>
</body>
</html>


Configuration

Inside the src folder, this code is the Phaser 3 basic template, you define the renderer, the configuration of the size of the game container, also the gravity of this world. In Phaser 3, games are structured around Scene objects. The scene has 2 functions.

import 'phaser'; var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,

height: 600,
scene: {
preload: preload,
create: create
}
};

var game = new Phaser.Game(config)


3 Main functions

The scene is defined with these 3 functions:

  1. function preload: Run 1 time. Loads up all the assets inside the scene like images and audio
  1. function create: Run 1 time. Position the assets that are already preloaded, animations, physics ...
function update: Run 1 time per frame, takes care of everything related to the game logic


  1. function preload() {
    }
    function create() {
    }
    function update() {

    }

    Update Function

    As you can see, there's no update function so let's create one. In the config, add to the scene an update parameter.

    update: update,

    Also, create a new function for update.

    function update() {

    }

    Then, let's use this new function. We will apply a transformation that runs over time to the logo. The transform will be a rotation. Declare the logo variable under the player and remove the var inside create function. Inside the update function, run the rotation action.

    logo.rotation += 0.01

    Conclusion

    In this section, we learned what Phaser 3 is and how to set it up. I hope you enjoyed the first step into making your javascript game.