This is an old revision of the document!
Getting Staried with COLOR BASIC: © 1980 Tandy Corporation, Fort Worth, Texas 76102 U.S.A. All Rights Reserved.
Reproduction or use, without express written permission from Tandy Corporation, of any portion of this manual is prohibited. While reasonable efforts have been taken in the preparation of this manual to assure its accuracy, Tandy Corporation assumes no liability resulting from any errors or omissions in this manual or from the use of the information obtained herein.
TRS-80 Color Computer System Software: © 1980 Tandy Corporation and Microsoft. All Rights Reserved.
The system software in the Color Computer micro- computer is retained in a read-only memory (ROM) format. All portions of this system soft- ware, whether in the ROM format or other source code form format, and the ROM circuitry, are copyrighted and are the proprietary and trade secret information of Tandy Corporation and Microsoft. Use, reproduction or publication of any portion of this material without the prior written authorization by Tandy Corporation is strictly prohibited.
10 9 8 7 6 5 4 3 2 1
INTRODUCTION
This book is for those of you who don’t know anything about Computers, and would like to be spared the long, technical explanations!
Using this as your guide, you'll be able to interact and enjoy your Computer right away. Later, you'll probably want to read some of our thorough and detailed books. For now, though, the easiest way to get started with your Computer is to use it and have fun doing it.
You'll find many of the things we have you do — especially in the first chapters — are games,. songs, or other fun-type programs. This is not to say your TRS-80 can’t be used for practical programs. It’s every bit as powerful as other computers its size. We start you off with the fun programs because it’s the easiest way for you to feel at ease with your Computer. Once you feel it’s truly an extension of yourself, you’ll find it much easier to write any kind of program you want.
So sit down and spend a couple of hours with it. Type away at it. Play with it. Try to make it do strange things. In other words…get to feeling comfortable with it. There’s an infinite number of things it can do for you.
TO GET STARTED …
Connect your Computer by referring to the Chapters on “Installation” and “Operation”, and to Figure 1 in your TRS-80 Color Computer Operations Manual1).
Then power up your Computer:
This message should appear:
COLOR BASIC v.r
© 1980 TANDY
OK
(v.r is two numbers specifying which version and release you have).
If you don’t get this message, turn the computer on and off again. Adjust the Brightness and Contrast on your T.V. set. Check all the connections. If you still don’t get this message, refer to “Troubleshooting and Maintenance” in the TRS-80 Color Computer Operations Manual.
Once you do get this message, you’re ready to begin.
HOW DO YOU TALK TO A COMPUTER?
In this book, you'll learn how to talk to your Computer. That’s all programming is, by the way. Once you learn how to communicate, you'll be able to get your Computer to do whatever you tell it. (well, almost). The Computer understands a language called COLOR BASIC. COLOR BASIC is form of BASIC — Beginners All-purpose Symbolic Instruction Code. There are lots of computer languages. COLOR BASIC just happens to be the language your Computer understands.
We'll introduce BASIC words in the order that it’s easiest to learn them. When you get mid-way in the book, you might forget what one of the words means. If this happens, simply look up the word in the back of the book or use your “Quick Reference Card” to find its meaning.
TABLE OF CONTENTS
CHAPTER 1: MEET YOUR COMPUTER
CHAPTER 2: YOUR COMPUTER NEVER FORGETS (unless you turn it off...)
CHAPTER 3: SEE HOW EASY IT IS?
CHAPTER 4: COUNT THE BEAT
CHAPTER 5: SING OUT THE TIME
CHAPTER 6: DECISIONS, DECISIONS
CHAPTER 7: GAMES OF CHANCE
CHAPTER 8: SAVE IT ON TAPE
CHAPTER 9: COLOR YOUR SCREEN
CHAPTER 10: ONE FANTASTIC TEACHER
CHAPTER 11: HELP WITH MATH
CHAPTER 12: A GIFT WITH WORDS
CHAPTER 13: BEAT THE COMPUTER
WHAT NOW?
APPENDIXES
A / MUSICAL TONES
B / BASIC COLORS
C / PRINT @ SCREEN LOCATIONS
D / GRAPHICS SCREEN LOCATIONS
E / ANSWERS TO EXERCISES
F / SAMPLE PROGRAMS
G / ERROR MESSAGES
H / BASIC SUMMARY
MEET YOUR COMPUTER
Is your Computer connected? Turned on? Ready to give it a first work-out?
In these first two Chapters, we’re going to introduce you to your Computer — the way it thinks, some of its many talents, and even a couple of little quirks it has. By the time you finish these chapters, you'll be ready to program … promise!
Type away on the keyboard and then press the (ENTER) key.
Don’t worry about anything but the last line of type on your screen. It should say:
OK
OK is the Computer’s “prompt”. It’s telling you — “OK, enough foolishness … as soon as you are ready …” (It patiently waits for your command.) You are the Master — you can tell the Computer to do anything you wish.
Give it your first command. Type this exactly as it is below:
PRINT "HI, I’M YOUR COLOR COMPUTER"
When you reach the end of the line on your screen, keep on typing. The last part of the message will appear on the next line.
Now check your line. Did you put the quotation marks where we have them? If you made a mistake, no problem. Simply press the (←) key and the last character you typed will disappear. Press it again and the next to the last will disappear (…and so on and so on…).
Ready? This should be on your screen:
OK PRINT "HI, I'M YOUR COLOR COMPUT ER"
Press the (ENTER) key and watch. Your screen should look like this:
OK PRINT "HI, I'M YOUR COLOR COMPUT ER" HI, I'M YOUR COLOR COMPUTER OK
“Hi, I’m Your Color Computer!”
Your Computer just obeyed you by printing the message you had in quotes. Give it another message to print. Type:
PRINT "2"
Press (ENTER)…….. The Computer again obeys you and prints your next message:
2
Try another one:
PRINT "2 + 2" (ENTER)
The Computer obeys you by printing:
2 + 2((remove incorrect period from example screen output))
You probably expect a lot more than just an electronic mimic… like maybe some answers! Well, try it without the quotation marks. Type:
PRINT 2 + 2 (ENTER)
Much better. This time the Computer prints the answer:
4
These quotation marks obviously must mean something. Try experimenting some more with them. Type each of these lines:
PRINT 5+4 (ENTER) PRINT "5+4" (ENTER) PRINT "5+4 EQUALS" 5+4 (ENTER) PRINT 6/2 "IS 6/2" (ENTER) PRINT "8/2" (ENTER) PRINT 8/2 (ENTER)
Have you come up with any conclusions on what the quotes do?
The Computer thinks of quotes like a journalist does. If the message is in quotes, the Computer must PRINT it exactly as it appears. If it’s not in quotes, the Computer can interpret it by adding, subtracting, multiplying or dividing it.
The Computer sees everything you type as STRINGS or NUMBERS. If it’s in quotes, it’s a STRING. The Computer sees it EXACTLY as it is. If it’s not in quotes it’s a NUMBER. The Computer will figure it out like a numerical problem.
A COLOR CALCULATOR, NO LESS!
Any arithmetic problem is a snap for your Computer. Let it do some long division. Type:
PRINT "3862 DIVIDED BY 13.2 IS" 3862/13.2 (ENTER)
Let’s do a multiplication problem:
PRINT 1589 * 23 (ENTER)
Notice that the Computer’s multiplication sign is an asterisk, rather than the X sign which you’ve always used in math. This is because the Computer is such a precise and literal creature that it would get the X multiplication sign mixed up with the X alphabetical character.
Try a few more problems:
PRINT "15 * 2 = " 15*2 (ENTER) PRINT 18 * 18 "IS THE SQUARE OF 18" (ENTER) PRINT 33.3/22.82 (ENTER)
N ’ For our next game, we'll first have to teach the Computer to roll the dice. To do this, the Computer must roll two dice; that is, it must come up with two random ' numbers. Type: / 10 CLS 20 X = RND() 30 Y = RND() 40 R=X+Y 50 PRINT @ 200, X 60 PRINT @ 214, Y 70 PRINT @ 394, “YOU ROLLED A” R 80 PRINT @ 454, “DO YOU WANT ANOTHER ROLL?” 90 INPUT A$ 100 IF A$ = “YES” THEN 10 | “Loser!” Aw = “Yes” RUN the program. Let’s look at it: Line 10 tells the Computer to CLear the Screen. Line 20 has the Computer pick a random number from 1 to 6 for one of the die. Line 30 has the Computer pick a random number for the other die. Line 40 simply adds the two dice to get the total roll. Lines 50-70 PRINT the results of the roll on the screen. In line 90, you are able to INPUT whether you want the program to RUN again. IF you type YES, the Computer goes back to line 10 and runs the program again. Otherwise, since this is the last line in the program, the program ends. CRAPS Now that you know how to get the Computer to roll the dice, it should be fairly easy for you to write a Craps program. These are the rules of the game (in its simplest form): 1. The player rolls the two dice. If he rolls a sum of 2 (“snake eyes”), a 3 (“cock-eyes”), or a 12 (“boxcars”) on the first roll, the player loses and the game is over. 2. Ifthe player rolls a 7 or 11 on the first throw, (“a natural”), the player wins and the game is over. 3. Ifany other number is rolled on the first roll, it becomes the player’s “point”. He must keep rolling until he either “makes his point” by getting the same number again to win, or rolls a 7, and loses. You already know more than enough to write this program. Do it. Make the Computer print it in an attractive format on your screen and keep the player informed on what is happening. It may take you awhile to finish, but give it your best. Good luck! pr ‘ SS. “Winner!” 67 Our answer to this is in the back. RND PRINT @ NOTES: 69 CHAPTER 8 { 10 T= RND (255) 20 Sovowhs T, | lo Peiwr ” —— 20 INPoT AS Ol - GG SAVE IT ON TAPE SAVE IT ON TAPE You'll soon be writing longer and more powerful programs. Perhaps you already are. It certainly cramps your style to have the program disappear everytime you turn the Computer off! You can “save” (make a copy of) any of your programs on cassette tape. Once the program’s on tape, you'll be able te “load” the program back into your Computer’s memory anytime you want. We recommend that you use Radio Shack’s CTR-80A cassette recorder (catalog number 26-1206) along with Radio Shack’s Computer Tapes (catalog number 26-301). This chapter is only for those of you that have this type of cassette recorder and want to use it. If you don’t, you'll probably want to skip this chapter for now, remembering that the information’s here whenever you need it. Once you're used to it, you’ll find cassette tape easy to use. Simply follow these steps: A. Connect the Tape Recorder 1. Locate the CTR-80A Cassette Recorder, Interconnecting Cable and Radio Shack Computer Recording Tape cassette. 2. Connect the short cable between the TAPE jack on the back of the TRS-80 and your Cassette Tape Recorder 71 72 You may substitute any name for NAME. e The small grey plug goes into the REM jack on the Recorder. e@ The large grey plug goes into the AUX jack. @ The black plug goes into the EAR jack. 3. Plug the Recorder into the wall outlet B. Save a Program 1. Type any program into your Computer. RUN it to make sure it works. 2. Load the cassette tape, positioning it to the beginning of the tape. Press the PLAY and RECORD buttons at the same time until they lock. 3. Name the program you want to SAVE. You may use any name with 8 or fewer letters. For our example, we’ll use “NAME”. 4. SAVE on tape by typing this command: CSAVE “NAME” The motor on the Recorder will start and you'll be recording the Computer’s program on tape. Watch the screen. When: OK returns and the motor stops, your program is recorded on tape. It is also still in the Computer’s memory. It has only been copied. LOADING Reversing the process and loading (copying) the program from tape into the Computer is just as easy: 1. Be sure the tape is fully rewound and the plugs are all in place. 2. Push the PLAY button down until it locks. Set the Volume Control to your CTR-80A’s “Recommended Volume Level”. Your CTR-80A Manual gives this recommended volume. 3. Type NEW to clear out any existing program. 4. Type the CLOAD command with the name of your program. For example: CLOAD “NAME” The Tape Recorder’s motor will start. Watch your screen. The letter: Ss will appear at the top left hand corner. This means the Computer is Searching for your program. When the Computer has Found your program, it will print the letter F and the name of your program. For example, if your program name is NAME: F NAME will appear at the top of your screen. When the Computer prints: OK and the recorder motor stops, the program is “loaded” in memory. You may now RUN the program. SAVING MORE THAN ONE PROGRAM To SAVE more than one program on the same tape, you must make sure you are not recording on top of another program. This is an easy way to position the tape to the end of your last program: 1. Rewind the tape to the beginning. 2. Press the PLAY button until it locks 73 74 _ You may replace the name X with any name you know is NOT on the tape. 3. Type SKIPF and the name of the last program on your tape. For example, if your last program is named “NAME”, type: SKIPF “NAME” The Computer will notify you when if Finds your program called NAME. When it reaches the end of NAME, the recorder’s motor will stop and: OK will appear on your screen. 4. Once you've positioned the tape to the end of the last program, press the RECORD and PLAY buttons, name your program, and CSAVE it. If you can’t remember the name of your last program, type: SKIPF “X”’ and watch the screen. The Computer will give you the name of each program it encounters on the tape. It will print an I/O ERROR when it reaches the end of the tape, but don’t worry about it. You’ve found what you were looking for — the name of the last program on the tape. Now you can type the SKIPF command with the name of this last program. (Don’t forget to rewind the tape first). TIPS ON MAKING GOOD RECORDINGS Here are some tips for making good recordings: @ When you're not using the Recorder for saving or loading, do not leave the RECORD or PLAY keys down. Press STOP. © Don’t attempt to re-record on a pre-recorded Computer tape. Even though the recording process erases the old recording, just enough information may be left to confuse the new recording. If you want to use the same tape a second or third time, use a high-quality bulk tape eraser to be sure you erase everything. e If you want to save a taped program permanently, break off the Erase Protect tab on the Cassette (see Tape Recorder’s Manual). When the tab(s) has been broken off, you can’t press the RECORD key on your Recorder. This will keep you from accidentally erasing that tape. Now type as long of programs as you want, knowing you can make a permanent copy of them on tape. Happy recording! 75 CHAPTER 9 \ COLOR THE SCREEN COLOR THE SCREEN You've learned enough now to really start using the colors. Since color graphics ideas usually come very quickly to people — and the good graphics programs usually end up long — this Chapter just shows you how to get started. While going through this Chapter, you'll probably want to stop from time to time and add on to our programs or build your own. We hope you do. That’s a fast way to learn. To get started, type: 10 CLS(0) to make the screen black. Add these two lines and RUN the program: Be sure to type line 30. We'll explain why later. 20 SET(0,0,3) 30 GOTO 30 Do you see the blue dot? It’s at the top left-hand corner of your screen. To put the dot at the bottom right-hand corner, change line 20 and RUN the program: 20 SET(63,31,3) Want to put it in the middle of the screen? RUN the program using this for line 20: 77 78 20 SET(31,14,3) SET tells the Computer to SET a dot on your screen at a certain horizontal and vertical location. e@ The first number you type is the horizontal location. This may be a number from 0 to 63. @ The second number is the vertical location. It may be a number between 0 and 31. In the Appendix, there’s a grid on your screen, “Graphics Screen Locations”. The grid divides your screen into the 64 (0 to 63) horizontal locations and 32 (0 to 31) vertical locations. Use this grid in planning your graphics illustrations. All of this explains what the first two numbers are for, but what about 3, the third number? Try using some numbers other than 3 for the third number. Type each of these lines and RUN the program: 20 SET(31,14,4) 20 SET(31,14,1) Got it figured out? With number 4, you get a red dot, and with number 1 you get a green dot. The number codes are the same as the CLS number codes — 0 to 8. These are listed in your Appendix, “BASIC Colors”. Now, what’s the GOTO line for? Try deleting the GOTO line from your program and RUN it: 10 CLS(0) 20 SET(31,14,1) It looks like no dot was SET this time. Actually the dot was SET, but when the program ended, the Computer printed its OK message on top of the dot. To avoid this, type the GOTO line at the end of the program. It sets up an infinite loop (going to itself over and over again) so that the program will never end. SETTING TWO DOTS To SET more than one dot, you need to do a little planning. Erase your program and RUN this program: 10 CLS(0) 20 SET(32,14,3) 30 SET(33,14,3) 40 GOTO 40 You should now have two blue dots—side by side—in the middle of your screen. Now change the color of the right dot so you'll have one blue and one red dot. Type: 30 SET(33,14,4) and RUN the program. . . Both dots are red. Look again at the “Graphics Screen Locations” grid in your Appendix. Notice the darker lines group the dots together into blocks of four. For instance, the block in the middle of the grid contains these 4 dots: Horizontal Vertical Location 32 14 Location 33 14 Location 32 15 Location 33 15 Each dot within the block must either be: 1. the same color (colors 1-8) or 2. black In our program, we tried to get the Computer to SET two dots with different colors — blue and red — within the same block. Since the Computer can’t do that, it SETs both dots the second color — red. Type this and RUN the program: (/ \) “Set Dot!” 79 80 “Funny Face!” 30 SET(34,14,4) Since the dot in location 34, 14 is in a different block, the Computer can SET the two dots in different colors. THE COMPUTER’S FACE With this groundwork, you can draw whatever you want. We'll draw a simple picture of a Computer. First draw the top and the bottom of the head. We'll make it buff. Type: 5 CLS(0) 10 20 30 40 50 and RUN. FOR H = 15 TO 48 SET (H,5,5) SET (H,20,5) NEXT H GOTO 50 This is what you should have on your screen. (The lines should be buff rather than white, like we have them): Notice we’ve changed line 50 — the GOTO line. / Lines 10 and 40 set up a FOR/NEXT loop for H — making the horizontal locations 15 through 48 for the top and the bottom lines. Line 20 SETs the top line. The horizontal location is 15 through 48 and the vertical location is 5. Line 30 SETs the bottom line. The horizontal location, again, is 15 through 48 and the vertical location is 20. To SET the left and right sides of the head type these lines: 50 FOR V = 57020 60 SET(15,V,5) 70 SET(48,V,5) 80 NEXT V 90 GOTO 90 and RUN. We'll make the nose orange. Type: 90 SET(32,13,8) and the mouth red. Type: 100 FOR H = 28 TO 36 110 SET(H,16,4) 120 NEXT H and blue eyes. Type: 130 SET(25,10,3) 140 SET(38,10,3) 150 GOTO 150 RUN the program. This is what your screen should look like now: 81 82 You don’t need to tell the Computer the color of the dot to RESET (erase) it. A BLINKING COMPUTER By adding a couple of lines, we can make the Computer blink. Type: 150 RESET(38,10) and RUN the program. What you should have on your screen now is the same face as above, except the right eye is missing. RESET tells the Computer to erase the dot in the horizontal location 38 and the vertical location 10. That’s the right eye. To make it blink, we’ll simply SET and RESET th e right eye over and over again, by adding line 160: aA & Ore 160 GOTO 140 LIST your program to see if it still looks like mine: 5 CLS(0) 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 and: RUN it… FOR H = 15 T0 48 SET(H,5,5) SET(H,20,5) NEMe Ho. eet pe ss bs fe FOR V = 5 T0 20 SET(15,V,5) SET(48,V,5) NEXT V SET(32,13,8) FOR H = 28 TO 36 SET(H,16,4) —————. 70 TH NEXT H SET(25,10,3) SET(38,10,3) RESET(38,10) GOTO 140 Snowe —_—_£qeu Try your hand at some pictures. I’m sure you have better artistic skills than we do. THE BOUNCING DOT By using SET and RESET, we can make a moving picture. Type and RUN these lines to make the dot go down: 5 CLS(0) 10 FORV =0T031 _20 SET(31,V,3) 30 RESETGL1,V) 40 NEXT V 84 Every dot that is SET on line 20 is RESET (erased) on line 30. Add these lines to make the dot go back up: 50 FOR V = 31700 STEP -1 60 SET(31,V,3) 70 RESET(31,V) 80 NEXT V and this line to make the dot go up and down, over and over again: 90 GOTO 10 and RUN it. To slow the dot down — it will look a little better — change lines 30 and 70: 30 IF V > 0 THEN RESET(31,V-1) 70 IF V < 31 THEN RESET(31,V +1) The > sign means the same as it does in math — greater than. The < sign means less than. SET and RESET opens up all sorts of possibilities — moving targets, animated pictures, etc. Use your imagination in experimenting with this combination. IF YOU HAVE THE JOYSTICKS… If you have joysticks with your Computer, you have many more options open to you. If you haven’t connected them yet, do it. Simply plug them in to the back of your Computer. They only fit in the correct slot, so don’t worry about connect- ing them to the wrong one. Now, type this short program which demonstrates how they work: 10 20 30 40 50 60 CLS PRINT @ 0, JOYSTK(0); PRINT @ 5, JOYSTK(1); PRINT @ 10, JOYSTK(2); PRINT @ 15, JOYSTK(3); GOTO 20 RUN the program. See the four numbers on your screen. These numbers tell the Computer the horizontal and vertical coordinates of your two joysticks’ “floating switches”. Now to see what each of the four numbers are referring to. Grab the “floating switch” of one of your joysticks. Keeping it in the center, move it from left to right. Either the first number or the third number of your screen will change, going through all the numbers from 0 to 63. Move the “floating switch of your other joystick from left to right. Place the joystick that makes the first number change on the left side. Move the floating switches up and down, keeping them in the center. Moving the left joystick up and down makes the second number change from 0 to 63. Moving the right joystick up and down makes the fourth number change from 0 to 63. This is how the Computer reads the position of your joysticks: LEFT JOYSTICK 0 0 63 63 JOYSTK(O) JOYSTK(1) RIGHT JOYSTICK — JOYSTK(2) 63 JOYSTK(3) ‘Be sure to type the semicolons at the ends of lines 20, 30, 40, and 50. The second or fourth number might change also, but NOT from 0 to 63. 85 86 JOYSTK(0) and JOYSTK(1) tell the Computer the read the position of your left joystick: e JOYSTK(0) makes it read the horizontal (left to right) coordinate. e@ JOYSTK(1) makes it read the vertical (up and down) coordinate. JOYSTK(2) and JOYSTK(3) tell the Computer to read the position of your right joystick: @ JOYSTK(2) makes it read the horizontal coordinate. @e JOYSTK(3) makes it read the vertical coordinate. One more thing. Delete line 50 and RUN the program. It works almost the same, doesn’t it, except it doesn’t read JOYSTK(3) — the vertical position of your right joystick. Now delete line 20 and change line 60: 60 GOTO 30 RUN the program. Move all the switches around. This time it doesn’t work at all. The Computer will not read any of the coordinates unless you first have it read JOYSTK(0). Type these lines: 20 A = JOYSTK(O) 60 GOTO 20 and RUN the program. Even though the Computer is not printing the location of JOYSTK(0), it is still reading it. Everything else works like it’s supposed to. Remember that anytime you're having the Computer read to coordinates of JOYSTK(1), JOYSTK(2), or JOYSTK(3), you must first have it read JOYSTK(0). MAKE PAINT BRUSHES OUT OF JOYSTICKS: Type this: 10 CLS(0) 20 H = JOYSTK(O) 30 V = JOYSTK(1) AQ JE M3), THEN Vi = V8 32 80 SET(H,V,3) 90 GOTO 20 RUN it… Use the revolving switch of your left joystick to paint a picture. (Move the switch slowly so that the Computer has time to read its coordinates). Line 20 reads H — the horizontal position of your left joystick. This could be a number from 0 to 63. Line 30 reads V — its vertical position. This also could be a number from 0 to 63. Since the highest vertical position on your screen is 31, we had to add line 40 to the program. Line 40 makes V always equal to a number from 0 to 31. Line 80 SETs a blue dot at H and V. Line 90 goes back to get the next horizontal and vertical positions of your joysticks. We haven’t even used the right joystick. Perhaps we could use it for color. Add these lines: 50 C = JOYSTK(2) 60 IF C < 31 THENC = 3 70 IF C > = 31 THEN C = 4 80 SET(H,V,C) RUN the program. Move your right joystick to the right and the Computer makes C = 3. It SETs red dots. Move it to the left and the Computer makes C = 4 and SETs blue dots. Want to make the buttons on your joysticks do something? Add these lines to the end of your program:
87 88 If you press the buttons when you're not RUNning the program you will get @ABCDEFG or HIJKLMNO. 100 P = PEEK(65280) 110 PRINT P 120 GOTO 100 Now type: RUN 100 This tells the Computer to only RUN lines 100 through the end of the program. Your computer should be printing either 255 or 127 over and over again. PEEK tells the Computer to look at a certain spot in its memory to see what number’s there. We had it look at the number in location 65280. As long as youre not pressing either of the buttons, this spot contains the number 255 or 127. Press the left button. When you press it, this memory location contains either the number 126 or 254. Press the right button. This makes this memory location contain either the number 125 or 253. Using this information, you can make the computer do whatever you want when you press one of the buttons. We'll make it go back to line 10 and CLS(0) —clear the screen to black — when you press the left button. Change lines 110 and 120: 110 IFP 120 IF P 126 THEN 10 254 THEN 10 Delete line 90 and add this line: 130 GOTO 20 RUN the program. Start your paintings. Press the left button when you want to clear the screen and start over again. ‘ SET RESET JOYSTK PEEK NOTES: 89 CHAPTER 10 SS ONE FANTASTIC TEACHER e epecece a 'e e' “canes e A ONE FANTASTIC TEACHER Your Computer has all the attributes of a natural born teacher. After all, it’s patient, tireless, and detail conscious (. . . perhapsa bit nit-picky . . .). Depend- ing on the programmer — we’re talking about you, of course — it can be imaginative, consoling, and quite enthusiastic. So lets get on with it! We can use RND to get the Computer to drill us on one math problem after the next. Type:= means greater than or equal to
| 20 X = RND(15) 30 Y = RND(15) 40 PRINT “WHAT IS” X “*” Y 45 INPUT A 50 IF A = X * Y THEN 90 60 PRINT “THE ANSWER IS” X*Y 70 PRINT “BETTER LUCK NEXT TIME” 80 GOTO 10010 CLS
: > 100 PRINT “PRESS <ENTER> WHEN READY FOR ANOTHER” 105 INPUT A$ : 110 GOTO 10 91 ay) This program will drill you on your multiplication tables, from 1 to 15, and check your answers. How would you change this program to get the Computer to drill you on addition problems from 1 to 100: Here’s the lines we changed: 20 30 40 45 50 60 X RND(100) ¥ RND(100) PRINT “WHAT IS” X “+” Y INPUT A IF A = X + Y THEN 90 PRINT “THE ANSWER IS” X + Y To make the program a little more interesting we can have the Computer keep a running total of all the correct answers. Type: 15-7 = T Has 9 C=C +1 98 PRINT “THAT IS” C “OUT OF” T “CORRECT ANSWERS” T keeps a count of all the questions the Computer asks you. When you first RUN the program T equals zero. Then, everytime the Computer gets to line 15, it adds 1 to T. C does just about the same thing. It keeps a count of the number of correct answers. Since it is on line 95, the Computer will not increase C unless you get _ When you first turn on the Computer, i _ all numeric variables equal 0. Also, — a correct answer. when you type NEW CENTER), all _ numeric variables equal0. There are many ways to make this program more entertaining. Add some lines = aoa to the program which will get the Computer to do one or more of the following: 1. Call you by name 2. Reward your correct answer with a sound and light show 3. Print the problem and messages attractively on your screen. (Use PRINT @ veges Tia Ree for this). _ There are many variations you could — _ try with this prograr 4. Keep a running total of the percent of correct answers. _ the Computer could 5. End the program if you get 10 answers in a row correct. is wee eee Use your imagination on this one. We have a program in back which does all five of the above. 94 FIRST BUILD YOUR COMPUTER’S VOCABULARY… To build your Computer’s vocabulary (so that it can build yours!) type and RUN this program: 10 DATA APPLES, ORANGES, PEARS 20 FOR X = 1 TO 3 30 READ F$ 40 NEXT X So what happened? Nothing? Nothing that you can see, that is. To see what the Computer is doing, add this line and RUN it: 35 PRINT “FS = :” F$ Line 30 tells the Computer to: 1. Look for a DATA line 2. READ the first item in the list — APPLES 3. Give APPLES an F$ label 4. “Cross out” APPLES The second time the Computer gets to line 30 it is told to do the same things: 1. Look for a DATA line 2. READ the first item — this time it is ORANGES 3. Give ORANGES the F$ label 4. “Cross out” ORANGES This is what is happening in your Computer’s memory when you RUN the program: YOUR COMPUTER’S MEMORY oe FS emp APPLES ya ORANGES PEJKRS What if you want the Computer to READ the same list over again? It’s already crossed everything out … Type: 60 GOTO 10 and RUN the program. It prints 70D ERROR IN 30. OD means Out of Data. The Computer has already crossed out its Data. Type this line and RUN the program: 50 RESTORE Now it’s as if the Computer never crossed anything out. The Computer will READ the list over and over again. The nice thing about DATA lines is that you can put them anywhere you want in the program. RUN each of these programs: 10 DATA APPLES 10 DATA APPLES, ORANGES 20 DATA ORANGES 20 DATA PEARS 30 FORX =1T03 30 FORX =1T03 40 READ F$ 40 READ F$ 50 PRINT “F$ = :” F$ 50 PRINT “F$ = :” F$ 60 NEXT X 60 NEXT X 70 DATA PEARS Remember how to make the Computer pause while RUNning a program? Press (SHIFT) © Press any key to get it to continue. 30 FORX =1T03 30 FORX = 1T03 40 READ F$ 40 READ F$ 50 PRINT “F$ = :” F$ 50 PRINT “FS = :” F$ 60 NEXT X 60 NEXT X 70 DATA APPLES 70 DATAAPPLES, ORANGES, PEARS 80 DATA ORANGES 90 DATA PEARS 95 96 “Cataclysmic!” They all work the same, don’t they? This knowledge should be handy for something… ..NOW HAVE IT BUILD YOUR VOCABULARY Here’s some words and definitions you might want to be tested on: Words Definitions 10 DATA TACITURN, HABITUALLY UNTALKATIVE 20 DATA LOQUACIOUS, VERY TALKATIVE 30 DATA VOCIFEROUS, LOUD AND VEHEMENT 40 DATA TERSE, CONCISE 50 DATA EFFUSIVE, DEMONSTRATIVE OR GUSHY Now to get the Computer to pick out a word at random from the list. Hmmm… . there are ten items in the list. Maybe this will work: 60 N = RND(10) 70 FORX=1TON (% READ A$ 90 NEXT X 100 PRINT “THE RANDOM WORD IS :” A$ RUN it a couple of times to see if it works. It doesn’t quite work like we want it to. The Computer is just as likely to stop at a definition as a word. What we really want the Computer to do is to pick a random word from items 1, 3, 5, 7, or 9. Fortunately, there is a word which will explain this to the Computer. Type: 65 IF INT(N/2) = N/2 THENN=N-—1 RUN the program a few times. It should work now. INT tells the Computer to only look at the whole portion of the number and ignore the decimal part. For instance, the Computer sees INT(3.9) as 3. Here’s how line 65 works. Say the random number the Computer picks is 10. The Computer does this calculation: INT(10/2) = 10/2 INT(5) = 5 5=5 Since this is true, 5 does equal 5, the Computer completes the THEN portion of the line and makes N equal to 9 (10 — 1). However, if the Computer picks 9, it does this: INT(9/2) = 9/2 INT(4.5) = 4.5 = 45 Since this is not true, 4 does not equal 4.5, the Computer doesn’t subtract 1 from N. 9 remains 9. Now that the Computer is able to READ a random word, it must also READ the word’s definition. You can do this simply by adding these lines to the end of the program: 110 READ B$ 120 PRINT “THE DEFINITION IS :” BS RUN it several times now. To get the Computer to print one random word and definition after the next, add this line to the beginning of the program: 5 CLEAR 100 to give the Computer plenty of string space. And add these lines to the end of the program: 130 RESTORE 140 GOTO 60 So that the Computer can pick out a new random word and its definition froma Oi; clean slate of data items. Here is the way the entire program looks now: 5 CLEAR 10 DATA TACITURN, HABITUALLY UNTALKATIVE 20 DATA LOQUACIOUS, VERY TALKATIVE 30 DATA VOCIFEROUS, LOUD AND VEHEMENT 40 DATA TERSE, CONCISE 50 DATA EFFUSIVE, DEMONSTRATIVE OR GUSHY 60 N = RND(10) 65 IF INT(N/2) = N/2 THENN=N-—-1 70 FORX =1TON C 80 READ A$ 90 NEXT X 100 PRINT “A RANDOM WORD IS :” A$ 110 READ B$ 120 PRINT “ITS DEFINITION IS :” BS 130 RESTORE 140 GOTO 60 Want to complete this program? Try it before turning the page to see ours. Program it so that the Computer will: PRINT the definition ONLY i 2. Ask you for the word 3. Compare the word with the correct random word 4, Tell you if your answer is correct. If your answer is incorrect, have it PRINT the correct word. HEHE aE ESA ASCH HCPA EES HICH HS CIECH HIC HACC COE a ARCH 2 2 2A 2K HACK OK ooh a a aK a a a aaa CHIC HACC a aE aaa ak a ARCHIE a aK a aR AICS Ke ae ae ake ak ke ae ease ae ae skeoke ate ok 98 Here’s our program: 5 10 20 30 40 50 60 65 70 80 90 110 120 130 140 ro koe 160 170 180 190 200 CLEAR 500 DATA TACITURN, HABITUALLY UNTALKATIVE DATA LOQUACIOUS, VERY TALKATIVE DATA VOCIFEROUS, LOUD AND VEHEMENT DATA TERSE, CONCISE DATA EFFUSIVE, DEMONSTRATIVE OR GUSHY N = RND(10) IF INT(N/2) = N/2 THENN=N-—1 FORX = 1TON | READ A$ NEXT X READ B$ PRINT “WHAT WORD MEANS :” B$ RESTORE INPUT R$ IF R$ = A$ THEN 190 PRINT “WRONG” PRINT “THE CORRECT WORD IS :” A$ GOTO 60 PRINT “CORRECT” GOTO 60 Feel free to dress the program up with — _a good looking screen format, sound, — 99 DATA READ RESTORE INT CLEAR 100 NOTES: 101 CHAPTER 11 AX {BY +: ©) =.D'+ E(G/W) =F veeooes a HELP WITH MATH Solving complicated math formulas with super speed and precision is an area where your Computer shines. But before driving yourself crazy typing a bunch of math formulas, there are some shortcuts and hints you'll probably want to know about. One easy way to handle complicated math formulas is by using SUBROU- TINES. Type and RUN this program: 10 PRINT “EXECUTING THE MAIN PROGRAM” 20 GOSUB 500 30 PRINT “NOW BACK IN THE MAIN PROGRAM” 40 END 500 PRINT “EXECUTING THE SUBROUTINE” 510 RETURN Line 20 tells the Computer to GO to the SUBroutine beginning at line 500. RETURN tells the Computer to return back to the BASIC word immediately following GOSUB. Delete line 40 and see what happens when you RUN the program. Did you delete it? 103 See something different about INPUT? We can have the Computer PRINT a message before waiting for us to INPUT something. 104 If you did, this is what’s on your screen: EXECUTING THE MAIN PROGRAM EXECUTING THE SUBROUTINE NOW BACK IN THE MAIN PROGRAM EXECUTING THE SUBROUTINE ?RG ERROR IN 510 RG means RETURN without GOSUB. Can you see why deleting END in line 40 would cause this error? At first, the Computer went through the program just like it did before you deleted the END line. It was sent to the subroutine in line 500 by GOSUB and it returned to the BASIC word immediately following GOSUB. Then, since you deleted END, it went to the next line in the program which was the subroutine. When it got to RETURN, it did not know where to return to, since this time it had not been sent to the subroutine by a GOSUB. Here’s a subroutine which raises a number to any power you want: 10 20 ee oO 40 50 INPUT “TYPE A NUMBER”; N INPUT “TYPE THE POWER YOU WANT IT RAISED TO”; P GOSUB 2000 PRINT: PRINT N” TO THE” P’” POWERIS” E GOTO 1090 PRINT “CORRECT!!!”
2010 E=1 2020 FORX =1TOP 2030 E=E*N 2040 NEXT X 2050 IF P = OTHENE =1 2060 RETURN Notice we introduced a couple of new things in the program. Look at line 40. If you find it easier, you can combine two or more program lines into one, using a colon to separate the two lines. Line 40 contains the two lines: PRINT and PRINT N” TO THE” P” POWERIS” E Line 2000 has something else new — REM. REM doesn’t mean anything to the Computer. The Computer ignores any line beginning with REM. You can put REM lines anywhere you want in your program, so that you can remember what the program does. These REM lines will make no difference to the way the program works. If you don’t believe us, add these lines and RUN the program to see if they make any difference: 5 REM THIS IS A PECULIAR PROGRAM, 17 REM THIS LINE SHOULD MESS UP THE PROGRAM 45 REM THE NEXT LINE KEEPS THE SUBPROGRAM SEPARATED Satisfied? … Good. Change the program so that the Computer prints a table of squares (a number to the power of 2) for numbers, say, from 2 to 10. é The answer is in the back of the book. en _ PRINT by itself tells the Computer to 105 GIVE THE COMPUTER A LITTLE HELP As math formulas get more complex, your Computer will need some help understanding them. For example, what if you want the Computer to solve this problem: Divide the sum of 13 + 3 by 8 The word “operation” means some- thing you’re getting the Computer to You might want the Computer to solve the problem like this: do. Here, we’re talking about the “operations” of adding, aL! 13 + 3/8 = 16/8 =2 multiplying, and dividing. 106 But your Computer solves it differently. Type this command line: PRINT 13 + 3/8 (ENTER) What the Computer did was logical according to its rules: CoS eee ee RULES ON ARITHMETIC 1. any multiplication and division operations are solved first 2. addition and subtraction operations are solved last The Computer solves arithmetic problems in this order 0 aa aT, \> in case of a tie (that is, more than one multiplication/division or addition/subtrac- 0 tion operation) the operations are performed from left to right ; WAAAARAARAARAAAAAA In the problem above the Computer followed its rules. It performed the division operation first (3/8 = .375) and then the addition (13 + .875 = 13.375). To get the Computer to solve the problem differently, you can use parenthesis. Type this line: PRINT (13 + 3)/8 (ENTER) ~ - Whenever the Computer sees an operation in parenthesis, it solves that before solving anything else. What do you think the Computer will PRINT as the answers to each of these problems: Finished? Type each of the command lines to check your answers. What if you want the Computer to solve this problem? Divide 10 minus the difference of 5 minus 1 by 2 That is what you’re actually asking the Computer to do: G0 = (5 — 12000 REM FORMULA FOR RAISING A NUMBER TO A POWER