Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 36 ยท Projects and Practice

Project: A Memory Match Game

Build a memory card game in Phaser with a size menu, grid layout, flip animations, input locking, a timer, a star rating and best scores.

Not every game needs physics. A memory match game (flip two cards, keep them if they match) is a perfect project for the other half of Phaser: input, tweens, timers and state. It teaches you how to lay out a grid from the screen size, animate a card flip, block input while an animation runs, and keep the game rules in a few clear variables. It also feels great on a phone, because it needs only taps.

You will build a game with a size menu (easy, medium, hard), a move counter and a timer, a star rating at the end and a best score for each size.

The rules as a state machine

The whole game logic comes down to a handful of states. Writing them down first prevents the usual tangle of flags:

stateDiagram-v2
  [*] --> Idle
  Idle --> OneUp: tap a face-down card
  OneUp --> Checking: tap a second card
  Checking --> Idle: match (cards stay up)
  Checking --> Idle: no match (flip both back after a pause)
  Idle --> Won: no pairs left
  Won --> [*]

While in the Checking state, taps are ignored. That is the "lock".

In code the states are not an enum but three variables: first (the first flipped card or null), locked (true while two cards are being compared and animated) and pairsLeft.

Layout from the screen size

The number of cards changes with the difficulty, so the card size must be computed. Given a grid of cols by rows, the available width and height and a gap, the card size is the smaller of what fits horizontally and vertically:

JavaScript
const size = Math.min(
  (availW - (cols - 1) * gap) / cols,
  (availH - (rows - 1) * gap) / rows
);

Then the position of the card in column c and row r is left + c * (size + gap) + size / 2, where left centers the grid. This pattern (compute from the screen, never hard-code) is also what makes the game ready for responsive scaling.

The flip animation

A card flip looks like a rotation around the vertical axis, but you can fake it in 2D by shrinking the width to zero, swapping the picture, and growing it back. A card is an ordinary image with two textures: the back and a face. Two chained tweens make the flip:

JavaScript
flip(card, showFace, done) {
  this.tweens.add({
    targets: card, scaleX: 0, duration: 120,
    onComplete: () => {
      card.setTexture(showFace ? 'face' + card.pairId : 'back');
      this.tweens.add({ targets: card, scaleX: card.baseScale, duration: 120, onComplete: done });
    }
  });
}

Note that baseScale is stored on each card, because we scale a 96-pixel texture down to the computed size, so "normal" is not 1.

Shuffling

Create an array with each pair id twice ([0, 0, 1, 1, 2, 2, ...]) and shuffle it. Use Phaser.Math.RND.shuffle(array), which uses Phaser's seedable random generator (a fixed seed gives the same layout every time, handy for testing and for "daily challenge" modes). The game logs the layout to the console so that you can see how debug output helps while building.

The complete game

Choose a size, then tap cards. Click the cards, not the keyboard: the whole game uses pointer input, so it also works by touch.

Memory Match: the complete game

How it works

  • Layout is computed once in create(). If you add a fourth size, add one line to SIZES.
  • The lock (this.locked) is what prevents a third card from being flipped while two are being compared. Without it, a fast player produces three face-up cards and breaks the state. Every code path that starts a comparison must release the lock again, including the "no match" path inside the delayed callback.
  • Face textures are drawn once in the menu scene and stay in the global texture cache, so the play scene finds them. In a real game you would load a spritesheet in a preloader.
  • Restart with data: scene.start('Play', { size }) passes the difficulty, and init(data) stores it. scene.restart() reuses the same data.
  • Stars and best score: the rating compares the number of moves with the number of pairs (a perfect game needs exactly one move per pair). Best scores live in the registry, so the menu shows them next time; to keep them after a reload, use the save manager from Events, Registry and Saving.

Ideas to extend it

  1. Sound effects for flip, match and win, and music on the menu.
  2. Themes: use real picture sets (animals, flags) and let the player choose.
  3. Two-player hot-seat mode: track a current player, and keep the turn after a match.
  4. A countdown mode where the timer decides the winner.
  5. Peek power-up: reveal all cards for one second, at a cost of 10 seconds.
  6. Accessibility: add symbols on the card backs for color-blind players, and keyboard control with arrow keys and Enter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a locked flag needed while two cards are being compared?

    Between flipping the second card and resolving the pair, taps must be ignored. The lock is released only when the pair is resolved.
  2. 2. How is a card flip animated in this game?

    Shrinking scaleX to 0, swapping the texture and growing it back looks like a flip with two small tweens.
  3. 3. How is the card size computed for different grid shapes?

    Taking the minimum guarantees that the grid fits in both directions at the largest possible card size.
  4. 4. Why use Phaser.Math.RND.shuffle instead of a hand-written shuffle with Math.random?

    A seedable generator makes layouts reproducible, which helps with tests, debugging and daily challenges.

Summary

  • A card game is a small state machine: no card up, one up, comparing (locked), won.
  • Compute the grid and card size from the available space so every difficulty fits.
  • A flip is two chained tweens on scaleX with a texture swap between them.
  • Lock input during comparisons, and release the lock on every path.
  • The registry stores best scores per difficulty; save them for persistence.

Next: Phaser Practice Challenges