These docs provide an overview of the Codetrain engine functionality.

Game Loop

There three built-in game loop functions, awake, start and update.

awake() runs once on initialization.
start() runs once at the start of the game.
update() runs once a frame after start.
// runs before initialization

// runs after initialization
function awake() { }

// runs after awake
function start() { }

// runs every frame after start
function update() { }
Elapsed time can be tracked with getTime and getDeltaTime.

getTime() returns the milliseconds since start was first called.
getDeltaTime() returns the milliseconds since update was last called.
function update() {
  // time milliseconds have passed since first start
  const time = getTime();

  // deltaTime milliseconds have passed since last update
  const deltaTime = getDeltaTime();


Dialogue can be displayed with say.

say(text) opens a dialogue box with given text.
// open dialogue box saying 'Hello World!'
say('Hello World!');
Text can be added and removed with addText and removeText.

addText(text, x, y, options?) creates and returns text text at x, y pixels.
- options is an optional object containing size, color, and id fields.
removeText(id) removes text with given id.
// create score text
addText('Score: 0', 0, 0);

// update score text
addText('Score: 1', 0, 0);

// make score text big, red, and id 'score'
addText('Score: 1', 0, 0,
  { size: 32, color: 'red', id: 'score' }

// remove score text by id


Keyboard input can be taken with isKeyDown and isKey.

isKeyDown(key) returns whether given key is down.
isKey(key) returns whether given key was pressed in the last frame.
function update() {
  // triggers when q pressed down
  if (isKey('q')) say('Q pressed');

  // triggers while q pressed down
  if (isKeyDown('q')) console.log('Q down');


Objects can be moved with move, moveTiles, and movePixels.

move(dir) moves object one tile up, down, left, or right.
moveTiles(x, y) moves object by x, y, in tiles.
movePixels(x, y) moves object by x, y, in pixels.
// tiled object movement
function update() {
  if (isKey('w')) move('up');
  if (isKey('a')) move('left');
  if (isKey('s')) move('down');
  if (isKey('d')) move('right');
// smooth object movement
function update() {
  if (isKeyDown('w')) movePixels(0, -1);
  if (isKeyDown('a')) movePixels(-1, 0);
  if (isKeyDown('s')) movePixels(0, 1);
  if (isKeyDown('d')) movePixels(1, 0);
Object position can be retrieved with getTilePos and getPixelPos.

getTilePos() returns x, y position in tiles.
getPixelPos() returns x, y position in pixels.
// get tile position
const pos = getTilePos();

// if at 0, 0, open dialogue box
if (pos.x === 0 && pos.y === 0) say('At 0, 0');
Object position can be set with setTilePos and setPixelPos.

setTilePos(x, y) moves object to x, y, in tiles.
setPixelPos(x, y) moves object to x, y, in pixels.
function update() {
  // if q pressed, move to top left corner
  if (isKeyDown('q')) setTilePos(0, 0);

  // if z pressed, move to bottom right corner
  if (isKeyDown('z')) setTilePos(7, 7);


Tiles can be retrieved with getTile and getTileAt.

getTile() returns index of tile at current position.
getTileAt(x, y) returns index of tile at x, y.
// get tile at position
const tile = getTile();

// if on coin, open dialogue box
if (tile === 'coin') say('You found the coin!');
Tiles can be modified with setTile and setTileAt.

setTile(tile) sets tile at current position to tile.
setTileAt(x, y, tile) sets tile at x, y to index tile.
// get tile at position
const tile = getTile();

// if tile empty, set to grass
if (tile === 'empty') setTile('grass');


Objects can be retrieved with getObject.

getObject(id) returns the first reference of an object with given id.
// get object with ID 'player'
const player = getObject('player');

// move player right
Objects can be created and deleted with createObject and deleteObject.

createObject(object, x, y, options?) creates object at x, y pixels.
- options is an optional object containing an id field.
deleteObject(id) deletes object with given id.
// create new player
const player2 = createObject('player', 0, 0, { id: 'player2' });

// delete new player
Object layers can be retrieved and set with getLayer and setLayer.

getLayer() returns the object layer.
setLayer(layer) sets the object layer to layer.
// send object to background

// returns 'back'


Sounds can be added and played with addSound and playSound.

addSound(name, url) registers sound with source url and given name.
playSound(name) plays sound with given name.
// add powerup sound
addSound('powerup', 'https://codetrain.org/sounds/powerup.mp3');

// play powerup sound