Phaser CE Game Template
Phaser games are JavaScript-powered web apps that run in a browser.
Your Phaser game will consist of an HTML file, a CSS file, and a JS file. The JavaScript file will contain your game code. You'll also have a folder containing the asset files (images, animated sprites, sound effects, etc.) for your game.
Your game will also need to load the Phaser CE game framework, which is a JavaScript file named phaser.min.js
that defines classes for JS objects you'll use in your game code. You can either load this Phaser CE file from a local copy (placed in the same location as your HTML, CSS, and JS files) or by linking to a URL from a CDN (Content Delivery Network).
Create New Files for Game
Using a code editor (such as repl.it), create a new project (or folder) for your game containing three new files:
HTML file named
index.html
CSS file named
style.css
JS file named
script.js
Depending on the code editor you're using, these new files might be completely blank — or they might have starter code that was automatically inserted by the code editor. Either way is fine. You'll add new code to each of these files.
JS FILENAME: This code guidebook will refer to script.js
as the name of the JS file containing your Phaser game code. However, you can choose a different name for this file (such as code.js
or game.js
). You'll just need to be sure the <script>
tag in your HTML lists the correct filename for your JS game code.
Create New Folder(s) for Assets
In your code editor, create a new folder named assets
in the same location as your HTML, CSS, and JS files. You'll use this folder to contain asset files used in your game, such as images, animated sprites, sound effects, etc.
If your game will have lots of assets, you may want to create subfolders inside the assets
folder to contain specific types of assets (to keep your files more organized). For example, you could create a subfolder called images
and another subfolder called sounds
.
Later, you'll add (or upload) your asset files into the assets
folder (or into one of its subfolders such as images
or sounds
).
HTML Template
Copy this HTML, and paste it into your index.html
file (replacing any other starter HTML that might already be present):
This is the basic HTML required for a Phaser CE game. All of your games will start with this HTML (though you can also insert additional HTML to customize the game's webpage).
Here is an explanation of the key things this HTML does:
STYLESHEET: The
<link>
tag on line 7 loads your CSS stylesheet file namedstyle.css
, which can be used to style the webpage your game is embedded within.GAME CANVAS CONTAINER: The
<div>
element on line 10 will contain your game canvas, which will literally be a<canvas>
element that will be inserted by your Phaser game code. This<div>
element has been given an id name ofmy-game
, so your Phaser game code can identify where to insert the canvas for your game.PHASER CE FRAMEWORK: The
<script>
tag on line 11 loads the Phaser CE game framework JS file namedphaser.min.js
. In this case, the file is being loaded remotely from a CDN (Content Delivery Network) using a URL. However, if necessary, this<script>
tag can be modified to load a local copy of the Phaser CE JS file.YOUR GAME CODE: The
<script>
tag on line 12 loads your JS file namedscript.js
, which will contain your Phaser game code. You can use a different name for this file (such as:code.js
orgame.js
) as long as the<script>
tag matches the filename of your game code.
IMPORTANT: Be sure the <script>
tag that loads the Phaser CE game framework (phaser.min.js
) is listed before the <script>
tag that loads your game code (script.js
). If their order is reversed, your game won't work.
CSS Template
A Phaser CE game does not require any special CSS in order for the game to work.
However, you'll add some optional CSS to style the <div>
element that will contain your game canvas. Remember that this <div>
element was given an id name of my-game
which can be used to select this element with your CSS.
Copy this CSS, and paste it into your style.css
file (replacing any other starter CSS that might already be present):
Here is an explanation of what this CSS does:
Lines 3 and 4 set the width and height (in pixels) for the
<div>
element that will contain the game canvas. Later in your JS game code, you'll set your game canvas to these same exact dimensions. You can change these dimensions as long as the width and height listed in your CSS and your JS game code match each other.Line 5 sets the margins around the
<div>
element. The top and bottom margins have been set to zero. The left and right margins have been set toauto
, which will center the game canvas horizontally on the webpage.Line 6 displays a border around the
<div>
element.Lines 11 and 12 set the
<body>
element to use a sans-serif font for all text, which will be center-aligned on the webpage.
Again, all of this CSS is optional. If you wanted, you could modify this CSS — or add other CSS to style additional properties for the elements on the webpage.
JS Template
Copy this JS, and paste it into your script.js
file (replacing any other starter JS that might already be present):
This is the core JS required in all Phaser CE games. Your game code will always start with this JS (though you will need to add more code to actually complete your game).
There are comments embedded in the JS to help explain the code. Here's a more detailed explanation of what this JS does:
PHASER GAME OBJECT: Line 2 creates a
Phaser.Game
object (using a class defined in thephaser.min.js
file) and assigns it to a global variable namedgame
. This also sets the width and height (in pixels) for the game canvas. In this case, the game will be 800 pixels wide and 600 pixels tall. This also specifies the id name of the<div>
container where the game canvas will be inserted. In this case, the id name used in your HTML ismy-game
. (If you were to change the width or height of your game canvas, just be sure to also adjust the size ofmy-game
in your CSS, so they match.)GLOBAL VARIABLES: Line 5 is an empty placeholder where you will eventually add code to declare other global variables needed for your game. For example, you might need to declare a global variable to store the player's score. You'll also need to declare global variables for images, sprites, sound effects, player input controls, etc.
PRELOAD FUNCTION: Line 8 defines a function named
preload()
that will run one time when the webpage first loads (or is refreshed). At the moment, there are no code statements inside the curly braces of thepreload()
function. However, you will eventually add Phaser code to load your game assets into memory (such as: images, sprites, sounds, etc.).CREATE FUNCTION: Line 13 defines a function named
create()
that will run one time after thepreload()
function has finished. At the moment, there are no code statements inside the curly braces of thecreate()
function. However, you will eventually add Phaser code to create your game world by adding images, sprites, sound effects, input controls, etc.UPDATE FUNCTION: Line 18 defines a function named
update()
that will run repeatedly in a loop after thecreate()
function has finished running. At the moment, there are no code statements inside the curly braces of theupdate()
function. However, you will eventually add Phaser code to update your game by checking for player input, checking for collisions between sprites, etc.CUSTOM FUNCTIONS: Line 23 is an empty placeholder where you will eventually add code to define your own custom functions needed for your game. For example, you might need to define a custom function to perform certain actions when the player's sprite collides with an enemy sprite. These custom functions will only run if and when they're specifically called within other functions (such as the
update()
function, etc.).
You're now ready to start adding additional code into your JS file to complete your game. Most of this additional code will be Phaser CE code.
Last updated