A LOOK BACK
As I mentioned in the last column, this one has to
be prepared early so while all of you are busy working on your
suggestions for the game outlined in issue 18 I will take this
opportunity to go back over a few items I have touched on before.
BOOLEAN ALGEBRA
Back in issue 11 I discussed Boolean algebra and I
have been asked if this can be utilised in any other subroutines, so
what follows will be additional to issue 11 which you may need to
peruse again.
Probably one of the most powerful, and possibly
one of the least used, commands is the ON .. GOTO/GOSUB statement.
If you have never used this before then I shall explain. Instead of
using several lines such as
10
IF X=1 THEN GOTO 100
20
IF X=2 THEN GOTO 200
we can use a single line of ON/GOTO. We must get a
value for the variable X, for example by using INPUT X, and then use
this in the ON/GOTO statement. In our example X must equal (say) 1
to 4 or else be re-input. When we have a value we can use it to
choose a subroutine as follows
10
? "PRINT CHOICE (1-4)"
20 INPUT X : IF X<0 OR X>4 THEN 10
30
ON X GOTO 100, 200, 300, 400
40 REM - Subroutines in lines 100,200,300,400
If the limitations did not exist in line 20, then
if X fell outside the range 1- 4 it would be ignored and the program
would continue to line 40. X must be in the range of 1 to (number of
choices) for this to work. The subroutine is chosen from the value
of X so that if X equals 10 then the tenth routine will be chosen.
You must be sure to have enough subroutines to cover the maximum
value of X.
What would happen if you used X=PEEK(764) when
looking at a keyboard entry? As this means you could have several
lines such as
10 IF X=250 THEN GOTO 100
you would then need 250 choices for the ON/GOTO
routine to work. Instead we can set the limitation by Boolean
algebra. For example, if we wanted X to equal 10, 20, 30,40 or 50
only we would have to use five single lines of IF/THEN statements. A
Boolean equivalent would be as follows
10
Y=(X=10)+2*(X=20)+3*(X=30)+4*(X=40) +5*(X=50)
20 ON Y GOTO 100, 200, 300, 400, 500
To use the subroutine on line 200, X would need to
equal 20. If it did then this is the only true statement in line 10.
So (X=20) would be the only true statement and the line would
evaluate as Y=(0)+2*(1)+3*(0)+4*(0)+5*(0), or 2. So on Y=2 the
computer will choose the second subroutine at line 200. The values
of the subroutines need not be in order, nor need the values of X,
just as long as they correspond numerically to the subroutines.
USING LOCATE
I was asked by Stephen Plunkett to explain about
the LOCATE statement. This is an excellent way of testing a location
on the screen to see what is stored at location X,Y. It can be used
in the same way as the PMG collision registers.
The statement takes the form LOCATE X, Y, Z. You
just position the cursor (even if switched off) at position X,Y and
the data under the cursor is stored in Z. So if the target is in
COLOR 1, you know you have hit it when Z=1. Z will equal 0-3 in four
colour modes, 0 or 1 in two colour mode - and 0-255 in the text
modes.
Now for the bad news. A PRINT or PUT directly
after the LOCATE will move the cursor one point to the right and may
modify the character under the cursor. Here is the solution to this
little problem. Locations 85 and 86 (cursor horizontal position) are
updated by the LOCATE statement by adding 1 to the number stored
here, thus repositioning the cursor. You can store the data from 85
and 86 first, use LOCATE, and then restore the data. For example
P1=PEEK(85): P2=PEEK(86):
LOCATE X,Y,Z: POKE 85,P1: POKE 86,P2
Location 93 holds the data found under the cursor,
so you would do exactly the same here to stop corruption of the data
i.e. PEEK 93 first, use LOCATE and then POKE 93. Another handy set
of locations is 94, 95 which holds the current position of the
cursor. By the way, location 84 holds the vertical position of the
cursor and you may need to use this after rollover when the cursor
moves down a line.
The LOCATE statement is just like using
POSITION X,Y: GET #6,Z
I have included a simple little program to
illustrate LOCATE in action. It should be well enough documented not
to need an explanation.
|
|
|
DO-IT-YOURSELF REPAIRS
I was interested by the letters in issue 12. John
Dimmer (a regular to PAGE 6) is quite right about paying someone to
push in a few chips. If your machine is outside the warranty you
could try this yourself but be careful to take anti-static precautions before touching chips, and if the IC
legs are gold-plated do not rub it off. Reader B. Sutcliffe must have
had a heart attack when he watched the service engineer open the
cartridge with a screwdriver. If you try this then use something
soft or plastic. Never, never use an eraser to clean anything - they
contain emery or pumice dust and this is lethal. Use a proprietary
cleaner/degreaser and cotton wool. Make sure that cotton buds are
not glued on as the cleaner will dissolve the glue and leave you
with a sorry mess. As a matter of fact, you should clean your
cartridge edge connectors regularly before use, but never with
anything abrasive.
I would like to wish all readers a very Happy
Christmas and New Year and I am looking forward to a full post bag
prior to my next column!
top