PAC-81 for Sinclair ZX-81
by Philip Harwood
from Your Computer, March 1984

Philip Harwood with a derivation of the game we all know and love.


Pac-81 allows a 16K ZX-81 to be used to play an almost authentic
version of the popular arcade game Pac-Man. Four independent ghosts
which will follow patterns, move randomly and actually chase you; with
random fruit appearing, flashing ghosts, eyes which run back to the
nest when ghosts are eaten, power pills, on-screen scoring, high
score, extra man, eight levels of play ranging from slow and easy to
very fast and nearly impossible to escape from the ghosts.

The game is just under 3K of machine code, and with a little patience
and persistence should not pose any real problems. The code is entered
in hexadecimal and can be written, checked and listed by the hex
loader given.

The machine code is stored in REM statements at the beginning of the
program. This means that the start address of the machine code is
16514. Ten REM statements are used and these should be created first
using any characters so that they have lengths according to Table 1.
The edit facility can be useful here and could save time. Also in
Table 1 are the first and last addresses for each of the REM
statements; this allows the length to be checked by POKEing these
addresses with 128, listing the line and seeing that black squares are
now at either end of them. For example, create a line:
	1 REM with 378 characters
now
	POKE 16514,128 and POKE 16891,128
LIST 1 should show black squares in the first and last positions of
this statement. If not then edit the line accordingly. Repeat this for
all 10 lines. 

Now enter in the hex loader; this allows the code to be entered and
checked, line by line. Enter the code for REM 1 and SAVE all this onto
a cassette. It is worthwhile always saving machine code before calling
it, as correcting typing errors is quicker than retyping the whole
statement should the program crash.

The program should be entered one REM statement at a time and tested.
Table 2 shows how to test each routine. Enter the corresponding lines
of BASIC and RUN 100. Keep to this line numbering as it gradually
builds up a program showing how the program is developed. If a line
number is duplicated, it means that it is replacing a previous command.

REM 1 creates the screen display of the maze and is drawn by the
single RAND USR 16842 statement. If it does not make a complete maze
then a mistake has been made, so check the data.

Moving the man is dealt with in REM 2. Enter this data and test the
program - do not delete line 100. Run 100 and a circle should appear;
this is your man. He is controlled by the keys: A - Up, Z - Down, N -
Left and M - Right. The movement here is very quick, but it will be
slowed down in the main game.

Moving the ghosts is done in two stages, REMs 3 and 4. Enter REM 3.
When the test program is RUN, one ghost - an inverse zero - should
rapidly move to the top right-hand corner of the maze and circle about
here until the break key is pressed.

The controlling of the ghosts is held in REM 4; enter this code.
Before running also enter the data for REM 9 - addresses 18790 to
18980 - this will reset all the ghosts and the man to their original
positions. Enter and run the test program for REM 4; note that most of
the previous test routines are either changed or deleted. All four
ghosts should now move about the maze; one should move at half the
speed of the others, one should move randomly, one should home in on
the man while the other follows a pattern.

If two of the ghosts cross each other a shadow is left; this is erased
in REM 5; the score should also appear. Things may seem a little odd
at the moment but, providing it does not actually crash, press on.

REM 6 brings in the gimmicks. When a ghost is eaten, the eyes rush
back to the nest to find a new body. If an inverse zero lands on you,
the game should stop.

Fruit - or as best as Sinclair graphics allow - flashing ghosts, extra
man at 10,000 points, and half speed movement through the tunnels,
along with a flashing screen when all the dots are eaten, are added in
REM 7.

To complete the game, add REMs 8 and 10; REM 9 should already be in.
Delete all the previous BASIC tests, except the initial REM statements,
line by line. RAND USR 19011 should play the complete game. The zero
key can be used as a break key during most of the game. If this works,
delete the hex loader and add the following:
	 50 GOTO 100
	 70 SAVE "PAC"
	100 RAND USR 19011
RUN 70 to SAVE the game; this will auto-RUN the program upon loading.
If a title page is required POKE 19224,201. The program now returns to
BASIC at the end of every game, where a title page in BASIC can be
added. To play the game just call RAND USR 19011.


Table 1. Lengths of REM statements.

  Rem  Length  Start add  End add  Main topic
---------------------------------------------------------------
    1    378     16514     16891   draw the maze
    2    198     16898     17095   move the man
    3    176     17102     17277   move a ghost
    4    393     17284     17676   control the ghosts
    5    227     17683     17909   subroutines 1
    6    351     17916     18266   return eyes to nest
    7    239     18273     18511   subroutines 2
    8    266     18518     18783   main controller
    9    191     18790     18980   reset variable
   10    240     18987     19226   end routines


Table 2. Test routines.

REM 1
 100 RAND USR 168842
REM 2
 101 POKE 16898,98
 102 POKE 16899,2
 103 POKE 16900,3
1010 RAND USR 17002
4000 GOTO 1010
REM 3
 111 POKE 17102,123
 112 POKE 17103,1
 113 POKE 17105,1
 114 POKE 17108,0
 115 POKE 17109,156
1020 RAND USR 17205
REM 4
(Enter REM 9 before running)
delete 101,102,103
 111 POKE 18891,201
 112 RAND USR 18853
 113 POKE 17648,53
 114 POKE 17649,67
 115 POKE 17292,1
 116 POKE 17306,2
 117 POKE 17320,3
 118 POKE 17334,4
1020 RAND USR 17598
REM 5
 111 POKE 18891,205
 113 POKE 17648,92
 114 POKE 17649,69
1025 RAND USR 17790
REM 6
 120 POKE 17291,139
 121 POKE 17305,139
1015 RAND USR 17917
1025 RAND USR 17917
1050 IF PEEK 17916=1 THEN STOP
REM 7
 130 POKE 18430,3
 132 POKE 18431,0
 135 POKE 18468,1
1030 RAND USR 18274
1040 RAND USR 18327
1045 RAND USR 18432
1047 RAND USR 18469
1060 RAND USR 18400
1065 IF PEEK 18273=1 THEN RAND USR 17832
REMs 8 and 10
delete all previous BASIC tests
 100 RAND USR 19011


Modification from YC Jun'84 (not included)
---------------------------
I want to thank you and Philip Harwood for that very good Pac-81 game.
But while my children are used to the W-D-X-A keys to move up-right-
down-left, I have made the following changes. Maybe they are of use
for other readers.
	POKE 16907,84
	POKE 16914,76
	POKE 16918,3
	POKE 16921,92
	POKE 16925,69
	POKE 16929,2
	POKE 16932,77
  They also like to start playing with high score set to zero. This
can be done by Poking 156 in the addresses 16865 and 16869.
[...]
G. Mannaerts
Kluislaan 9
B-2070 Ekeren (Antwerp)

