API — Game Framework
Game-framework helpers: a 2D camera, a scene manager, and per-app persistent save files. Three sub-namespaces: picocalc.game.camera, picocalc.game.scene, and picocalc.game.save.
picocalc.game.camera
Section titled “picocalc.game.camera”A 2D camera with position, zoom, target following, world bounds, and screen shake.
Functions
Section titled “Functions”picocalc.game.camera.new()
Section titled “picocalc.game.camera.new()”Create a new camera, positioned at (0, 0) with zoom 1.0.
- Parameters: None
- Returns: (userdata) PicoDeckCamera object
local camera = picocalc.game.camera.new()Camera Methods
Section titled “Camera Methods”Objects returned by picocalc.game.camera.new().
camera:setPosition(x, y)
Section titled “camera:setPosition(x, y)”Set the camera position in world coordinates.
- Parameters:
x(number): World X coordinatey(number): World Y coordinate
- Returns: None
camera:setPosition(160, 160)camera:move(dx, dy)
Section titled “camera:move(dx, dy)”Move the camera by a relative offset.
- Parameters:
dx(number): X deltady(number): Y delta
- Returns: None
camera:move(4, 0) -- pan rightcamera:getPosition()
Section titled “camera:getPosition()”Get the camera position in world coordinates.
- Parameters: None
- Returns: (number, number) World
x, ycoordinates
local x, y = camera:getPosition()camera:setZoom(zoom)
Section titled “camera:setZoom(zoom)”Set the camera zoom factor. 1.0 is unscaled.
- Parameters:
zoom(number): Zoom factor
- Returns: None
camera:setZoom(2.0) -- 2x zoomcamera:getZoom()
Section titled “camera:getZoom()”Get the current zoom factor.
- Parameters: None
- Returns: (number) Current zoom factor
camera:setTarget(x, y [, lag])
Section titled “camera:setTarget(x, y [, lag])”Follow a world position: camera:update(dt) eases the camera towards it. Call it again each frame to track a moving object.
- Parameters:
x,y(number): World position to followlag(number, optional): Smoothing time constant in seconds (default 0.15)
- Returns: None
camera:setTarget(player.x, player.y)camera:clearTarget()
Section titled “camera:clearTarget()”Stop following the current target.
- Parameters: None
- Returns: None
camera:setBounds(minX, minY, maxX, maxY)
Section titled “camera:setBounds(minX, minY, maxX, maxY)”Constrain the camera to a world rectangle — the camera will not scroll outside it. The arguments are two corners, not a size.
- Parameters:
minX(number): Left edge of the worldminY(number): Top edge of the worldmaxX(number): Right edge of the worldmaxY(number): Bottom edge of the world
- Returns: None
camera:setBounds(0, 0, 1024, 1024)camera:clearBounds()
Section titled “camera:clearBounds()”Remove the world bounds constraint.
- Parameters: None
- Returns: None
camera:getBounds()
Section titled “camera:getBounds()”Get the current world bounds.
- Parameters: None
- Returns: (number, number, number, number)
minX, minY, maxX, maxY, or nothing at all when the camera is unbounded
camera:shake(amplitude, duration)
Section titled “camera:shake(amplitude, duration)”Shake the camera on both axes.
- Parameters:
amplitude(number): Shake intensity in pixelsduration(number): Shake duration, counted down by thedtgiven tocamera:update(dt)(seconds whendtis)
- Returns: None
camera:shake(4, 0.3) -- rumble for 0.3 scamera:shakeX(amplitude, duration)
Section titled “camera:shakeX(amplitude, duration)”Shake the camera horizontally only.
- Parameters:
amplitude(number): Shake intensity in pixelsduration(number): Shake duration incamera:update(dt)units (seconds)
- Returns: None
camera:shakeY(amplitude, duration)
Section titled “camera:shakeY(amplitude, duration)”Shake the camera vertically only.
- Parameters:
amplitude(number): Shake intensity in pixelsduration(number): Shake duration incamera:update(dt)units (seconds)
- Returns: None
camera:stopShake()
Section titled “camera:stopShake()”Stop any active shake immediately.
- Parameters: None
- Returns: None
camera:worldToScreen(wx, wy)
Section titled “camera:worldToScreen(wx, wy)”Convert world coordinates to screen coordinates.
- Parameters:
wx(number): World X coordinatewy(number): World Y coordinate
- Returns: (number, number) Screen
sx, sycoordinates
local sx, sy = camera:worldToScreen(player.x, player.y)camera:screenToWorld(sx, sy)
Section titled “camera:screenToWorld(sx, sy)”Convert screen coordinates to world coordinates.
- Parameters:
sx(number): Screen X coordinatesy(number): Screen Y coordinate
- Returns: (number, number) World
wx, wycoordinates
camera:update(dt)
Section titled “camera:update(dt)”Advance target following and screen shake. Call once per frame.
- Parameters:
dt(number): Seconds since the last update
- Returns: None
camera:update(1 / 30)camera:getOffset()
Section titled “camera:getOffset()”Get the screen position of world (0, 0), with the camera position, zoom and shake folded in: screen = world * zoom + offset. The values are whole pixels (truncated toward zero).
- Parameters: None
- Returns: (integer, integer) Offset
ox, oy
local ox, oy = camera:getOffset()local z = camera:getZoom()picocalc.display.fillRect(player.x * z + ox, player.y * z + oy, 8, 8, 0xFFFF)picocalc.game.scene
Section titled “picocalc.game.scene”A scene manager. Scenes are table-like objects with update(), draw(), enter(), and exit() lifecycle methods.
Functions
Section titled “Functions”picocalc.game.scene.new()
Section titled “picocalc.game.scene.new()”Create a new (empty) scene object.
- Parameters: None
- Returns: (userdata) PicoDeckScene object
picocalc.game.scene.add(name, scene)
Section titled “picocalc.game.scene.add(name, scene)”Register a scene under a name.
- Parameters:
name(string): Scene namescene(table): Scene object with lifecycle methods
- Returns: None
picocalc.game.scene.add("menu", menuScene)picocalc.game.scene.remove(name)
Section titled “picocalc.game.scene.remove(name)”Remove a registered scene.
- Parameters:
name(string): Scene name
- Returns: None
picocalc.game.scene.has(name)
Section titled “picocalc.game.scene.has(name)”Check whether a scene is registered.
- Parameters:
name(string): Scene name
- Returns: (boolean)
trueif the scene exists
picocalc.game.scene.switch(name)
Section titled “picocalc.game.scene.switch(name)”Switch to another scene. Calls exit() on the current scene, then enter() on the next.
- Parameters:
name(string): Scene name
- Returns: None
picocalc.game.scene.switch("play")picocalc.game.scene.push(name)
Section titled “picocalc.game.scene.push(name)”Overlay a scene on top of the current one without exiting it.
- Parameters:
name(string): Scene name
- Returns: None
picocalc.game.scene.push("pause")picocalc.game.scene.pop()
Section titled “picocalc.game.scene.pop()”Pop the overlaid scene and return to the previous one.
- Parameters: None
- Returns: None
picocalc.game.scene.getCurrent()
Section titled “picocalc.game.scene.getCurrent()”Get the currently active scene.
- Parameters: None
- Returns: (table or nil) Current PicoDeckScene, or
nilif no scene is active
picocalc.game.scene.update()
Section titled “picocalc.game.scene.update()”Call update() on the current scene. Call once per frame.
- Parameters: None
- Returns: None
picocalc.game.scene.draw()
Section titled “picocalc.game.scene.draw()”Call draw() on the current scene. Call once per frame.
- Parameters: None
- Returns: None
picocalc.game.scene.objectPool(name, factory)
Section titled “picocalc.game.scene.objectPool(name, factory)”Creates an object pool with the given name. Each call creates a new pool, up to 8 per app (further calls raise an error).
- Parameters:
name(string): Pool namefactory(function): Factory used to create the pool’s objects (required)
- Returns: (table) The object pool
local bullets = picocalc.game.scene.objectPool("bullets", function() return {} end)Object Pool Methods
Section titled “Object Pool Methods”A pool holds up to 64 idle objects. Call these with method syntax on the table returned by objectPool().
pool:acquire()
Section titled “pool:acquire()”Takes an idle object from the pool, or calls the factory to make a new one if the pool is empty.
- Returns: (any) The object
pool:release(obj)
Section titled “pool:release(obj)”Returns an object to the pool for reuse. If the pool already holds 64 objects, the object is silently dropped.
- Parameters:
obj(any): Object to return
- Returns: None
pool:prewarm(count)
Section titled “pool:prewarm(count)”Calls the factory to fill the pool with idle objects ahead of time, up to the 64-object limit.
- Parameters:
count(number): Number of objects to create (clamped to 0-64)
- Returns: None
pool:clear()
Section titled “pool:clear()”Discards all idle objects in the pool. Objects currently in use are not affected.
- Returns: None
local bullets = picocalc.game.scene.objectPool("bullets", function() return {x = 0, y = 0} end)bullets:prewarm(16)local b = bullets:acquire()-- ... use b ...bullets:release(b)picocalc.game.scene.setGlobal(key, value)
Section titled “picocalc.game.scene.setGlobal(key, value)”Store a value shared across all scenes.
- Parameters:
key(string): Key namevalue(any): Value to store
- Returns: None
picocalc.game.scene.getGlobal(key)
Section titled “picocalc.game.scene.getGlobal(key)”Read a value shared across scenes.
- Parameters:
key(string): Key name
- Returns: (any) Stored value, or
nil
picocalc.game.scene.clearGlobals()
Section titled “picocalc.game.scene.clearGlobals()”Clear all values shared across scenes.
- Parameters: None
- Returns: None
picocalc.game.save
Section titled “picocalc.game.save”Per-app save slots, one JSON file each at /data/<app id>/saves/<key>.json.
- Keys are 1-128 bytes of
[A-Za-z0-9._-], contain no..and do not start with.. Any other key is refused:setreturnsfalse, "invalid save name",getreturns nil,exists/deletereturn false. - The value passed to
setmust be a table (nested tables, strings, numbers, booleans). Integer-keyed tables come back with string keys; whole floats keep.0somath.typesurvives. - Saves used to live in a shared
/saves/<key>.json. The first time an app reads a key (get/exists) that it has no slot for and the old file exists, the file is copied into the app’s slot; the old file is never modified or removed. list()returns only this app’s slot names.
Functions
Section titled “Functions”picocalc.game.save.set(key, value)
Section titled “picocalc.game.save.set(key, value)”Save a value under a key.
- Parameters:
key(string): Key namevalue(table): Value to save
- Returns:
true, orfalse, err
picocalc.game.save.set("highscore", { score = 12500 })picocalc.game.save.get(key)
Section titled “picocalc.game.save.get(key)”Load a saved value.
- Parameters:
key(string): Key name
- Returns: (any) Saved value, or
nilif the key does not exist
local best = (picocalc.game.save.get("highscore") or {}).score or 0picocalc.game.save.exists(key)
Section titled “picocalc.game.save.exists(key)”Check whether a key has a saved value.
- Parameters:
key(string): Key name
- Returns: (boolean)
trueif the key exists
picocalc.game.save.delete(key)
Section titled “picocalc.game.save.delete(key)”Delete a saved value.
- Parameters:
key(string): Key name
- Returns: (boolean)
falseif the slot did not exist or the key is invalid
picocalc.game.save.list()
Section titled “picocalc.game.save.list()”List all saved keys.
- Parameters: None
- Returns: (table) Array of key name strings
for _, key in ipairs(picocalc.game.save.list()) do picocalc.sys.log("save key: " .. key)endExample
Section titled “Example”A play scene that follows a player sprite with the camera and saves the high score on exit.
The scene reads picocalc.gamepad (see API Gamepad), which needs firmware with API version 9. To also run on older firmware, check for it once and fall back to picocalc.input:
local pad = picocalc.gamepadlocal input = picocalc.input-- held mask, and the masks to test it againstlocal read = pad and pad.getButtons or input.getButtonslocal RIGHT = pad and pad.PAD_RIGHT or input.BTN_RIGHTlocal camera = picocalc.game.camera.new()
local score = 0local player = { x = 160, y = 160 }function player:getPosition() return self.x, self.yend
local play = {}
function play:enter() score = 0 camera:setTarget(player.x, player.y) camera:setBounds(0, 0, 1024, 1024)end
function play:update(dt) picocalc.input.update() -- The gamepad is the recommended way to read game input: players can -- rebind it in Settings > Controls. Esc stays on picocalc.input. local pad = picocalc.gamepad local buttons = pad.getButtons() if buttons & pad.PAD_RIGHT ~= 0 then player.x = player.x + 2 end if buttons & pad.PAD_LEFT ~= 0 then player.x = player.x - 2 end if buttons & pad.PAD_DOWN ~= 0 then player.y = player.y + 2 end if buttons & pad.PAD_UP ~= 0 then player.y = player.y - 2 end score = score + 1 camera:setTarget(player.x, player.y) camera:update(dt)end
function play:draw() picocalc.display.clear(0x0000) local ox, oy = camera:getOffset() picocalc.display.fillRect(player.x - 4 + ox, player.y - 4 + oy, 8, 8, 0xF800) picocalc.display.drawText(4, 4, "SCORE " .. score, 0xFFFF) picocalc.display.flush()end
function play:exit() local best = (picocalc.game.save.get("highscore") or {}).score or 0 if score > best then picocalc.game.save.set("highscore", { score = score }) endend
picocalc.game.scene.add("play", play)picocalc.game.scene.switch("play")
while true do picocalc.game.scene.update() picocalc.game.scene.draw() -- play:draw() flushes, which paces the loopend