Your first QBasic program: 1HELLO.BASEdit
The following paragraph requires a computer with QBasic installed
To begin, write down everything from the program below ("PRINT "Hello World") into a text editor or into the QBasic IDE (Integrated Development Interface) itself and save it as "1HELLO.BAS". Next open the file in QBasic (unless you used QBasic IDE in which case it is already open) and press F5. Optionally you can use the "RUN" menu located on the menu bar at the top of the IDE window. This will execute (run) the program. The words "Hello World" should appear on the upper left hand side of the screen. You have just executed your first QBasic program. If you press F5 again, another line of text saying "Hello World" will appear on the upper left hand side of the screen pushing the first one down to the second row of the screen. You can follow the same procedure for the rest of the example programs in this wikibook.
PRINT "Hello World"
PRINT is QBasic's text output function. It is the command that we will be exploring through this section. PRINT is a QBasic function that requires arguments. The argument in the "Hello, World!" program we just ran were the words "Hello, World!". So, PRINT is the function and "Hello, World!" is the argument we pass to the function.
PRINT [Text to screen]
Note: For a short cut, just use a question mark "?" in place of the command "PRINT". Likewise you can use a single quote "'" in place of the key word REM to insert comments in your code
PRINT "This line will be erased" CLS PRINT "Hello"; PRINT " World", PRINT "Hello Jupiter" PRINT "Good Bye",,"For";" Now" PRINT 1,2,3,4,5
PRINT, Commas, Semicolons, tab (n) and CLSEdit
This is what the program output should look like:
Hello World Hello Jupiter Good Bye For Now 1 2 3 4 5
The first line of 2HELLO.BAS outputs "This line will be erased." to the screen. However, in the second line, the CLS command clears the screen immediately after. So, it will only flash momentarily. The text "Hello Jupiter" should line up with '2' under it. More than one comma can be used consecutively. In this example, after "Good Bye" two commas are used to move "For Now" over two tab columns. "For Now" should line up with '3'.
My final statement on this topic is to play around with it. Try using commas and semicolons in a program.
CLS hello$ = "Hello World" number = 12 PRINT hello$, number
Variables are used to store information. They are like containers. You can put information in them and later change the information to something else. In this first example they may not seem very useful but in the next section (Input) they will become very useful.
In this example we use two types of variables: string variables and numeric variables. A string variable holds a string of characters, such as words. (A character is a letter, digit or symbol.) In this case, the characters are letters. A string variable is denoted by ending the name of the variable with a dollar sign. The string variable in this program is hello$. Whatever value you assign to hello$ will be displayed in the PRINT statement. The numeric variable is number. Numeric variables do not have a special ending like string variables.
CLS LOCATE 14, 34 'position the left eye PRINT "<=>" 'draw the left eye LOCATE 14, 43 'position the right eye PRINT "<=>" 'draw the right eye LOCATE 16, 39 'position the nose PRINT "o|o" 'draw the nose LOCATE 18, 36 'position the mouth PRINT "\_______/" 'draw the mouth LOCATE 19, 42 'the bottom PRINT "The Face by QBasic"
LOCATE allows you to position the cursor for the next piece of text output. Contrary to Cartesian coordinates which read (X,Y), the locate statement is LOCATE Y,X. In this case Y is the distance down from the top of the screen and X is the distance from the left side of the screen. The reason that LOCATE does not follow the standard coordinate system is that it is not necessary to include the X portion; you can use the format LOCATE Y which just specifies the line to start on.
CLS LOCATE 14, 34 COLOR 9 PRINT "<=>" LOCATE 14, 43 PRINT "<=>" COLOR 11 LOCATE 16, 39 PRINT "o|o" COLOR 4 LOCATE 18, 36 PRINT "\_______/" COLOR 20 LOCATE 19, 42 PRINT "U" LOCATE 1, 1 COLOR 16, 1 PRINT "Hello World"
The program 5FACE.BAS is broken into many sections to make it easier to read. This is an example of a good programming habit. Each three-line piece of code specifies what the color, location and form of its part of the face. The order of the position and the color is unimportant. The new statement COLOR allows you to change the color of the text. Once changed, all output will be in the new color until COLOR or CLS is used.
COLOR [foreground], [background]
The colors are designated by numbers which will be discussed in the next section.
Color by NumberEdit
There are 16 colors (in screen mode 0), numbered from 0 to 15.
|6||Brown/Orange||14||Yellow (Light Orange)|
|7||Light Grey (White)||15||White (Light White)|
If you look carefully at this chart you can see that there are 8 main colors (0 through 7) and then those colors repeat, each in a lighter shade. You may also notice that the colors act as a combination of binary values (where blue=1, green=2, red=4, etc.) This makes it much easier to memorize the color scheme. Blinking colors are also available: at 16, the colors start over again with blinking black and extend through 31 (blinking white). However, the blinking option is not available for the background, only for the text (foreground). Add 16 to the color you wish to blink. e.g.: 2+16=18 - Blinking Green, 4+16=20 - Blinking Red.
It is possible to switch the blinking foreground text with an intense background, but this task is beyond the scope of this QBasic textbook, and may not work when MS Windows displays the console in a windowed mode.
On a VGA compatible video card, you can inspect and change the font used in screen mode 0.
OUT &H3CE, 5: OUT &H3CF, 0 'Clear even/odd mode OUT &H3CE, 6: OUT &H3CF, 4 'Map VGA mem A0000-BFFFF OUT &H3C4, 2: OUT &H3C5, 4 'Set bit plane 2 OUT &H3C4, 4: OUT &H3C5, 6 'Clear even/odd mode again
You can now use PEEK and POKE to access the character data. It starts at absolute address &HA0000 and every character is 32 bytes, each of which is a row of eight bits. The highest bit of each byte corresponds to the leftmost pixel of each row. Usually only the first 16 or 8 rows are used, depending on the WIDTH setting.
When you're done, it's important to put the memory mapping back to what QBasic expects:
OUT &H3CE, 6: OUT &H3CF, 14 'Map VGA mem B8000-BFFFF
In this section we looked at several methods to manipulate text output. All centered around the PRINT statement. LOCATE and COLOR modified where the text was displayed and how it looked. We used CLS to clear the screen and gave a brief introduction to variables which will be expanded upon in later sections.