CxD Archive
Video Game Project
Video Game Project
  • Project Introduction
  • Project Challenge
  • Project Outline
    • 1-1 Determine Gamer Motivation Profile
    • 1-2 Analyze External Motivations in Games
    • 1-3 Analyze Internal Motivations in Games
    • 1-4 Analyze Game Design Elements
    • 1-5 Phaser Practice 1: Matching Game
      • P1: Steps 1-5
      • P1: Steps 6-10
    • 1-6 Phaser Practice 2: Top-Down Game
      • P2: Steps 1-5
      • P2: Steps 6-10
      • P2: Steps 11-15
    • 1-7 Phaser Practice 3: Side-Scrolling Game
      • P3: Steps 1-5
      • P3: Steps 6-10
      • P3: Steps 11-15
    • 2-1 Form Project Teams
    • 2-2 Create Persona for Target Players
    • 2-3 Generate Game Ideas
    • 2-4 Refine Ideas to Create Game Treatments
    • 2-5 Evaluate Game Treatments
    • 3-1 Draft Game Design Document
    • 3-2 Create Paper Prototype of Game
    • 3-3 Playtest Paper Prototype
    • 3-4 Present Game Proposal
    • 4-1 Create Development Plan
    • 4-2 Code Game in Iterative Stages
    • 4-3 Create Art and Sound for Game
    • 4-4 Create Marketing Website
    • 5-1 Evaluate Game With Playtesters
    • 5-2 Evaluate Marketing Website
    • 5-3 Analyze Evaluation Data to Improve Solution
    • 6-1 Create Project Poster
    • 6-2 Present Project to Public
    • 6-3 Write Personal Reflection
  • Project References
    • Phaser Introduction
    • Phaser Game Template
    • Visual Assets
    • Audio Assets
    • Phaser Coding
      • Game Display
      • Game World
      • Game Camera
      • Text
      • Images
      • Sprites, Animations, and Health
      • Group of Sprites
      • Tilesprite Scrolling
      • Audio
      • Input
      • Physics and Collisions
      • Weapon
      • Particles
      • Tweens
      • Timers
      • Random Numbers
      • Enemy Behavior
      • Misc Game Features
  • Notes for Teachers
Powered by GitBook
On this page
  • Phaser coding references in this section:
  • Phaser API Documentation:
  • Set Size of Game Display
  • Change Background Color of Game Display
  • Change Background to Random Color
Export as PDF
  1. Project References
  2. Phaser Coding

Game Display

PreviousPhaser CodingNextGame World

Last updated 6 years ago

The game display is the HTML <canvas> element that Phaser adds to the webpage to display your game. Phaser also refers to this as the Stage.

The game display shows the player's view into your . The game world can be the same size as the game display (e.g., a single-screen game like Space Invaders) — or the game world can extend beyond the game display (e.g., a scrolling game like Crossy Road). For extended game worlds, the position of the determines what is shown in the game display.

Phaser coding references in this section:

  • Set Size of Game Display

  • Change Background Color of Game Display

  • Change Background to Random Color

Phaser API Documentation:

Set Size of Game Display

You set the width and height of your game display when you first create your Phaser.Game object, which is typically done at the beginning of your JavaScript game code — before the preload() function.

// create Phaser.Game object named "game"
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'my-game',
    { preload: preload, create: create, update: update });

In the code above, the game display will be 800 pixels in width by 600 pixels in height. You can change these numbers for your game. However, you'll want the game display to fit within the available width and height of the player's browser window. For beginning game developers, just choose a width and height that fits your own screen.

REMINDER: Remember that 'my-game' represents the id name of your game container <div> in your HTML. If your CSS sets a width or height for #my-game, then be sure to update those values to match the size of the game display in your JavaScript.

Change Background Color of Game Display

By default, Phaser uses black as the background color for your game display. However, you can change this to any color using a CSS hex string.

game.stage.backgroundColor = '#ffffff';

In most cases, you would add this command in your create() function (probably towards the beginning — just to make it easy to find it). For example, if you only need to change the background color once, then do this in the create() function.

However, you can also use this command in your update() function or in a custom function. For example, you might do this if you wanted to change the background color based on certain conditions or events in the gameplay.

Change Background to Random Color

You can also let Phaser choose a random color for the game display background:

game.stage.backgroundColor = Phaser.Color.getRandomColor();

While we won't cover details here, Phaser has a that can be used to scale your game display to fit the size and orientation (landscape vs. portrait) of the player's device, so your game is playable on a range of device sizes. This is a feature for advanced game developers.

In the code above, '#ffffff' represents white — replace this with your own .

game world
game camera
Properties and Methods for Game
Properties and Methods for Stage
Scale Manager
CSS hex string