Game carts
A cart is a whole game in one folder. It holds the code, a sheet of sprites, a tile map, sound effects and songs. Macaw OS has an editor for each part. When the game is finished, you can pack the cart into a single .cart file (at most 256 KB) that anyone can double-click to play.
Make one
In Terminal:
newcart jumper
This makes /home/carts/jumper with a small platform game in it, and opens its main.lua in Code. Press Ctrl+R to play it. Code's Cart menu opens the cart's editors:
| Editor | What it edits | The file |
|---|---|---|
| Sprites | 256 sprites of 16x16 pixels, and 8 flags for each | gfx.pxl, gfx.flags |
| Map | Layers of tiles, where each tile is a sprite number | map.map |
| SFX | 64 sound effects | sfx.sfx |
| Music | Songs | music/*.song |
cart.info holds the cart's title, its author and a short description:
return {
title = "Jumper",
author = "me",
about = "Jump between the platforms.",
}
Sharing it
| In Terminal | What it does |
|---|---|
pack jumper | Packs /home/carts/jumper into /home/carts/jumper.cart |
unpack jumper.cart | Unpacks a cart into a folder in /home/carts, so you can look inside or change it. It won't unpack over a folder that's already there |
Double-click a .cart in Files to play it. The games in /games are cart folders too. Right-click one in Files and choose Show Contents to see how it's made.
Sprites
Sprite 0 is the top-left 16x16 square of the sheet. Sprite 1 is the next one along, and sprite 16 is the first sprite in the second row. Colour 0 is see-through.
| Call | What it does |
|---|---|
spr(n, x, y, [w], [h], [flipx], [flipy]) | Draws sprite n. w and h draw a block of sprites, so 2, 2 draws a 32x32 sprite made of four |
sspr(sx, sy, sw, sh, x, y, [scale], [flipx], [flipy]) | Draws any rectangle of the sheet, given in pixels |
sprimage(n, [w], [h]) | Returns a sprite as its own picture, so you can scale it or use it as a 3D texture |
fget(n, [flag]) | Returns sprite n's flags (0-255), or whether one flag (0-7) is set |
fset(n, [flag], value) | Sets the flags, or one flag |
Flags mean whatever you decide. The template's game uses flag 0 to mark solid tiles:
-- is the tile at this pixel solid?
local function solid(x, y)
return fget(mget(x // 16, y // 16), 0)
end
The map
The map is a grid of tiles, and each tile is a sprite number (0 is empty). A map can have several layers, such as a background, the ground and things in front. You refer to a layer by its number or its name.
| Call | What it does |
|---|---|
map([cx, cy], [x, y], [cw, ch], [layer]) | Draws cw x ch tiles of the map, starting from tile cx, cy, at pixel x, y. With no arguments it draws the whole map at 0, 0 |
mget(x, y, [layer]) | Returns the tile at x, y |
mset(x, y, n, [layer]) | Sets the tile at x, y to n |
mapsize([layer]) | Returns how many tiles the map has across and down |
The map is usually bigger than the window, so scroll it with camera:
function _draw()
cls(colors.sky)
camera(cam_x, 0) -- everything after this is drawn moved left by cam_x
map()
spr(1, player.x, player.y)
camera() -- back, for the score
text(score, 4, 4, colors.white)
end
map only draws the tiles that are inside the window, so a big map doesn't slow it down.
Maps made in Tiled come in with the Map Editor's File > Import Map (or by dropping the .tmx and its picture on it): Tiled's tile n becomes sprite n, cut from the tileset's picture into the sprite sheet. Keep Tiled's Tile Layer Format as CSV. A picture of a whole level comes in with File > Import Map from Picture: it's cut into tiles, and each different one becomes a sprite. Your own code can do the same:
| Call | What it does |
|---|---|
importmapimage(path, [tilesize]) | A picture of a level (a PNG) cut into tiles tilesize across (16 if you don't say; others are made 16 x 16): each different tile becomes a sprite, numbered in the order they're first met, and the same tile again is the same sprite. See-through tiles are 0. Returns the same as importmap |
importmap(path) | A Tiled map (.tmx, .json) or a CSV of tile numbers. Returns the map (layers as newmap makes them), a 256 x 256 picture of its tiles in their sprites' places (or nil), which sprites they fill (used[n] = true), and notes on what was left out; or nil and why |
Sound and music
| Call | What it does |
|---|---|
sfx(n, [voice]) | Plays the cart's sound effect n (0-63) |
sfx(-1) | Stops all sound effects |
music(name, [loop]) | Plays music/name.song from the cart. A number works as a name too, so music(1) plays music/1.song |
music(-1) | Stops the music |
sfx("coin") with a name still plays the built-in sounds. music.volume() and the other music chip calls work in carts too.
Handy for games
These calls work in carts and in every other program:
| Call | What it does |
|---|---|
rnd(n) | Returns a random number from 0 up to n. rnd() returns one from 0 to 1, and rnd(list) returns a random item from the list |
flr(n), ceil(n) | Rounds n down or up to a whole number |
mid(a, b, c) | Returns the middle value of the three. mid(0, x, 100) keeps x between 0 and 100 |
clamp(v, lo, hi) | Keeps v between lo and hi. It does the same job as mid, with the value first |
sgn(n) | Returns -1 if n is below 0, otherwise 1 |
lerp(a, b, t) | Returns a value part way from a to b, where t goes from 0 to 1 |
round(n) | Rounds n to the nearest whole number |
add(list, v) | Puts v on the end of a list |
del(list, v) | Takes v out of a list |
all(list) | Goes through a list with for v in all(list). You can safely del items as you go |