Article 46434 of comp.sys.cbm:
Xref: undergrad.math.uwaterloo.ca comp.sys.cbm:46434
Path: undergrad.math.uwaterloo.ca!watserv3.uwaterloo.ca!torn!howland.reston.ans.net!newsfeed.internetmci.com!ncar!noao!CS.Arizona.EDU!ruby.ucc.nau.edu!dana.ucc.nau.edu!pap
From: pap@dana.ucc.nau.edu (Paul Allen Panks)
Newsgroups: comp.sys.cbm
Subject: Re: PROGRAMS TO TRY: BASIC 2/7
Date: 13 Dec 1995 19:02:47 GMT
Organization: Northern Arizona University, Flagstaff AZ, USA
Lines: 330
Message-ID: <4an80n$7vo@ruby.ucc.nau.edu>
NNTP-Posting-Host: dana.ucc.nau.edu
X-Newsreader: TIN [version 1.2 PL2]

(continued from previous thread):

ADVANCED BASIC 2.0 COMMANDS:

Earlier, we discussed such commands as LOAD, SAVE, VALIDATE, etc. 
However, as you probably have learned by now, there are many more 
commands that you can use to your advantage. In the following couple of 
paragraphs, we will examine five of these new commands:

1. PRINT
2. TAB
3. SCREEN CODES (COLOR) (Colors 1-8)    (Colors 9-16 NEXT thread!)
4. POKE (brief overview)
5. SCREEN CODES (CHR$,SPC, and others)  (NEXT thread!)

1. PRINT - Probably one of the simplist to use of all BASIC 2.0 commands, 
the PRINT statement is used to display characters, letters, and even 
numbers on the screen. One of the simplist uses for the PRINT statement 
is used as an example in the PROGRAM below:

10 PRINT"HELLO"

When RUN, the screen will say:

HELLO
READY.

The READY prompt is always there, but above it we have the word "HELLO". 
Try the following program for another example of the PRINT statement (be 
sure and type NEW before you enter this program):

10 PRINT"I ADORE MY COMMODORE 64"
20 GOTO 10

When RUN, the screen will say:

I ADORE MY COMMODORE 64
I ADORE MY COMMODORE 64
I ADORE MY COMMODORE 64
I ADORE MY COMMODORE 64
I ADORE MY COMMODORE 64

...

And so on. The screen will keep on scrolling the message "I ADORE MY 
COMMODORE 64" until the user presses the RUN/STOP KEY. The screen now says:

I ADORE MY COMMODORE 64
I ADORE MY COMMODORE 64
I ADORE MY COMMODORE 64
I ADORE MY COMMODORE 64
I ADORE MY COMMODORE 64
?BREAK ERROR IN LINE 10
READY.

The error message "?BREAK ERROR IN LINE 10" is really not an error 
at all. What the computer is really telling us is that program  
execution has been halted (via the RUN/STOP key).

Line 10 PRINTs the message "I ADORE MY COMMODORE 64" to the screen, and 
Line 20 tells the computer to go back to line 10 continuously. Thus, the 
scrolling output. This program will RUN as long as the program is not halted.

Now that we have the BASICs of the PRINT statement down, let's move on 
to more advanced discussion of the PRINT statement: formatting.

Using the TAB Function

Formatting involves a command known as the TAB function. What this 
function does is position text a specified number of spaces (up to 40) 
horizontally along a single screen line at a time (this function works 
almost exactly like the TAB function on a typewriter).

In order to use the TAB function properly, keep in mind that you can use 
any number between 0 and 255, however, you should keep it to 0-39 per 
line for simplicity purposes. The program below demonstrates a good use 
for the TAB function:

10 PRINT"THE 'TAB' FUNCTION IN ACTION!"
20 PRINT TAB(3)"THIS IS 3 SPACES TO THE RIGHT"
30 PRINT TAB(10)"THIS IS 10 SPACES TO THE RIGHT";

Note the ';' after Line 30 (more on that in a moment). Here is what the 
screen would PRINT in the above example:

|THE 'TAB' FUNCTION IN ACTION!          |
|   THIS IS 3 SPACES TO THE RIGHT       |
|         THIS IS 10 SPACES TO THE RIGHT|

Line 10 tells the computer to PRINT the words "THE 'TAB' FUNCTION IN 
ACTION!". Line 20 is a little more complicated. What it does is TAB the 
cursor over 3 spaces to the RIGHT, and start PRINTing the words "THIS IS 
3 SPACES TO THE RIGHT". 

The ';' after Line 30 serves an important purposes when formatting text 
to the screen. Because the screen display can accomodate only 40 X 25, a 
maximum of 40 spaces (or characters) per line is allowed, for up to 25 
lines. If a ';' were NOT used in the above example, the following would 
occur:

|THE 'TAB' FUNCTION IN ACTION!          |
|   THIS IS 3 SPACES TO THE RIGHT       |
|                                       |
|         THIS IS 10 SPACES TO THE RIGHT|

Note that there is a one line space between the second phrase and the 
third one. This is because the screen "wraps" around to a new line upon 
reaching the 40th space (or character) on a line. This is because only 40 
spaces (or characters) can be displayed per line at a time. This is a 
useful feature in Word Processors, however, it doesn't seem to help us 
any here. So the ';' is needed in this instance.

The ';' flag can also be used to append one phrase to another. An example 
of this is given below:

10 PRINT"THE 'TAB' FUNCTION IN ACTION!"
20 PRINT TAB(3)"THIS IS THREE SPACES TO THE RIGHT"
30 PRINT TAB(13)"THIS IS TEN SPACES TO THE"; 
40 PRINT "[2 SPACEs]RIGHT"

In the above exmaple, if a ';' flag were NOT used after line 30, the 
following would occur:

 |THE 'TAB' FUNCTION IN ACTION!           |
 |   THIS IS THREE SPACES TO THE RIGHT    |
 |            THIS IS 13  SPACES TO THE RI|
 |GHT                                     |

But since we did not forget to use it, here is what the program above 
would PRINT:

 |THE 'TAB' FUNCTION IN ACTION!           |
 |   THIS IS THREE SPACES TO THE RIGHT    |
 |            THIS IS 13  SPACES TO THE   |
 |RIGHT                                   |

Notice that the word "RIGHT" wrapped around the screen to the next line. 
This illustrates yet another use for the ';' flag.

Hopefully, the above explanation will clear things up a bit. Now, moving 
on to yet another advanced use of the PRINT statement: PRINTing control 
characters.

PRINTing in 'Reverse Video'

One of the first things you will learn is how to PRINT in what's called 
'Reverse Video'. This control code will allow you to PRINT anything on 
the screen in a 'highlighted' fashion. For instance, the program below 
demonstrates this command:

10 PRINT"[RVSON]THIS IS REVERSE VIDEO[RVSOFF]"

[RVSON] and [RVSOFF] are obtained by pressing the CONTROL key and the 
number keys '9' and '0' simultaneously, respectively.

The screen will say:

|THIS IS REVERSE VIDEO|

The "|" both before and after this phrase represent the beginning and end 
of the reverse video used in this exmaple. Don't worry, when you try this 
out on your Commodore 64, it will look ALOT better than this example 
(trust me). ;)

If you look on your keyboard, you'll notice that down below each key on 
the side of the "face" will be an obscure graphics symbol. This symbol 
represents a character in the Commodore 64's built-in character set. 
These are handy for making low-resolution pictures, symbols, designs, etc.

But if you also look on the keys 1-9 and 0, you'll find PRINTed below the 
numbers (on the sides of the "face" are abbreviated words. As an example, 
look at the '1' key:
  ______
 /| BLK|\
| | !  | |
| | 1  | |
|/------\|
|  ORG   |
----------

The abbreviations are BLK (BLACK) and ORG "ORANGE". These abbreviations 
represent color codes. These color codes can be used within a program to 
change the color of text on the screen. The default colors are light blue 
(cursor & text color) on a dark blue background w/ a light blue border.

In order to use these color codes within your PRINT statements, you must 
enter what is called QUOTE MODE by doing the following:

10 PRINT"

Don't press RETURN yet. When you are done typing PRINT", enter these 
additional commands:

[CONTROL 1]THIS IS BLACK."

Enter [CONTROL 1] by pressing down the CONTROL KEY _first- and while 
holding it DOWN, press the '1' key at the same time. You will get a small 
'box' symbol in front of the words THIS IS BLACK. What this symbol means 
is, it is a token (or abbreviated command) for the color code BLACK (BLK on 
the keyboard). 

You now have a general idea of how to enter color codes 
within your PRINT statements (don't worry about the other colors for 
now...all you do is HOLD DOWN the CONTROL key and simultaneously press 
the corresponding number of the color that you want, BLACK through YELLOW 
(colors 1-8. also keys 1-8). If you want to access the colors BELOW each key 
(colors 9-16, keys 1-8), HOLD DOWN the key that has the COMMODORE LOGO on it 
and simultaneously press the corresponding number of the color that 
you want,ORANGE through LIGHT GREY).

Type NEW, and enter this PROGRAM:

10 PRINT"[SCN CLR]COLOR BAR"
15 PRINT"[CONTROL 1][RVSON][17 SPACEs]BLACK[18 SPACEs][RVSOFF]"
20 PRINT"[CONTROL 2][RVSON][17 SPACEs]WHITE[18 SPACEs][RVSOFF]"
25 PRINT"[CONTROL 3][RVSON][18 SPACEs]RED[19 SPACEs][RVSOFF]"
30 PRINT"[CONTROL 4][RVSON][18 SPACEs]CYAN[18 SPACEs][RVSOFF]"
35 PRINT"[CONTROL 5][RVSON][17 SPACEs]PURPLE[17 SPACEs][RVSOFF]"
40 PRINT"[CONTROL 6][RVSON][17 SPACEs]GREEN[18 SPACEs][RVSOFF]"
45 PRINT"[CONTROL 7][RVSON][18 SPACEs]BLUE[18 SPACEs][RVSOFF]"
50 PRINT"[CONTROL 8][RVSON][17 SPACEs]YELLOW[17 SPACEs][RVSOFF]"

For this program, we will only deal with the primary colors (1-8). Here 
is a breakdown of what the program does.

LINE 10 - Clears the screen and PRINTs the message "COLOR BAR" (NOTE: to 
access the screen clear (SCN CLR) code within the quotation marks, HOLD 
DOWN the key SHIFT and simultaneously press the key marked 'CLR HOME').
LINE 15-50 - PRINTs the messages BLACK,WHITE,RED,CYAN,PURPLE,GREEN,BLUE, 
and YELLOW as 'bars' across the screen in 'Reverse Video'.

Note that each bar is separated by a one line space. This is because I 
intentially left off the ';' flag at the end of each line from LINE 15 on.

This about covers the PRINT statement, even though there are more 
advanced ways to use PRINT not discussed yet in this thread. We will now 
move on to the next topic of our thread, the POKE statement.

POKEing to the screen

Essentially, the POKE statement is responsible for executing a specific 
address in memory from BASIC. This differs from other statements of this 
genre (i.e. the SYS statement) in that it can be accessed without 
difficulty from within a BASIC program.

The syntax for the POKE statement can be any one of the following:

POKE 1,PEEK (1) OR 4    :REM '1' is a valid address, but will not be 
                         discussed as to its usefulness in this thread.

POKE 646,2              :REM '646' is the address which changes the cursor 
                         color to any number desired, in this case, 
                         with '2' (WHITE) as the argument.

POKE 53280,0:           :REM '53280' and '53281' are the addresses which 
POKE 53281,0             change the color of the screen (BORDER 
                         and BACKGROUND, respectively) color to 
                         any number desired, in this case, with '0' 
                         (BLACK) as the argument.            

A valid POKE can be any number from 1-65535.

For example, to change the screen color to BLACK and print the message 
"THIS IS YOUR COMMODORE 64 SPEAKING." in WHITE, enter the following 
program: 

10 POKE 53280,0:POKE 53281,0
20 POKE 646,2:PRINT"THIS IS YOUR COMMODORE 64 SPEAKING."

POKEing color changes to the screen is infinately more useful (and 
easier) than PRINTing it. Recall that in our 'COLOR BAR' program we had 
to type [CONTROL] and any number from 1-8 to get a desired color. Well, 
below is a modified version of the program using the POKE statement to 
change the color of each bar:

5 POKE 53280,0:POKE 53281,0
10 PRINT"[SCN CLR]COLOR BAR"
15 POKE 646,2:PRINT"[17 SPACEs]BLACK[18 SPACEs]"
20 POKE 646,2:PRINT"[RVSON][17 SPACEs]WHITE[18 SPACEs][RVSOFF]"
25 POKE 646,3:PRINT"[RVSON][18 SPACEs]RED[19 SPACEs][RVSOFF]"
30 POKE 646,4:PRINT"[RVSON][18 SPACEs]CYAN[18 SPACEs][RVSOFF]"
35 POKE 646,5:PRINT"[RVSON][17 SPACEs]PURPLE[17 SPACEs][RVSOFF]"
40 POKE 646,6:PRINT"[RVSON][17 SPACEs]GREEN[18 SPACEs][RVSOFF]"
45 POKE 646,7:PRINT"[RVSON][18 SPACEs]BLUE[18 SPACEs][RVSOFF]"
50 POKE 646,8:PRINT"[RVSON][17 SPACEs]YELLOW[17 SPACEs][RVSOFF]"

Note a couple of key changes to this program:

We've added a LINE 5 which is responsible for changing both the 
BACKGROUND and BORDER colors to BLACK. Instead of having cumbersome color 
codes within the quotation marks, we are now free to add the POKEs before 
the PRINT statements. Also, notice that in LINE 15 there is no 'Reverse 
On' or 'Reverse Off' control codes. 

This is because the screen color and 
the cursor color would be the same if we had used [RVS ON] and [RVS OFF]. 
Thusly, we could not have seen the BLACK bar. Therefore, it was printed 
in regular mode by using WHITE as the color.

There are other, more advanced uses of the POKE statement, but we will 
not get into them...or at least not yet.

Hopefully, the information contained in this particular thread is helpful 
in getting beginning programmers started with the wonderful language of 
BASIC programming!

Look for a further addition to this material in future threads of this 
article.

Regards,

Paul Allen Panks
E-mail: pap@dana.ucc.nau.edu


------------------------------------------------------------------------------
This signature file was brought to you by Paul Panks, not Micro$oft, IBM, 
or any of those other money-sapping, machine-wasting companies bent on world 
domination. Did you here that Micro$oft wants to purchase the Catholic 
Church? I don't think Bill Gates would make a very good choir boy. I think 
its time to put our foots down and stop this maniac before he declares his 
campaign for the Presidency!
*** Check out Jim Brain's WWW Page at: Http://www.msen.com/~brain/ ***
(You'll be glad you did!)
E-mail: pap@dana.ucc.nau.edu

"If you quote me on this, I'll have to deny it. Besides, my memory is 
*terrible*. I forget things often. Also, my memory is *horrible*."
------------------------------------------------------------------------------



