docs.rodeo

MDN Web Docs mirror

Initialize the framework

{{PreviousNext("Games/Tutorials/2D_breakout_game_Phaser", "Games/Tutorials/2D_breakout_game_Phaser/Scaling")}} 

This is the first of 16 tutorials to learn how to use Gamedev Phaser. Before we can start writing the game’s functionality, we need to create a basic structure to render the game inside. This can be done using HTML—the Phaser framework will generate the required {{htmlelement("canvas")}}  element.

The game’s HTML

The HTML document structure is quite simple, as the game will be rendered entirely on the {{htmlelement("canvas")}}  element generated by the framework. Using your favorite text editor, create a new HTML document, save it as index.html, in a sensible location, and add the following code to it:

<!doctype html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <title>Gamedev Phaser Workshop - lesson 01: Initialize the framework</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
    </style>
    <script src="js/phaser.min.js"></script>
    <script src="js/script.js" defer></script>
  </head>
  <body></body>
</html>

And create a new js directory in the same location as your index.html file, and create a new file called script.js inside it. This is where we will write the JavaScript code that controls the game. Initially it should contain the following:

class ExampleScene extends Phaser.Scene {
  preload() {}
  create() {}
  update() {}
}

const config = {
  type: Phaser.CANVAS,
  width: 480,
  height: 320,
  scene: ExampleScene,
};

const game = new Phaser.Game(config);

Downloading the Phaser code

Next, we need to go through the process of downloading the Phaser source code and applying it to our HTML document. This tutorial uses Phaser v3 (v3.90.0 as of the time of writing, though newer minor versions should work the same).

  1. Go to the Phaser download page.
  2. Choose an option that suits you best—we recommend the phaser.min.js option as it keeps the source code smaller, and you are unlikely to go through the source code anyway.
  3. Save the Phaser code in the js directory. If you use another file name, make sure to update the src value of the first {{htmlelement("script")}}  element in the HTML accordingly.

Walking through what we have so far

At this point, we have a charset defined, {{htmlelement("title")}} , and some basic CSS in the header to reset the default margin and padding. We also have a {{htmlelement("script")}}  element to apply the Phaser source code to the page. The body contains a second {{htmlelement("script")}}  element, where we will write the JavaScript code to render the game and control it.

The {{htmlelement("canvas")}}  element is generated automatically by the framework. We are initializing it by creating a new Phaser.Game object and assigning it to the game variable. The parameters are:

Running the application

To run the app, you cannot directly open the index.html file, because later we will be loading external assets, which will be blocked by the browser’s same-origin policy.

To fix the problem, you need to run a local web server to serve the HTML files and the image files. As the official document of Phaser suggests, we have a lot of options to run a local web server. We also have our own tutorials for setting up a local server—use any option you prefer. For example, if you choose to use the Python HTTP server, then open a terminal, navigate to the directory where your index.html file is located, and run the following command:

python3 -m http.server

This will start a simple HTTP server on port 8000. Then, open your web browser and navigate to http://localhost:8000/index.html.

Compare your code

Here’s what you should have so far, running live. To view its source code, click the “Play” button.

<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.90.0/phaser.js"></script>
* {
  padding: 0;
  margin: 0;
}
class ExampleScene extends Phaser.Scene {
  preload() {}
  create() {}
  update() {}
}

const config = {
  type: Phaser.CANVAS,
  width: 480,
  height: 320,
  scene: ExampleScene,
};

const game = new Phaser.Game(config);

{{EmbedLiveSample("compare your code", "", 480, , , , , "allow-modals")}} 

Next steps

Now we’ve set up the basic HTML and learned a bit about Phaser initialization, let’s continue to the second lesson and learn about scaling.

{{PreviousNext("Games/Tutorials/2D_breakout_game_Phaser", "Games/Tutorials/2D_breakout_game_Phaser/Scaling")}} 

In this article

View on MDN