29
1.1 - Introduction To Programming A program is essentially something you write that's easy for you to understand because it's written in a form understandable to people. However, when you RUN or COMPILE it (make the computer do something with it) it's first changed into a form which can be run by the computer, and would be very hard for the average person to understand without a great deal of effort and training. How do you write a program. Basically, you write commands. These are special keywords telling the computer to do something. For example, one of the simplest commands in BASIC is the PRINT statement. It goes like this: PRINT "Hello!" When you type this in (you'll need a special program which can interpret BASIC, which I'll get to in a minute) and RUN it, you'll see a blank screen come up with the word Hello! in the corner. We can then conclude that what the print statement does is print out text on the screen. Then, why the "" around Hello! ? It will make sense later as you learn about variables, subprograms and other such more complex parts, but for now just accept that that is the syntax it should be in. You should have quote marks around the text to print. Don't assume though that all commands are a command followed by something in quote marks. That couldn't be further from the truth. Now, I guess maybe you'll want to be able to actually write and run programs. The program we'll be making the programs with is called QBASIC. You might be able to dig it up on your windows CD, but if not, I recommend getting ahold of QB 4.5 somehow. 1.2 - A few simple commands We'll start with a few simple commands. The first you already learned - PRINT. Here's a few more, which we'll go over: COLOR LOCATE CLS

Basic Programming Techniques in QBASIC

Embed Size (px)

DESCRIPTION

Basic Programming in QBASIC

Citation preview

Page 1: Basic Programming Techniques in QBASIC

1.1 - Introduction To Programming

A program is essentially something you write that's easy for you to understand because it's written in a form understandable to people. However, when you RUN or COMPILE it (make the computer do something with it) it's first changed into a form which can be run by the computer, and would be very hard for the average person to understand without a great deal of effort and training. How do you write a program. Basically, you write commands. These are special keywords telling the computer to do something. For example, one of the simplest commands in BASIC is the PRINT statement. It goes like this:

PRINT "Hello!"

When you type this in (you'll need a special program which can interpret BASIC, which I'll get to in a minute) and RUN it, you'll see a blank screen come up with the word Hello! in the corner. We can then conclude that what the print statement does is print out text on the screen. Then, why the "" around Hello! ? It will make sense later as you learn about variables, subprograms and other such more complex parts, but for now just accept that that is the syntax it should be in. You should have quote marks around the text to print. Don't assume though that all commands are a command followed by something in quote marks. That couldn't be further from the truth.

Now, I guess maybe you'll want to be able to actually write and run programs. The program we'll be making the programs with is called QBASIC. You might be able to dig it up on your windows CD, but if not, I recommend getting ahold of QB 4.5 somehow.

1.2 - A few simple commands

We'll start with a few simple commands. The first you already learned - PRINT. Here's a few more, which we'll go over:

COLORLOCATECLS

To start, let's go over color. Well, like it sounds, it has something to do with color (DUH!). It's important to note that most of the time a command with either be a descriptive word about what it does, or an acronym for a descriptive word.

COLOR does one thing. It changes the current color. What do we mean by current color? Well, when you use the print statement the text will be printed in whatever the "current color" is. This by default is grey. You use COLOR to change this. Unfortunately, COLOR is just a little bit complicated because instead of just saying:

COLOR Green

You have to use special numbers which are assigned to each color. The code number for green is 2. So let's see color in action by adding to our earlier example:

Page 2: Basic Programming Techniques in QBASIC

COLOR 2PRINT "Hello!"

Our first program print Hello! in the default grey. Now that we put

COLOR 2

in front of it, it will print that same text in green. Notice that the number for the color is NOT inside quotes like the text for print was. Commands that take numbers generally don't need the number in quotes.

Here's a handy table which lists all the colors and their code numbers:

0 = black

4 = red 8 = grey 12 = light red

1 = blue5 = magenta

9 = light blue13 = light magenta

2 = green

6 = brown10 = light green

14 = yellow

3 = cyan 7 = white 11 = light cyan 15 = bright white

It's time to make another important point. What should this program do?:

COLOR 6PRINT "Hello!"COLOR 1

Will it print the text in brown, or in blue? The answer is brown. Why? BASIC programs are in order, line by line. Even though

COLOR 1

changes the current color to blue, it will have no effect because of when it happens. This command comes after the print statement, so it doesn't affect it - by this time, the print statement has already done it's job. However, this program will print the text in both brown and blue:

COLOR 6PRINT "Hello!"COLOR 1PRINT "Ahoy hoy"

It will print "Hello!" in brown, then on the next line down, "Ahoy hoy" in blue.

So, a print statement not only takes into account what the current color is, but also the current row number it should print on. Print automatically prints on the next line unless you tell it otherwise. You do this by using LOCATE.

Page 3: Basic Programming Techniques in QBASIC

LOCATE takes two things with it called parameters. The number that color needs to work, or the text that print needs are also called parameters. Basically, any information you give to the command to make it go is called a parameter. For locate, you give it two numbers. First the row number then the column number. The screen has about 24 rows and 80 columns. Each number can only be whole (because there's no in between) and must be >0. For example, to move the text down to the 5th row:

LOCATE 5,1

Again, no quotes here. Perhaps you want to move it to the middle of the screen:

LOCATE 12,40

Which is about the middle of the screen. Locate is pretty easy to understand. Just don't give it numbers too big, or less than zero, because you'll get error messages and your program won't work. Something even as minor as that is called a bug. Little things like that can mess up a whole program, so watch even for such minor details. It can be very irritating to find when your program is in the 100s or 1000s of lines.

That's simple enough. Here is a command even simpler: CLS. This is an acronym for CLear Screen. It does what it sounds: Clears the screen. The screen is not automatically cleared of text after you run the program, so you may notice junk text leftover if you run a program over and over. Usually you'll want to start a program with CLS, but as you go on you'll find less and less use for it.

1.3 - Interaction

Well, it's all fine and dandy to be able to give the user some information, but to really do anything interesting you'll need to get input from the user. To start, we'll get input by asking questions and so on, or prompting them to type something in. Later, we can make games by using the keyboard as a control. And if you're really ambitious, with a deep understanding of BASIC you can move on to using the mouse (this is a rather technical procedure if you're into not just doing, but understanding - which i advocate: it's for advanced programmers only). So, here's what we'll be looking at:

INPUTIF...THEN...ELSE

Again, it does like it sounds. It gets some input. This one's a bit different though. The parameter is a variable.

Variables are a BIG part of programming. Unlike in the math sense, you use them to store things. There's different kinds of variables to store the different types of data. For example, there's numeric variables to store.... numbers, of course. Then there's string variables to store strings (one or more characters of text. the message in the earlier print statement is a string). There's actually more than one type of numeric variable, but don't worry about that as of yet. So, let's make a simple program to ask the user their name.

Page 4: Basic Programming Techniques in QBASIC

PRINT "What's your name?"INPUT n$PRINT n$ ; "? What a terrible name!"

So, what here does input n$ do? It tells the computer to prompt the user to type something in, then it stores whatever they type in in that variable. They'll probably unwittingly enter their name, only to be told by the computer a second later that it's a terrible name. But something is different about this print statement. It has a semi-colon. It's very simple - in a print statement, you can connect multiple things with a semicolon. You need it if you want to print out both n$ and ? What a terrible name!, on the same line. Notice that you can print out variables as well as plain messages. Because n$ is not in quotes, it prints out whatever's in that variable. If it was in quotes, it would literally print out n$ on the screen, rather than whatever was entered through INPUT.

Notice now that the variable is called n$. Why is it called this specifically. Well, you can name a variable whatever you want! It just has to only numbers and letters, and cannot start with a number. But doesn't the $ at the end break this rule? NO. That $ tells the computer that this variable will hold a string. If you drop the $, it signifies that it's holding numbers. It may seem that a string is no different than a number, but that's not true. Numbers and strings are treated differently, so properly label your variables!!

Names of variables can be pretty long. I could have chosen name$, username$, nameofthepersonusingthisprogram$, anything i want pretty much. But i prefer short variables. However, name$ is probably better because it makes your program easy to read, something very important for long programs.

Another very important part of programming is making decisions based on the input. A simple way to do this is the IF statement. This is throwing rather a lot at you at once, but I don't want to lose your interest.

With IF you basically give it something that could happen. There's a word that goes with it; THEN. Whatever comes after THEN is what it should do; But, it only does it if the IF thing happens. For example, let's looking at a little number guessing game:

num = 7PRINT "I'm thinking of a number between 0 and 15. Try to guess it in one try!"INPUT guessIF guess = num THEN PRINT "Yeah! You got it right" ELSE PRINT "Sorry! That's wrong!"

Wow, that if statement is long! Let's break it down. It says first:

IF guess = num

So, it's going to check to see if guess and num contain the same number.

THEN PRINT "Yeah! You got it right"

Page 5: Basic Programming Techniques in QBASIC

a THEN statement only happens if an IF statement is true. so if they put in 6, 0, 1, 6.83451, -2, and on the whole anything but 7, this wont happen. but there's still one more part:

ELSE PRINT "Sorry! That's wrong!"

If they get it wrong, the THEN doesn't happen. But, we want to give them a message anyway. That's where else comes in. Else happens if the IF turns out to be false - what it was looking for didn't happen.

1.4 - Eliminating redundant code: Loops

What if we wanted to give them more than 1 guess? Say, 6 guesses this time. Well, we could do it like this:

num = 7PRINT "I'm thinking of a number between 0 and 15. Try to guess it in one try!"INPUT guessIF guess = num THEN PRINT "Yeah! You got it right" ELSE PRINT "Sorry! That's wrong!"INPUT guessIF guess = num THEN PRINT "Yeah! You got it right" ELSE PRINT "Sorry! That's wrong!"INPUT guessIF guess = num THEN PRINT "Yeah! You got it right" ELSE PRINT "Sorry! That's wrong!"INPUT guessIF guess = num THEN PRINT "Yeah! You got it right" ELSE PRINT "Sorry! That's wrong!"INPUT guessIF guess = num THEN PRINT "Yeah! You got it right" ELSE PRINT "Sorry! That's wrong!"INPUT guessIF guess = num THEN PRINT "Yeah! You got it right" ELSE PRINT "Sorry! That's wrong!"

Well, that's very redundant. You typed (or maybe copy+pasted) the same code 6 times! That's terrible programming practice, and just flat out boring to do. So, let's look at something at takes care of things that need to be done many times: Loops. Here's a couple kinds of loops and loop-related commands we'll look at:

For...NextDo...LoopExit forExit do

Okay, let's use the first one to make this program a few lines shorter. In a for loop, we give a variable and a value it should start at. Then, we make it loop, adding to this variable every time it runs through, until it gets to a certain point. An example loop might look like this:

FOR a = 1 TO 10....

Page 6: Basic Programming Techniques in QBASIC

....

....NEXT a

The dots in the middle are just my way of saying you'll put some stuff between there, and it'll be whatever you want, as you'll see in a second. So, this loop will take the variable A (remember it will store a number because it doesn't have a $ after it - you can't do number math with a string variable) and make it equal 1. Then it will do whatever stuff you want it to. It will keep doing things until it comes to the words Next A. This closes the loop. It goes back to the beginning, adds 1 to a, and does it again and again like this unless a is more than 10.

num = 7PRINT "I'm thinking of a number between 0 and 15. Try to guess it in 3 tries"FOR a = 1 TO 3INPUT guessIF guess = num THEN

PRINT "You got it!"EXIT FORELSE

PRINT "No, sorry"END IFNEXT a

So, we now know this loops 3 times. First A is 1, then 2, then 3. So, it asks for a guess 3 times. Notice I did something different with the IF...THEN...ELSE part. It's still the same statement, but it's spread across multiple lines to allow it to do more: It sees if guess = num. If it does, then it does 2 things. It prints a positive message, and it breaks out of the loop. Obviously we don't want to ask them over and over after they got it right like our first program did. So, we break out or exit the loop. Exit for means exit the for loop, whether a is 3 yet or not.

If they didn't guess right we go to the else statement and do what comes after it. We give them a negative message. Obviously we could have a gazillion lines after THEN if we wanted to, and the same with ELSE, because this format lets us do more than one thing per IF and more than one thing per ELSE. So, we have to tell the computer to end the if statement somewhere, just like the loop ends (and loops again) somewhere. That's what END IF does. It ends the IF statement.

When we stretch an IF...THEN on for multiple lines like this, and end it with END IF, it's called an IF-END IF block

I realize this has been a lot of information to digest at once. As you read my tutorial, make sure you understand it pretty well before you move on. Programming, like math or language, builds on itself, and the more you press on knowing less, the harder it gets. If you go to far without understanding very much, you'll eventually just break down and reteach yourself anyway.

Let's take a different approach to this program. Lets make it so that the user keeps guessing until they get it right. For this, we'll need a loop that doesn't just go for a set number of times. This is where the DO LOOP is handy. A do loop may look like this

DO

Page 7: Basic Programming Techniques in QBASIC

.....

.....

.....LOOP UNTIL num = guess

This loop theoretically could go forever, because num has to = guess for the loop to end. If that never happens, the loop never ends (another easy place for bugs....). So, here's yet another version of the program, using a DO LOOP:

num = 7PRINT "I'm thinking of a number between 0 and 15. Try to guess it in 3 tries"DOINPUT guessIF guess <> num THEN PRINT ":Sorry! NO!"LOOP UNTIL guess = numPRINT "You finally found the number! Good job!"

So, what's happening here. Well, again the basic stuff. It asks for input every time the loop goes. Notice i made an if statement that was a little different though. I said if guess <> num. What's with that? Well, <> means "Greater than or less than". So, if they guess something that's greater the number, they get told it's wrong, and if they guess less than the number, they're told it's wrong. So, the only thing that wouldn't make it say no would be if it's equal. If they guess the right number then, they're not told it's wrong. And look at the part that says loop. It only loops is guess doesn't = num. So, it basically loops again, giving them another chance, if they're wrong. If they are right, the loop ends and it says they're right. This program is much easier and cleaner than the two previous, and runs slightly faster (although not noticeable, it does take milliseconds less to process because less stuff happens)

One last thing. Remember EXIT FOR to get out of a FOR LOOP? Well, there's a similar statement, EXIT DO, that you can use if you want to prematurely end a DO LOOP for one reason or another.

1.5 - Even more input

Commands/keywords for this section:

SCREEN()INKEY$AND

If you got all previously discussed, you've already mastered a small chunk of the language. So far, I've given you some very important commands in BASIC. But i tend to gear my tutorials to eventually culminate in game design, so let's look some very humble beginnings for a pac-man style game.

For simplicity, let's make a very tiny, basic maze. This is not a maze we'd really want, but it's okay to learn from. Let's just put it right on the screen with some print statements:

Page 8: Basic Programming Techniques in QBASIC

PRINT "0000000"PRINT " "PRINT "0 000 0"PRINT "0 0 0 0"PRINT "0 0"PRINT "0 000 0"PRINT " 0 "PRINT "0 0"PRINT "0000000"

Wow! It's craptacular! Anyhow... that'll do for now. Now, in this maze, our character will be an asterisk - we'll do more aesthetically pleasing games once we get into graphics, files, and all that fun stuff. Again, for the sake of simplicity, you'll control him with the number pad.

So, let's think a bit on how we'll accomplish this. We'll draw a maze, with print, and draw the asterisk character with print. We'll need the help of locate to position him in the maze. The user will hit keys - which we'll show you how to do in a second - to move the guy around. Of course, if we LOCATE him, we're gonna be using coordinates.

So, let's plan that out: The first empty space in the maze is at 2,1. We'll start him there. If he moves up he'll go to 1,1 and if he moves down he'll go to 3,1. So, we'll subtract to move him up, add to move him down. The same logic applies to left and right. From 2,1 if he goes right he'll be at 2,2 and if he goes left he'll be at 2,0 (yes, that zero is not allowed with locate, but this is purely hypothetical). So, we'll add to go right and subtract to go left. Good, that's most of the work right there.

But, what about not running into walls? Well, I'll introduce you to a seldom used command called the SCREEN function. It can actually get info straight off of the screen, after it's been printed! Unfortunately, I also have to introduce you to a hideous monster called ASCII.

Let's start with the really confusing part to get it out of the way. What is ASCII?. It's an acronym for "American Standard Code for Information Interchange". Essentially, it goes like this: It gives a special code to 256 different characters, letters, numbers, and symbols. Believe me, it is helpful, but I hate to have to throw such things at you which are not easy to comprehend at first glance. You'll see as you program more that it is useful, and start to understand why it's around, but whether you do or not it's an important part of programming, and unfortunately must be covered. Here's the ASCII table as it appears in QBASIC's help menu

Page 9: Basic Programming Techniques in QBASIC

The reason why i mentioned this is because the SCREEN function will spit out what it's supposed to, but instead of a simple character of text like you'd expect, it gives you an ASCII code. For example, say we've printed our maze and we do this:

PRINT SCREEN(1,1)

It will give you the number 48. Clearly, there's a 0 at (1,1), not a 48. Well, 48 is the ASCII code for 0. So, we'll only let our guy move onto something if there's not a 0. To do this, we'll look at where he's trying to move with SCREEN(). If it gives us a 48, he's not allowed to move there and he stays put.

Now for the other technicality: INKEY$ We don't want input statements constantly prompting the user, making them type something in, and hitting return. We want to be able to move around easily. INKEY$ is a special variable. Whenever you hit a key at any time (outside an input statement), even if seemingly nothing else is happening, QBASIC takes that key and puts it into INKEY$. So INKEY$ is very handy in a situation like a video game, because you can let the enter single keystrokes without any prompting. Take this loop for example:

DO

Page 10: Basic Programming Techniques in QBASIC

LOOP UNTIL INKEY$ = "1"PRINT "You broke out of the loop!"

There's nothing in it! That doesn't matter, because if the user is hitting buttons while this empty loop runs, the key goes into INKEY$. When they hit anything but 1, nothing happens. But if they hit 1, the loop ends. However, I usually write that kind of loop like this:

DOa$ = INKEY$LOOP UNTIL a$ = "1"PRINT "You broke out of the loop!"

Why do that if you can just look at INKEY itself? Isn't that extra pointless code? Well, INKEY$ is constantly changing and very touchy, so this loop works better, because you have something more solid to look at. INKEY$ changes so frequently that hitting the right key might not break the loop. I am just in the habit of doing A$ = INKEY$, then checking it. That is the style I'll use in this tutorial as well.

Let's start to write the code based on what we just discussed

CLSy = 2x = 1

COLOR 15PRINT "0000000"PRINT " "PRINT "0 000 0"PRINT "0 0 0 0"PRINT "0 0"PRINT "0 000 0"PRINT " 0 "PRINT "0 0"PRINT "0000000"

DO

a$ = INKEY$ checky = y: checkx = x LOCATE y, x: COLOR 0: PRINT "X" IF a$ = "8" AND y - 1 > 0 THEN checky = y - 1 IF a$ = "4" AND x - 1 > 0 THEN checkx = x - 1 IF a$ = "2" THEN checky = y + 1 IF a$ = "6" THEN checkx = x + 1 IF SCREEN(checky, checkx) <> 48 THEN y = checky x = checkx END IF IF X > 7 then x = 7 LOCATE y, x: COLOR 15: PRINT "X" LOCATE 10, 10: PRINT x, y, checkx, checkyLOOP UNTIL a$ = "Q" OR a$ = "q"

Page 11: Basic Programming Techniques in QBASIC

A few new things here, but the rest isn't too complex. Firstly, there's AND. It's a special word you can use in IF statements, making them more specific. This line for example:

IF a$ = "8" AND y - 1 > 0 THEN checky = y - 1

Recall that the thing after THEN won't happen if the IF didn't. We use AND to give it more conditions. A$ has to be 8 AND y - 1 has to be more than zero. If those are both true, then we'll give the variable checky a value that's one less than y. This thinking applies to the next line also.

The next new thing here is colons. They let you put more than one command on a line. This is just a matter of style. This line:

LOCATE y, x: COLOR 0: PRINT "X"

Works the same as these lines:

LOCATE y, xCOLOR 0PRINT "X"

There is absolutely no difference, i just felt like putting them on one line to make it look neater (to me anyway) Same with the indenting; i like to indent code that's between DO....LOOP, and indent code between IF...END IF. Although it's perfectly fine not to indent, you'll see most programmers do this indenting with ifs and loops, and other such blocks of code.

Now that that's out of the way, let's step through this program.

First we clear whatever may be on the screen from before. Then we set the variables that store where the 'character' is on the screen. We draw the maze (using COLOR 15 because i wanted a white maze). Now we enter the main loop. Good programs have a main loop (where necessary). Then, we put INKEY$ into another variable to make it easier to work with. Here it gets going:

checky = y: checkx = x

Since we're going to be checking to see if they try to run over a wall, i chose to use different variables that remember where they're 'trying to move'. Later the program looks at what character is on the screen there. If it's character 48 according to the ASCII table, which is a maze wall, then they don't actually move there.

Just like LOCATE, SCREEN ( ) can't have zeros in its parameters. So the ifs for moving up or moving left make sure that the program doesn't even bother checking if they would be trying to move off the screen [to (0, x) or (y, 0)]. Also, after all the checks are done and stuff like that there's this line:

IF X > 7 THEN x = 7

There's no maze past 7 on the x axis, so we don't let them move there!

Page 12: Basic Programming Techniques in QBASIC

We'll get back to finishing this game later as we learn more. It would be big and difficult using only what we know now. A pac-man like game has more things to it: pellets, power pellets, enemies, multiple levels. We'll need more knowledge to work with before we can accomplish these.

2.1 - Graphics

Let's diverge for a while into a very important topic in programming. If you want to design games, as this tutorial uses what you learn to show you how to do, graphics will be a big part.

There's a whole lot of graphics commands in BASIC. Let's first cover the basic ones. You'll probably toss them out later in favor of sprites, tiles, and other such advanced techniques, but it's a good start.Here's what we'll cover here:

SCREEN (a different command with different syntax but the same name)LINECIRCLEPAINTDRAWPSET

So far we've only put text on the screen. But you can put graphics on the screen too. However, if you didn't get locate, go back and review - there will be a lot of stuff with coordinates. In fact, graphics are all about coordinates!

You may know already that computer screens (as well as TVs) draw a picture with little dots of color called pixels. If you're just dying to know, pixel is short for PICture ELement, (and i guess somehow that C becomes and X when they're put together).With the locate statement, we can make pictures like the maze out of text. But if we could make a picture with even more tiny parts to it, it would look better. The amount of pixels a picture has is called its resolution (sometimes people say res or rez for short)For example, take an impressionist painting made out of single dots. With thousands of little dots close together, it looks like a picture. The more dots we cram into it, the better it looks - and the harder it is to see every little dot.So, depending on how high a resolution you want, BASIC has different screen modes. Each one has a different amount of dots across and down, therefore, it has a different resolution. Just like with some commands we've already encountered, each screen mode has a code number. We'll use screen #12 - or screen 12. So, how do we tell BASIC to change the screen to this mode? With the screen command. As simple as this:

SCREEN 12

Yes, this is a different command than the SCREEN () you're already familiar with.Screen 12 is BASIC's highest resolution screen. You have 640 dots across, and 480 down, to draw with. That's about 300,000 dots in total! So, let's look at our first graphics command, LINE.It's kind of like locate, but it has a different range of coordinates allowed, and needs 2 sets of

Page 13: Basic Programming Techniques in QBASIC

numbers - of course, since it draws a line; a line must start somewhere, and go somewhere. Also, you give it what color you want the line to be. For example:

LINE (0,0) - (639,0), 1

Will draw a line blue across the top of the screen. Or this:

LINE (0,0)-(0,479), 2

makes a green line down the left side. Line has another special thing about it though. It makes boxes too! If you want a box, write B after the color:

LINE (0,0)-(10,90),5,B

will make a tall purple rectangle in the upper left corner of the screen. However, this is an empty box. If you say BF rather than just B it will make a solid box (in this case, it would be a solid purple rectangle in the upper left corner instead)

That's covered pretty well. Next command:

Circle

Circle needs at least 4 parameters. Where to put it (an x & y), a size (or radius), and a color. Like so:

CIRCLE (320, 240), 10, 4CIRCLE (320, 240), 50, 3

You should see a little red circle in about the center of the screen, with a much bigger light blue circle around it. Pretty simple, so let's move on. Now for

Paint

Paint can sometimes be tricky to get it to do what you want. What paint does is starts from where you tell it, and paints inside something. But, you have to tell it what color to stop painting at. So, you give it a color, and a border color (the color where it should stop painting). This way you can paint solid inside of figures, like circles you just learned about. Or, inside hollow boxes, if you don't want the inside to be the same color as the border. Take a look at this:

CIRCLE (320,240), 25, 4PAINT (320,240), 15, 4

Here's a red circle in the middle of the screen again [this time a little bigger], and we'll paint the inside white. Now try changing the 4 in the paint statement only to something else.... It paints the whole screen white! Because it didn't hit whatever color you told it (since it wasn't there) and just kept right on painting.I don't know what more i can say about this command. Have fun with what you know, experiment around with things.

Page 14: Basic Programming Techniques in QBASIC

Now for the last two commands that kind of go together. First let's start with

DRAW

Most programmers despise this command, and think it's a terrible waste to even learn. I sort of agree; about despising it, i mean. It's just something very cumbersome and tedious, but you can have fun messing around with it, and it's an easy way to draw complex graphics from the start. Draw goes like this: You give it a string of different things to to and it draws them. It has little commands of its own in a way. Here's a partial list:

B move, no line N line, no moveUn Up Dn Down Ln Left Rn RightEn Up and right Fn Down and right Gn Down and left Hn Up and left

The little n by something means you put a number there. That's how many pixels you want to draw in that direction. Let's see a few examples of how draw works:

DRAW "R50 D50 L50 U50"

You're telling draw to go right for 50 pixels, then go down for 50 pixels, the left 50, then up 50. Congrats! You made a square!. How about this:

DRAW "e10 f10 g10 h10"

You go up and left, then down and right, then down and left, then up and left, 10 pixels each time. You've now made a diamond!Now let's change slightly what we did earlier:

DRAW "nr50 nd50 nl50 nu50"

perhaps you're saying it will draw a square again. No... - why? Notice N says "Line, no move". That means it will draw something in that direction, then go back to where it was to draw the next thing. Actually, it will draw more of a cross. If you put n in front of each part of the second example, you get an x. Without B and N, the draw statement is much like an Etch-A-Sketch.B is like the opposite of N - instead of drawing without moving, you move without drawing. So this:

DRAW "r10 br10 r10 br10 r10 br10"

Will give you dashed line. Draw 10, move silently along for 10, draw 10..... and so on.

Our final command for this section helps you out with DRAW

PSET

It's short for "Pointer SET". You'll notice that when you use draw, it's in the upper left corner to start. That's the default position of the imaginary pointer that tells basic where to draw at. You

Page 15: Basic Programming Techniques in QBASIC

can tell it to go somewhere else on the screen by using PSET. For example, if you want to go to the other corner:

PSET (639, 479)

then maybe you want to go back to the upper left again:

PSET (0,0)

One tiny thing you should notice. PSET makes a dot on the screen at the point you tell it to go to. This will be very handy later.

2.2 - Handling lots of data - Arrays

At this point is gets harder to make an even, flowing progression. So, I'll try to show you important parts of the language, however, they're not necessarily woven interconnectingly.

This section will cover, like it says, how to handle big amounts of data. For example, a variable only holds 1 type of data, and only 1 thing. Now that's about to change as we discuss arrays: they're like a bunch of variables rolled into one. For example, a simple program to find average, without the use of arrays:

PRINT "Enter 4 numbers, I'll find their average"INPUT aINPUT bINPUT cINPUT daverage = (a+b+c+d) / 4PRINT "Their average is "; average

( the slash / means to divide)

That's great and all, but it's not very flexible. Say perhaps you wanted the program to be able to find the average of 50 numbers; You'd have to modify the program so that it had 50 variables, add them al together, and divided them by 50. Or even worse, what if you wanted to modify it for up to 50 numbers - you'd have to ask them how many numbers there were putting in, it would have lots of IFs, and be generally of poor quality.Arrays solve the problem of storing and retrieving lots of numbers with little code and effort. That's because they go hand-in-hand with For...Next loops. To start, here's a program that just gets 50 numbers - it doesn't average them... yet:

DIM numbers(50)

PRINT "Enter 50 numbers. Enter -1 to stop"FOR a = 1 to 50INPUT nIF n <> -1 THEN numbers(a) = n ELSE EXIT FORnext a

Page 16: Basic Programming Techniques in QBASIC

Like always, i threw in a new command, which you probably guess has something to do with arrays. The DIM command makes an array. Unlike variables, which you don't have to "make" before you use them (although you can, and it's a really good habit to do so) you do have to with arrays (the correct term is declare, rather than "make"). You say DIM arrayname() with some number in the parenthesis. This is how high you want your array to go. Your array holds 50 things in this case, and each one is numbered. Then, treat it just like a normal variable. This is perfectly legal:

numbers(1) = 12numbers(6) = x + y - 5

and so on - anything you could do with normal numeric variables. Since each part of the array is numbered, they work really well with loops as was mentioned.

To break down our program: First, it makes an array, which holds up to 50 things. Then it tells you to enter up to 50 numbers, and type in -1 to stop. It gets the number from you, then checks it to see if you want to stop. If not, it actually puts it into the array. Yes, you COULD do this:

INPUT numbers(a)IF numbers(a) = -1 THEN EXIT FOR

but then when they do -1 to stop, -1 will get recorded before it breaks out of the loop.

Now let's average them, which isn't too hard. Add this to the end of the program:

FOR b = 1 TO a - 1sum = sum + numbers(b)NEXT b

average = sum / (a - 1)PRINT "The average is: "; average

Just to clarify, why a-1? Since a holds how many times we looped when entering numbers, we use it to determine how many numbers go into the average. But remember they have to enter a number to get out of the loop too - that number doesn't factor in. So, if they entered 2 numbers, then entered -1 to get out, we'd have looped 3 times. So, a-1 tells us how many numbers actually figure into the average.

Now to break down the second part: After all the numbers are entered, we have another loop with a different variable go back an add them all together. Then we find the average by taking the sum and dividing (the slash means divide) by how many numbers there were, then print it out.

In summary, you can see arrays are good for storing a bunch of numbers that are all for the same thing. They also happen to go well with For... Next loops. However, let's take a look at another thing they go well with:

DATA statements

Page 17: Basic Programming Techniques in QBASIC

DATA statements are in themselves inert. You just put them in your program, and they hold data. What they hold can't be changed from within the program - you want them changed? you do it by hand! Basically, they're not like arrays and variables, because they don't hold data quite the same and can't be changed by your program. A quick example; be aware of these things involved with DATA statements:

RESTOREREADDATALine labels

This program tells you what day of the week (The first day, second day, third day, etc) it is, by you giving it the name of the day it is (sunday, monday....):

DIM daynames(7) as string

RESTORE daynameslistFOR a = 1 TO 7READ daynames(a)NEXT a

PRINT "Enter the day, in ALL caps."PRINT "For example, WEDNESDAY not Wednesday"INPUT dayname$

FOR a = 1 TO 7IF daynames(a) = dayname$ THEN PRINT "It is day number "; a ; " today!"NEXT a

daynameslist:DATA SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY

See, data records a bunch of stuff within the program and lets us look at it. It doesn't DO anything, in a sense. Notice

daynameslist:

before it, and that there's a line that says

RESTORE daynameslist.

Yes, these two are definitely connected. Suppose you had more than one chunk of data - you're gonna want to look at one at a time obviously. By putting a label (followed with a colon!!!!) you give your data a name. Then with restore, you tell it that name, letting it know you want to look at the data with that name. Read reads the data. Each time you do read, it reads the next piece of data (each piece is separated by a comma, notice) unless you do a restore, where it goes back to the start of the data. When it reads it, it puts it in whatever variable you want it to - here i put it in an array, because if you can put it in a variable, you can put it in an array.

Page 18: Basic Programming Techniques in QBASIC

So, to step through this program. At the end, we have our data. Data usually goes at the end. We make an array to hold the daynames so we can look at them easily. Our loop reads them from the data. Now we're ready to do something. We ask the user what day it is - it's gotta be in all caps, because to basic SUNDAY = SUNDAY, but SuNDAY doesn't = SUNDAY. Then we look at all names, which we have in an array. If it sees the right day, it tells you the matching number. Okay, it's not the best example, but it gets the point across.

Why learn this? Mainly, because arrays are very very very important. But also, because arrays are very very very important. And lastly because they make doing complex graphics so much easier.

2.3 - Arrays and graphics: So happy together

SPRITES!

Finally, you have the background knowledge necessary to learn about sprites. What's a sprite you say? If you've ever played old video games, you've seen tons of them. They're the main part of a 2D video game, and are on occasion (with ugly results) used in 3D games. A sprite is any little picture made to be moved around. Here's a couple of sprites from super Nintendo games (bomberman actually)

True, bomberman has tons of animations, all which are a separate picture, but bomberman is still considered 1 sprite, as is that little red thing...As we know, computer graphics are made up of lots of tiny little dots. Rather than draw each of those dots, we can draw with commands like CIRCLE, LINE, etc.But, as we have the need for more complex graphics, it becomes necessary to do just that - draw every little dot. That's essentially a sprite.To start, let's jump right in and make a simple sprite. We'll do this by saving a dot-by-dot image in DATA statements. Of course the important piece of information you need to know about a dot is it's color. So we'll of course put the color number of each dot in the data statement.A simple yellow smiley face. Take a look at this little program:

SCREEN 12FOR y = 0 TO 15 FOR x = 0 TO 7 READ col PSET (x, y), col NEXT xNEXT y

DATA 00,14,14,14,14,14,14,00DATA 14,00,00,00,00,00,00,14DATA 14,00,00,00,00,00,00,14DATA 14,00,14,00,00,14,00,14

Page 19: Basic Programming Techniques in QBASIC

DATA 14,00,14,00,00,14,00,14DATA 14,00,14,00,00,14,00,14DATA 14,00,00,00,00,00,00,14DATA 14,00,00,00,00,00,00,14DATA 14,00,00,00,00,00,00,14DATA 14,00,00,00,00,00,00,14DATA 14,00,14,00,00,14,00,14DATA 14,00,00,14,14,00,00,14DATA 14,00,00,00,00,00,00,14DATA 14,00,00,00,00,00,00,14DATA 14,00,00,00,00,00,00,14DATA 00,14,14,14,14,14,14,00

The data is the bulk of the program. It contains color numbers, either 00 (black), or 14 (yellow). They're arranged in such a way that if you were to take those color numbers and draw them as pixels rather than write them as #'s, they'd form an image of a smiley face.

Then look at the 2 loops. Our picture is made 8 pixels across and 16 down. Notice the inside loop draws 8 pixels. The outside loop makes it loop 16 times. So, this draws 16 lines of 8 pixels, the end result being our smiley face.

How do arrays fit into this as they were mentioned to. The answer - suppose you need to draw that smiley face over and over (to move it around, for example). Doing it this way, you'd need to RESTORE and READ every time you drew it. To make it easier and more efficient, you should just store all the numbers somewhere. As we know, arrays are great for storing a whole lot of numbers which you basically use for the same thing. A modified version of the program:

DIM Smiley(127)FOR a = 0 to 127 READ smiley(a)Next a

SCREEN 12FOR y = 0 TO 15 FOR x = 0 TO 7 PSET (x, y), smiley((8*y)+x) NEXT xNEXT y

( * means 'times' or 'multiply')

You'll just have to take that (8*y)+x formula on faith for now.... An array is essentially 'one-dimensional', but the screen is two-dimensional, so we must convert between the two. This method is very slow, although not too noticeable with such a small sprite.

Let's add in the necessary code so we can move the face around. To clarify it, look back at the pac-man example:

DIM Smiley(127)SCREEN 12

Page 20: Basic Programming Techniques in QBASIC

Smileyx = 0Smileyy = 0

For a = 0 to 127 READ smiley(a)NEXT a

DO

FOR y = 0 TO 15 'Draw smiley face FOR x = 0 TO 7 PSET (smileyx + x, smileyy + y), smiley((8*y)+x) NEXT x NEXT y

Do A$ = inkey$ LOOP UNTIL a$ <> "" 'Wait until they hit a key

LINE (smileyx, smileyy)-(smileyx+7, smileyy+15),0,bf 'Draw over old smiley before coords change 'and before new smiley is drawn

IF a$ = "4" THEN smileyx = smileyx - 8 'Left IF A$ = "6" THEN smileyx = smileyx + 8 'Right IF a$ = "8" THEN smileyy = smileyy - 8 'Up IF a$ = "2" THEN smileyy = smileyy + 8 'Down

LOOP UNTIL a$ = CHR$(27) 'The ASCII code for 'escape' - quit when they hit it

Notice the change in in the PSET command. The coordinates are not simply (x,y). If you drew at x,y every time, the smiley would not move! however, if you add x to the smiley's x position, and y to the smiley's y position, the pic is drawn wherever the smiley's been moved to.Just like in the pac-man example earlier, we must draw over the old picture before drawing a new one, otherwise there'd be a trail of smilies across the screen. This is what

LINE (smileyx, smileyy)-(smileyx+7, smileyy+15),0,bf

does. The rest of the program is pretty easy to figure out.

2.4 - Get/Put

I mentioned earlier that this method of drawing would be pretty slow with a large spryte. The more commands that have to be done, the longer the program takes.

The loop to draw the smiley loops 128 times total (8 on the inside * 16 on the outside). That not a lot, but it's not exactly efficient. Remember that it loops 128 times every time you hit a key. If your picture was say, 100 * 200, that would take considerably longer - 20,000 pset commands! But nonetheless it's often necessary to draw lots of graphics at once. For this reason, basic has

Page 21: Basic Programming Techniques in QBASIC

the GET and PUT commands.GET takes an rectangular area of the screen and copies it so it can be drawn again and again. Then, the PUT command draws what you copied with GET. The advantage is that GET and PUT are very fast (compared to doing a bunch of PSETs, anyway)For an example, lets go back to our original smiley face program, that just draws the smiley face and add in GET and PUT commands:

SCREEN 12DIM Smiley(127)FOR y = 0 TO 15 FOR x = 0 TO 7 READ col PSET (x, y), col NEXT xNEXT y

GET (0,0)-(7,15), smileyPUT (320, 240), smiley

(again, i didn't include the DATA statements, in order to save space. Make sure you include them when you run the program for yourself)

Get works like this:

GET (x1, y1) - (x2, y2), arrayname

which means you give it starting coordinates (x1, y1) and ending coordinates (x2, y2), and it GETs an image from that area of the screen. Kind of like line when you made a box (with BF), this gets a rectangular area of the screen. It stores the image it gets in whatever array you specify. As a general rule i just use an array as big as how many pixels the image will have. However, this may be too much depending on what screen mode you use... Don't worry about this right now, it gets more technical than you care to know, i'm sure.

Once we have the image, we use put, which is very similar:

PUT (x, y), arrayname

This takes the image you stored with get from whatever array you tell it and puts it on the screen at coordinates x, y

Maybe you can see where i'm going with this: First we went over how to begin to make a game and started to talk about graphics. Then we digressed into other topics, and now we know how to do graphics even faster, and more efficient methods of storing/using them.The next logical step is to apply our new knowledge of get and put to our game from before. Let's begin to put a pac-man game together. I choose pac-man because it's pretty simple, but complex enough to give us a lot of material to cover. don't limit yourself - take what you learn here and do what you want with it.

Page 22: Basic Programming Techniques in QBASIC

Now that we can use real graphics - not those sucky text.. erm "graphics" from before, let's use real graphics!Let's make pac-man look like pac-man, and the pac-man maze look like a pac-man maze.