Skip to main content

Cheatsheet

scope pattern

engine.scope(({ start, cls, spr }) => {
    // all functions in closure
    start({ /* ... */ });
});

comments

// this is a comment

vars and types

var points = 100;
var amount = 4.20;
var enabled = true;
var name = 'shooty game';

typeof name == 'string';

// more: {{ route('redirects.vars-and-types') }}

operators

// + - * / ^ % =
// += -= *= /= %= ^=
// < > <= >= == !=
// && || !

strings

var game = 'Mario Kart';
var platform = 'Wii';

text(game + ' ' + platform);
text(`${game} ${platform}`);
text(`${kills * 100} points`);

text(str, x, y, c);

// more: {{ route('redirects.strings') }}

functions

function difference(x, y) {
    return x - y;
}

var result = difference(16, 12);

conditionals

if (position > 120) {
    position = 120;
} else {
    position += 1;
}

math functions

Math.sin(x);
Math.abs(1.1);
Math.floor(1.6);
Math.ceil(1.4);

// more: {{ route('redirects.math-functions') }}

rnd(); // ← random float between 0 and 1
rnd(128); // ← random float between 0 and 128

engine.randomFloatBetween(min, max);
engine.randomIntegerBetween(min, max);

arrays

var particles = [];
particles.push(new Particle(6));
particles.push(new Particle(7));

particles.length == 2;

particles.pop(); // ← remove and return the last particle

var enemies = [1, 2, 3];

enemies.includes(2) == true;

enemies.map(function(number) {
    return number + 1;
});

enemies.filter(function(number) {
    return number % 2;
});

// more: {{ route('redirects.arrays') }}

objects

var player = {
    x: 64,
    y: 64,
};

player.speed = 2;

Object.keys(player) == ['x', 'y', 'speed'];
Object.values(player) == [64, 64, 2];
Object.entries(player) == [['x', 64], ['y', 64], ...];

// more: {{ route('redirects.objects') }}

loops

for (var i = 0; i < 10; i += 1) {
    text('number: ' + i);
}

for (var i = 0; i < enemies.length; i += 1) {
    text(`enemy: ${enemies[i]}`);
}

for (var value of enemies) {
    text(`value: ${value}`);
}

for (var key in abilities) {
    text(`key: ${key}`);
}

for (var [key, value] of Object.entries(abilities)) {
    text(`key: ${key}, value: ${value}`);
}

controls

btn('a');
btnp('d');

btnp(); // ← any key

// more: {{ route('redirects.supported-keys') }}

mouse(); // {x, y}
mdown();
mup();
click(); // consumed on read
dragging();

cursor(false); // hide
cursor(); // or cursor('pointer')

sprites

spr(
    key, x, y,
    [w = 1], [h = 1],
    [flipX = false], [flipY = false],
);

fget(key, flag);
fset(key, flag, value);

pixels

pset(x, y, c);
pget(x, y);

shapes

line(x1, y1, x2, y2, c);
rect(x1, y1, x2, y2, c);
rectfill(x1, y1, x2, y2, c);
circ(x, y, r, c);
circfill(x, y, r, c);

text

text('hello', x, y, c);
textSize('hello'); // { x, y }
textPixels('hello', x, y, c); // [[x, y, c], ...]

collisions

circlesCollide([x, y, r], [x, y, r]);
boxesCollide([x, y, w, h], [x, y, w, h]);

sounds

// sfx (plays by key from sounds object)
sfx('jump');
sfx('coin', 0.5); // with volume

// background music (key, URL, or data URI)
music('bgm');
music('/music/theme.mp3');
music('bgm', { volume: 0.5 });
music(false); // stop

random

rnd(10); // 0 to 10
randomFloat(); // 0 to 1
randomIntegerBetween(1, 6); // 1 to 6
randomFloatBetween(0.5, 1.5); // 0.5 to 1.5

camera

camera(x, y); // set offset
cget(); // { x, y }
creset(); // reset to (0, 0)

tilemap

mset(x, y, 'brick'); // set tile
mget(x, y); // get tile
map(0); // draw layer 0
mclear(0); // clear layer 0

assets

// defer loading
const sprites = {
    player: queue('https://example.com/player.json'),
};

// fetch all queued assets
async function init() {
    await load();
}

lifecycle

cls(c); // clear screen

start({ sprites, sounds, init, update, draw });

fps((value) => { /* ... */ });

colors

Click a color to copy its hex code.

Copied!

pal(to, from);
pal();
cls(color = 0);