Drawing
Everything you draw goes into your window. The top-left pixel is 0, 0 and the bottom-right pixel is WINDOW.w - 1, WINDOW.h - 1. Put your drawing calls in _draw.
Shapes
function _draw()
cls(colors.navy) -- fill the window
rectfill(10, 10, 60, 30, colors.orange) -- x, y, width, height
rect(10, 50, 60, 30, colors.white) -- just the outline
circfill(120, 40, 20, colors.yellow) -- centre x, y, radius
line(0, 100, 239, 100, colors.lime)
tri(160, 120, 200, 60, 230, 120, colors.pink)
pset(5, 5, colors.red) -- one pixel
end
| Call | What it does |
|---|---|
cls(c) | Fills the window with colour c |
pset(x, y, c) | Sets one pixel to colour c |
pget(x, y) | Returns the colour of a pixel |
line(x0, y0, x1, y1, c) | Draws a line |
rect(x, y, w, h, c) | Draws a rectangle's outline |
rectfill(x, y, w, h, c) | Draws a filled rectangle |
box(x, y, w, h, fill, [outline]) | Draws a filled rectangle with an outline |
circ(x, y, r, c) | Draws a circle's outline |
circfill(x, y, r, c) | Draws a filled circle |
tri(x0, y0, x1, y1, x2, y2, c) | Draws a filled triangle |
Text
Each character of the font is 5x8 pixels in a 6x9 cell. CHAR_W and CHAR_H hold the cell's size.
| Call | What it does |
|---|---|
text(s, x, y, c, [scale]) | Writes s with its top left at x, y, and returns the x just after it |
textwidth(s, [scale]) | Returns how wide s would be, in pixels |
centertext(s, x, y, c, [scale]) | Writes s centred on x |
shadowtext(s, x, y, c, [shadow], [scale]) | Writes s with a shadow one pixel down and to the right |
wraptext(s, columns) | Breaks s into lines of at most columns characters and returns them as a list |
Because text returns where it stopped, you can write in several colours along one line:
local x = text("Score: ", 4, 4, colors.white)
text(score, x, 4, colors.yellow)
Colours
You can use 256 colours at once, numbered 0 to 255.
- 0 to 31 have names, such as
colors.black,colors.redandcolors.sky. The full list is below. 0 is black, and it's the colour you usually pass toblitas the see-through colour. - 32 to 47 are the logo's colours.
- 48 to 63 are greys, from black to white.
- 64 to 159 are sixteen hues, each in six bright shades from dark to light.
- 160 to 255 are the same hues in six muted shades, which give you browns, skin tones and olives.
The 32 names are black, night, slate, steel, mist, white, brown, tan, sand, red, orange, yellow, green, blue, purple, pink, maroon, rust, cream, forest, lime, navy, sky, plum, lavender, teal, mint, charcoal, darkgrey, grey, silver and paper.
| Call | What it does |
|---|---|
nearestcolor(r, g, b) | Returns the closest of the 256 colours to an RGB colour (each part 0-255) |
pal(c) | Returns the r, g, b that colour c shows as now |
pal(c, r, g, b) | Changes what colour c looks like, everywhere on the screen |
pal() | Puts every colour back to normal |
drawpal(c1, c2) | Draws colour c1 as c2 from now on. Use it to flash a sprite white, for example |
drawpal() | Stops swapping colours |
Pictures
A picture (an image) is a grid of colours. You can draw it into your window, and you can draw into it.
local ship = loadimage("/home/ship.pxl") -- made in Paint
local dot = parseimage({ ".9.", "999", ".9." })
function _draw()
cls(0)
blit(ship, 50, 40, 0) -- colour 0 is see-through
blit(dot, 10, 10, 0, 4) -- four times as big
end
| Call | What it does |
|---|---|
image(w, h) | Makes a new picture filled with colour 0 |
loadimage(path) | Loads a picture saved by Paint (.pxl) |
saveimage(img, path) | Saves a picture as .pxl |
parseimage(rows, [map]) | Makes a picture from text, one character per pixel. map says what colour each character is. Without map, . is 0, and 0-9 and a-v are colours 0-31 |
decodeimage(png, [maxw], [maxh]) | Makes a picture from PNG data, shrunk to fit inside maxw x maxh |
importpicture(png, [maxw], [maxh]) | A PNG made elsewhere, for editing: each pixel the nearest of the 256 colours, see-through parts colour 0, made smaller only to fit maxw x maxh (256 x 256 if you don't say). Returns the picture and its own width and height, or nil and why |
decodeimage(png, maxw, maxh, nil, false, true) | The same for pixel art (a sprite): see-through pixels stay see-through (colour 0), and the others take the nearest colours without dithering, so the picture doesn't shimmer as it moves |
loadtexture(path, [size]) | Loads a photo texture (.tex) for 3D. It's at most 64 pixels across, or size if you give one (up to 1024) |
blit(img, x, y, [key], [scale], [flipx], [flipy]) | Draws a picture. key is a colour to leave out, usually 0 |
blitpart(img, sx, sy, sw, sh, x, y, [key], [scale], [flipx], [flipy]) | Draws part of a picture, such as one frame from a sheet |
Each picture also has its own calls:
| Call | What it does |
|---|---|
img:get(x, y) / img:set(x, y, c) | Reads or sets one pixel |
img:size() | Returns the width and height. img:width() and img:height() return one each |
img:clear([c]) | Fills the picture with one colour |
img:copy() | Makes a new picture the same as this one |
img:getrgb(x, y) / img:setrgb(x, y, r, g, b) | Reads or sets a pixel's true colour, which shows when the screen is in thousands of colours |
Drawing into a picture
target(img) sends drawing into a picture instead of your window, and target() sends it back to the window. Use it to build something once and draw it many times, such as a background or a minimap:
local bg = image(240, 160)
target(bg)
cls(colors.sky)
for i = 1, 40 do circfill(rnd(240), rnd(100), 2 + rnd(6), colors.white) end
target()
function _draw()
blit(bg, 0, 0)
end
target returns where you were drawing before, so local was = target(img) ... target(was) goes back to exactly that place. targetsize() returns the size of whatever you're drawing into.
Scrolling and clipping
| Call | What it does |
|---|---|
camera(x, y) | Moves everything drawn after this by -x, -y. Use it to scroll a big world |
camera() | Puts the camera back to 0, 0 |
clip(x, y, w, h) | Limits drawing to this rectangle |
clip() | Lets you draw anywhere again |
The screen
The screen starts at 640x360 with 256 colours. People can change it in the Monitors section of System Preferences, to 640x480, 800x600 or 1024x768, and to 8 colours, 256 or thousands.
| Call | What it does |
|---|---|
SCREEN_W, SCREEN_H | The screen's current size |
display.current() | Returns the screen's width, height and colours ("8", "256" or "thousands") |
display.sizes() | Returns the sizes the screen can be, as { {640, 360}, ... } |
display.request(opts, fn) | Asks for a different size or number of colours |
A game that wants a bigger screen can ask for one. The person sees Allow, Always Allow and Don't Allow buttons. If they allow it, the screen changes back when your program quits:
display.request({ width = 800, height = 600, colors = "thousands" }, function(ok)
if not ok then print("staying as we are") end
end)
_display(w, h, colours) is called whenever the screen changes. With thousands of colours, photos and 3D show in true colour. With 8 colours, everything is dithered to black, red, green, yellow, blue, magenta, cyan and white.