By C. Regena — originally published in Volume 1 – Issue 1 – July, 1983
ISSUE: July 1983
I’m happy to be writing a monthly column on beginning programming for the VIC-20 and Commodore 64 computers. I own several microcomputers and have published programs and articles in several national magazines for the TI-99/4A, TRS-80 Color Computer, and VIC-20 computers, and I publish “TIdbits” for the TI-99/2. I have also written two books on programming BASIC, one for the TI-99/4A and one for the TRS-80, which are published by COMPUTE! Books.
I enjoy programming and helping others learn how much fun it is to get a computer to do what you want it to do. The main goal of this column will be to discuss programming concepts as they apply specifically to Commodore’s BASIC which is built in the VIC-20 and Commodore 64. With each column, I’ll try to include a short program for you to type in and RUN.
In future columns, I’ll try to cover interactive programming, colors, graphics, sounds, DATA statements, arrays, built-in functions, strings, and programming techniques and hints to help you with your own programming. I’ll assume you have available the guide that comes with your computer. If you want more advanced information, you should have the VIC or 64 Programmer’s Reference Guide published by Commodore. Have your computer handy while you read my articles, and actually try things on your computer while you are reading.
I’ll rarely mention hardware (another section of this magazine will cover that) unless it is really necessary for a particular application. But you should have the Commodore Datassette or a 1540/1541 disk drive to save your programs for later use. All programming in this column can be done on the standard VIC or 64 with no expansion memory or other peripherals.
First, Commands
Let’s get started on an actual program. A computer “program” consists of numbered statements or commands. You may type the statements in any order, and the computer will keep them in memory. However, it is less confusing if you type them in the proper order. When the program is RUN, the computer executes the statements in numerical order. If you type a command without a line number, the computer will execute the statement immediately (and the statement will not be saved in memory). This is called immediate mode or direct mode. When I start to program, I often number the lines in increments of ten – 10, 20, 30, etc. – so later lines may be inserted between existing statements if necessary.
The PRINT statement is one of the easiest to start with. It is used to print something on the screen. At the beginning of a program, I usually like to clear the screen so there is a blank screen to draw a title or start printing. To clear the screen, use the following statement:
10 PRINT “{CLR}”
In listings in this column, and throughout the magazine, the braces will indicate keys or special function keys to be pressed. In this case, hold the SHIFT key while you press the CLR/HOME key. An inverse heart will be printed.
Now try a few more PRINT statements. Remember, as long as there is a line number before the command, you will not see results until you RUN the program. Remember to press RETURN after typing each line.
Now RUN this program.
You will notice that the screen clears, then the messages within the quotation marks are printed, starting at the top left of the screen. Each message is on a separate line. The PRINT statement starts a new line and begins printing at the left margin. If you wanted the word HELLO on the top line, you could include HELLO in the quotes of line 10 right after the {CLR}.
Careful With Punctuation
Lines 30 and 40 show that you may print punctuation marks if they are included inside the quote marks. Line 50 is just the word PRINT and prints a blank line. Lines 60 and 70 show the difference in printing items separated by a semicolon or a comma (called “print separators”). Notice how they are outside the quotation marks. The semicolon puts the second item right next to the first. The comma indicates to print in the next print column, sort of like the TAB key on a typewriter.
Line 80 is an END statement. Although in Commodore BASIC the program will end by itself anyway, it is good programming practice to use END as the very last statement in your program. Some forms of BASIC require it.
Try a few statements with messages of your own in quotes. Notice that if you have a long message, longer than 22 characters on the VIC or 40 characters on the 64, the printing simply goes to the next line. When you are printing messages on the screen, you may use extra spaces between words so that you don’t divide a word between two lines.
Another way to print a blank line is to use the down cursor key. Add this line, then RUN:
72 PRINT “{2 DOWN}GO DOWN”
(Press the down cursor twice. A reverse Q will be printed for the cursor.)
You may use the right cursor key to position words so they aren’t at the left column. Try adding this line, then RUN:
74 PRINT “{3 RIGHT}MOVE RIGHT”
(Press the right cursor three times. A reverse left bracket will be printed for the cursor. In listings, this may also be expressed as {RIGHT 3}.)
Another way to move something to the right is to use the TAB function. The columns on the screen are numbered 0 to 21 on the VIC, and 0 to 39 on the 64, with the left column zero. You may TAB over to a certain column, and your printing will start in that column. Add this line and RUN:
76 PRINT TAB(6);”SIX”
You can see there are many ways to print. Different methods or combinations of commas, semicolons, TABs, and cursor controls will accomplish the same visual effect. Some methods may use more memory (and that’s a subject of a whole column), but there is really no “wrong” way if the end result is what you want it to be.
The sample program uses PRINT statements and the built-in graphics characters to create this scene on the VIC-20. The program also works on the 64, but looks slightly different due to the screen variations between the computers.
Now, Color
Now let’s add some color. Changing colors is very easy to do on the computer.
First type NEW, then press RETURN to get rid of the old program and start a new program. If you also want to start with a clean screen, press SHIFT and CLR/HOME.
To change colors in a program, you still use the PRINT statement, but press CTRL and one of the color keys on the top row of keys just before the word you want printed. Be sure the color is inside the quote marks. You’ll notice that the color keys print an inverse symbol – it may be difficult for us to remember what symbol goes with which color, but the computer keeps track with just the one symbol. Whatever you print will be the new color until you change colors again. Here is a sample.
As you are programming, your cursor will not change colors. You are writing statements that tell the computer to change colors when you RUN the program. By the way, if the cursor does change colors while you’re typing, it means you have made a typing error and did not put the color change within quotes. The easiest thing to do is press RETURN and type the line over.
Now RUN the program. Notice that line 50 said to print in black. The next line ends the program; since I didn’t return to blue, all printing now will be black. If you really want to get back to blue, press CTRL and BLU. CTRL and RVS ON will make the letters print with reversed colors, and CTRL RVS OFF returns the printing to normal.
Note: To save typing effort and memory, you may leave out the space after the line number and after the word PRINT. To abbreviate the word PRINT and save even more memory, use the question mark. Line 20 above may be typed:
20?”HELLO”
If you list the program, even though you used the question mark, the listing will show the word PRINT. However, the computer remembers the question mark (or “token”). If you are typing in listings from this magazine, you may freely substitute the question mark for the word PRINT.
Enough Words
Let’s get to graphics. You have probably already discovered that SHIFT plus a key will print the symbol on the right face of the key, and the Commodore key plus a key will print the symbol on the left face of the key. To draw graphics in a program, just PRINT combinations of graphics symbols.
I usually like to sketch on graph paper the picture I’m going to draw. For the VIC, either mark off graph paper in 22 columns by 23 rows or use a copy of page 144 in the VIC manual. For the 64, mark off graph paper in 40 columns by 25 rows. Sketch your picture on the graph paper, then try to decide which graphics characters would best fit your design. Use colored pencils to make it more attractive.
Now for your program, go down the graph paper line by line with PRINT statements to match the graphics. Remember, you may change colors before any printed graphics character. You may also want to experiment with CTRL RVS ON. By the way, once you turn the reverse ON, all characters in that set of quotes will be reversed unless you turn it back OFF. The next PRINT statement, however, will automatically return characters to normal. Rather than using separate PRINT statements, it may be more convenient to use the cursor keys to move around as you draw. To move the cursor up or left, be sure to press the SHIFT key as you press the appropriate CRSR key.
To keep your design on the screen without the word READY appearing at the end of the program (or without scrolling), use a line such as
900 GOTO 900
To stop the program, press the RUN/STOP key. Line 900 here tells the computer to go to line 900, which just keeps the computer endlessly looping on that line until you “break” or STOP the program.
Here is a simple graphic design using PRINT statements. You may type it in and try it—but by next month’s issue you should be proficient in your own graphics or printing programs.
Line 10 clears the screen and moves down two lines. Line 30 illustrates the use of cursor keys rather than separate PRINT statements. Line 40 is the Commodore key and the minus sign. Line 50 uses two reversed characters, then CTRL RVS OFF to get back to normal. Line 70 uses spaces to move over three spaces. You could also use the right cursor to move over. Line 130 will print the symbol across the screen. Line 140 holds the picture on the screen until you press STOP.
See program listing on page 114.
