Microsoft QBasic
Click here for site map

Home
Intro
Lessons
Library
Reference
About
www.microsoft.com

This is a line. . .a short one

Lesson 3

This is a line. -------- If you don't know what a line is, you're brain dead.

What you will learn in this lesson
  • Simple Graphic Commands
  • QBasic Mathematical Signs
  • Advanced Control Flow

Lesson 3

This is a line. -------- If you don't know what a line is, you're brain dead.

Lesson 3 today! We're going to do palm printing! Dig out those paints from kindergarten, and get ready your palms. Dig out that old print from kindergarten and try to copy it. . . Here we go. Fasten your seat belts, you don't want to die young, do you?


Simple Graphic Commands

Types of Graphics you can do

Your Art teacher taught you how to draw stick figures. Your Literature teacher taught you how to draw a picture of Dorian Gray. And now, we're going to teach you how to draw simple graphics on the screen. Makes a change from your staples of text and numbers, right?

Remember the Locate command we learnt back early in the Module? Well, the first command we are going to teach you is by far the simplest, and it uses about the same principle as that Locate command. This new command is the Line command, and it very simply draws a nice line on the screen. Here's the syntax for the command:

Line STEP(x,y)- STEP(x,y), color%, B or BF, type%

Let's go through the syntax bit by bit so you can understand what each term is.

Line - This is the basic command to draw a line.
Step - If this optional command is added in, the coordinates specified after it will be relative to the cursor's current position with locate. For example, if I have a Locate 15, 15 before, then I type Line Step(1,1)-Step(2,1) , then the actual output will be a line from 16,16 to 17,16 and not 1,1 to 2,1. Get it?
(x,y) - The first x,y is the Locate coordinates where the line should start. The second is the Locate coordinates where the line should end. Remember to put them in brackets.
color% - This is a number, same as specified in the Color command, that determines the color of the line.
B or BR - If you type in B here, an empty rectangular box will appear with its top left corner at your 1st x,y coordinate and its bottom right corner at your second x,y coordinate. If you type in BR here, the box will be filled with the color specified for color%. If nothing is typed in the output will remain a line.
type% - This is a 16-bit value, namely $HFF00 ; if added, this will make the line look like this: - - - - -

Remember to add in your commas!

Back to Top


Different graphics you can create

That's it for a line. But what's the next step? Well, what do four lines make? A Rectangle! And what do 3 lines make? A Triangle! And what does a half-dead worm when its bent over look like? A Circle! Yes, a circle! And that's what we're going to do. Have a look at the syntax.

Circle STEP(x,y) radius%, color%, start%, end%

Let's go over it like just now, so you can understand this command better.

Circle - This is the basic command to draw a circle.
Step - This has the same implication as in Line.
(x,y) - These are the Locate coordinates where the centre of the circle should be.
radius% - This is how long the radius of the circle should be in pixels.
color% - This determines the color of the circle.
start% - This is the angle that the starting "imaginary line from centre to end" should start from. It is used to draw irregular circles.
end% - This is the angle the "line" should end with.

Back to Top


So, now you've learnt to draw circles! Isn't that great? Try making circles, it's really fun. And as for triangles, create them using the Line command, just draw 3 lines. But how do you fill your shapes with colour? Well, that's what the next command does; the Paint command fills areas with colour. Spolsh and slosh paint everywhere! Read on to find out.

The Paint Command

Here's the code for the Paint statement:

Paint STEP(x,y), color%

And as before, the code is dished out on a silver platter for your scrutinization. Phew!

Paint - This is the basic command to fill the area.
STEP - This has the same implications as in Line and Circle.
(x,y) - These are the Locate coordinates where painting starts.
color% - This is the color QBasic should fill the area with.

Note - Note: Remember those days when you were a kid and your dad was painting the house? The Paint command is something like that. If your dad kept the paint cans behind locked doors, you couldn't get the paint and make a mess with it. Now if he left the paint can right in front of you, what would happen? Splosh, splosh, splosh! Waaa, waaa, waaa! You make a mess (and get whacked)! Similarly, if your shape isn't enclosed when you try to paint, it overflows into the screen and makes a mess! So be careful!

Back to Top


Graphics are fun, eh? Much more fun than text. Anyway, the last commands we are going to teach you are the Pset and Palette and View commands. We shall teach you the Palette Using command later in Module 2. Well, what Pset does is it draws a point on the screen, a small dot like this: .

I don't think theres much use for it, but you never know. Anyway here's the syntax:

Pset STEP(x,y)

I think you can guess it out. Use your brains. Next up is the Palette command. Well, Palette allows you to create your own custom palettes. Just type Palette and the colour you want to assign the value to, e.g. 4, and then the number you want to refer to this colour as, e.g. 5. So Color 4 in your screen mode is now known as Color 5. e.g. Red is now Color 5, instead of 4 as it was formerly. It's very simple.

Finally, the View command. If you look at the QBasic help file, View is a command that allows you to create a viewport to place graphics in. Here's the syntax:

View (x,y)-(x2,y2), color%, border%

View is of course the basic command. (x,y) and (x2,y2) are the Locate coordinates of diagonally opposite corners of the viewport. color% is the color of the viewport. And border% is the border color of the viewport. Easy!

Well, that's all for Graphics, now it's Control Flow again!

Back to Top


Advanced Control Flow

Advanced Control Flow with For, Next, Do and Loop Until

Welcome back to your dear old friend Barney the Dinosaur! Oops, I meant Control Flow. In Lesson 2 you learnt simple control flow with the If and Then commands. But these commands can only do so much. So now we are going to teach you a few new commands, mainly the Do, Loop (Until), For and Next commands. These commands will make up the bulk of our section on Advanced Control Flow.

Let's start with the Do and Loop commands. Basically, the Do command specifies the start of a Do, Loop loop. So you have something like this:

Raw Code

SCREEN 12: COLOR 15: CLS
a = 0
DO
LOCATE 1, 1: PRINT a
a = a + 1
LOOP UNTIL a = 100
END

Program Output

(a counter starts from 0 to 100 here)
0 1 2 3 4 5 6 7 8 9 10. . .and so on 
(the numbers all appear at the top left-hand corner)

Dissection

Do and Loop Commands

So you've seen the Do and Loop commands in action. And weren't you amazed? Look at the grace, the elegance, the style! That's the beauty of QBasic! Basically, the Do statement is used by itself, as you can see in line 3. The lines of code between the Do and Loop commands is known as the sequence. This sequence is repeated by the Loop command until a condition is met. (Of course, if you don't specify a condition, it keeps on going for ever!)

Thus, as you can see, in the example above, we have set the condition to "When a = 100". Notice that in the second line we set the value of a to 0. Now the sequence we ask the Do command to repeat has this line: a = a + 1. In short, every time QBasic repeats this sequence, a will be a + 1 (or 1 more than before). Then we ask it to print a at the top left hand corner. Thus the output we get is a swirling counter going from 1 to 100 before it stops. Why does it stop? Because the condition has been fufilled (a is now 100), and thus QBasic moves on. Get it? In other words, Loop means "repeat the sequence until this condition is met".

P.S: So, obviously, something in the sequence must be done to somehow meet the condition sooner or later!

Back to Top


The commands For and Next are very similar to Do and Loop. The syntax is:

FOR (counter) = x TO y STEP z
Print "haha"
NEXT (counter)

Well, here are the meanings:
For (counter) - This signifies the start of the For, Next loop. (counter) is a numeric variable used as the loop counter e.g. c%
x To y - This signifies how many times the sequence should repeat in relation to z, for example 1 to 15 with a z of 3 will repeat it 5 times. 6 to -6 with a z of -3 will repeat it 4 times. Some simple math here.
Step z - This is the increment or the amount the counter is changed each time through the For, Next loop.
Print "haha" - In this loop, this line is the sequence that is to be repeated (something like the Do, Loop loop).
Next (counter) - This signifies the end of the loop.

In this case, we have told QBasic to loop the thing until the counter is finished, not using a variable or a condition. That's the difference between this and a Do, Loop loop. Here the numbers are fixed. However, Variable Subsitution can be used here in the For statement (to replace x and y). You got it? So learn this well.

Next up, QBasic math signs, then Lesson 4! So keep going, kids, its gonna be playtime soon. . .

Back to Top


QBasic Mathematical Signs

The last topic for this lesson is here at last. All you have to learn is these mathematical signs that QBasic recognizes:

= - Obviously this is an equal sign!
< - Is less than
> - Is more than
<= - Is less than or equal to
>= - Is more than or equal to

But how, you may ask, how do we use these signs in QBasic? Well, they are very useful in our Control Flow commands. . . we use them to help us set our conditions. For example:

a = 0
DO
a = a + 1
LOOP UNTIL a >= 50

See. . . the condition is "until a is more than or equals 50". We used the >= sign to set that condition. So, that's how you use them! Now finally a last command to learn: Randomize. Here's the syntax and explaination:

RANDOMIZE TIMER
x% = INT(RND * y%) 

Randomize Timer - This activates the Randomize thingy.
x% - This is the variable you want to define with the value created by Randomize.
INT - This tells QBasic that you want a random number and not anything else! INT stands for integer (that means a number!). I don't think you want QBasic to generate a random string of text, right?
RND - Emm. . .this means. . .Random?
y% - This is the number the QBasic will generate up to. . .e.g. if its 2, QB will give you either 0, 1 or 2.

Take heart, people. You did it! It's the end of Lesson 3! Well. . . not quite. There's still the Assignments. . .

Back to Top


QB Assignment

Create a casino program (No, we Do Not encourage gambling) in QBasic that asks the user the number of rounds he wants to play, the amount of money he wants to bet each round, and for his bet (a number from 1 to 12). Make him start with 50 money. Also, make the program generate a random number from 1 to 12, and if the inputted number is the same, he wins 4 times his bet. Obviously he loses his bet if it isn't the same. If at any time his money reaches 0, he is to be shown a "loser" screen. If he reaches 200 money then show him a winner screen. Display the amount of money and round number at all times at the top corner.
Click here to check your answer!

QB Assignment

Create a Timer "Nag Screen" Program that shows a rebuking face on the screen (use graphic commands) and a message saying that you are using an unregistered version of a program and how to register. Include a timer that makes the user wait from 2000 to 0. This timer must be visible at all times.
Click here to check your answer!

QB Assignment

Draw a nice picture of yourself with Graphic Commands! Try to make it look nice. . .
I don't think we can make a standard photo for this. . .Ha Ha Ha.

Have fun!

(P.S It's the end at last. No kidding.)

Back to Module 1

This is a line. -------- If you don't know what a line is, you're brain dead.

This is an imagemap menubar. Hey I think you know what an imagemap is.

Home Intro Lessons Library Reference About
[ Home ] [ Intro ] [ Lessons ] [ Library ] [ Reference ] [ About ]

This is a line. -------- If you don't know what a line is, you're brain dead.

This Webpage is ThinkQuest entry Unknown.
Email: lccorp1997@hotmail.com

This Webpage is Designed for Netscape Navigator 3.0 and MSIE 3.0
We prefer a 24-bit True Color mode with 640x480 screen mode.
This Webpage created for Thinkquest 1998.