Classic Computer Magazine Archive COMPUTE! ISSUE 50 / JULY 1984 / PAGE 120

PROGRAMMING THE TI

C. Regena

Programming Techniques In TI BASIC

This month, by answering some of the common questions I have received from readers, I'm going to give you a variety of programming techniques that you can use in your own programs.

How do you clear part of a screen?

Let's say you have onscreen a nice picture with a description underneath. CALL CLEAR will clear the whole screen; but you want to clear the printing, not the picture. Use CALL HCHAR with the row and column parameters under the picture, and use the number of repetitions that will clear the section you want. For example, to clear the lower half of the screen, CALL HCHAR (13,1,32,32*12). We're starting with row 13, column 1, and clearing with the space (character code 32) for 32*12 squares—32 columns times 12 more rows.

To clear with a different color, redefine a character (in a color set you are not using) as a colored square, then use CALL HCHAR to put that character on the screen:

300 CALL COLOR (13, 16, 16)
310 CALL HCHAR (13, 1, 128, 32*12)

To clear a vertical section of the screen, use CALL VCHAR:

CALL VCHAR(1, 17, 32, 24*16)

To try out this technique, try this sample program:

100 CALL HCHAR (1, 1, 42, 32*24)
110 CALL HCHAR (13, 1, 32, 32*12)
900 GOTO 900

Change line 110 to the CALL VCHAR statement above and try the program. Next take out line 110 and put in lines 300 and 310 listed above. Experiment with different numbers of repetitions.

How do you get a border around the screen?

CALL SCREEN(c), where c is a number from 1 to 16, defines the screen color. When you use this statement in a program, the whole screen instantly changes color. CALL COLOR (s,f,b) defines the character colors. The characters are divided into sets of eight characters each. The s in the parentheses is the set number and can be from 1 to 16. The f is the foreground color of the character, b the background color, and they can be one of the 16 color numbers, from 1 to 16.

Now take a look at the characters in set 1. The space is code 32 in set 1. The screen is filled with spaces wherever there isn't any printing or graphics. If you change the color of set 1 to something other than the screen color (background color 1), you'll get color where all the spaces are.

100 CALL CLEAR
110 CALL SCREEN (14)
120 CALL COLOR (1, 2, 16)
900 GOTO 900

Press FCTN 4 (CLEAR) to stop the program. You've got a border on the top and on the bottom, but you would like the sides also. When we PRINT messages we have a 28-column line, but when we do graphics we actually have 32 columns—there are two columns on each side of the regular printing section. They currently have spaces in them. To get the screen color in those columns, add

115 PRINT : : : : : : : : : : : : : : : : : : : : : : : : :

Or, as you print messages, those extra columns fill with the screen color. (As you PRINT, columns 1, 2, 31, and 32 will contain character 31.) A quicker way to get rid of the spaces in those columns is to fill the columns with a character in the screen color. You may add these lines instead:

115 CALL CHAR (152, " ")
116 CALL VCHAR(1, 1, 152, 48)
117 CALL VCHAR(1, 31, 152, 48)

Now try a few PRINT messages, such as

150 PRINT "HELLO"

Notice that the letters have little squares of the screen color around them. All the color sets are automatically defined as CALL COLOR(S,2,1), which is black with a transparent background. The color number 1, transparent, will be the screen color. If you want the printing to be black on your inner screen color (the color of the spaces), you need to define the sets with the background color that you used in set 1. Change line 120 above to

120 FOR S = 1 TO 12
130 CALL COLOR(S, 2, 16)
140 NEXT S

This defines a white background for the first 12 character sets, those sets which have letters and symbols. Now run the program and you will see that the message no longer has the screen color background.

How do you make a simple math drill with graphics?

I have had quite a few requests for an arithmetic drill program. Many readers would like to develop such programs on their own and want to know how to draw a certain number of pictures for the numbers chosen randomly in a simple math problem.

Here is a short program to give you the general idea of using the graphics. I defined character 128 to be the picture. The variables A and B can be numbers from zero to four. Lines 170–200 print the problem on the screen—a simple addition problem. Lines 210 and 220 draw the right number of characters for A and B.

Program 1: Simple Math Drill

100 REM SIMPLE MATH
110 CALL CLEAR
120 CALL CHAR (128, "0024002418")
130 CALL COLOR (13, 2, 11)
140 RANDOMIZE
150 A = INT (5*RND)
160 B = INT (5*RND)
170 CALL HCHAR (8, 10, A+48)
180 CALL HCHAR (10, 8, 43)
190 CALL HCHAR (10, 10, B+48)
200 CALL HCHAR (11, 8, 95, 3)
210 CALL HCHAR (8, 12, 128, A)
220 CALL HCHAR (10, 12, 128, B)
230 CALL SOUND (150, 1497, 4)
240 CALL KEY (0, K, S)
250 IF S < 1 THEN 240
260 IF K = 32 THEN 400
270 IF K = A+B+48 THEN 310
280 CALL SOUND (100, 330, 2)
290 CALL SOUND (100, 262, 2)
300 GOTO 240
310 CALL HCHAR (13, 10, K)
320 PRINT "CORRECT!"
330 CALL SOUND (100, 262, 2)
340 CALL SOUND (100, 330, 2)
350 CALL SOUND (100, 392, 2)
360 CALL SOUND (200, 532, 2)
370 CALL SOUND (1, 9999, 30)
380 CALL CLEAR
390 GOTO 140
400 CALL CLEAR
410 END

If you prefer to have a space between graphics characters, place a character in every other space. You can do this by changing lines 210 and 220 above to the following:

210 FOR C = 12 TO 12+2*(A - 1) STEP 2
212 CALL HCHAR (8, C, 128)
214 NEXT C
220 FOR C = 12 TO 12 + 2*(B - 1) STEP 2
222 CALL HCHAR (10, C, 128)
224 NEXT C

In this sample program, an addition problem is presented and the student answers by pressing a number. If it is incorrect, there is an "uh-oh" sound. If it is correct, an arpeggio is played and the computer goes to the next problem. To stop, press the space bar.

How can you draw a bar graph?

This procedure is similar to the previous sample program. The easiest way to draw a bar graph is to use HCHAR with the appropriate number of repetitions (or VCHAR). You may need to scale the numbers. Take the highest number you'll need to graph, relate it to the greatest number of repetitions you can have in your HCHAR statement, and stay on that row.

Another method is to use PRINT and print the right number of characters for the bar. The following sample program segment demonstrates this method. Character 128 will be a red square. For purposes of illustration, I will use random numbers N up to 90 for the amounts to be graphed. You would probably have specific numbers that have been calculated or read in from DATA.

A is the scaled value (rounded) for N—for every four units one square can be drawn. Line 170 prints the number N then says to start the next printing in the fifth print column. Lines 180–200 print the appropriate number of red squares.

Program 2: Bar Graph Generator

100 REM BAR GRAPH
110 CALL CLEAR
120 CALL COLOR (13, 7, 7)
130 FOR I = 1 TO 10
140 RANDOMIZE
150 N = INT (90*RND)
160 A = INT (N/4+.5)
170 PRINT N; TAB(5);
180 FOR B = 1 TO A
190 PRINT CHR$(128);200 NEXT B
210 PRINT ::
220 NEXT I
230 GOTO 230
240 END

How do you print a list of items in more than two columns?

As you know, the comma in PRINT statements prints items in two columns—items start either in the first print position or the center position. To get three columns or more, use the TAB function. TAB works like the tab key on a typewriter. You may specify which column to start printing. TAB(7) would start the next print item in the seventh print column. Here's a sample that types three columns of names.

100 CALL CLEAR
110 READ L$, M$, N$
120 IF L$="@" THEN 180
130 PRINT L$; TAB(10);M$; TAB(19);N$
140 GOTO 110
150 DATA MIKE, BOB, DICK, RICH
160 DATA JIM, JERRY, MARY, PAULA
170 DATA CHRIS, KEVIN, KATHY, KIRK,@, @
    , @
180 END

How can you print a screen without seeing the scrolling?

Some people don't like to see scrolling as they print. Messages on the TI are always printed on the twenty-fourth row then moved upward. To block this motion, change the screen to black first (because the printing is black), print the messages, then change the screen back to a different color so you can read the printing.

100 CALL CLEAR
110 CALL SCREENC2)
120 PRINT "THIS IS AN EXAMPLE"
130 PRINT ::"TO SEE A SCREEN"
140 PRINT ::"ALL AT ONCE.":::::
150 CALL SCREEN(4)
160 GOTO 160

How can you print what is on the screen to the printer?

I'm sorry, but I don't know how to do a screen dump of graphics because none of the printers I have right now has the graphics capabilities. You will need to look at your own brand printer manual to see how to use the dot-addressable graphics. If you have a screen of printing, however, with regular printed symbols, you can use the following procedure. The character in each row and column is determined, then that character is printed on the printer. You may need to change the OPEN statement in line 100 to suit your particular printer configuration.

100 OPEN #1:"RS232.BA=600"
110 FOR ROW=1 TO 24
120 FOR C0L=3 TO 30
130 CALL GCHAR(ROW, COL, G)
140 PRINT #1:CHR$(G);
150 NEXT COL
160 PRINT #1
170 NEXT ROW
180 CLOSE #1
190 END

If you want everything you are printing to go both to the screen and to the printer, use both a PRINT statement and a PRINT #1 statement for items printed.

100 OPEN #1:"RS232.BA=600
110 CALL CLEAR
120 PRINT #1:CHR$(12)
130 PRINT "HELLO"
140 PRINT #1:"HELLO"
150 PRINT "ANY MESSAGE"
160 PRINT #1: "ANY MESSAGE"
170 CLOSE#1
180 END

Line 120 above goes to the top of a page.

How can you simulate time on the TI?

If you need an exact time, use the CALL SOUND statement in which you can specify an exact duration in milliseconds. If you don't want to hear the sound, use a high frequency and the softest volume.

100 PRINT "START"
110 CALL SOUND(1000,9999,30)
120 CALL SOUND(1,9999,30)
130 PRINT "END"
140 END

Line 120 is necessary to end the first sound.

If you want to time someone as they are pressing keys to move or are answering a question, use a counter in your CALL KEY loop. You can't relate this counter to an exact time because in each program it will be different—depending on how you do the programming, how long your program is, and how full the memory is. However, once you have your program working, you can print the counter value and use a stopwatch to figure out a formula that relates the actual time to the counter value. ("Type-ette Timer" in my Programmer's Reference Guide to the TI-99/4A from COMPUTE! Books uses this technique to time how fast you can type sentences.) Here is a sample:

100 T=0
110 CALL KEY(0, K, S)
120 T=T+1
130 IF S<1 THEN 110
140 PRINT T
150 GOTO 100
160 END

The faster you press a key, the lower the value for T will be. The longer, you wait, the more times the computer will go through the loop and increment T.

Other computers use PRINT AT; how can we do it?

In TI Extended BASIC you can specify a row and column to begin printing an item. However, we don't have that feature in regular console BASIC on the TI. There are several ways to accomplish this, though they're slower than regular printing. First, you can use the regular PRINT statement, perhaps with the TAB function, and then use colons to move the message up to the proper row.

PRINT TAB(9);"START PRINTING"::::::

The main problem with this method is that it scrolls the screen. If I am labeling graphics, I do all the printing first, then use CALL HCHAR and CALL VCHAR to put up the graphics.

Another method is to treat the letters in the printed message as graphics characters, and use CALL HCHAR to specify the row and column to place the letters on the screen. Here's a general-purpose subroutine that you can use. M$ is the message you want printed, R is the row, and C is the column you want the message to start in.

300 FOR L=l TO LEN(M$)
310 CALL HCHAR(R, C-1+L, ASC(SEG$(M$, L, 1) ) )
320 NEXT L 330 RETURN

Before you call the subroutine with a GOSUB, specify a row R and a column C and the message M$:

900 M$="TEST PRINTING"
910 R=6
920 C=12
930 GOSUB 300

How can I put a code in my program?

I have had lots of young people ask me how they can write a program so that whoever runs it must enter a code before the program continues— they don't want their brothers and sisters using their program. The general idea is that you put a code name in the program as a string variable. Next, use INPUT for the user who is running the program to type in the code. Now compare the INPUT value with the code to see whether to continue or not.

100 CALL CLEAR
110 CODE$="RANDY"
120 INPUT "ENTER CODE NAME: ";: A$
130 IF A$=CODE$ THEN 160
140 PRINT ::"SORRY, INVALID CODE."
150 STOP
160 REM PROGRAM CONTINUES

The only problem with specifying the code in line 110 is that anyone can load the program, then LIST it to find out what the code name is. One method I use so people can't read the code name is to hold down the CTRL key (key with the red dot) while you type your code message. Line 110 will now look like this:

110 CODE$=""

or you may get some funny-looking graphics characters between the quotes. Now when someone lists your program, they can't tell what the code name is. When you run the program, be sure to hold the CTRL key down when you IN-PUT the code name, and it will match the code in the program.

A Couple Of Warnings

Always use the SHIFT key on the left side of the keyboard to type the plus sign. You don't want to go for the right SHIFT key and accidentally hit the FCTN key—and quit\

Do not use TI Extended BASIC to run regular TI BASIC programs because they may not run properly. One reason is the double colon used in PRINT statements, and another reason is that I often use graphics in character sets 15 and 16, which are not available in Extended BASIC.

If you have a disk drive attached to your computer, the disk uses up some memory. For any of my published programs, type in CALL FILES(l) and press ENTER, then type NEW and press ENTER, then proceed normally (load a program or start typing a program). This procedure clears about 1000 bytes of memory so a program can fit.

Until Next Time …

I hope these ideas help you in your programming. Your computer can be a lot of fun. Part of the joy of programming is getting that machine to do what you want it to do. As I continue these columns I hope to present a variety of programs so you can see that this computer is really quite versatile. Your suggestions and letters are always welcome.