BASIC Tutorial
Introduction
Printing Text
Waiting…
User inputs
GOTO
Subroutines
Tips I
Clearing the screen
Lines
IF…THEN…ELSE
Random Numbers
Tips II
Introduction
BASIC is a simple computer programming language. Sharp BASIC Software Development Kit is the program that Sharp originally intended to be used to make add-in programs for the Wizard. BASIC is a good language for beginners because you can’t really damage your organiser, but you can do some good things using BASIC. If you want to make some really good programs though, I advise you to learn C.
Printing Text
In BASIC, you can only print text on 7 lines (In C, you can print it wherever you want). There is also only one text size and font. If you want to print the traditional "Hello World" message, you would do it like this:
10 PRINT "Hello World"
At the beginning of every line, you have to put a line number (between 10 - 30000) in ascending order. The BASIC command (PRINT) doesn’t have to be in capitals, but I think that it looks neater that way. You must enclose whatever you print in speech marks (""). Therefore, you can’t actually print speech marks. Instead, just use ‘’.
If you ask the program to print text that is longer than what a line can hold, it automatically goes onto the next line.
This code automatically prints text on the next free line on the page. If you want to print it on a specific line, say in the very middle of the page, then you can use the command LOCATE:
10 LOCATE 97,3
20 PRINT "Hello World"
This would print the text "Hello World" in the middle of the screen.
Waiting…
If you just ran the program using the code above, you wouldn’t be able to read it. This is because the program would print the text as instructed, and then as there are no more commands for it to obey, the program would quit. You must make the program wait if you want to read the text properly:
10 LOCATE 97,3
20 WAIT 10
30 PRINT "Hello World"
This would make the program wait for about a second while it printed the text, and then it would end. You have to put the WAIT command before printing a line of text or else it won’t make any difference. Or you can do this:
10 LOCATE 97,3
20 PRINT "Hello World"
30 WAIT 10
40 PRINT
The first number in the LOCATE command is the X co-ordinate. It is the number of pixels across the screen. The second number is the Y co-ordinate. This is not in pixels, but lines. The one at the top of the screen is 0, and they go down to 6, so there are 7 lines in total.
If you use the command PRINT without printing anything, it works the same as if you do print something. If you use the command WAIT without a number, the program will wait for any key to be pressed before continuing.
If the program carries on after you use the wait command, you must reset the command to 0 or else the program will continue to wait before it prints each line. This can become very irritating. To reset the WAIT command, just type:
50 WAIT 0
That will tell the program not to wait at all.
User Inputs
If you want the program to ask the user to type in some information, like their name, then you can use the code below:
10 PRINT "What is your name"
20 INPUT ":";A$
30 WAIT
40 PRINT "Hello ";A$
This would ask the user for their name, and then say "Hello" and then whatever they typed in as their name. In line 20, the program calls whatever the user types in A$. You can use any combination of numbers and letter. IE, you could use ABCD$
If you want the program to ask the user to input a single letter, then you can use the code below:
10 PRINT "Think of a letter"
20 A$=INKEY$(1)
30 PRINT "You thought of the letter: ";A$
40 B$=INKEY$(1)
This would not display a cursor for the user to type with. The user could only type in a single letter (or number)
In line 40, this is the same as using WAIT except you don’t have to reset WAIT back to 0 and you don’t have to print text after it to make it work. I use this much more than WAIT. I recommend that you use WAIT if you want the program to wait after every line as you don’t have to keep on re-typing it.
GOTO
If you want the program to go to a particular line, then you can use the command GOTO. This is then followed by a line number:
10 GOTO 30
20 PRINT "The program should never get here!"
30 PRINT "This is where the program should jump to!"
40 A$=INKEY$(1)
This code would just skip out line 20
Instead of using line numbers (which can sometimes change if you have to add in more lines), you can use labels:
10 GOTO *START
20 PRINT "The program should never get here!"
30 *START
40 PRINT "This is where the program should jump to!"
50 A$=INKEY$(1)
This would do the same as the first example. You can’t use some particular words as labels, like ones that the program uses, but ones that you can use all have to be started using an asterisk (*)
Subroutines
Subroutines are like separate parts of code. You can set up subroutines for lines of code that you will use a lot in your program:
10 GOTO *START
20 *SEPARATE
30 PRINT "OoOoOoO"
40 RETURN
50 *START
60 PRINT "Hello"
70 GOSUB *SEPARATE
80 PRINT "Bye"
90 GOSUB *SEPARATE
100 A$=INKEY$(1)
This code would print "Hello" and "Goodbye separated by "OoOoOoO"
The RETURN in the subroutine tells the program to return to the plave that it was first told to go to the subroutine
Tips
Here are some basic tips for BASIC:
Instead of putting every command on a new line, you can separate them by a colon:
10 PRINT "Hello World": WAIT 10: PRINT
In all programs for the wizard, there is a 32kb limit. Therefore, try to save as much space as possible. Use subroutines so you don’t have to keep on retyping all of the code.
If you want the program to quit at a certain point, you can use the command END or STOP.
Space out your code. You don’t have to put every new line of code on the next line down. Separate out things like subroutines by a few lines. Just leave these these lines totally blank. Don’t put line numbers in them.
If you want to put some comments to remember an a certain line, you can type REM and then your comment. This does not affect the program at all, and you don’t have to enclose your comment in speech marks:
10 GOTO 30
20 REM The program never gets here
30 PRINT "Hello": REM DON’T CHANGE LINE NUMBER
40 A$=INKEY$(1)
Clearing the screen
The command that is used to clear the screen is CLS. This just goes on a line on its own (with line numbers obviously). It sets all of the pixels on the screen to the off position.
Lines
If you want to draw a line across the screen, you use the commands below:
10 LINE (0,0) - (238,69)
20 A$=INKEY$(1)
This draws a line from the top left of the screen to the bottom right. Unlike the LOCATE command, both the X and the Y co-ordinates are the number of pixels across and down from the origin (the top left corner). The screen is 238 pixels across and 69 pixels down. NOTE: it is quite useful to learn these numbers, as if you are going to use BASIC a lot, then you will find that these numbers come up a lot.
You can do more with the LINE command that just drawing lines. You can draw boxes, and clear part of the screen aswell. To draw boxes, you use the same format and the LINE command, but the 2 co-ordinates are the top left and bottom right corners of your rectangle. You then put a B after the co-ordinates if you want the rectangle to be unfilled, or BF if you want it to be filled. You also put X if you want the pixels in the box to be reversed in colour (useful if you are making menus), S activates all the pixels in the box (makes them all black) and R deactivates all the pixels (makes them all white):
10 LOCATE 10,1: PRINT "Hello World"
20 LINE (10,10) - (30,30),X,BF
This would print the text "Hello World" inside a black box. The text would be white. To clear part of the screen, just draw a filled rectangle over the area you want to clear, and then put ,R after the co-ordinates.
IF…THEN…ELSE
These are three words that you will use over and over again in BASIC programming. They basically mean if a variable is equal to another variable or an expression, then go to a particular part of the code else go to a different part of the code. This may sound confusing, but it is not:
10 INPUT "What is your name? :";A$
20 IF A$="bob" THEN PRINT "Hello Bob" ELSE PRINT "You are not Bob"
30 A$=INKEY$(1)
This code basically asks the user for their name. If they answer bob, then the program says, "Hello Bob". If they answer anything else, then the program says "You are not Bob". This is a very useful piece of code and I use it a lot.
Random Numbers
Random numbers are also useful. They can make the program go to different pieces of the code, as well as just getting a random number.
To generate a random number, use this code:
10 RANDOMIZE: NUMBER=RND 10
20 PRINT "The number is ";NUMBER
30 A$=INKEY$(1)
This will randomise a number between 1 and 10 and then print it. You can call the number what you like, but I called it number. You don’t have a $ sign after it, because it is a number. If it was a letter, then you would. To go to different places in the code, you can use the code below:
40 IF NUMBER=1 THEN GOTO 100
50 IF NUMBER=2 THEN GOTO 200
If you wanted a random letter, you could randomise a number between 1 and 26, and then use the IF…THEN sequence to change the numbers into letters.
Tips II
If you want the program to ground a loop for a specific so many times, then you can use the code below:
10 A=0
20 PRINT "This is the loop"
30 A=A+1
40 IF A>30 THEN END
50 GOTO 20
This basically says that A=0 and after each time the program goes through the loop, A increases by 1 each time. When A is bigger than 30, the program ends
This code is also very useful for doing menus where you select each option with a black box around it (like the menus of the Wizard’s OS). You basically say that IF M=1 THEN draw a box around the first option. If M=2 draw a box around the second option and so on. You put an A$=INKEY$(1) in the code. If the user presses the down key, you put M=M+1, CLS and then go to the top of the loop again. Make sure to put in: IF M>4 THEN M=1 and IF M<1 THEN M=4. That menus that you can go from the bottom option to the top option and vice versa. This may sound incredibly complicated, but it is really quite simple
If you want the user to input keys like [ENTER] and [UP] then you can use ASC:
10 A$=INKEY$(1)
20 IF ASC(A$)=5 THEN M=M+1
30 IF ASC(A$)=4 THEN M=M-1
ASC 5 is the down key, and ASC4 is the up key. Here is a table with some useful ASC values in:
|
ENTER |
10 |
|
ESC |
27 |
|
UP |
4 |
|
DOWN |
15 |
|
LEFT |
15 |
|
RIGHT |
14 |
|
PAGE UP |
6 |
|
PAGE DOWN |
7 |
|
MENU |
9 |
|
NEW |
30 |
|
SPACE |
32 |
|
SYMBOL |
31 |