Making a Simple Game - Part 5: Menu and Game Over Screens
In Part 4 you built a score from a Screen and a Text Element. In this part you use the same pieces, plus an image, to make a title screen and a game over screen.
You need the project you made in Parts 1 to 4. To compare your work, open the finished project.
1. Build the Menu Screen
The menu is a title, a ring around the ball and a line telling the player what to do.
- Select UI, then + (Add Entity) > User Interface > 2D Screen. Name it
Menu Screen, and set Ref Resolution to720by1280and Scale Blend to1, as you did for the score. - Add four elements to Menu Screen from the User Interface menu, and set them up as in the table. Select Menu Screen again before you add each one, because a new element is added inside whatever is selected.
| Name | Type | Position | Settings |
|---|---|---|---|
Title Keepy | Text | 0, 455, 0 | Text KEEPY, Font Size and Line Height 150 |
Title Up | Text | 0, 317, 0 | Text UP, Font Size and Line Height 150, Color FFD13D |
Ring | Image | 0, 87, 0 | Texture ring.png from the UI folder, then Width and Height 264 |
Prompt | Text | 0, -147, 0 | Text TAP THE BALL, Font Size and Line Height 54 |
Give each of the three text elements the font, outline and shadow that you gave the score in Part 4. Set the ring's texture before its size, because the Editor resizes an Image Element to fit a new texture.
To save time, select the Score entity and duplicate it with Ctrl+D (Cmd+D on a Mac). The copy appears below the original, with the same name. Drag it onto Menu Screen, set its Preset to Center Anchor & Pivot, and it already has the right font, outline and shadow.
To open the UI folder without losing your selection, select it in the folder tree on the left of the Assets panel. Double-clicking a folder in the main part of the panel changes the selection, so the Inspector no longer shows the element you were working on.
Positions are measured from the middle of the screen, because these elements are anchored to its center. The ring sits at 87 because that is where the ball's starting place, 0.7 units above the camera's height, lands on a screen 1280 units tall.
2. Hide the Menu When Play Starts
Create a script called ui-menu.mjs:
import { Script } from 'playcanvas';
/**
* Hides the menu when the first round starts.
*/
export class UiMenu extends Script {
static scriptName = 'uiMenu';
initialize() {
this.app.once('game:start', this.onGameStart, this);
this.on('destroy', () => {
this.app.off('game:start', this.onGameStart, this);
});
}
onGameStart() {
this.entity.enabled = false;
}
}
Add a Script component to Menu Screen and add the uiMenu script. It has no attributes to parse.
The script uses this.app.once and not this.app.on, so its handler runs for the first game:start only. The menu is disabled along with everything under it, and it never needs to come back.
3. Build the Game Over Screen
This screen has to appear and disappear many times. Its script lives on the screen, which stays enabled, and switches a group of elements under it on and off.
- Add another 2D Screen to UI called
Game Over Screen, with the same Ref Resolution and Scale Blend. - With Game Over Screen selected, select + > User Interface > Group Element. Name the group
Paneland set its Preset to Stretch, so that it covers the whole screen.
Then add these four elements to Panel. The quickest way is to duplicate the ones on the menu and drag the copies onto Panel.
| Name | Type | Position | Settings |
|---|---|---|---|
Game | Text | 0, 388, 0 | Text GAME, Font Size and Line Height 92 |
Over | Text | 0, 300, 0 | Text OVER, Font Size and Line Height 92, Color FFD13D |
Ring | Image | 0, 87, 0 | As on the menu |
Prompt | Text | 0, -147, 0 | As on the menu |
The two words are smaller than the title because they have to fit between the score and the ring.
4. Show It Between Rounds
Create a script called ui-game-over.mjs:
import { Entity, Script } from 'playcanvas';
/**
* Shows the game over message between rounds.
*/
export class UiGameOver extends Script {
static scriptName = 'uiGameOver';
/**
* The entity that holds everything on the game over screen.
*
* @attribute
* @type {Entity}
*/
panel;
initialize() {
this.panel.enabled = false;
this.app.on('game:over', this.onGameOver, this);
this.app.on('game:start', this.onGameStart, this);
this.on('destroy', () => {
this.app.off('game:over', this.onGameOver, this);
this.app.off('game:start', this.onGameStart, this);
});
}
onGameOver() {
this.panel.enabled = true;
}
onGameStart() {
this.panel.enabled = false;
}
}
- Add a Script component to Game Over Screen, add the
uiGameOverscript and select its Parse button. - Drag the Panel entity onto the script's
panelslot.
The panel is hidden when the game loads, shown on game:over and hidden again on the next game:start.
Check Your Progress
Launch the game. It opens on the title:

Kick the ball and the title gives way to the score. Lose the ball and the game over screen appears, with the ball back in its ring, ready for another round:

The game is complete, but it is silent. Part 6: Sound, Particles and Publishing adds the finishing touches and puts it online.