Adding interest to your programs - part two
from ZX Computing, June/July 1983

Tim Hartnell continues in his quest to illustrate just how a simple listing
can be transformed into a program you can be proud of.


First off, I hope you had a lot of fun with the three listings, Programs 1,
2 and 3, that I introduced you to last issue, We now take off the kid
gloves and take a PEEK at Program 4. As you can see from a quick glance,
the listing looks considerably different from the listings we've examined
up to now.

At the very least, you must admit that line 40 now looks extremely strange.
This line is where a user-defined graphic (which, believe it or not, does
look like a duck when the program is RUNning) takes the place of the
randomly-chosen letters. No matter how hard you look, you will not find
anything that looks like that little duck on the keyboard. So, where has it
come from?

Duck defining!
The duck has been "user-defined". User-definable graphics are one of the
really advanced features of the Spectrum, and although certain other
computers (which shall remain nameless) have similar features, few (if
any) are as simple to use as the one on your Spectrum. It is very simple to
define a graphic. I'll take you through the way I created the alleged duck,
and from this explanation you should be able to produce anything you like.
The key to the user-defined graphics is an eight by eight grid like that
shown below.

To work out your graphic, you simply fill in the squares on this grid which
you wish to print as solid dots in the final graphic. Our duck (in grid
form) looks like that shown below.

If you look to the lines from 140 onwards in the fourth version of Duck
Shoot, you'll see a little loop, which includes a READ statement and (line
170) the disturbing-looking POKE USR ... and so on. There are 21 user-
definable graphics on the Spectrum, and you print them by getting into the
graphics mode (so the cursor becomes a G) and then pressing the keys from A
to U. They will look just like the ordinary letters until they have been
re-defined. In fact, when line 40 was first typed in, it was made up from a
number of the letter 'A', entered while the computer was in the graphics
mode, separated by spaces entered in the ordinary mode.

Once you have a filled-in grid like the one we've just looked at, you have
to get the numbers (such as the ones in the DATA statement, line 200) to
POKE into the letter so it will come out as a graphic. It is simple to do
this. On the bottom row of keys, above the B key, you'll see the word BIN,
which stands for "binary". Each row across our eight by eight grid can be
represented by a single binary number, where an empty square is zero, and a
filled-in square is a one. You indicate that you are talking about a binary
number by preceding it with the word BIN.

I'll now give you the BIN numbers for each row of squares on the "duck
grid". Compare them with the filled-in and the empty squares, until you
understand how they compare.

BIN 00000000
BIN 00000100
BIN 01001001
BIN 11011110
BIN 00111110
BIN 00001000
BIN 00000000
BIN 00000000

If you look at this pattern of ones and zeros, you'll see that they
correspond with the squares on our grid. Now, to convert them to decimal
numbers (which are easier to manage) to include in our DATA line (200) we
simply type in directly:

PRINT BIN 00000000

for the first line, and a 0 appears. Take note of this zero. Then, try
the second number, by typing in directly:

PRINT BIN 00000100

The computer answer 4, since 00000100 (or 100) in binary is 4 in decimal.
Note down the 4. Continue to do this, working through each line in turn.
This will give you a sequence of eight numbers:

0, 4, 73, 222, 62, 8, 0, 0

You put these in the DATA statement; and put in line 170, within the quote
marks, the letter you wish to re-define. Then, by simply RUNning the
program, the designated key will be changed. From now on, whenever you
press the key 'A' after getting into the graphics mode, the little duck
will appear instead. Although the duck will not survive you turning off the
computer, it will survive a NEW  so you can use graphics defined outside
a program within it if you wish to do so. And the graphic will be SAVEd
when you SAVE the program. (Note also that there is a change to the end of
line 70 in this version of the program with the user-defined duck.)

Try working out a duck-shape of your own on a grid you have drawn up
yourself, and enter the numbers for this duck in line 200.

Birds of a feather
The final version of this program we will discuss (Program 5) has three
rows of flying ducks. It is best to aim at the middle row of ducks (which
fly more quickly than the bottom row) when you RUN this program because
they are worth 517 points each, as opposed to the 57 that each of the
ducks on the bottom row are worth. The top row is just there to confuse
you; the ducks there disappear automatically as the ducks in the middle row
are shot, but they cannot be shot directly and they do not contribute to
your score.

The middle row of ducks is held in the string, B$, which is set equal to A$
in line 45 as you can see in the listing. Note that you must change the
BEEP in line 90. The middle row of ducks is shot at in line 95. If you
shoot a duck in the bottom row, your shot ends there - you cannot expect it
to continue on to get a duck from the middle row as well! The GOTO 100 at
the end of line 90 ensures this.

Line 135 moves the middle and top rows of ducks across, changing the
elements in the string by an extra element compared to the changes occuring
in line 130. Line 70 prints all three rows of ducks, "inventing" the top
(dummy) row by printing B$ out of register, so the ducks there appear ahead
of, although flying in synchronisation with, those in the middle row. This
will be clear when you RUN the program.

Room for Improvement?
That brings us to the end of this series of Duck Shoot games. There are,
however, four things you could do to further develop the program:

* Cut the number of shots available down to make it more challenging.

* User-define the figure firing the gun, so it is not just an 'X'.

* Add a "high score" feature so the game will re-start, preserving a high
score you can try and better.

* Allow the computer to detect when all the ducks have been shot (this will
happen when A$ and B$ contain 32 spaces each) and add a bonus to the score
if this occurs before all the shots have been fired.

Once you have mastered these simple techniques, try going over some of your
earlier programming efforts and "spice" them up a little. You won't be
disappointed...
