Programming Reference manual

Module description

Module Scene

This module handles functions for dealing with the scene.

init()

Clears the scene and sets everything to default value.

set_color(color)

Sets the background color of the scene. When the system renders a new frame, it will use this color to clear the display before starting drawing the objects in the scene. The color is defined as RGB565.

set_update(function(delta))

Set the update function for the scene. The update function is called approx every 20 ms to allow the game to do game logic e.g. change position of sprites, read buttons etc. The parameter delta contains the time in seconds passed after the previous call to update.

Module Gamepad

This module contains function for dealing with the gamepad. Note that the menu button is handled by the firmware and it is not possible to read it.

read()

Reads the current button status and returns an integer where each bit represents if button is being pressed (1) or not (0).

is_a_pressed()

Returns true if button A is pressed else false

is_b_pressed()

Returns true if button B is pressed else false

is_left_pressed()

Returns true if left direction button is pressed else false

is_right_pressed()

Returns true if right direction button is pressed else false

is_up_pressed()

Returns true if up direction button is pressed else false

is_down_pressed()

Returns true if down direction button is pressed else false

Module Assets

The module Assets handles assets that the game includes. An asset is something that is used in the game but is not code. It can be a bitmap, a background image, sound effect or music. It can be reused multiple times.

get(name)

Returns the asset with the name defined by the input parameter name.

Objects

An object can be of different types but they all share some basic features. Every frame the system parses the allocated objects in the scene and renders them to the screen. The following paragraph explains the common set of functions that every object share and the next paragraph each specific functions for individual object types are explains.

Common functions for all type of objects

set_position(x, y)

Set the world position of the object

x, y = get_position()

Returns the position of the object

x, y = move(dx, dy)

Moves the object relative to the current position. Returns the new position.

set_update(function(delta))

Set the update function for the object. The update function is called approx every 20 ms. The parameter delta contains the time in seconds passed after the previous call to update.

Module Sprite

Sprite is a type of Object and is used for representing movable objects in the scene such as the player and bullets.

new(bitmap_asset, x, y)

Creates a new sprite and adds it to the scene.

Module Text

Text is a type of Object and represent a string displayed on the screen. It can be used for showing the score or other textual information to the user. The actual text is limited to max 32 characters.

new(size, color, x, y, text)

Creates a new text object and adds it to the scene.

set_text(string)

Set the text.

set_color(color)

Set the color in RGB565.

set_alignment(string, string)

Set the text alignment. First parameter sets the horizontal alignment and can be ”left”, ”center” or ”right”. The second parameter sets the vertical aligment and can be one of the following values ”top”, ”middle” or ”bottom”.

set_size(int)

Set the size of the font. The following values are valid 8, 12, 16, 20 or 24.

Examples

Hello, world-example

A very simple example how to draw a sprite and move it across the screen. This example uses the update function of the scene.

Scene.init()
Scene.set_color(0xffff)
player = Sprite.new(Assets.get("player"), 0, 64)
Scene.set_update(function (delta)
    local x, y = player:move(10*delta,0)
    if x > 128 then 
       player:set_position(0,64)
    end
  end)

Move sprite using the gamepad

A compact example showing how to move a sprite by reading the gamepad.

Scene.init()
Scene.set_color(0xffff)
player = Sprite.new(Assets.get("player"), 64, 64)
speed = 50
player:set_update(function(delta)
  if Gamepad.is_left_pressed() then player:move(-speed*delta,0) end 
  if Gamepad.is_right_pressed() then   player:move(speed*delta,0) end
  if Gamepad.is_up_pressed() then player:move(0,-speed*delta) end 
  if Gamepad.is_down_pressed() then player:move(0,speed*delta) end
end)