Chapter
4:
Advanced
BASIC
Programming
127
A
CURSOR
MOVEMENT
SUBROUTINE
With three strings containing the cursor control characters to move to
the home position, down,
and
to
the right, it
is
possible to move
to
any
coordinate position
on
the VIC
20
display. How?
The coordinates
of
the upper left corner
of
the screen are (0,0). You can
move the cursor anywhere by printing a string containing the
CLRj
HOME
control key, followed by strings
that
move the cursor down and
to
the right.
Here
is
an
example.
10
REM
PROGRAMMED
CURSOR
MOVEMENT
20
PRINT
":']":REM
CLEAR
THE
SCREEN
30
R~.20:CX.4:00SUB
1000
40
PRINT
"IGNORANCE
IS
BLISS"
58
OOTO
58
18e0
REM
CURSOR
POSITIONING
SUBROUTINE
1810
R$."~"
1020
C.-"
••••••••••••••••••••••
"
1030
PRINT
"ill"; :
REM
MOVE
CURSOR
TO
(0,0)
1040
PRINT
LEFT.(R.,RX);LEFT$(C.,CX);
1050
RETURN
Line
20
clears the screen, removing any old text from the display. (This
has nothing to do with cursor positioning; it
is
just
"housekeeping"
to
make
a cleaner display.) The integer variables R% and C%
on
line 30 stand for
"row"
and
"column." The three statements combined
on
line
30
set the row
(20)
and
the column (4), followed by a GOSUB
that
moves the cursor to the
coordinates (20,4).
You may
not
understand why the cursor must go to (0,0) first. In order
for the program to move the cursor
to
the correct coordinates, it has to know
how many rows
and
columns the cursor
is
from the coordinates you
selected. The easiest
and
most efficient way to do this
is
to move to (0,0) first,
because the program will always know exactly how many times the cursor
must move in order
to
reach the coordinates (in this case,
20
rows down
and
4 columns right).
Let's take the cursor movement subroutine apart line by line, starting
with lines 1010
and
1020. These two lines are assignment statements
that
set
up (initialize) the cursor movement strings. R$ contains 22
CRSR
DOWN
control characters; C$ contains
21
CRSR RIGHT characters.
Line 1030 prints the
CLRjHOME
character, thus positioning the cursor
at
(0,0). Line
1040
does the real work:
It
uses the LEFT$ string function
to
print the first
20
characters in R$, then the first
10
characters in C$.