Machine code from zero

A course for the Dragon 32 · Chapters 0 to 15

Chapter 11

Movement: a cartoon

About 25 minutes · Erase, move, draw, wait

1How does something move on a screen?

It doesn’t really move. Like in a cartoon, you draw a picture, wait a moment, erase it, and draw it again one step further on. When this happens fast enough, the eye sees movement.

Figure 25: four pictures of the same block, and one lap of the track: draw, wait, erase, move, and again.

That is exactly what we already have: a loop (Chapter 9), a waiting loop (Chapter 9), and a finger Y that moves.

2A block that drives to the wall

The block sets off from the left edge of row 8, and drives right until the wall. CMPY #$051F asks “have you reached the last square in the row?”, and if so, BEQ KEY stops there.

Try the three versions. Without the wait the block “jumps” to the end, because the whole drive takes less than a thousandth of a second. And without erasing, the block leaves a trail behind it: all the pictures stay on the screen.

3A block on the arrows

The same idea, only instead of waiting for the clock, we wait for a key. On every press: erase, move by the arrow, and draw. Press the arrows under the screen (or on your own keyboard).

4A bouncing ball

Now the ball doesn’t stop at the wall: it turns round. For that we keep the direction in drawer B: 1 = right, -1 = left. LEAY B,Y moves finger Y by whatever is in B. At the right wall we put -1 in B, and at the left wall 1.

And the program stops when you press a key: it asks the keyboard once on every lap, and doesn’t wait. If nothing was pressed, it keeps running. If something was, RTS: back to DREAM.

-1 in a drawer is $FF. When B is used as a distance (B,Y), the processor reads numbers from $80 upwards as negative numbers: $FF = -1, $DF = -33. That is lamp 128 from Chapter 8, again.

5Further on: a ball all over the screen

A ball bouncing between four walls needs two directions: left-right, and up-down too. In the folder there is a program like that, “BALL — bouncing ball”, which already runs on our Dragon machine. It uses a few tools we haven’t learned yet, like putting a drawer aside for a moment. We’ll learn that in the next chapter.

?Puzzles: movement

In the movement puzzles we also check the time: if everything is over in less than a second on the Dragon, the eye won’t see movement, and it doesn’t count.

▶On the real Dragon

What we learned: movement = draw, wait, erase, move, and again. Without the wait it is too fast for the eye; without erasing, a trail is left.
The direction sits in a drawer: LDB #1 or LDB #-1, and LEAY B,Y. At the wall you flip B.

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).