Rendering Functions¶
These functions handle all visual output: clearing the screen, drawing sprites, tilemaps, and geometric shapes.
clear¶
- clear(colorIndex)¶
Clear the entire screen with a palette color.
- Parameters:
colorIndex (number) – Palette index used as the background color.
Call this once at the start of
_draw()to reset the screen before drawing the new frame.Invalid palette indexes raise an error in the output panel.
clear(0) -- black background clear(12) -- light blue background
sprite¶
- sprite(index, x, y, width, height)¶
Draw one sprite or a rectangular block of sprites from the sprite sheet.
- Parameters:
index (number) – Sprite index in the sprite sheet (0–255).
x (number) – Destination x position in pixels.
y (number) – Destination y position in pixels.
width (number) – Width in sprite units (1 unit = 8 pixels).
height (number) – Height in sprite units (1 unit = 8 pixels).
Use
width = 1andheight = 1for a single 8 x 8 sprite. Larger values draw a block of tiles from the sprite sheet. Positions are floored to whole pixels by the renderer.sprite(0, 10, 20, 1, 1) -- single sprite at (10, 20) sprite(32, 40, 50, 2, 1) -- 2-sprite-wide strip starting at index 32 sprite(0, x, y, 1, 2) -- 1 wide, 2 tall (8x16 px character)
map¶
- map(x, y)¶
Draw the tilemap created in the map editor.
- Parameters:
x (number) – X position in pixels where the map starts.
y (number) – Y position in pixels where the map starts.
This draws the entire map at once. The map uses the same 8 x 8 tile size as the sprite sheet.
map(0, 0) -- draw the map at the world origin
To read map tiles from Lua, use
mget(). To inspect sprite flags on a tile sprite, usefget().
camera¶
- camera(x, y)¶
Set the camera offset. All subsequent drawing calls are shifted by this offset.
- Parameters:
x (number) – Horizontal camera offset in pixels.
y (number) – Vertical camera offset in pixels.
A common pattern is to center the camera on the player inside
_draw(). Callcamera(0, 0)to reset the camera.-- Center on the player (320x180 screen) camera(player.x - 160, player.y - 90)
line¶
- line(colorIndex, x0, y0, x1, y1)¶
Draw a straight line between two points.
- Parameters:
colorIndex (number) – Palette color index.
x0 (number) – Start x position.
y0 (number) – Start y position.
x1 (number) – End x position.
y1 (number) – End y position.
line(8, 0, 0, 319, 179) -- diagonal across the screen
rect¶
- rect(colorIndex, x, y, width, height)¶
Draw an outlined (unfilled) rectangle.
- Parameters:
colorIndex (number) – Palette color index.
x (number) – Top-left x position.
y (number) – Top-left y position.
width (number) – Width in pixels.
height (number) – Height in pixels.
rect(7, 20, 20, 32, 16) -- white outline rectangle
fill_rect¶
- fill_rect(colorIndex, x, y, width, height)¶
Draw a filled rectangle.
- Parameters:
colorIndex (number) – Palette color index.
x (number) – Top-left x position.
y (number) – Top-left y position.
width (number) – Width in pixels.
height (number) – Height in pixels.
fill_rect(11, 0, 160, 320, 20) -- green ground strip fill_rect(8, 30, 40, 10, 10) -- small red square