Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 14

The project: a “Catch” game

About 40 minutes · five stages

★The game from Chapter 0

Remember the game you saw in Chapter 0? A yellow apple falls, and a blue basket on the bottom row catches it. Now you know everything you need to write it. Not all at once: in five stages, and each stage is a program that works by itself.

Figure 29: the five stages. 1: a basket that moves with the arrows. 2: an apple that falls. 3: both together, and a check whether it was caught. 4: a score, and five apples. 5: each apple falls from a different place.

In each stage one line is missing, and there are three choices. Try the wrong ones too: they show what each line does. The last stage you will write by yourself.

1Stage 1: a basket that moves

The basket ($AF, blue) is on the bottom row. Its finger is Y. The left arrow moves it one step left, the right arrow one step right, and Q quits. And at the walls it stops: CMPY #$05E0 asks “am I at the left edge already?”.

There is nothing new here: it is the block moved with the arrows from Chapter 11, only on the bottom row.

2Stage 2: an apple that falls

The apple ($9F, yellow) starts on row 1, in column 16, and falls to the bottom row. Like the apple from the puzzle in Chapter 11. Only one thing is new: its finger is U, a third finger (purple). X is busy with the keyboard checker, Y with the basket. U works exactly like Y: LDU, STA ,U, LEAU, CMPU.

3Stage 3: both together, and caught or not?

The hard part: the basket has to move while the apple falls. But the keyboard loop from Chapter 10 waits for a key, and while it waits nothing moves. So on every lap the big loop does a little of everything:

  1. It draws the basket and the apple.
  2. It waits a short moment (LDX #800: about 7 thousandths of a second).
  3. It asks the keyboard once. Nothing pressed? No waiting: it carries on.
  4. An arrow pressed? It moves the basket.
  5. And only once every 25 laps: the apple goes down a row.

How do you know when 25 laps have gone by? You need a counter, and all the drawers are taken. So you keep it in memory, in a house of its own, after the fixed ending: TICK FCB 25. FCB is not a command for the processor. It tells DREAM: “Put one house here, with 25 in it.” DEC TICK takes 1 off it, straight in memory. You saw in Chapter 13: the program lives on the street, so its numbers can live next to it too.

And when the apple reaches the bottom row? Look at what is on the screen at the place where it lands: LDA ,U. Is there blue there ($AF)? It landed in the basket. Write Y (yes) or N (no) in the corner, and that’s it.

4Stage 4: a score, and five apples

Two more houses in memory: SCORE (the score) and LEFT (how many apples are left). At the start, CLR SCORE sets the score to zero. Every apple caught: INC SCORE. After every apple: DEC LEFT, and a new apple from the top. After five, the end.

The score shows in the corner as a digit: $70 is the digit 0 on the screen, so $70 plus the score is the digit of the score.

5Stage 5: each apple from a different place. This time on your own

▶On the real Dragon

What we learned: a game is built in stages, and each stage is checked by itself. One loop does a little of everything on every lap: it draws, waits a little, asks the keyboard without waiting, and moves.
FCB keeps a house in memory for a number (a counter, a score). U is a third finger. ANDB #31 cuts a number down to a column from 0 to 31.

A hobbyist tribute project, not affiliated with or endorsed by Dragon Data. The course, drawings and text are our own. Every program in the course was checked on the site's Dragon machine (XRoar running the BASIC we wrote).