QBASIC 6 - bits 'n' bobs Some of you may have noticed that, so far, this serious has shown very little in the way of actual direction. To enhance that impression, this tutorial will cover a bit of this and a bit of that and also a few other things, again with no definite direction. Don't worry though, it is leading somewhere - it's leading towards you being a good programmer. Okay, let's get started one some interesting commands: SOUND Frequency, duration This command produces a sound using the PC's internal speaker. Any complex sounds, or music, are pretty much out of the picture but it is possible to produce some neat effects. Frequency is in hertz and duration is in clock ticks, of which there are apparently 18.2 per second. Here's an example program: main: FOR f = 1000 TO 1040 STEP 10 SOUND f, 1 NEXT GOTO main This produces a rather neat alarm-like sound. Play around with different FOR...NEXT loops, or anything else you want to try; you can get some reasonably good sound effects. Bear in mind, though, that I won't be covering playing music or sound files; some things are not possible from QBASIC. RND In this form RND gives a random number between 0 and 0.999999999; there are other ways of using it (take a look at the QBASIC help file) but none of them are much use. Anyway, to get a random number from 1 to 10 you can use: r = INT(RND * 10) + 1 PRINT r For a better description of why this works, have a look at the description of the INT command below. Anyway, another important command in the generation of random numbers is: RANDOMIZE TIMER You don't need to worry about how this works (although if you are curious, as ever the QBASIC help file will explain). You may have noticed with the above code for a random number that you get the same random numbers every time you run the program. The RANDOMIZE TIMER command basically makes sure that every time the program is run you get a new set of random numbers. You need to put it at the start of your program. And that about wraps it up for random numbers. INT This is an immensly useful command which turns any number into an integer, or whole number. Bear in mind that it does NOT round the number; it chops off everything to the right of the decimal place - so: INT(0.9999)=0 INT(1.0001)=1 INT(1.0)=1 INT(47.8994)=47 It's quite simple, really. Anyway, when using the RND function INT comes in useful for selecting a random number between one and anything else. This is done with: INT(RND*max)+1 This works because when you multiply RND by max, you get a number between 0 and just less than max. Using the INT command on this would give you a whole number between 0 and max-1, because any number just below a whole number is rounded down. Then you add one to get a number between 1 and max. No problem. So a dice roll could be simulated with: spots = INT(RND * 6) + 1 Two dice could be simulated with: spots1 = INT(RND * 6) + 1 spots2 = INT(RND * 6) + 1 total = spots1 + spots2 This assumes, of course, that the dice are not biased in any way. DEFINT A-Z Putting this at the start of almost any program will speed it up, sometimes dramatically. It may also stop it working. And here's why: it tells the computer that every variable will be an INTeger, or a whole number. Whole numbers are much quicker to deal with than real numbers, so the program runs faster. However, if the program relies on whole numbers to work, then it will not work properly. Try this program with and without DEFINT A-Z at the start: CLS FOR a = 1 TO 100 STEP .5 PRINT a; NEXT As you can see, the program is speeded up dramatically but it completely fails to work anymore. There is never any harm in trying DEFINT A-Z at the start of a program; it will either work faster or not at all. READ/DATA Okay, this is an interesting one. Suppose you have an array which you need to fill with data form within the program, what do you do? You could do it like This: DIM n$(10) n$(0) = "David" n$(1) = "Helen" n$(2) = "Peter" n$(3) = "Joseph" n$(4) = "Sarah" n$(5) = "John" etc, etc Or, you could do it like this: DIM n$(10) FOR c = 0 TO 10 READ n$(c) NEXT ...rest of program... DATA David,Helen,Peter,Joseph,Sarah,John,etc,etc This would do exactly the same job. The command READ looks for the next piece of data (strangely enough, it must come after a DATA statement) and puts it in the variable specified. You can have any kind of information after a DATA statement although bear in mind that if you're going to use characters like colons you have to put any text in speech marks: DATA "Please specify:",one,two,"The answer is:" There are some other important things to remember about data statements. You must ALWAYS have enough data left otherwise the program will stop and complain. So if you're going to READ 10 pieces of informtion, have 10 pieces of information in the DATA statements. If you want to use some data again, use the RESTORE command. This basically sets the next READ to the first piece of DATA in the program. As always, look at the QBASIC help file definition and example for these commands so that you don't miss any subtleties which I may have left out. By the way, it's often useful to just look through the index of commands and read the definiton of any that look interesting; believe it or not, that's how I learned to program. I think I'll leave it at that, apart from this interesting program which I encourage you to try and understand (although don't stress over it): DEFINT A-Z SCREEN 13 FOR x = 0 TO 319 FOR y = 0 TO 199 PSET (x, y), x XOR y NEXT NEXT And that's yer lot.