
So far the only thing that moves is the mouse, and only when you tell it to. The cats need to move by themselves - back and forth along their platforms, turning round at the ends - without anybody pressing anything.
The idea is simple, and it is the same one gravity used: a cat has a direction as well as a position, and every pass the direction is added to the position.
Each cat gets one more number: D1 for the red cat, D2 for the magenta one. It is how far the cat moves each pass - 2 to the right, -2 to the left.
250 CX1=CX1+D1:IF CX1<=20 OR CX1>=94 THEN D1=-D1:SPRITE 1,CX1,CY1,8,138-D1
Move the cat by D1. If it has reached either end of its platform, turn it round by changing the sign of D1 - two becomes minus two, and back again.
The ends are worked out from the platform: it runs from x=20 to x=110, and a cat is sixteen pixels wide, so the cat's x can go from 20 to 94 before any of it hangs over the edge.
A cat walking left with its face on the right looks as if it is reversing. When a cat turns, the program swaps its picture too - that is the second half of line 250, which gives the sprite a new shape:
SPRITE 1,CX1,CY1,8,138-D1
138-D1 is a small piece of arithmetic doing the work of an IF: when D1 is 2 it gives 136, the cat facing right, and when D1 is -2 it gives 140, facing left. The rest of the time line 270 moves the cats with three numbers, keeping whichever way they are facing.
Shape 140 is the cat facing left. It would be tedious - and a chance to make mistakes - to draw it all again the other way round. The program makes it from the first one:
1060 FOR R=1 TO 16:T$=""
1070 FOR B=16 TO 1 STEP -1:T$=T$+MID$(P$(R),B,1):NEXT B
1080 P$(R)=T$:NEXT R
1090 S$="":GOTO 920
Each of the sixteen rows is still in P$ from the last picture read. Line 1070 builds the row backwards, one character at a time from the end, which reverses it - a mirror image. Line 1090 then jumps into the middle of the shape subroutine from S22, at the point just after it has read its rows, so the mirrored rows are turned into a shape exactly as a picture from DATA would be.
That GOTO into another subroutine is worth a second look. It works because the RETURN at the end of line 1040 returns from the GOSUB that called this routine - the program is simply borrowing the second half of S22's subroutine rather than copying it out.
The mouse has been walking backwards to the left since S24. The same mirror fixes it, as shape 144:
45 N=144:GOSUB 1060
It has to be line 45, straight after line 40 has read the mouse's picture and before line 50 reads the cat's - the mirror works on whatever picture was read last.
FC remembers which way the mouse is facing, and the ground subroutine changes the shape only when the mouse actually turns:
300 VX=0:IF K=64 THEN VX=-6:IF FC<>144 THEN FC=144:SPRITE 0,MX,MY,14,FC
310 IF K=32 THEN VX=6:IF FC<>132 THEN FC=132:SPRITE 0,MX,MY,14,FC
Walking left, FC is already 144 on every pass after the first, so the second IF is false and the shape is left alone. Only in the air does the mouse keep whichever way it faced when it jumped - which is right, since that is the way it is going.
Watch a cat walk over a piece of cheese. It passes straight over it, and the cheese is still there afterwards, untouched. That is S22's promise kept: a sprite is drawn in front of the picture and changes nothing in it.
Add lines 45, 55 and 177, the patrol at 240 to 280, and the mirror at 1050 to 1090, and change lines 90, 110, 290, 300, 310, 800, 810 and 820.
45 N=144:GOSUB 1060
55 N=140:GOSUB 1060
90 SPRITE 0,MX,MY,14,FC
110 SPRITE 2,CX2,CY2,13,140
177 GOSUB 250
240 REM -- THE CATS PATROL --
250 CX1=CX1+D1:IF CX1<=20 OR CX1>=94 THEN D1=-D1:SPRITE 1,CX1,CY1,8,138-D1
260 CX2=CX2+D2:IF CX2<=150 OR CX2>=224 THEN D2=-D2:SPRITE 2,CX2,CY2,13,138-D2
270 SPRITE 1,CX1,CY1:SPRITE 2,CX2,CY2
280 RETURN
290 REM -- ON THE GROUND: WALK, TURN OR JUMP --
300 VX=0:IF K=64 THEN VX=-6:IF FC<>144 THEN FC=144:SPRITE 0,MX,MY,14,FC
310 IF K=32 THEN VX=6:IF FC<>132 THEN FC=132:SPRITE 0,MX,MY,14,FC
800 MX=20:MY=22:G=-1:VX=0:VY=0:FC=132
810 CX1=60:CY1=63:D1=2
820 CX2=170:CY2=98:D2=-2
1050 REM -- MAKE SHAPE N FROM THE MIRROR IMAGE OF THE LAST PICTURE --
1060 FOR R=1 TO 16:T$=""
1070 FOR B=16 TO 1 STEP -1:T$=T$+MID$(P$(R),B,1):NEXT B
1080 P$(R)=T$:NEXT R
1090 S$="":GOTO 920
Starting from
LOAD "MOUSE9" from S26.
What you should see
After the screen has been drawn, both cats pace their platforms, turning round at each end and facing the way they are walking. The mouse still moves under your control, and can still eat cheese - and now it turns round when you walk it to the left.

The cats can walk straight through the mouse. Nothing happens yet: that is the next section.
Save this. SAVE "MOUSE10".
D1=2 in line 810 to D1=4. The red cat is faster - but watch the right-hand end of its platform closely. Where does it turn now, and why not at 94?...THEN D1=-D1. What does the red cat look like on the way back?16 TO 1 STEP -1 to 1 TO 16. What does the magenta cat look like now, and why?27.1 Make the magenta cat faster than the red one.
27.2 Put a third cat on the floor, patrolling the whole width of the screen. Remember the four-sprites-to-a-line rule from S22: could it ever be broken now?
27.3 Make a cat that does not just patrol but turns towards the mouse whenever the mouse is on its platform.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| A cat walks off the end of its platform | The limits in its IF are wrong for that platform. Right-hand limit is the platform's right edge minus 16. |
| A cat hangs a little over the end of its platform | Counting from where it starts, its steps do not land exactly on the limit, so it goes one step past before it turns. |
| A cat walks backwards | Its shape is not being changed when it turns. |
| The mouse turns into a cat when it walks left | Line 45 is after line 50, so it mirrored the cat's picture instead of the mouse's. |
| The mirrored cat is scrambled | The mirror loop reads the wrong rows - it must run straight after the picture it mirrors, while P$ still holds it. |
| The game is noticeably slower | Two more moving sprites each pass. S31 measures it. |
Something that moves on its own has a direction as well as a position, added every pass, and turned round by changing its sign. Changing a sprite's shape when it turns keeps it facing the way it walks. A mirror image is the same picture with each row read backwards, and a program can make it rather than you drawing it twice.
S28, Caught - what happens when a cat and the mouse meet.