Pacman Dissected

“If Pac-Man had affected us as kids, we’d all be running around in dark rooms, munching pills and listening to repetitive electronic music”
Markus Brigstocke

In my previous post, I wrote about learning TypeScript by writing a game.

The game I chose was Pacman (play it here).

I never intended to make the source code available, but a few people asked for it, so I’ve tidied it up a bit and put it on GitHub (it’s far from tidy though, so go easy – plus it’s my first attempt at TypeScript!)

I’ve described the major bits of the code below. I’ve described:

If there’s anything I’ve missed, please let me know.

I hope you find this useful. Please be aware that this is not a shining example of TypeScript or the best patterns to use in TypeScript. The style leans heavily towards C# as that’s my day-to-day language. It barely scrapes the surface of TypeScript features and I’m sure there are many things in it that could be made more elegant (readable) by using other TypeScript features. I’d love to get feedback on the code as I’d like to evolve it over time. So please free to provide feedback, pull-requests, etc. etc.

Build and Run #

The following should download and run the game (assuming you’ve got git and npm installed):

git clone
cd pacman
npm install
start http://localhost:8080

Game Startup #

index.html loads the JavaScript scripts for howler (sound), hammer (touch), the loading screen, the control panel, and require.js.

When the page loads, it loads all of the sound files and then all of js files. require.js fires an event (load), when a script is loaded. We subscribe to this event and tell the loading screen that a script is loaded (loadState.scriptLoaded(moduleName)).

The main game is held within a div named gameDiv. Within that div is a canvas:

<!--ideal canvas size = 672/944 (0.711 aspect ratio) (or 224 x 314) -->
<div id="gameDiv" style="opacity: 0.75;">
<canvas id="gameContainer" width="672" height="944">
Your browser does not support the HTML5 canvas tag.

The next bit then instantiates the Engine:

require(["js/Engine", "js/GameStorage"],
function (pacManModule) {
var engine = new pacManModule.Engine();

Engine (in Engine.ts) is a small type which handles:

Game Loop #

The game-loop runs 60 times per second via a call to window.requestAnimationFrame

The game-loop updates and draws everything (60 times per second). It calls MainWindow.Update (MainWindow.ts) with the time elapsed since the last call. The time elapsed is important as it allows timers to be run accurately. MainWindow is the, er, main window. It handles:

Game Flow #

Everything in the game is an Act:

import { Canvas, GameContext } from "../Core/_exports";

import { ActUpdateResult } from "./ActUpdateResult";

* An 'act' is something that's run in a loop. The main window continaully updates and draws whatever
* the 'current act' is. Acts are things such as DemoAct, GameAct, GameOverAct etc.

export abstract class Act {
abstract update(context: GameContext): ActUpdateResult;
abstract draw(canvas: Canvas): void;
abstract get nextAct(): Act;

Here are the different Acts:

The welcome screen (or the ‘attract screen’ as they call it in arcade circles) is called the AttractAct. You can see it being set as the main Act in MainWindow.ts:

MainWindow.currentAct = new AttractAct();

// POINTER: You can change the starting Act by using something like:
//MainWindow.currentAct = new TornGhostChaseAct(new AttractAct());

When the update method returns Finished, the game-loop starts to run the Act returned by nextAct.

Graphics #

The graphics are drawn onto an HTML Canvas. A sprite-sheet is loaded in index.html:

<img hidden id="spritesheet" src="img/spritesheet.png" />

It looks like this:

It contains all of the graphics in one image. The sprites then reference a particular rectangle of this image and are drawn on the canvas. All sprites derive from Sprite:

export abstract class Sprite {

loadContent(): void { // nothing

abstract get position():Point;

abstract update(context: GameContext): void;

abstract draw(canvas: Canvas): void;

abstract get origin(): Point;

abstract get size(): Vector2D;

abstract get spriteSheet(): HTMLImageElement;

abstract get spriteSheetPos(): Point;

Each sprite has the following facets:

The Maze #

The maze (as shown above) is drawn to the canvas every frame. The ‘pills’ (normal pills and ‘power pills’) are removed from maze (well, a copy of each as there’s one for each player) when the pill is eaten.

The maze is broken down into ’tiles’ that are 8×8 pixels in size. Sprite positions are converted to the associate ’tile’. The game then refers to a lookup that says what’s in the current tile. The lookup looks like this:

private static readonly map: string[] = [
// 0,0 29,0
" ",
" oooooooooooo oooooooooooo ",
" o o o o o o ",
" * o o o o * ",
" o o o o o o ",
" oooooooooooooooooooooooooo ",
" o o o o o o ",
" o o o o o o ",
" oooooo oooo oooo oooooo ",
" o + + o ",
" o + + o ",
" o ++++++++++ o ",
" o + + o ",
" o + + o ",
"++++++o+++ +++o+++++++",
" o + + o ",
" o + + o ",
" o ++++++++++ o ",
" o + + o ",
" o + + o ",
" oooooooooooo oooooooooooo ",
" o o o o o o ",
" o o o o o o ",
" *oo ooooooo++ooooooo oo* ",
" o o o o o o ",
" o o o o o o ",
" oooooo oooo oooo oooooo ",
" o o o o ",
" o o o o ",
" oooooooooooooooooooooooooo ",
" "

A tile is represented by the Tile class. Some of the main methods on here are:

Ghosts #

Ghosts move around the maze and either chase pacman or run away from him. Here’s the various states of a ghost:

export enum GhostState {
// heading towards pacman or their home corner (scatter)

// blue - running away from pacman (in a random pattern)

// heading back to the 'House'

The state of a ghost can differ from the ‘movement mode’ of a ghost:

export enum GhostMovementMode {
// the ghost is chasing pacman
// the ghost is heading back to his 'home corner'

// the ghost is heading back to the house (after he's been eaten)

// the ghost is in the house

// the ghost is blue

I mentioned that the ‘ghost state’ and ‘movement mode’ can differ; an example is that a ghost can be ‘blue’ (Frightened) while still being in the ghost house

There are a number of types responsible for moving ghosts. They all implement GhostMover:

The general logic of a ghost comprises of ‘head to the home corner for X seconds, chase pacman for X seconds’. The time spent in each phase varies throughout the level. Each level specifies different patterns.

Timing and Difficulty #

Getting the difficulty to match that of the arcade game was tricky. There are many variables used throughout each level. These variables are described in the type LevelProps:

export class LevelProps {
public readonly introCutScene: IntroCutScene,

public readonly fruit: FruitItem,
public readonly fruitPoints: number,

public readonly pacManSpeedPc: number,
public readonly pacManDotsSpeedPc: number,

public readonly ghostSpeedPc: number,
public readonly ghostTunnelSpeedPc: number,

public readonly elroy1DotsLeft: number,
public readonly elroy1SpeedPc: number,
public readonly elroy2DotsLeft: number,
public readonly elroy2SpeedPc: number,

public readonly frightPacManSpeedPc: number,
public readonly frightPacManDotSpeedPc: number,

public readonly frightGhostSpeedPc: number,
public readonly frightGhostTime: number,
public readonly frightGhostFlashes: number) {

At a glance, there are different speeds for:

There’s a rather large array created in LevelStats that contains all of these variables for the first 21 levels.


Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.