Game Design & Development · Lesson 4

2D Game Engines: Kaplay & Phaser

Game Engines Kaplay Phaser 3 Sprites Scenes Physics

Building from scratch teaches you the fundamentals — but a game engine handles the repetitive plumbing so you can focus on what matters: game design. Learn what engines do, compare Kaplay and Phaser, and understand which to reach for and when.

What Does a Game Engine Do?

A game engine is a library of pre-built tools for common game development tasks. Instead of writing the game loop, sprite management, physics, collision, audio, and input handling from scratch every time, you use the engine's systems.

  • Scene Management — organize game states (menu, level 1, game over) as separate scenes with clean transitions
  • Sprite System — load PNG images, cut sprite sheets, animate frames automatically
  • Physics Engine — realistic gravity, velocity, bounce, and AABB/circle collision baked in
  • Input System — clean API for keyboard, mouse, touch, and gamepad — no raw event listeners
  • Audio Manager — play, loop, stop, and volume-control sound effects and music
  • Asset Loader — preload images and sounds before the game starts so nothing is missing mid-game
  • Camera System — follow the player, shake on impact, zoom in and out
From Scratch vs Engine: Writing from scratch teaches you how things work. Using an engine lets you build bigger things faster. Both skills matter. During a Game Jam, use an engine — there's no time to rebuild physics from scratch.

Kaplay vs Phaser 3

These are the two most popular JavaScript 2D game engines for classroom use. Both are free, open-source, and run in any browser without installation.

Kaplay Beginner Friendly

  • Successor to KaboomJS — fun, fast API
  • Very readable code — game objects in one line
  • Component-based: add gravity, sprite, collision as "tags"
  • Built-in platformer physics
  • Great for jams, prototypes, classroom projects
  • Active community, good docs
  • CDN install: one script tag and you're coding

Phaser 3 Industry Standard

  • Most widely used 2D JS engine in the world
  • Scene lifecycle: preload → create → update
  • Arcade Physics, Matter.js, Impact physics options
  • Tiled map editor support built-in
  • Tweens, particles, tilemaps, cameras
  • Excellent for shipping real games
  • Steeper learning curve — more powerful
FeatureKaplayPhaser 3
Setup time⚡ 1 line⏱ 10 min
Learning curve✓ Gentle⚠ Moderate
Physics quality⚠ Basic/Medium✓ Advanced
Documentation✓ Good✓ Excellent
Community size⚠ Medium✓ Large
Game Jam speed✓ Best⚠ Good
Ship quality⚠ Good✓ Professional

The Scene Lifecycle

Both engines organize game logic into scenes — discrete states of the game (main menu, gameplay, game over). Phaser uses a strict three-phase lifecycle:

preload() Load assets
(images, audio)
before play
create() Build the scene:
spawn player,
set up world
update() Called every
frame — move,
collide, check

Kaplay is looser — you define a scene as a function and use add() calls to create objects. The engine runs its own loop internally.

Code Comparison — Moving a Player

The same feature — a player square that moves with arrow keys — in both engines:

// Kaplay — minimal setup, very readable
kaplay();

scene("game", () => {
  // create a player with components
  const player = add([
    rect(32, 32),           // draw a 32×32 rectangle
    pos(200, 200),          // starting position
    color(BLUE),             // color it blue
    area(),                  // enable collision detection
    body(),                  // enable gravity/physics
    "player",               // tag for collision queries
  ]);

  // movement — checked every frame automatically
  onKeyDown("left",  () => player.move(-200, 0));
  onKeyDown("right", () => player.move( 200, 0));
  onKeyPress("up",    () => player.jump());
});

go("game");
// Phaser 3 — explicit scene class, more structured
class GameScene extends Phaser.Scene {
  preload() {
    // load assets here — images, tilemaps, audio
  }

  create() {
    // create player as a rectangle physics body
    this.player = this.add.rectangle(200, 200, 32, 32, 0x3b82f6);
    this.physics.add.existing(this.player);
    this.player.body.setGravityY(400);

    // set up cursor keys
    this.cursors = this.input.keyboard.createCursorKeys();
  }

  update() {
    const body = this.player.body;
    body.setVelocityX(0);

    if (this.cursors.left.isDown)  body.setVelocityX(-200);
    if (this.cursors.right.isDown) body.setVelocityX( 200);
    if (this.cursors.up.isDown && body.blocked.down)
      body.setVelocityY(-500);
  }
}

new Phaser.Game({ scene: GameScene, physics: { default: 'arcade' } });

Core Concepts in Any 2D Engine

Sprites & Sprite Sheets

A sprite is a 2D image used for a game character or object. A sprite sheet packs many animation frames into a single image file. The engine extracts each frame by its pixel coordinates.

Tilemaps

A tilemap is a grid of small tile images used to build levels. You design levels in a free tool called Tiled and export JSON data that Phaser (or Kaplay) can load and render efficiently — thousands of tiles drawn in milliseconds.

Physics Groups & Overlap

Engines let you create groups of objects (e.g., all enemies) and then run collision checks against the whole group in one line — much more efficient than looping yourself.

Tweens & Easing

A tween animates a value over time — move an object from X to Y, fade it in, scale it up. Combined with easing functions (ease-in, bounce), this makes games feel polished with very little code.

Which Engine Should You Use?

Choose based on your situation:

First time building a game→ Kaplay
Game Jam with 5-day limit→ Kaplay
Platformer with levels and maps→ Phaser 3
Want to build a portfolio piece→ Phaser 3
Learning game dev concepts→ From Scratch first
Teaching K-5 students→ Kaplay or Scratch.mit.edu
3D game with a real engine→ Godot (see next lesson)
One-day builds: Whichever engine you pick, start from a tiny-loop archetype — Catch, Dodge, Clicker, Maze, and Pong all fit comfortably in under 50 lines of Kaplay. Don't design a new genre and learn a new engine on the same day.