Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 36 ยท Projects and Practice

Project: A Platformer Game

Build a complete Phaser platformer: a tile level from text, good jump feel, coins, patrolling enemies, spikes, lives, a goal flag and a following camera.

It is time to put the earlier lessons together. In this project you build a complete little platformer called Coin Run: run and jump through a level made of tiles, collect coins, stomp on slimes, avoid spikes and gaps, and reach the flag. It uses tilemaps and tile collision, Arcade Physics, a following camera, groups, overlaps, events and a HUD.

The project is split into small steps so that you can understand each part before the full program. All art is drawn in code with Graphics, so the whole game runs in one page with no files. When you build your own version, replace those shapes with real sprites and an atlas.

The plan

flowchart TB
  L["1. Level as text rows"] --> T["2. Tilemap + collision"]
  T --> P["3. Player controls<br/>(good jump feel)"]
  P --> C["4. Coins and spikes<br/>(overlaps, tile callbacks)"]
  C --> E["5. Enemies<br/>(patrol, stomp, hurt)"]
  E --> G["6. Goal, lives, camera, HUD"]

Each step adds one system and can be tested alone.

Step 1: describe the level as text

A level written as rows of characters is easy to read, edit and even generate. Each character stands for one tile or object:

Character Meaning
# Solid ground tile
= Floating brick platform tile
^ Spikes (hurt the player)
o Coin
E Enemy (slime) spawn
P Player start
F Goal flag
. Empty

The code reads each row and decides what to create. Tile characters become numbers in a data array for the tilemap, and the other characters become objects placed on top of an empty tile.

Step 2: the feel of the jump

A platformer lives or dies by how the jump feels. Three small tricks make a big difference, and they are standard in nearly every good platformer:

  • Coyote time: the player may still jump for a short moment (about 100 ms) after walking off a ledge. It makes the game forgiving.
  • Jump buffering: if the player presses jump slightly before landing, remember it for about 100 ms and jump the moment they land.
  • Variable jump height: if the player releases the jump key while rising, cut the upward speed, so a short tap gives a small hop and holding gives a full jump.

Try them in isolation first. Run the example, press the arrow keys and Space, and watch the numbers in the corner. Walk off the right edge of the platform and press Space late: coyote time still allows the jump.

Step: jump feel lab

The three values are easy to tune: 100 ms for coyote time and buffer are a good start, and a jump speed of -430 with gravity 1100 gives a jump height of about 84 pixels (speed squared divided by twice the gravity). Change them and see how the game feels; "game feel" is mostly adjusting numbers.

Step 3: tiles and collisions

The level text becomes a tilemap exactly as in Tilemap Basics. Ground and bricks are solid (setCollision([0, 1])), spikes use a tile index callback (see Tile Collision and Interactions), and the camera is bound to the map.

Step 4: coins, enemies and the rules

  • Coins are stars in a static group. An overlap with the player destroys the coin and raises the score.
  • Spikes are tile index 2, with an overlap callback that hurts the player.
  • Slimes are a group of Arcade sprites that walk back and forth. They turn at walls and at ledges (by checking whether there is a tile under the next step).
  • Stomping: when the player touches a slime while moving down and above it, the slime dies and the player bounces. Otherwise the player is hurt, bounces back and is invulnerable for a short time.
  • Hurt rules are in one function, hurt(), that every danger calls.
  • Win and lose: the flag ends the level; falling below the map or losing all lives ends the game. Press R or click to restart.

The complete game

Read the code in the order of the steps above. Use the arrow keys (or A and D) to move and Space, Up or W to jump. Click the canvas first so that it gets keyboard focus.

Coin Run: the complete platformer

How the pieces fit

  • One pass builds everything. The loop over the rows both fills the tile array and creates coins, slimes, the start point and the flag. Because each LEVEL row is just a string, a new level is nothing more than new text.
  • The tile callback (setTileIndexCallback(2, ...)) works together with physics.add.overlap(player, layer), as explained in the tile collision lesson. Spike tiles are not in the collision list, so the player can walk into them and be hurt.
  • hurt() is the single place for damage. Spikes, slimes and falling all call it, so invulnerability and knockback behave the same everywhere. That is a design habit worth copying.
  • Stomping compares the player's bottom with the slime's top, and requires downward motion. Without the speed check, a player jumping up through a slime could kill it.
  • Slime AI looks one step ahead for ground (getTileAtWorldXY) and turns around at ledges. It is a tiny piece of code that makes the enemies feel purposeful.

Ideas to extend it

  1. Real art: replace the generated textures with a tileset and a character spritesheet, and add walk, jump and idle animations.
  2. Sound: a coin ping, a jump whoosh and a hurt sound (see Audio Basics).
  3. Several levels: store levels as text files or Tiled maps and pass a level number through scene.restart({ level }).
  4. A HUD scene and a pause menu using HUDs, Menus and Pause Screens.
  5. Moving platforms, a double jump, a dash, or enemies that shoot.
  6. Saving the best score and unlocked levels with the save manager from Events, Registry and Saving.
  7. Touch controls and FIT scaling for phones (Scale Manager and Mobile).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does coyote time allow?

    Coyote time keeps the 'can jump' window open for about 100 ms after leaving the ground, which feels fair to the player.
  2. 2. How does the variable jump height work in this project?

    Releasing early limits the upward speed, so a tap gives a short hop and holding gives a full jump.
  3. 3. Why does touching a slime check that the player is moving downward before counting a stomp?

    A stomp needs a falling player above the enemy. Any other contact is a hit.
  4. 4. Why is damage handled in one hurt() function?

    A single place for a rule means you fix a bug or tweak the feel once.

Summary

  • The level is plain text; one loop creates the tilemap data and all objects.
  • Game feel comes from coyote time, jump buffering and variable jump height.
  • Solid tiles use collision, spikes use a tile callback, and coins and slimes use groups with overlaps.
  • Stomp, knockback, invulnerability, lives, win and lose conditions are small rules in single functions.
  • The camera follows the player inside map bounds; the HUD is fixed with setScrollFactor(0).

Next: Project: A Space Shooter