Chapter
5:
Game
Controllers
163
down, + I for up,
and
0 for neither.
If
the Fire
button
is
pressed, FB will
bea
I; otherwise, it will be a
O.
63000
POKE371541127:Xr~=PEEK(37152)AHD128:POKE371341255
63010
XT~=XT~OR(PEEK(37137)AHD
127)
63020
XI=SON(XT~AND128)-SGN(XTr.AND16)
63030
YI=SON(XT~AND8)-SON(XT~AND4)
63040
FS=1-S0H(XT~AND32)
63050
RETURN
This program uses some tricks to make it run faster. Let's examine its
operation line by line.
63000
63010
63020
63030
63040
63050
Reads the Right switch
and
uses the technique described
above to preserve the keyboard scanner.
Reads the other switches and combines them with the Right
switch into a single variable. Notice the use
of
AND in both
lines 63000 and 63010 to mask off the non-joystick bits.
These two lines derive the X
and
Y increments from
the switch values.
To
reduce the
amount
of
time needed to
calculate them, the SGN function
is
used. This returns a I if
the switch
is
off and a 0 if it
is
on. This
is
faster
than
comparing the result
of
the AND to zero.
Gets the value
of
the Fire button.
Returns to the main program.
USING
THE
JOYSTICK
SCANNER
The following simple program illustrates the capabilities
of
the joystick
scanner.
It
moves
an
object
around
on the screen in response to the move-
ment
of
the stick.
100
PRINT":"JII"
200
(JOSUS
63000
300
IF
XI-0
AND
YI=0
THEN200
400
PRINTCHR$(20)
:
REM
DELETE
CHRR
ON
SCREEN
'500
IF
XI=-l
THEN
PRINT"IIIII";
600
IF
XI=
1
THEN
PR
I
NT
".l1li
" ;
700
IF
YI=-l
THEN
PRINT".";
800
IF
YI-
1
THEN
PRINT"~";
900
OOTO
200