168
The
VIC
20
User
Guide
arrays in the program. The first
array
(KT$) contains the values
of
the keys
that
make up
our
joystick. The other two arrays contain the values
of
XI
and
YI
that
correspond
to
those keys.
For
example, KT$(5) contains the letter
"U", which means
"up
and left," so XT(5) contains
-1,
and
YT(5) contains
1.
To
save time when scanning the keyboard, we'll build these tables in a
separate subroutine
that
is
executed only once
at
the beginning of the
program.
62000
REM
KEY
TABLE
VALUES
62100
DATA
I,O,L,".", ",",
M,J,U
62200
REM
X
INCREMENT
VALUES
62300
DATA
0,1,1,1,0,-1,-1,-1
62400
REM
Y
INCREMENT
VALUES
62500
DATA
1,1,0,-1,-1,-1,0,1
62600
FOR
1-0
TO
7 :
RERD
KT$(Z)
62700
FOR
1-0
TO
7 :
READ
XT(I)
62900
FOR
1=0
TO
7 :
READ
YT(I)
62950
REM
MAKE
ALL
KEYS
REPEAT
62900
PC~E
650,129
:
NEXT
NEXT
:
NEXT
Your program must call this subroutine before attempting
to
use the
"joystick" in order to translate the keys correctly.
When using this routine, be careful where you place the
DATA
state-
ments. Remember
that
the
READ
statement starts with the first
DATA
statement in the program.
If
you put additional
DATA
statements in this
program, you may find it helpful to separate the ones above from the
subroutine
and
group them with
your
own
to
keep them
in
the right order.
THE
KEYBOARD
INTERPRETER
SUBROUTINE
The subroutine
to
read the keyboard
and
translate the key has high
statement numbers to force it to the end
of
the program.
63000
XI=0
:
YI-0
:
FB-e
:
LI-0
63010
GET
KE$
63020
IF
KEf-""
THEN
RETURN
63030
IF
KE$=KT$(LI)
THEN
YI-YT(LI)
XI=XT(LI)
RETURN
63040
LI=LI+l
:
IF
LI(S
THEN
63030
63050
IF
KEf-"K"
THEN
FBs1
63060
RETURN
This subroutine has a few tricky parts, so
we
will go over it line by line.
63000 XI, YI, and FB are set to zero first. Since there are three
places where
we
RETURN,
the
old values must be erased
before
we
start.