Give your power pills more punch

Author(s): Tony Rickwood
Publication: LISTing Newsletter
Date: November 1994
Pages: 5-7

Tony Rickwood demonstrates how much faster a game will work when Basic is replaced by machine code in key areas.

IF YOU have followed this series on machine code so far, you will by now, be raring to go with some practical games applications. Most commercial games are 100% machine coded for speed and animation quality, but it would be much too ambitious for us to present and explain what might be an enormous assembler listing in these few pages.

A much more practical approach is to see how a traditional maze type game, written in Basic, can be made much more fun to play by replacing some of the routine basic programming with machine code. Admittedly, maze games are a bit old hat now, but the game I will be presenting here has been specially written to give readers a rare opportunity to see, at a glance, just how much better than Basic m/c really is.

At the touch of a key, you will be able to get your Spectrum to throw out a large chunk of Basic and call in a m/c routine instead. See for yourself how much more energy a diet of power pills and m/c will give Pacman and the four ghosts.

First, enter and run Program one which POKEs 199 bytes of machine code into memory, starting at location 64000 (the game is designed for the 48K Spectrum). As a wrong number may cause the computer to crash, a check is made to make sure data is entered correctly. When you have got the “data entry o.k.” message, SAVE program 1 to tape.

  10 REM Machine Code Setup for Ghostchase @ Tony Rickwood
20 CLEAR 63999: LET s=0: FOR i=64000 TO 64198: READ n: POKE i,n: LET s=s+n: NEXT i
30 READ sum: IF s <> sum THEN PRINT, "error in data entry"'"check line 40": STOP
40 DATA 97,107,98,108,99,109,100,110,101,111,102,112,103,113,104,114,120,121,97,98,99,100,101,102,103,104,0,221,33,0,250,6,8,205,153,250,126,87,221,35,205,153,250,114,221,35,16,241,205,153,250,70,221,35,205,153,250,78,221,35,221,126,0,254,0,200,205,153,250,94,221,35,205,153,250,86,221,43,120,147,40,28,56,11,28,205,169,250,254
50 DATA 6,32,14,29,24,15,29,205,169,250,254,6,32,3,28,24,4,205,153,250,115,221,35,121,146,40,28,56,11,20,205,169,250,254,6,32,14,21,24,15,21,205,169,250,254,6,32,3,20,24,4,205,153,250,114,221,35,205,169,250,54,6,24,163,197,42,75,92,1,200,0,221,126,0,237,177,35,35,193,201,213,197,33,0,88,75,90,22,0,6,5,203,35,203,18,16,250,123,137,48,1,20,95,25,126,193,209,201,209,201,22690
60 PRINT "Data entry o.k"'"m/c now ready for running": STOP
70 SAVE "GCODE" CODE 64000,199

Now enter Program two. When entering a long Basic program, I find that the best plan is to start by entering and testing the part which sets up the User Defined Graphics. This is contained in lines 6000 onwards. Now RUN this part (do not worry about the “RETURN WITHOUT GOSUB” message) and go into graphics mode (Caps/9). Pressing any letter from a to u will demonstrate that all 21 UDGs have been used. Graphic letters a to n are for the maze walls, o to u are for Pacman (moving in four directions), ghosts, power pills, and dots.

The next part to enter and check is the subroutine which sets up the screen (in this case, by printing the maze). Lines 5000-5040 do this. With the graphics already set up in memory, you will see the maze emerge as you enter the two PRINT statements, which will help you to correct any mistakes as they are made. Now type RUN 6000 to make sure that the maze is printed complete with dots and power pills. You should have everything in white on a black background.

As the way the program works depends on different objects having different colours, these are best put into the PRINT statements using colour control characters. The colours you MUST use are: WALLS=yellow, DOTS=white, POWER PILLS=cyan.

The way to put these codes into the PRINT statements is described in Chapter 16 of the Spectrum manual, but to get you started, first bring down line 5010 with edit. Run the cursor to just past the quotes and change to extended mode (caps shift/symbol shift). Now hold the Capitals key and press key 6. You will see that everything that follows is now printed in yellow ink. Now run the cursor right by one space (to just before the first dot), change cursor to Extended again, and press Caps/7 to change back to white ink. Continue throughout until everything is the correct colour.

IMPORTANT NOTE: The game depends on having these colours right. It also depends on the two exits from the maze being sealed to the ghosts (but not to Pacman), to allow Pacman to shake them off. This is done by putting an invisible wall at the two exits by printing the blank character positions to the left and right of the middle row of dots in yellow ink with the appropriate colour code in line 5010. You will see how this works when I explain ghost control.

Now you can enter the rest of the program and test it as a whole, although it would be a good idea to do an intermediate SAVE of your work so far.

  10 REM GHOSTCHASE @ Tony Rickwood
20 GO SUB 100
25 LET s=0: LET v=5
30 GO SUB 6000
45 GO SUB 5000
50 INPUT "Basic or m/c(b/m)?";g$
51 IF g$ <> "b" AND g$ <> "m" THEN GO TO 50
60 GO SUB 1000
100 REM Initialize control variable
110 LET a=3: LET b=1: LET c=27: LET d=1: LET e=3: LET f=19: LET g=27: LET h=19: LET x=15: LET y=10
120 LET k=a: LET l=b: LET m=c: LET n=d: LET o=e: LET p=f: LET q=g: LET r=h
130 RETURN
1000 REM Main Control Routine
1005 LET di=1: LET s=s+1
1010 LET xx=x: LET yy=y: PRINT AT yy,xx;" "
1020 PRINT INK 1; OVER 1; AT b,a;"T"; INK 2; AT d,c;"T"; INK 3; AT f,e;"T"; INK 4; AT h,g;"T": BEEP .005,45
1025 IF ATTR (y,x) <= 4 THEN GO TO 3000
1030 LET x=x-( INKEY$ ="5")+( INKEY$ ="8"): LET y=y+( INKEY$ ="6")-( INKEY$ ="7")
1035 LET di=di*(xx=x)*(yy=y)+(x>xx)+2*(x<xx)+3*(y<yy)+4*(y>yy)
1036 IF x=1 THEN LET x=28
1037 IF x=29 THEN LET x=2
1040 IF ATTR (y,x)=6 THEN LET x=xx: LET y=yy: GO TO 1070
1055 IF ATTR (y,x)=5 THEN LET s=s+10: FOR i=1 TO 10: BEEP .005,20: BEEP .005,30: NEXT i: GO TO 1070
1060 IF POINT (3+x*8,171-8*y)=1 THEN LET s=s+1: BEEP .005,30
1070 PRINT AT 21,8;s: PRINT AT y,x;"OPQR"(di)
1080 IF ATTR (y,x) <= 4 THEN GO TO 3000
1082 IF g$="m" THEN RANDOMIZE USR 64027
1090 IF g$="b" THEN GO SUB 4000
1100 PRINT OVER 1; AT l,k;"T"; AT n,m;"T"; AT p,o;"T"; AT r,q;"T"
1110 GO TO 1010
2000 REM Screen Cleared
2010 GO SUB 100
2020 GO SUB 5000
2030 GO SUB 1000
2040 RETURN
3000 REM Pacman eaten
3010 LET v=v-1: PRINT AT 21,28;v
3020 FOR i=1 TO 20: BEEP .01,20: BEEP .01,30: NEXT i
3030 IF v=0 THEN GO TO 3040
3035 PRINT INK 7; OVER 1; AT b,a;"T";; AT d,c;"T"; AT f,e;"T"; AT h,g;"T"
3036 GO SUB 100
3037 GO SUB 1000
3040 CLS : PRINT AT 10,0;"YOU SCORED ";s; AT 11,0;"ANOTHER GAME?(y/n)"
3050 IF INKEY$ ="" THEN GO TO 3050
3060 IF INKEY$ ="n" THEN STOP
3070 RUN
4000 REM Ghost Control
4010 LET k=a: LET l=b: LET m=c: LET n=d: LET o=e: LET p=f: LET q=g: LET r=h
4020 LET a=a+(x>a)-(x<a): IF ATTR (b,a)=6 THEN LET a=k
4030 LET b=b+(y>b)-(y<b): IF ATTR (b,a)=6 THEN LET b=l
4040 IF a <> k OR b <> l THEN PRINT OVER 1; INK 6; AT b,a;" "
4050 LET c=c+(x>c)-(x<c): IF ATTR (d,c)=6 THEN LET c=m
4060 LET d=d+(y>d)-(y<d): IF ATTR (d,c)=6 THEN LET d=n
4070 IF c <> m OR d <> n THEN PRINT OVER 1; INK 6; AT d,c;" "
4080 LET e=e+(x>e)-(x<e): IF ATTR (f,e)=6 THEN LET e=o
4090 LET f=f+(y>f)-(y<f): IF ATTR (f,e)=6 THEN LET f=p
4100 IF e <> o OR f <> p THEN PRINT OVER 1; INK 6; AT f,e;" "
4110 LET g=g+(x>g)-(x<g): IF ATTR (h,g)=6 THEN LET g=q
4120 LET h=h+(y>h)-(y<h): IF ATTR (h,g)=6 THEN LET h=r
4140 RETURN
5000 REM Print Maze
5005 PAPER 0: BORDER 0: INK 7: CLS
5010 PRINT " CAAAAAAAAAAAANAAAAAAAAAAAAD"'" BSSSSSSSSSSSSBSSSS8SSSSSSSB"'" BSCADSCANAAISGSJAANADSCADSB"'" BSB BSB BSSSSSSSSSB BSB BSB"'" BSEAFSEAFSJAANAAISEAFSEAFSB"'" BSSSSSSSSSSSSBSSSSSSSSSSSSB"'" BSJAISJANAAISGSJAANAISJAISB"'" BUSSSSSSBSSSSSSSSSBSSSSSSUB"'" EAAAAADSBSCAISJADSBSCAAAAAF"'" AAAAAAFSGSBSSSSSBS[illegible]SEAAAAAA"'" SSSSSSSSSSBSSSSSBS8888SSSSS "
5020 PRINT ;" AAAAAADBHSBSSSSSB[illegible]HSCAAAAAA"'" CAAAAAFSGSEAANAAF[illegible][illegible]SEAAAAAD"'" BUSSSSSSSSSSSBSSS8SSSSSSSUB"'" BSJADSJAAAAISGSJAAAAISCAISB"'" BSSSBSSSSSSSSSSSSSSSSSBSSSB"'" KAISEAISHSJAANAAISHSJAFSJAL"'" BSSSSSSSBSSSSBSSSSBSSSSSSSB"'" BSJAAAAAMAAISGSJAAMAAAAAISB"'" BSSSSSSSSSSSSSSSSSSSSSSSSSB"'" EAAAAAAAAAAAAAAAAAAAAAAAAAF"
5030 PRINT AT 21,2;"SCORE"; AT 21,22;"LIVES ";v
5040 RETURN
6000 REM UDG Setup
6010 FOR i= USR "a" TO USR "u"+7: READ j: POKE i,j: NEXT i
6020 DATA 0,255,j,j,j,j,j,0
6030 DATA 126,j,j,j,j,j,j,j
6040 DATA 0,31,63,127,j,j,j,126
6050 DATA 0,248,252,254,j,j,j,126
6060 DATA 126,127,j,j,j,63,31,0
6070 DATA 126,254,j,j,j,252,248,0
6080 DATA 126,j,j,j,j,60,24,0
6090 DATA 0,24,60,126,j,j,j,j
6100 DATA 0,248,252,254,j,252,248,0
6110 DATA 0,31,63,127,j,63,31,0
6120 DATA 126,127,j,j,j,j,j,126
6130 DATA 126,254,j,j,j,j,j,126
6140 DATA 126,255,j,j,j,j,j,0
6150 DATA 0,255,j,j,j,j,j,126
6160 DATA 62,127,248,240,j,248,127,62
6170 DATA 124,255,31,15,j,31,254,124
6180 DATA 66,195,j,231,255,j,126,60
6190 DATA 60,126,255,j,231,195,j,66
6200 DATA 0,0,0,24,24,0,0,0
6205 DATA 60,126,219,255,255,255,165,165
6210 DATA 0,0,60,126,j,60,0,0
6220 RETURN
Program two: GHOSTCHASE listing (lines 10-6220)
Program two: GHOSTCHASE listing (lines 10-6220)

When RUNning the whole program, it will begin by asking whether you want Basic or M/C? This gives you the option of letting your Spectrum control the ghosts, using EITHER the Basic in lines 4000-4140 OR by using the machine code set up by Program one. They do exactly the same job, though with a remarkable difference in speed. To satisfy yourself that these are directly interchangeable, look at lines 1082/1090 which call the appropriate routine according to the response to the only prompt used. Make sure you give the program a thorough testing in each mode, although I guarantee you will not be using the Basic option for very long!

If you want to keep Ghostchase in your library of games, it would be a good idea to put the m/c itself (not Program one, which only sets it up) onto tape immediately after your dump of Program two. First, get a new dump of Program two onto tape with the following line added: 15 LOAD “GCODE” CODE. If you want auto start, then dump by typing SAVE “ghost” LINE 1. Assuming that the m/c is in memory, dump it onto tape using SAVE “GCODE” CODE 64000,199. LOADing “ghost” will now automatically LOAD the m/c and RUN.

A few words about playing the game, as I have not included instructions in the program. Pacman control is by cursor keys. As usual, eat as many dots and power pills (10 point bonus) without getting caught, though you do have five lives. A screenful (300 points) earns you another screenful.

Obviously, you want to know how the machine code works, because, unlike routines I have presented so far, this one is not portable. Special effects m/c routines seen previously can be used by any Basic program. They are portable in the sense that they do not depend on, or affect anything else, and are also relocatable to use any available memory space. The routine used here obviously does affect, and is affected by, the environment in which it operates. So, if you want to supercharge a chase routine of your own (not necessarily of the maze variety), you really must be able to understand it and adapt it to your own program.

The key to understanding the machine code is the Basic routine in lines 4000-4140. The principle is always to get the computer to calculate intelligently the new coordinates of the chaser(s) in order to place it nearer the character being chased. There are many levels of artificial intelligence which could be used (to prevent the chaser from staying blocked for example). Here, though, it is simply a matter of getting the four ghosts nearer without going through walls. Line 4010 starts by storing all the old coordinates as figure one:

Fig. one

           New/Current
Ghost        Coords        Coords
            x     y        x     y
Blue        a     b        k     l
Red         c     d        m     n
Magenta     e     f        o     p
Green       g     h        q     r

Pacman coordinates are called x,y. Before calculating new positions, current positions must be stored. This is to be able to erase old ghosts and for re-setting positions if a move is invalid. Lines 4020 and 4030 deal with x and y coordinates for the blue ghost (a,b). Using x>a and x<a for pacman to right or left of the blue ghost, a is increased or decreased by one.

Similarly for b in the vertical sense (note: x=a or y=b means no change). Both a and b must be tested separately to allow the ghost to move horizontally, vertically or diagonally toward Pacman. The only test for a valid move is that it does not collide with a wall.

No off screen tests are necessary because the ghosts can never get out of the maze. So all we need to test is ATTR(b,a)=6 to tell the computer that there is a (yellow) wall at the new position calculated for the blue ghost. If true, the position reverts back to k and/or l. Other ghosts are handled in the same way.

What about collision with Pacman and other ghosts? The “Pacman eaten” condition is picked up in the main control routine at line 1080. Collision with another ghost is a bit more subtle. We cannot test ATTR(b,a)<=4, for example, to see if there is another ghost already there, for the simple reason that no new ghosts can be printed until ALL new positions have been calculated. Line 4030 solves the problem for the blue ghost (4070 and 4100 for magenta and green). In effect, this line is saying that, if the blue ghost has been moved (a<>k) or (b<>l) then temporarily PRINT a piece of invisible wall OVER the new position (to preserve the dot) to prevent it from being occupied by another ghost. This is only temporary while computer control is inside the ghost control subroutine.

Soon after control is returned to the main program (starting at line 1000), the ghosts are printed and so overwrite the invisible wall. Obviously, no such device is required for the green ghost as this is the last to be moved.

Do try to understand thoroughly how the subroutine starting at line 4000 works. You will then have a much better chance of understanding the machine coded version which I will be explaining in the next article.

Transcription note: The capitals A-U in the maze strings stand for user-defined graphics.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.