QBasic Tutorials: Starting QBasic Part 8 - 9

Part 8 - Drawing

Many programmers want to draw in qbasic and there are many different ways, depending on what you want to do!! Here is the code to draw a circle in screen 2 :

SCREEN 2 CIRCLE (x , y), radius, color

The x and y stand for the coordinates you want the circle at, so you define these a number eg 10, the radius is how big you want your circle, eg 10, and the color is used the same as the color statement in part 7 so you also give that a number eg 3!!

In screen 2 you cannot use colors!!!

So an example of the code you could use would be :

CLS SCREEN 2 CIRCLE (100,100),5,5

This will draw a small circle, to draw a larger circle, you just increase the radius!! To draw a line in screen 2, (obviously where x and y are your starting points, and x2 and y2 are your finishing points) the code you need is:

LINE( x, y)-STEP( x2, y2)

We recommend screen 12 and 13, for drawing as they have the highest resolution.

Part 9 - The Input Statement

Input is a handy bit of qbasic code. What is it I hear you ask,well input means you can ask the user a question and they can reply to it! Also as you learn more you can use the data entered by the user for different things.

eg 1 - The Basic Question

The basic input statement is to ask a question to the user, to do this you use the following code :

INPUT "What is your name" ; q1$

Please note you do not need a question mark because with input 'it automatically puts one in for you. Also the q1$ is there as qbasic stores the answer into a variable called q1$ - there doesnt have to be a number just a letter or word, make sure its a good word so you remember what its for, i chose q because it is for question, it could have been called question$, or anything! Although it has to be a $.

eg 2 - Using The Results


To use the results of your question is easy, it just involves the print statement which you have already learnt! I will show you two ways of displaying the answers to the question, D1 and D2:

D1 :

CLS INPUT "What is your name" ; q1$ PRINT "Your name is " ;q1$

This stores the answer into the variable q1$ and then you are telling qbasic to type the answer back out in the print statement. Please note to put a space after the "name" part in the input statement or it will print; name and whatever the variable is as 1 word.

D2 :

CLS INPUT "What is your name" ; q1$ CLS PRINT "Your name is " ;q1$ ;" and i dont like you"

All i have done which is different to D1 is i have added a clear screen so the print statement is on a blank screen, and looks better. Also I have added an extra bit at the end of the print statement, this is just something I thought may be useful, it just means the name is put into the middle of a sentence.

eg 3 - Using numbers

There is a few good things qbasic can do with numbers in the input statment, however you have to change the "$" to a "%" sign, so instead of being a variable it is stored as an integer and qbasic knows that it is a number! This means if qbasic knows its a number you can do some better things with it. Here is an example of how you tell qbasic you want the answer stored as an integer :

CLS INPUT "What is your age " ; age% CLS PRINT "So you are ";Age% ; " years old"

This puts the information into the integer age%, we can use this to perform simple sums :

CLS INPUT "What is your age " ; age% CLS PRINT "So you are ";Age% ; " years old" INPUT "What is your dads age " ; dadsage% dadsage% - age% = differenceage% CLS PRINT "The difference in years between you and your dad is " ; differenceage%

This has included a sum in to work out the difference in years between you and your dad, it is just a simple minus sum! And from this it has produced a new integer called differenceage%, as you know this could be called anything!

This is the end of the StartingQB we hope it has helped be ready for part 2 and the other tutorials that we will produce! Keep checking Pickers Games Website, at www.pickersgames.co.uk If you have any problems I can be emailed at: pickers@pickersgames.co.uk

thanks
Pickers
Pickers Games
www.pickersgames.co.uk
StartingQB was finished on 06/01/01