Lua API documentation

How your script runs

Your script can define these optional functions:

Think of it like this:

Your first tiny script

Paste this and press Run:

local x = 0
local y = 4

function update(delta_time)
  x = x + 6 * delta_time
  if x > SCREEN_W then
    x = 0
  end
end

function draw()
  clear()
  set_pixel(math.floor(x), y, rgb(255, 100, 40))
end

What you should see: one bright pixel moving left-to-right, then restarting.

Input basics

Smooth movement (slide while held): poll is_pressed in update:

local x = 10

function update(dt)
  if is_pressed("L_LEFT") then
    x = x - 10 * dt
  end
  if is_pressed("L_RIGHT") then
    x = x + 10 * dt
  end
end

Stepped movement (one tile per repeat, e.g. grid cursor): on_press for the first step, on_repeat for held repeats:

function on_press(button)
  move_cursor(button)
end

function on_repeat(button)
  move_cursor(button)
end

Coordinates and screen model

Input key map

These are the default keyboard keys and button names used by is_pressed, on_press, on_repeat, and on_release.

Logical button Keyboard key
L_UP w
L_LEFT a
L_DOWN s
L_RIGHT d
R_UP Arrow Up
R_LEFT Arrow Left
R_DOWN Arrow Down
R_RIGHT Arrow Right
L_BUMP q
R_BUMP e
MENU 1
ESC 2

Project files

In the browser SDK, your project lives in a virtual file system stored locally in the browser.

Loading other Lua files

You can split a game or library across multiple .lua files and load them with require.

How to call require

Pass the full path to the file in the project, replacing / with . and omitting the .lua extension:

local mylib = require("mygame.lib.mylib") -- Will load /mygame/lib/mylib.lua

What require does

Example: main + library

/mygame/lib/colors.lua:

local M = {}

function M.player()
  return rgb(255, 100, 40)
end

return M

/mygame/main.lua:

local colors = require("mygame.lib.colors")

function draw()
  clear()
  set_pixel(10, 4, colors.player())
end

Create both files in the explorer, open /main.lua and press Run.

What is not available

For sandboxing, only require can load other Lua files. The following are not available in scripts:

Use the editor + Run for your entry script, and require("path.to.luafile") for any other lua script file you might need. To read other kinds of data files, use read_file().

Common libraries

Common libraries are available in the built-in files, under the lib folder. This directory contains many useful shared libraries, such as:

These libraries are currently not documented; explore the Lua scripts and examples to understand how they work !

API reference

Constants

Name Type Value Description
SCREEN_W number 20 Screen width in pixels
SCREEN_H number 10 Screen height in pixels

Color

rgb(r, g, b)

Create an RGB color table.

Parameters:

Returns: table RGB color table {r, g, b}

Example:

local red = rgb(255, 0, 0)

hsl(h, s, l)

Convert HSL values to an RGB color table.

h is wrapped over 360 degrees, s and l are clamped to 0..1.

Parameters:

Returns: table RGB color table {r, g, b}

Example:

local cyan = hsl(180, 1.0, 0.5)

Console

Write a line to the SDK console panel.

Handy for quick debugging while you are iterating on game logic. (This replaces the default Lua print)

Parameters:

Returns: nil

Example:

print("Hello, world!")       -- Output: Hello, world!
print("Hello", 1, true, nil) -- Output: Hello    1    true    nil

Display

get_pixel(x, y)

Read the current color of one pixel.

Useful for effects that react to what is already drawn (sampling, simple collision checks against color, post-processing tricks, etc.).

Parameters:

Returns: table RGB color table {r, g, b}

Example:

set_pixel(0, 0, rgb(42, 0, 134))

local my_color = get_pixel(0, 0)

print(my_color[1]) -- 42
print(my_color[2]) -- 0
print(my_color[3]) -- 134

set_pixel(x, y, rgb_color)

Set one pixel to an RGB color.

This is the most direct drawing primitive: great for point effects, particles, and any algorithm that draws pixel by pixel.

Parameters:

Returns: nil

Example:

set_pixel(3, 2, rgb(255, 0, 0))

set_pixel_blend(x, y, rgb_color, alpha)

Blend a color onto one pixel instead of replacing it.

Use this for transparency and softer visuals (trails, fades...) by mixing with the color that is already on screen.

Parameters:

Returns: nil

Example:

set_pixel_blend(3, 2, rgb(255, 0, 0), 0.5)

set_pixel_f(x, y, rgb_color)

Draw a point using floating-point coordinates.

Applies bilinear interpolation to spread out brightness on the surrounding pixels based on floating-point coordinates. Useful for smooth movement/animation where object positions are in floating-point coordinates.

Parameters:

Returns: nil

Example:

set_pixel_f(3.25, 2.75, rgb(0, 255, 0))

set_unsafe_pixel_brightness(x, y, brightness)

UNSAFE !!! Set the brightness of a pixel.

This is useful for setting the brightness of a pixel to a value that is different from the default brightness, for short-term effects like flashing. In the web SDK, shows a corona effect on the display to simulate the brightness effect. If the total current exceeds the limit, an error checkerboard pattern will be displayed (currently not supported in the web SDK). Use with moderation.

Parameters:

Returns: nil

Example:

set_unsafe_pixel_brightness(3, 2, 5)

rect(x, y, w, h, rgb_color)

Draw a filled axis-aligned rectangle.

Great for UI blocks, paddles, bars, and simple game objects. Notes:

Parameters:

Returns: nil

Example:

rect(1, 1, 5, 3, rgb(255, 255, 255))

rect_blend(x, y, w, h, rgb_color, alpha)

Blend a filled rectangle with what is already on screen.

Useful for overlays, tint zones, and soft UI panels.

Parameters:

Returns: nil

Example:

rect_blend(0, 0, SCREEN_W, SCREEN_H, rgb(255, 0, 0), 0.5)

rect_f(x, y, w, h, rgb_color)

Draw a rectangle using floating-point coordinates.

Applies bilinear interpolation to spread out brightness on the surrounding pixels based on floating-point coordinates. Useful for smooth movement/animation where object positions are in floating-point coordinates.

Parameters:

Returns: nil

Example:

rect_f(1.2, 1.2, 5.5, 3.5, rgb(0, 0, 255))

fill(rgb_color)

Fill the entire screen with one color.

Most scripts call this at the start of draw() to clear the previous frame and paint a background color in one call.

Parameters:

Returns: nil

Example:

fill(rgb(255, 0, 0))

fill_blend(rgb_color, alpha)

Blend one color over the whole screen.

This keeps existing pixels visible while tinting the frame, which is useful for fades, flashes, and mood/color shifts.

Parameters:

Returns: nil

Example:

fill_blend(rgb(255, 0, 0), 0.5)

clear()

Clear the screen to black (rgb(0, 0, 0)).

Equivalent to fill(rgb(0, 0, 0)) and commonly used at the start of draw().

Returns: nil

Example:

clear()

File system

read_file(path)

Read a complete file at the given path

Parameters:

Returns: string: binary string representing the file contents or errors if the file couldn't be found

Example:

local my_file = read_file("/file.bin")

read_file_chunk(path, offset, size)

Read a chunk of data from the file at the given path, offset and size. If the size at the given offset exceeds the file size, only the remaining bytes will be returned.

Parameters:

Returns: string: binary string representing the file contents or nil if the file couldn't be found

Example:

local my_chunk = read_file_chunk("/file.bin", 0, 512) -- reads the 512 first bytes of the file

file_size(path)

Get the size of the file at path

Parameters:

Returns: int: size of the file in bytes

Example:

size = file_size("/file.bin")

list_directory(path)

Get a list of the contents of the directory at the given path.

Parameters:

Returns: table:<string,bool> a table with paths as keys, and a boolean representing if the path is a file (true) or a directory(false)

Example:

local items = list_directory("/my/directory/path")

Input

is_pressed(key)

Check whether a mapped button is currently held down.

Use this inside update() for continuous input (movement while a key is held), as opposed to one-shot input events from on_press/on_release. The key string must match the host's key map, e.g.:

Parameters:

Returns: boolean true if pressed, otherwise false

Example:

if is_pressed("L_UP") then ... end

set_repeat_delay(delay)

Set the repeat delay which is used to configure the behavior of on_repeat

Parameters:

Returns: nil

Example:

set_repeat_delay(0.5)

set_repeat_interval(interval)

Set the repeat interval which is used to configure the behavior of on_repeat

Parameters:

Returns: nil

Example:

set_repeat_interval(0.5)

Sound

buzz(frequency, duration)

Play a tone at the given frequency for the given duration.

Useful for beeps, notifications, and simple audio effects.

Parameters:

Returns: nil

Example:

buzz(440, 1000) -- Play a 440Hz tone for 1 second

Time

get_time()

Get elapsed runtime in seconds since the script started.

Useful for timers, cooldowns, oscillations, and any time-based animation.

Returns: number Seconds (floating point) since the Lua state was created.

Example:

local t = get_time()

Lifecycle callbacks

The host calls these globals by name if they exist as Lua functions.

setup()

Called once after your script is loaded and before the first frame starts.

Use this to initialize game state and set up any values that should persist across frames.

Returns: nil

Example:

function setup()
  clear()
end

update(delta_time)

Called every frame before draw().

Use this for simulation/state updates (movement, timers, collisions, input handling). Keep rendering out of update() and perform drawing in draw().

Parameters:

Returns: nil

Example:

function update(delta_time)
  -- movement logic
end

draw()

Called every frame after update().

Use this to write pixels and render the current state for the frame. This function should focus on drawing, while state changes stay in update().

Returns: nil

Example:

function draw()
  clear()
  set_pixel(1, 1, rgb(255, 0, 0))
end

process(delta_time)

Called on every main loop tick (faster than framerate)

Use this for specific things which need a higher resolution than the framerate, like for example a music sequencer.

Parameters:

Returns: nil

Example:

function process(delta_time)
  -- stuff
end

on_press(button_name)

Called when a mapped button transitions from up to down (key press edge).

Use this for one-shot actions like firing, toggling, or the first step of movement. For smooth hold (e.g. sliding), poll is_pressed() in update(). For stepped autorepeat while held, also define on_repeat.

Parameters:

Returns: nil

Example:

function on_press(button_name)
  if button_name == "L_UP" then
    set_pixel(2, 2, rgb(0, 255, 0))
  end
end

on_release(button_name)

Called when a mapped button transitions from down to up (key release edge).

Use this for release-driven actions or cleanup that should happen exactly once when a button is released.

Parameters:

Returns: nil

Example:

function on_release(button_name)
  if button_name == "L_UP" then
    clear()
  end
end

on_repeat(button_name)

Called while a mapped button is held, after an initial delay, then at a fixed interval.

Does not fire on the initial press; use on_press for the first action when the button goes down. Use this for stepped input (grid cursors, menus), not smooth motion. Repeat delay and interval can be configured using set_repeat_delay() and set_repeat_interval()

Parameters:

Returns: nil

Example:

function on_repeat(button_name)
  move_cursor(button_name)
end