Issue 2 - April 2000


Introduction to Qbasic series #2


 

Writer: Matthew River Knight

Welcome to part 2 of the Introduction to Qbasic Series, QB newbies! I hope you have been doing some experiments with what you have learnt! Last month we covered only the simplest of Qbasic statements/functions. With that knowledge it would be possible to make some simple programs...pherhaps even a very simple game...but that's not where we're headed with these tuts! We want to make kick @$$ games like Wetspot 2 and Shadow of Power! Yeah! ^_^

There is a lot that we have to cover in this series...I want to go over a few more of the Qbasic text handling routines, after which I'd like to cover the basics behind graphics in Qbasic, and the various methods of controlling program flow. I'll bet even at this stage of the tutorial you're confused... don't worry, soon it will all make sense!

Okay, to get started, let's make a program similar to the type of programs that we were doing last issue - but with a few enhancements!

'Program to test if the product of two numbers is greater than 10.
CLS
COLOR 1
INPUT "Enter your 1st number:", n
INPUT "Enter your 2nd number:", n2
product = n * n2 'Multiply the two numbers together.
COLOR 12
IF product > 10 THEN PRINT "Your 1st number x 2nd number is bigger than 10"

Alright, the above program introduces several new concepts of programming in Qbasic. The first thing you're probably wondering about is the COLOR statement. Let me explain...the COLOR statement is used to select the color of the text that will next be PRINTed or displayed from within an INPUT statement.

The COLOR statement is always followed by a number. In the example program above, it is 1. The number tells the computer which color to use. The number may range from 0 to 15, and each number displays a different color. Try experimenting with different numbers and see what color is shown!

Okay, now that you've mastered that, lets's do something a bit cooler with COLOR. COLOR not only allows you to select the color of the text being PRINTed, it also allows you to select the background color! Let me demonstrate this by means of an example. Try this:

COLOR 2, 5
PRINT "Pretty cool, huh?"

Let's get back to the example code we wrote earlier. The last line of the program uses the IF statement. We have never encountered IF before, but it is likely that you can already see how it works. The line of code translated to english means "If number is greater than 10 then print...". Simple no?

There are other ways that you can use IF. It can also be accompanied by the ELSE statement. Let's demonstate this by example:

INPUT "Enter a number:", n
IF n = 10 THEN PRINT "Number is 10" ELSE PRINT "Number is not 10"

The ELSE statement really has exactly the same meaning in Qbasic as it does in english! Wow! Is Qbasic easy to understand or what?!

The last thing you are probably wondering about is the ' character in the code. That means the same as REM. It is slightly more versatile than REM though, since you can also use it like this:

PRINT "Hello world" 'Say hi to the world

You are advised to use ' instead of REM since it is more versatile and it is obviously much easier to use.

Controlling program flow:

Up until now we have only written programs that when run are executed from the first line, down to the last, one after another. Fortunately we are not limited to this. It is infact possible to make the computer go to another line of code, run it, then jump somewhere else in the code, and run code from there, and so on.

Before I explain this in any further detail, it is necessary that I explain what line labels are. You may not be aware of it but a programmer is capable of giving a certain line of code in his program a name. There are two ways of doing this:

You can either give it a name which is just a number, like this:

10 PRINT "Hello, this is line of code 10"
11 COLOR 2
12 PRINT "Hello world!"

Or, you can give your lines of code more meaningfull names like this:

FirstLineOfCode: PRINT "MyProg.bas - Written by Joe Soap"
PRINT
PRINT "What is your name"; name$
PRINT "What is your age"; age
WriteName: PRINT name$
WriteAge: PRINT age
EndOfCode: PRINT "Goodbye!"

You can also use a combination of the two if you want.

Okay, now let's see how we can make the computer read lines of code in different ways instead of just top to bottom!!!

PRINT "Hello"
GOTO EndOfCode
PRINT "This line will be skipped"
EndOfCode: PRINT "Bye"

The above program uses a new statement: GOTO. As you probably already guessed, GOTO stands for go to. It is used to GOTO a certain specified line label. Let's try another example just to clarify this further!

count = 0
Start: PRINT "This will be PRINTed 10 times"
count = count + 1
IF count <10 THEN GOTO Start
PRINT "That's it!"

The above program uses GOTO to perform what is known as a loop. This is a really bad way of performing loops. There are better ways of doing this. Here's a demonstration:

FOR n = 1 TO 10
PRINT "This will be PRINTed 10 times"
NEXT
PRINT "That's it!"

This program uses a FOR...NEXT type of loop structure. Everything in between FOR and NEXT will be done 1, up until 10 times. Try experimenting with this!

What actually happens in the FOR...NEXT loop is that the variable, in this case n, starts at 1, and is then increased by 1 until it reaches 10. When n is equal to 10 then the loop stops! Simple eh?!

But what if we didn't want the variable n to increase by only 1 at a time? Well, it is actually possible to make it increase by any number we want! Let's demonstrate this by means of an example:

FOR n = 1 TO 20 STEP 2
PRINT "Another way of doing a FOR...NEXT loop"
NEXT

In the above example, n is increased by 2 every loop! You could have made the STEP any number you like. Try it! Experiment! That's the best way to learn about programming!

FOR...NEXT loops are not the only kind of loops in Qbasic. There is another type too! Let's demonstrate it as an example:

number = 0
DO
PRINT "Loop the loop!"
number = number + 1
LOOP UNTIL number = 10

As you can see, the DO...LOOP performs the loop until the variable called number is equal to 10.

What if you wanted to create a loop that would go on forever you ask? Well, it's simple to do with DO...LOOP. Try this:

DO
PRINT "This message will go on forever!"
LOOP

In the above program the message will continue to be PRINTed for as long as your computer is on, or until you push reset. In order to get out of the program, just press CTRL and BREAK together.

It's up to you whether you want to use the FOR...NEXT or DO...LOOP type of loop structures in your code, though, for certain types of programs you will find the one type of loop more useful than the other.

Graphics:

Until now we have only written programs that display text. However, text is rather boring, and it would be nice to know how to draw graphics eh?!

Okay, in order to use Qbasic's graphics functions we are first going to have to tell the computer which graphics mode to use. You have probably all heard of CGA, EGA, VGA and SVGA. These are all different graphics modes.

CGA is the worst of the graphics modes. It has only a few colors available for its use, and any graphics drawn in this mode will be very blocky.

EGA is a little better than CGA. The graphics will also be blocky, but at least you have 16 colors to play with, which is much better than in CGA!

VGA is the mode used by most Qbasic programmers today. It is also the newest graphics mode supported by Qbasic since it was made quite a long time ago. But don't worry, lots of Qbasic programmers, including myself, are constantly working on new functions for Qbasic, and many of these functions allow you to use graphics modes more up to date than VGA! In VGA you have 256 colors to play with. With this you can do some pretty cool graphics.

SVGA is the latest in graphics modes. It is not supported by Qbasic, although new add-on functions have been made for Qbasic which does allow you to use it. SVGA comes in many flavours. We'll discuss this screen mode at a later stage, when we have improved our programming abilities.

Okay, in order to tell the computer which screen mode to use, we use the SCREEN statement. SCREEN is followed by a number, and this number tells the computer which screen mode to use. To use CGA we type SCREEN 1. To use EGA we type SCREEN 7. To use VGA we type SCREEN 13

There are a number of other numbers that can be put after SCREEN, but the ones that I have listed above are the most commonly used ones.

Now it's time to learn our first graphics drawing function! Let's first type in some code and then we'll explain how it works afterwards...

SCREEN 13
PSET (160, 100), 14

All that this simple program does is draw a dot (known as a pixel) on the screen in yellow, in SCREEN mode 13(VGA).

It is now necessary that I explain what the term screen resolution means. Your monitor is like a grid. Every square on the grid is a place where a pixel can be drawn. In SCREEN mode 13, there are 320 blocks on the grid going across your screen, and 200 going downwards. SCREEN mode 13 is thus said to have a screen resolution of 320x200 since there are 320x200 blocks on the 'grid' in that screen mode.

In the example code above, we used the PSET statement to draw a dot on the screen. Let's say we wanted to draw a red dot on the screen 'grid' that is 10 pixels across on the grid (measured from the top left hand corner of the screen), and 100 blocks down on the 'grid'. We would use this code:

SCREEN 13
PSET (10, 100), 4

As you can see, the syntax for PSET is:

PSET (x, y), c

where:

x is the horizontal position on the screen 'grid' measured from the top left hand corner of the screen.

y is the vertical position on the screen 'grid' measured from the top left hand corner of the screen.

c is the color of the pixel you want to draw.

This is all very good, but it would take a very long time to draw a car, for example, using PSET. Can you imagine trying to draw the whole thing one pixel at a time?! That would take all day!

I bet right now you'r thinking "Boy would it be great if there was a Qbasic statement to draw lines!" Well, guess what, there is! And it's called LINE!

LINE is used like this: let's say we wanted to draw a blue line from the PSET position (10,10) to (80,95) then all we would have to do is this:

SCREEN 13
LINE (10,10)-(80,95)

That's it! Simple eh?!

If you're still confused, take a look at Provgamer's excellent graphics tutorial, which explains these graphics commands in greater depth.

There are a lot of other Qbasic graphics commands, but this entry into the series has got far too big already! We'll cover that another time! Until next issue, experiment with all that you have learned so far! It would not be a bad idea to try make a simple game of some sort - an adventure game, pherhaps. If you make anything, and you would like others to see it, then send it to me via email and I'll include it with the next issue of QBCM!

Happy programming!