Build a HTML5 game like Wordle using Phaser and TypeScript – Step 3: design the game field

Read all posts about "" game

Here we go with the 3rd step of Wordle tutorial series.

In first step I showed you how to handle keyboard input and result management.

In second step we saw how to add a virtual keyboard.

Now it’s time to design the game field and add some improvements to the virtual keyboard itself, this way:

Use the keyboard to write the word. You can play with your physical keyboard, using Backspace to delete the last letter, Enter to submit the word or Space to restart the game with a new randomly picked word.

You can see the word to guess in the console. If you are reading this post with a mobile device, you can play directly with the prototype at this link.

Let’s have a look at the assets used in the prototype:

bigfont.fnt and bigfont.png: respectively the XML information and the image of the big font.

bigkey.png: a sprite sheet containing Enter and Delete keys.

box.png: sprite sheet the box where to write the each letter of the word.

font.fnt and font.png: respectively the XML information and the image of the small font.

key.png: sprite sheeet of the virtual keyboard key.

words.json: the list of allowed words.

And this is the completely commented source code of the prototype, split in one html file, one css file and nine TypeScript files:

index.html

The web page which hosts the game, to be run inside thegame element.

<!DOCTYPE html>
<html>
	<head>
        <link rel = 'stylesheet' href = 'style.css'>
        <script src = "main.js"></script>
    </head>
	<body>
        <div id = "thegame"></div>
	</body>
</html>

style.css

The style sheet of the web page

* {
    padding : 0;
    margin : 0;
}

body{
    background : #ffffff;
}

canvas {
    touch-action : none;
    -ms-touch-action : none;
}

gameOptions.ts

Configurable game options

// CONFIGURABLE GAME OPTIONS
 
export const GameOptions = {

    // number of rows, that is the amount of tries to guess the word
    rows : 6,

    // vertical position of the first row, starting from the top of the screen
    firstRowY : 150
}

main.ts

This is where the game is created, with all Phaser related options.

// MAIN GAME FILE

// modules to import
import Phaser from 'phaser';
import { PreloadAssets } from './preloadAssets';
import { PlayGame } from './playGame';

// object to initialize the Scale Manager
const scaleObject : Phaser.Types.Core.ScaleConfig = {
    mode : Phaser.Scale.FIT,
    autoCenter : Phaser.Scale.CENTER_BOTH,
    parent : 'thegame',
    width : 700,
    height : 1244
}

// game configuration object
const configObject : Phaser.Types.Core.GameConfig = {
    type : Phaser.AUTO,
    backgroundColor : 0xffffff,
    scale : scaleObject,
    scene : [PreloadAssets, PlayGame]
}

// the game itself
new Phaser.Game(configObject);

preloadAssets.ts

Here we preload all assets to be used in the game, such as the JSON object with all words, the images and the bitmap fonts.

// CLASS TO PRELOAD ASSETS

// this class extends Scene class
export class PreloadAssets extends Phaser.Scene {

    // constructor    
    constructor() {
        super({
            key : 'PreloadAssets'
        });
    }

    // method to be execute during class preloading
    preload(): void {

        // this is how we preload a JSON object
        this.load.json('words', 'assets/words.json');

        // preload images
        this.load.spritesheet('key', 'assets/key.png', {
            frameWidth: 70,
            frameHeight: 90
        });
        this.load.spritesheet('bigkey', 'assets/bigkey.png', {
            frameWidth: 105,
            frameHeight: 90
        });
        this.load.spritesheet('box', 'assets/box.png', {
            frameWidth: 100,
            frameHeight: 100
        });
        // this is how we preload a bitmap font
        this.load.bitmapFont('font', 'assets/font.png', 'assets/font.fnt');
        this.load.bitmapFont('bigfont', 'assets/bigfont.png', 'assets/bigfont.fnt');
	}

    // method to be called once the instance has been created
	create(): void {

        // call PlayGame class
        this.scene.start('PlayGame');
	}
}

playGame.ts

Main game file, where we handle input and result management.

// THE GAME ITSELF

import KeyboardKey from './keyboardKey';
import { GameOptions } from './gameOptions';
import { GameGrid } from './gameGrid';

// possible word states:
// perfect, when the letter is in the right position
// correct, when the letter exists but it's not in the right position
// wrong, when the letter does not exist
enum letterState {
    WRONG = 1,
    CORRECT,
    PERFECT  
}

// keyboard layout, as a string array, each item is a row of keys
// > represents Enter
// < represents Backspace
const keyboardLayout : string[] = ['QWERTYUIOP','ASDFGHJKL','>ZXCVBNM<'];

// this class extends Scene class
export class PlayGame extends Phaser.Scene {

    // array with all possible words
    words : string[];

    // string where to store the current word
    currentWord : string;

    // string where to store the word to guess
    wordToGuess : string;

    // variable where to store game width
    gameWidth : number;

    // main game grid
    gameGrid : GameGrid;

    // virtual keyboard, as an array of KeyboardKey instances
    virtualKeyboard : KeyboardKey[][];
   
    // constructor
    constructor() {
        super({
            key: 'PlayGame'
        });
    }

    // method to be executed when the scene has been created
    create() : void {        

        // set gameWidth to actual game width
        this.gameWidth = this.game.config.width as number;

        // store JSON loaded words into words array
        this.words = this.cache.json.get('words');

        // at the beginning, current word is empty
        this.currentWord = '';

        // pick a random word to guess
        this.wordToGuess = this.words[Phaser.Math.Between(0, this.words.length - 1)].toUpperCase();

        // let's display somewhere the word to guess
        console.log(this.wordToGuess);

        // initialize virtual keyboard
        this.virtualKeyboard = [];

        // loop through keyboardLayout array
        keyboardLayout.forEach((row : string, index : number) => {

            // initialize virtual keyboard row
            this.virtualKeyboard[index] = [];
            
            // determine position of key sprites
            // some values are still hardcoded, and need to be optimized
            let rowWidth : number = 70 * row.length;
            let firstKeyPosition : number = (this.game.config.width as number - rowWidth) / 2;

            // loop through string
            for (let i : number = 0; i < row.length; i ++) {

                // get the i-th character
                let letter : string = row.charAt(i);

                // add the keyboard key
                this.virtualKeyboard[index][i] = new KeyboardKey(this, firstKeyPosition + i * 70 - (letter == '>' ? 35 : 0), 900 + index * 90, row.charAt(i));
            }
        });

        // add the game grid
        this.gameGrid = new GameGrid(this, GameOptions.rows, (this.gameWidth - 540) / 2, GameOptions.firstRowY);

        // waiting for keyboard input
        this.input.keyboard.on('keydown', this.onKeyDown, this);
    }
    
    // method to process a key pressed
    onKeyDown(e : KeyboardEvent) : void {

        // store key pressed in key variable
        var key : string = e.key;
        
        // if the key is space, restart the game
        if (key == ' ') {
            this.scene.start('PlayGame');
            return;
        }

        // backspace
        if (key == 'Backspace') {
            this.updateWord('<');
            return;
        }

        // regular expression saying "I want one letter"
        const regex = /^[a-zA-Z]{1}$/;

        // letter a-z or A-Z
        if (regex.test(key)) {
            this.updateWord(key);
            return;
        }

        // enter
        if (key == 'Enter') {
            this.updateWord('>');
        }
    }

    //method to be called each time we need to update a word
    updateWord(s : string) : void {
        switch(s) {

            // backsace
            case '<' :

                // if the word has at least one character, remove the last character
                if (this.currentWord.length > 0) {

                    // remove last current word character
                    this.currentWord = this.currentWord.slice(0, -1);

                    // call gameGrid's removeLetter method
                    this.gameGrid.removeLetter();
                }
                break;

            // enter
            case '>' :
                
                // if the word is 5 characters long, proceed to verify the result
                if (this.currentWord.length == 5) {

                    // if the word is a valid word, proceed to verify the result
                    if (this.words.includes(this.currentWord.toLowerCase())) {

                        // at the beginning we se the result as a series of wrong characters
                        let result : number[] = Array(5).fill(letterState.WRONG);

                        // creation of a temp string with the word to guess
                        let tempWord : string = this.wordToGuess;

                        // loop through all word characters
                        for (let i : number = 0; i < 5; i ++) {

                            // do i-th char of the current word and i-th car of the word to guess match?
                            if (this.currentWord.charAt(i) == tempWord.charAt(i)) {

                                // this is a PERFECT result
                                result[i] = letterState.PERFECT;

                                // remove the i-th character from temp word
                                tempWord = this.removeChar(tempWord, i);
                            }

                            // i-th char of the current word and i-th car of the word to guess do not match
                            else {

                                // loop through all character of the word to guess
                                for (let j : number = 0; j < 5; j ++) {

                                    // do i-th char of the current word and j-th car of the word to guess match,
                                    // and don't j-th char of the current word and j-th car of the word to guess match?
                                    if (this.currentWord.charAt(i) == this.wordToGuess.charAt(j) &amp;&amp; this.currentWord.charAt(j) != this.wordToGuess.charAt(j)) {
                                        
                                        // this is a correct result
                                        result[i] = letterState.CORRECT;

                                        // remove the i-th character from temp word
                                        tempWord = this.removeChar(tempWord, j);
                                        break;    
                                    }
                                }   
                            }    
                        }

                        // loop through all result items and compose result string accordingly
                        result.forEach((element : number, index : number) => {
                            
                            // get letter position in our virtual keyboard
                            let position : Phaser.Math.Vector2 = this.getLetterPosition(this.currentWord.charAt(index));

                            // if the key of the virtual keyboard has not already been painted, then paint it.
                            if (parseInt(this.virtualKeyboard[position.x][position.y].frame.name) < element) {
                                this.virtualKeyboard[position.x][position.y].setFrame(element);
                            }
                        });

                        // reset current word
                        this.currentWord = '';
                      
                        // call gameGrid's showResult method
                        this.gameGrid.showResult(result);
                    }
                }
                break;
            
            // a-z or A-Z
            default :

                // if the word is less than 5 characters long, remove last character
                if (this.currentWord.length < 5) {

                    // add the letter
                    this.gameGrid.addLetter(s);

                    // update current word
                    this.currentWord += s.toUpperCase();
                   
                }                
        }
    }

    // method to get the position the virtual keyboard key, given a letter
    getLetterPosition(letter : string) : Phaser.Math.Vector2 {
        
        // set row to zero
        let row : number = 0;

        // set column to zero
        let column : number = 0;

        // loop though all keyboardLayout array
        keyboardLayout.forEach((currentRow : string, index : number) => {
            
            // does current row include the letter?
            if (currentRow.includes(letter)) {

                // set row to index
                row = index; 

                // set column to letter position inside current row
                column = currentRow.indexOf(letter);   
            }
        })     

        // return the coordinate as a 2D vector
        return new Phaser.Math.Vector2(row, column);
    }

    // simple method to change the index-th character of a string with '_'
    // just to have an unmatchable character
    removeChar(initialString : string, index : number) : string {
        return initialString.substring(0, index) + '_' + initialString.substring(index + 1);
    }
}

keyboardKey.ts

Custom class for the virtual keyboard key.

// KEYBOARD KEY CLASS

import { PlayGame } from "./playGame";
import KeyboardLetter from "./keyboardLetter";

// this class extends Sprite class
export default class KeyboardKey extends Phaser.GameObjects.Sprite {

    // letter bound to the key
    boundLetter : string;

    // parent scene
    parentScene : PlayGame;

    constructor(scene : PlayGame, x : number, y : number, letter : string) {

        // different image key according if it's a letter character or '<' or '>'
        super(scene, x, y, '<>'.includes(letter) ? 'bigkey' : 'key');

        // assign parent scene
        this.parentScene = scene;

        // assign bound letter
        this.boundLetter = letter;

        // set sprite registration point to top, left
        this.setOrigin(0);

        // add the sprite to the scene
        scene.add.existing(this); 
        
        // set the sprite interactive
        this.setInteractive();

        // listener for pointer down on the sprite, to call handlePointer callback
        this.on('pointerdown', this.handlePointer);

        // add a keyboard letter accoring to 'letter value
        switch(letter) {

            // backspace
            case '<' :
                this.setFrame(0);
                break;
            
            // enter
            case '>' :
                this.setFrame(1);
                break;

            // normal key
            default :
                new KeyboardLetter(scene, x + 10, y + 10, letter, 36);
        }
    }

    // method to be called when the user clicks or taps the letter
    handlePointer() : void {

        // call 'updateWord' method on parent scene
        this.parentScene.updateWord(this.boundLetter);
    }
}

keyboardLetter.ts

Custom class for the letter to be printed on the virtual keyboard key.

// KEYBOARD LETTER

import { PlayGame } from "./playGame";

// this class extends BitmapText class
export default class KeyboardLetter extends Phaser.GameObjects.BitmapText {
    constructor(scene : PlayGame, x : number, y : number, text : string, size : number) {
        super(scene, x, y, 'font', text, size);
        
        // add the keyboard letter to the scene
        scene.add.existing(this);
    }
}

gameGrid.ts

This class manages the grid where players write and try to guess the word.

// GAME GRID

import BigLetterBox from './bigLetterBox';

export class GameGrid {

    // current row where to write the letter
    currentRow : number;

    // current column where to write the letter
    currentColumn : number;

    // array to store all letter boxes
    letterBox : BigLetterBox[][];

    constructor(scene : Phaser.Scene, rows : number, firstRowX : number, firstRowY : number) {
        
        // set current row to zero
        this.currentRow = 0;

        // set current column to zero
        this.currentColumn = 0;

        // initialize letterBox array
        this.letterBox = [];

        // loop from 0 to 4
        for (let i: number = 0; i < 5; i ++) {
            
            // initialize letterBox[i] array
            this.letterBox[i] = []; 

            // loop through all rows
            for (let j : number = 0; j < rows; j ++) {

                // assign to letterBox[i][j] a new BigLetterBox instance
                this.letterBox[i][j] = new BigLetterBox(scene, firstRowX + i * 110, firstRowY + j * 110);
            }  
        }  
    }

    // method to add a latter
    addLetter(letter : string) : void {

        // set the letter at current row and column
        this.letterBox[this.currentColumn][this.currentRow].setLetter(letter);

        // increase current column
        this.currentColumn ++;
    }

    // method to remove a letter
    removeLetter() : void {

        // decrease current column
        this.currentColumn --;

        // unset the letter ant current row and column
        this.letterBox[this.currentColumn][this.currentRow].setLetter('');    
    }

    // show guess result
    showResult(result : number[]) : void {

        // loop through all result items
        result.forEach((element : number, index : number) => {

            // set letterBox frame according to element value
            this.letterBox[index][this.currentRow].setFrame(element);

            // paint the letter white
            this.letterBox[index][this.currentRow].letterToShow.setTint(0xffffff);
        });

        // increase current row
        this.currentRow ++;

        // set current column to zero
        this.currentColumn = 0;
    }
}

bigLetterBox.ts

Class to mange the big letter box inside game grid

// BIG LETTER BOX

import BigLetter from "./bigLetter";

// this class extends Sprite class
export default class BigLetterBox extends Phaser.GameObjects.Sprite {

    // letter to show in the big letter box
    letterToShow : BigLetter;

    constructor(scene : Phaser.Scene, x : number, y : number) {
        super(scene, x, y, 'box');

        // set registration point to top left corner
        this.setOrigin(0);

        // add the letter box to the scene
        scene.add.existing(this);

        // calculate letter box bounds
        let bounds : Phaser.Geom.Rectangle = this.getBounds();

        // assign letterToShow an instance of BigLetter
        this.letterToShow = new BigLetter(scene, bounds.centerX, bounds.centerY + 8);
        
    }

    // method to set the letter
    setLetter(letter : string) : void {

        // tint letterToShow black
        this.letterToShow.setTint(0);

        // set letterToShow text according to "letter" argument
        this.letterToShow.setText(letter.toUpperCase());
    }
}

bigLetter.ts

Custom class to manage the big letter which appears in the big letter box

// BIG LETTER

// this class extends BitmapText class
export default class BigLetter extends Phaser.GameObjects.BitmapText {
    
    constructor(scene : Phaser.Scene, x : number, y : number) {    
        super(scene, x, y, 'bigfont', '');

        // set registration point to center
        this.setOrigin(0.5);

        // add the letter to the scene
        scene.add.existing(this);
    }
}

And the prototype is almost complete I only need to add an options menu and a couple of game modes. Download the source code of the entire project.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers