Upgrading Your Body and Mind With Training and Technology
Categories |
How to Programme in QbasicBy Adam Sinicki
Being able to programme in any language is a highly valuable skill that
will enable you to create things as and when you need them in order to
help you perform tasks. Many people think of programming only in terms
of a way to make software that they intend to sell, little realising
that it can be just as helpful for their own personal use to create
software for specific tasks they need - for handling equations and
sums, for doing data entry, for creating websites, for making
simulations, for creating graphics and editing tools and for much more.
At the same time it's also an incredibly challenging an rewarding
process that teaches you to work at solving problems in a logical way.Here we will discuss programming in QBASIC (Quick Beginners' All Purpose Instruction Code) and outline some of the basics. This is a programming language that is completely free to download and use and that can be very simple to learn. At the same time, while it is not advanced enough to handle 3D gaming or other advanced projects, it is a real programming language that is completely flexible for any tasks you want to use it for, and that can be compiled into C. Getting QBASIC Old versions of Windows used to come with a copy of QBASIC already installed but this was mostly pre XP. Today then, you need to download QBASIC yourself. QB 4.5 was the official last version of the language, but some individuals have since put together QBX which can work better with newer versions of windows and includes a good compiler. Installation simply requires all the files to be downloaded (there are loads of sites offering the free download) and then placed into one folder. When you run this a window will open up with an empty blue screen which is where you type your code. When you select RUN from the top menu this will run the programme. Like anything else you can also SAVE and OPEN other QBASIC files (called *.BAS). Basics of BASIC To start you will need to use labels these are line numbers and titles and can be just numbers such as '10' or words with a colon such as 'beginning:'. You can use any labels you like for your lines and your numbers don’t even need to be in order. However it will help you to navigate around the programme with the 'GOTO' command which makes QBASIC continue running the file from the specified point. This programme: 10 GOTO 10 Would then cause QBASIC to endlessly go round in a loop. Normally though the first programme you make in a programming language s the 'Hello World' programme where you get the language to print 'Hello world' on the screen. This simply needs the 'PRINT' command, which prints anything that follows it in quotation marks. To make hello world then you simply need to run: PRINT 'Hello world!' To clear the screen you then use the command 'CLS' (short for clear screen). However if you place this straight after your PRINT command, then the writing would flash up on the screen so quickly that no one would be able to read it. Thus you would use 'SLEEP' followed by a number which is how many seconds before QBASIC moves to the next line. SLEEP '0' meanwhile pauses indefinitely until the user presses a key. Thus: 10 COLOR 2: PRINT 'Hello world!' SLEEP 0 CLS COLOR 4: PRINT 'Goodbye world!' SLEEP 0 CLS STOP Would print 'Hello world!', pause until you pressed something, go blank, print 'Goodbye world!' then pause then clear the screen. Notice we've also included the 'COLOR' command - here the color is indicated by a number (here Hello world! Is in green while Goodbye world! Is in red). Between COLOR 2 and PRINT is a colon, and this has the same effect as putting the two commands on the same line. STOP meanwhile ends the programme. The basic color codes for QBASIC are: 0 = black 1 = blue 2 = green 3 = turquoise 4 = red 5 = magenta 6 = orange 7 = light grey 8 = dark grey 9 = light blue You can also use graphics in Qbasic. Here you first need to change the screen 'mode' (resolution) from the default 'SCREEN 0' to either SCREEN 9 (high res) or SCREEN 13 (more colours) to stat art. Here all commands use coordinates that are in brackets. 'PSET' for example is short for 'pixel set' and draws a point anywhere no the screen. PSET (100, 150), 3 For example then would draw a dot 100 pixels along and 150 down. The comma with the 3 after denotes the colour, so here the colour is turquoise. From here if you use the command DRAW you can then tell it to draw any shapes. DRAW 'R10L4U10R4' draws right 10, left 4, up 10 and right 4 again - so a rectangle. Other commands are: LINE (100, 150)-(100, 200), 3 Which would make a vertical line, while: CIRCLE (30, 20), 3, 2 Would make a green circle (the two) with a radius of 3 pixels. Finally you can also play sound using PLAY. Which looks like: PLAY 'ABCDEG16>AGA4D2' Here the letters are notes the '>' means up an octave ('<' means down) and the numbers are beats (these can only be multiples of 2). Strings, Variable and Inputs For real programming to begin however you need to also understand strings and variables. These are words or letters that can be given any value. A string is anything with a '$' sign after it so name$, while a variable is just a word or letter. Variables contain numbers, while string$ contain data. To make a variable or string you just write its value at the top. So: 10 Height = 200 Shade = 4 PSET (20, Height), shade Would put a dot at 20 along, 200 down and colour it red. Meanwhile you can also do maths in a variable so: Height = 20 * 10 PSET (20, Height), 4 Would put the height at 30. Where you get clever is that you can then make things move - for example: Left = 1 10 PSET (left, 10), 4 Left = left + 1 CLS GOTO 10 You will then have a dot that moves quickly to the right. If you want it to move more slowly you can make Left = left + 2.
Here we will demonstrate how to get QBASIC to print the Fibonacci sequence. Rib = 1 10 PRINT fib Fib = fib + fib SLEEP 0 GOTO 10 Strings meanwhile are text, so you would use it only really with PRINT. For example: Name$ = 'Adam' PRINT 'Hello'; name$; '!' This then prints 'Hello Adam!' on the screen, but notice how the name$ is outside of the quotation marks (with semi colons) to avoid confusion. You can also get the user to input a string… PRINT 'Hi, what's your name please?' INPUT name$ PRINT 'Hi'; name$; '!' This then asks for the user's name, then brings up a cursor, saying hi to that person after they press enter. This is one form of input and this is how the user interacts with the variables on the screen to make the programmes interactive. They can also interact in real time in order to change variables like so: 10 PSET (20, 10),4 SELECT CASE INKEY$ CASE 'P' X = x + 1 CLS CASE 'O' X = x - 1 CASE 'E' STOP CLS END SELECT GOTO 10 Here 'SELECT CASE INKEY$' tells it to expect input, while the following parts mean if the input is 'P' then X = X + 1 and the pset moves to the right, if they press 'O' however then it moves to the left. Everything beneath the 'CASE 'E'' is what happens if you press 'E' until the next 'CASE' or the 'END SELECT'. IF, AND, OR, ELSE and THEN Finally this is where things get interesting and you can create interesting sequences of events. The 'IF, AND and THEN' commands all do precisely what you would expect them to in English, but are then followed by commands or maths. For example: PRINT 'What is your name?' INPUT name$ IF name$ = 'Adam' THEN PRINT 'My name is Adam too!' ELSE PRINT 'Hi'; name$; ', nice to meet you!' Alternatively you could use it to make games. Here is a very simple game: X = 1 10 PSET (20, 10), 6 PSET (x, 10), 2 SELECT CASE INKEY$ CASE 'P' X = x + 1 CLS CASE 'O' X = x - 1 CLS CASE 'E' STOP END SELECT IF x = 20 THEN GOTO out GOTO 10 Out: CLS PRINT 'Well done you reached the gold!' SLEEP 0 STOP Here you have a green dot, you press 'P' to move it right across the screen, and when it reaches the red dot (20 along) it tells you well done and exits on a key press. Of course all this is really just a very basic introduction to QBASIC and does not really touch on the huge number of things you can do with it. However with just these few tools, it is possible to make a vast range of different programmes and games, and to start learning extra code on top to give them even more uses. Discus this article on the FORUM! Copyright 2012 The Biomatrix.Net
|
|