Programming Macaw OS

Make a game: Seed Catcher

In Seed Catcher, seeds fall from the sky and you move a macaw along the ground to catch them. Miss three and the game is over. The finished game is about a hundred lines. Each step below leaves you with a program that runs, so try it as you go.

Open Terminal and type new seeds.lua. The new file opens in Code. Delete everything the template put in it, because you'll write the whole program yourself.

1. A window and the sky

WINDOW = { w = 240, h = 160, title = "Seed Catcher", realtime = true }

local GROUND = 148          -- the grass starts here

function _draw()
  cls(colors.sky)
  rectfill(0, GROUND, WINDOW.w, WINDOW.h - GROUND, colors.green)
end

Press Ctrl+R to run it. realtime = true tells Macaw OS to draw the window every frame. A game needs this because things move all the time. cls fills the window with a colour, and rectfill(x, y, w, h, colour) draws a filled rectangle. Here it draws the grass, from GROUND to the bottom of the window.

2. The macaw

You can make a picture from text. Each character is a pixel, and a table says which colour each character stands for. A . is see-through. Put this under the GROUND line, because the macaw's position uses it:

local BIRD = parseimage({
  "...rr...",
  "..rrrr..",
  ".rrkrry.",
  ".rrrryy.",
  "rrrrrr..",
  "bbrrrr..",
  ".bbrr...",
  "..b..b..",
}, { r = colors.red, y = colors.yellow, b = colors.blue, k = colors.night })

local bird = { x = 116, y = GROUND - 16, flip = false }

Draw it in _draw, after the grass:

  blit(BIRD, bird.x, bird.y, 0, 2, bird.flip)

blit(picture, x, y, see-through colour, scale, flip) draws the picture. The macaw is 8x8, drawn at twice the size, so it covers 16x16 pixels. Colour 0 (the dots) isn't drawn at all. For bigger pictures, draw them in Paint and load them with loadimage.

3. Moving

_update runs 60 times a second. btn says whether a key is held down:

function _update()
  if btn("left") then bird.x = bird.x - 2; bird.flip = true end
  if btn("right") then bird.x = bird.x + 2; bird.flip = false end
  bird.x = clamp(bird.x, 0, WINDOW.w - 16)
end

clamp keeps the macaw inside the window. flip turns it to face the way it's moving.

4. Seeds

The seeds are kept in a list. Every so often a new seed starts just above the window at a random place. Each frame, every seed falls a little. Put this under the macaw, above _update:

local seeds = {}
local timer = 0

local function drop_seed()
  add(seeds, { x = 4 + rnd(WINDOW.w - 8), y = -4, speed = 0.6 + rnd(0.6) })
end

In _update:

  timer = timer - 1
  if timer <= 0 then
    drop_seed()
    timer = 40 + rnd(30)            -- the next one in 40 to 70 frames
  end
  for s in all(seeds) do
    s.y = s.y + s.speed
  end

And in _draw, before the macaw:

  for _, s in ipairs(seeds) do circfill(s.x, s.y, 2, colors.brown) end

rnd(n) gives a random number from 0 up to n. add puts something on the end of a list. all goes through a list in a way that lets you take things out of it as you go, which the next step needs.

5. Catching

A seed is caught when it's inside the macaw's 16x16 square. Change the seed loop to this:

  for s in all(seeds) do
    s.y = s.y + s.speed
    if s.y > bird.y and s.y < bird.y + 16 and s.x > bird.x and s.x < bird.x + 16 then
      del(seeds, s)
      score = score + 1
      sfx("coin")
    elseif s.y > GROUND then
      del(seeds, s)
      lives = lives - 1
      sfx("hit")
    end
  end

Put local score, lives = 0, 3 at the top of the file, and show both in _draw:

  text("score " .. score, 4, 4, colors.navy)
  for i = 1, lives do circfill(WINDOW.w - 8 * i, 7, 3, colors.red) end

del takes the seed out of the list. sfx plays one of the built-in sounds: blip, coin, jump, hit, lose, squawk, rawk and chime.

6. Game over, and again

When lives reaches 0, the game stops and waits for Enter. Put everything a new game needs in one function, so that starting again only takes one call:

local function restart()
  bird = { x = 116, y = GROUND - 16, flip = false }
  seeds = {}
  score, lives, timer, over = 0, 3, 0, false
end

restart now gives the variables their values, so they only need declaring. Replace local bird = ..., local seeds = {}, local timer = 0 and local score, lives = 0, 3 with the one line below. Put it straight under the GROUND line, above everything that uses the variables, including restart. _init starts the first game:

local bird, seeds, score, lives, timer, best, over

function _init()
  restart()
end

In the seed loop, after sfx("hit"), end the game when the last life is gone:

      if lives == 0 then
        over = true
        sfx("lose")
      end

At the start of _update, wait for a key while the game is over:

  if over then
    if btnp("enter") or btnp("space") then restart() end
    return
  end

btnp is true in the frame a key goes down, and again now and then as the key repeats while it's held. btn is true every frame the key is down. With btnp, one press starts one game. To show the message at the end of _draw, use centertext, which writes text centred on an x position. Its last number makes the text bigger:

  if over then
    rectfill(40, 56, 160, 40, colors.night)
    centertext("Game over!", 120, 64, colors.yellow, 2)
    centertext("Enter plays again", 120, 84, colors.white)
  end

7. A best score that stays

Files in /home are kept after the program quits. Read the best score when the game starts. Put best = load_best() in _init, before restart(), and put this above _init:

local function load_best()
  return tonumber(fs.read("/home/seedcatcher.txt") or "") or 0
end

Show it under the score in _draw with text("best " .. best, 4, 14, colors.navy). Then save it when it's beaten, inside if lives == 0 then, after sfx("lose"):

        if score > best then
          best = score
          fs.write("/home/seedcatcher.txt", tostring(best))
        end

Finally, make the seeds fall faster as the score goes up by adding + score * 0.03 to their speed in drop_seed.

The whole game

Here is the complete program. It's also in /home/examples/seedcatcher.lua, and you can run it from Terminal with run /home/examples/seedcatcher.lua.

-- Seed Catcher: the game the programming guide's tutorial builds (macaw:docs/tutorial).
-- Seeds fall; the macaw catches them. Miss three and it's over.
WINDOW = { w = 240, h = 160, title = "Seed Catcher", realtime = true }

local BIRD = parseimage({
  "...rr...",
  "..rrrr..",
  ".rrkrry.",
  ".rrrryy.",
  "rrrrrr..",
  "bbrrrr..",
  ".bbrr...",
  "..b..b..",
}, { r = colors.red, y = colors.yellow, b = colors.blue, k = colors.night })

local GROUND = 148          -- the grass starts here
local bird, seeds, score, lives, timer, best, over

local function load_best()
  return tonumber(fs.read("/home/seedcatcher.txt") or "") or 0
end

local function restart()
  bird = { x = 116, y = GROUND - 16, flip = false }
  seeds = {}
  score, lives, timer, over = 0, 3, 0, false
end

function _init()
  best = load_best()
  restart()
end

local function drop_seed()
  -- seeds come faster as the score goes up
  add(seeds, { x = 4 + rnd(WINDOW.w - 8), y = -4, speed = 0.6 + rnd(0.6) + score * 0.03 })
end

function _update()
  if over then
    if btnp("enter") or btnp("space") then restart() end
    return
  end

  -- move
  if btn("left") then bird.x = bird.x - 2; bird.flip = true end
  if btn("right") then bird.x = bird.x + 2; bird.flip = false end
  bird.x = clamp(bird.x, 0, WINDOW.w - 16)

  -- a new seed every second or so
  timer = timer - 1
  if timer <= 0 then
    drop_seed()
    timer = 40 + rnd(30)
  end

  -- fall, get caught, or land
  for s in all(seeds) do
    s.y = s.y + s.speed
    if s.y > bird.y and s.y < bird.y + 16 and s.x > bird.x and s.x < bird.x + 16 then
      del(seeds, s)
      score = score + 1
      sfx("coin")
    elseif s.y > GROUND then
      del(seeds, s)
      lives = lives - 1
      sfx("hit")
      if lives == 0 then
        over = true
        sfx("lose")
        if score > best then
          best = score
          fs.write("/home/seedcatcher.txt", tostring(best))
        end
      end
    end
  end
end

function _draw()
  cls(colors.sky)
  rectfill(0, GROUND, WINDOW.w, WINDOW.h - GROUND, colors.green)
  for _, s in ipairs(seeds) do circfill(s.x, s.y, 2, colors.brown) end
  blit(BIRD, bird.x, bird.y, 0, 2, bird.flip)

  text("score " .. score, 4, 4, colors.navy)
  text("best " .. best, 4, 14, colors.navy)
  for i = 1, lives do circfill(WINDOW.w - 8 * i, 7, 3, colors.red) end

  if over then
    rectfill(40, 56, 160, 40, colors.night)
    centertext("Game over!", 120, 64, colors.yellow, 2)
    centertext("Enter plays again", 120, 84, colors.white)
  end
end

Where next