Vic's QBasic Programming Tutorial Basic Tutorial III GAME TEQUINIQUES! ----------------------------------------------------------------------------- since the last tutorial (GRAPHICS), you are probably wondering how you can incorperate the graphics into a game... In this tutorial I will teach you how to give the computer input and ask for output in many different ways... The first thing that you will need to learn is... HOW TO MOVE THINGS ON THE SCREEN Remember in the last tutorial when I told you about the locations of X and Y ? If I wanted to put a pixel at 27, 24 then I would use the command PSET (27,24) in which X = 27 and Y = 24 RIGHT? Remember X = left and right Y = up and down If I wanted to move the pixel to the right I would change the 27 (X) to 28... so insted of putting the pixel at 27,24 it would now be at 28,24 ( 1 step to the right) In order to do this in a program you would do it like this --- SCREEN 13 LET X = 27 LET Y = 24 PSET (X,Y),1 LET X = X + 1 PSET (X,Y),1 --- In the beggining you tell x that it now equalls 27 and y = 24 NOTICE: (you don't need to use the LET, it just helps you see..) When you say x=x+1 the value of x (27) is added by 1, so now it equalls 28 (27 + 1)... If you want the point to go left instead of right you would say LET X = X - 1 x was equall to 27, but now it equalls 26, one step to the left... do you get it yet? if you wanted the point to go down you would say Y = Y + 1 Up you would say Y = Y - 1 Down and to the right you would say X = X + 1 Y = Y + 1 Down and to the left you would say X = X - 1 Y = Y + 1 Do you get the picture? I hope so... If not, you should get it after a few examples... READ ON... ---------------------------------------------------------------------------- Getting INPUT From the KEYS... What you usually use to get input from an individual key is the command INKEY$ Another important thing to mention before going on... Everygame has to have some kind of loop to keep the game from ending... the most common is DO ...programming stuff... LOOP ok, here we go... You could use the inkey$ command like this... DO If INKEY$ = "a" print "Huh, you pressed the letter a":END LOOP But it would work 10 times better if you do it like this DO Press$ = inkey$ If Press$ = "a" the print "This will catch the keypresses better":END LOOP you don't need to know why this works better, just use it... --------- As you will probably learn in the future, the best way to learn is from reviewing other peoples source code.. so here is an example... w = up s = down a = left d = right q = quit '---- SCREEN 13 x = 50 y = 50 DO press$ = INKEY$ PSET (x, y), x IF press$ = "w" THEN y = y - 1 IF press$ = "s" THEN y = y + 1 IF press$ = "d" THEN x = x + 1 IF press$ = "a" THEN x = x - 1 LOOP UNTIL press$ = "q" '--- In this code example you can move the pixel with the w,s,a,d keys... it draws lines because the screen is never cleared. don't worry about that now... If you don't understand it at first, take a look at it again and again. change some things and see what happens. This is one of the most important concepts to get... Remember, to move down you say y = y + 1 to go up you say y = y - 1 left you say x = x - 1 right you say x = x + 1 Ok... Thats that... ---------------------------------------------------------------------------- CLIPING In the last programm you may have noticed that you can move the pixel off the screen and it will disappear. In order to stop that you must use cliping... here is a programming example of no clipping... '--- SCREEN 13 x = 50 y = 50 DO press$ = INKEY$ WAIT &H3DA, 8 LINE (x - 8, y - 8)-(x + 8, y + 8), 0, BF CIRCLE (x, y), 7 PAINT (x, y), 4, 15 IF press$ = "w" THEN y = y - 1 IF press$ = "s" THEN y = y + 1 IF press$ = "d" THEN x = x + 1 IF press$ = "a" THEN x = x - 1 LOOP UNTIL press$ = "q" '--- In this example (which if you look hard enough you can understand) it allows you to move a circle around the screen, here you can move the circle off the screen. ( you don't have to understand the WAIT &H3DA,8 or the PAINT command they just make the program look good... They would work without them, but The graphics would suck...) Now, here is an example of the same program but with clipping... '--- SCREEN 13 x = 50 y = 50 DO press$ = INKEY$ WAIT &H3DA, 8 LINE (x - 8, y - 8)-(x + 8, y + 8), 0, BF CIRCLE (x, y), 7 PAINT (x, y), 4, 15 IF y > 20 THEN IF press$ = "w" THEN y = y - 1 IF y < 180 THEN IF press$ = "s" THEN y = y + 1 IF x < 300 THEN IF press$ = "d" THEN x = x + 1 IF x > 20 THEN IF press$ = "a" THEN x = x - 1 LOOP UNTIL press$ = "q" '--- In this example, you can't move the circle off the screen... If you look long and hard at this you should know why... In the command IF y > 20 THEN IF press$ = "w" THEN y = y - 1 it says that unless y is greater than... just a minute... > this is the greater than sign < this is the less than sign An easy way to remember this is to think of a number line... 0 1 2 3 4 5 6 7 8 9 10 ... less < > greater than they are kind of like arrows pointing down or up the line. The greater than arrow points UP the line The less than arrow points DOWN the line If you allready know this then it will help you out a lot. ok, lets get back! In the command IF y > 20 THEN IF press$ = "w" THEN y = y - 1 it says that unless y is greater than 20 on the screen... wait a minute... Before I go on, do you remember that screen 13 has a heigth of 200 and a width of 320? In the command IF y > 20 THEN IF press$ = "w" THEN y = y - 1 it says that unless y is greater than 20, look and compare the key that is pressed. If Y is less than this ammount, don't do anything... This will keep you from being able to move up when you get to the top of the screen... If you don't get this, look at the programm again and again, you may change things and see what happens, eventually you will learn what it I'm talking about... -------- Another example... In this example there is no keyboard input other than pressing Q for quit. This is an excelent example to learn clipping from. '--- SCREEN 13 x = 50: y = 50 xadj = 1: yadj = 1 DO press$ = INKEY$ WAIT &H3DA, 8 LINE (x - 8, y - 8)-(x + 8, y + 8), 0, BF CIRCLE (x, y), 7 PAINT (x, y), 4, 15 IF y <= 20 THEN yadj = 1 IF y >= 180 THEN yadj = -1 IF x >= 300 THEN xadj = -1 IF x <= 20 THEN xadj = 1 x = x + xadj y = y + yadj LOOP UNTIL press$ = "q" '--- In this example, a ball bounces around the screen. When it gets about ready to go off the screen it changes direction, and looks like it bounced. We will use this later to make a pong like game... ----------------------------------------------------------------------------- COLLISION For the longest time this was something that I could just not understand or figure out on my own, I had to be taught this. And still I admit, this is probably not the most reliable method. This will be your first oficial game!!! Use asdw as keys W = up S = down A = left D = right Q = quit '--- START COPYING HERE... x1 = 1: y1 = 1 x2 = 20: y2 = 10 DO press$ = INKEY$ IF oldx1 <> x1 OR oldy1 <> y1 THEN CLS oldx1 = x1: oldy1 = y1 LOCATE oldy1, oldx1: PRINT " " LOCATE y1, x1: PRINT "O" LOCATE y2, x2: PRINT "X" END IF IF y1 >= 2 THEN IF press$ = "w" THEN y1 = y1 - 1 IF y1 <= 22 THEN IF press$ = "s" THEN y1 = y1 + 1 IF x1 >= 2 THEN IF press$ = "a" THEN x1 = x1 - 1 IF x1 <= 70 THEN IF press$ = "d" THEN x1 = x1 + 1 IF x1 = x2 AND y1 = y2 THEN : PRINT "YOU WIN!!!": END LOOP UNTIL press$ = "q" '--- STOP COPYING!! The goal of this game is to reach the X. If you reach it you win!! The colision detection in this program is... IF x1 = x2 AND y1 = y2 THEN : PRINT "YOU WIN!!!": END you could also say it like this If x1 = x2 then if y1 = y2 then print "YOU WIN!!!" END END IF END IF But the first is obviously shorter... When you use this command you are saying, If this... Just a minute... -- I think it is a good time to introduce you to the word SPRITE... The first thing that pops into your head is probly 1. a soft drink 2. a fairy / Gym Teacher Well, maybe not your gym teacher, you can use mine as an example, (he shaves His legs!! and says it makes him swimm better) In programming, a moveable object on the screen is called a SPRITE... If you have ever watched the OLD OLD tv cartoon REBOOT you migh have a better handle on what I'm talking about... OK, Lets get back! -- When you use this command you are saying, If this sprite has the same X value and the Same Y value it has to be touching, so print YOU WIN and end the game... --- That works great when you are in text mode and the sprites take up only one spot, but what if you have a drawing that you want to detect collision... with this you have to look at more than an exact location.. ANOTHER EXAMPLE... --- SCREEN 13 x = 60: y = 15 x2 = 120: y2 = 100 xadj = 1: yadj = 1 LINE (x2, y2)-(x2 + 10, y2 + 10), 1, BF DO press$ = INKEY$: WAIT &H3DA, 8 PSET (x, y), 4 IF y <= 20 THEN yadj = 1 IF y >= 180 THEN yadj = -1 IF x >= 300 THEN xadj = -1 IF x <= 20 THEN xadj = 1 x = x + xadj: y = y + yadj IF x > x2 AND x < x2 + 10 AND y > y2 AND y < y2 + 10 THEN PRINT "HIT" END END IF LOOP UNTIL press$ = "q" --- In this example a pixel goes around the screen until it hits the blue square in the middle of the screen... You can change around the x,y x2, and y2 to move things around more... If you think really (well not really) hard you can make this into a game... Lets try... First, try to make your own little game, then see what I did and compare, HERE IS MY GAME... --- SCREEN 7, 0, 1, 0 x = 100: y = 0 x2 = 120: y2 = 100 xadj = 1: yadj = 1 delay = 1 DO press$ = INKEY$ LINE (0, 0)-(320, 200), 0, BF PSET (x, y), 4 LINE (x2, y2)-(x2 + 10, y2 + 10), 1, BF PCOPY 1, 0 IF y <= 20 THEN yadj = 1 IF y >= 180 THEN yadj = -1 IF x >= 300 THEN xadj = -1 IF x <= 20 THEN xadj = 1 x = x + xadj: y = y + yadj IF x > x2 AND x < x2 + 10 AND y > y2 AND y < y2 + 10 THEN : PRINT "HIT": END IF press$ = "w" THEN y2 = y2 - 1 IF press$ = "s" THEN y2 = y2 + 1 IF press$ = "a" THEN x2 = x2 - 1 IF press$ = "d" THEN x2 = x2 + 1 FOR i = 1 TO delay: NEXT LOOP UNTIL press$ = "q" --- In this game the objective is to move the box around with the keys W = up S = down A = left D = right Q = quit Remember... The point is to GET HIT... Not to not get hit... If the game is to fast for you than you can slow it down by changing the value of DELAY at the begining, change it as high as you want... The above game has a few little tricks to help in the programming... First you probly noticed in the begining you see SCREEN 7,0,1,0 This means that you are entering SCREEN 7 the ,0,1,0 means that you are using one page, you don't have to understand this, just use it... If you try drawing on the screen in this mode you won't be able to see anything... you have to use the command PCOPY 1,0... Here is an example '--- SCREEN 7,0,1,0 print "Hello World!" '--- If you try this in qbasic nothing will show up... '--- SCREEN 7,0,1,0 print "Hello World!" PCOPY 1,0 '--- This example will show something... The next thing is the LINE (0,0)-(320,200),0,bf This is the same as CLS only it is much faster... --- The other thing is the FOR I = 1 to DELAY this slows the program down... The higher the delay the slower it runs... I think that is it... You now know enough to start making your own little games... I will leave you with one more example... In the next game tutorial I will give you more advanced tequniques like how to use the arrow keys... THE PONG EXAMPLE... use the < key to move left and the > key to move right... If I were you I would copy this and paste it into a msdos window by starting up QBASIC, pushing ALT + ENTER (to shrink it) and push the paste button on the tool bar (looks like clipboard and a peice of paper)... HERE WE GO !! 'THE PONG EXAMPLE... '--- SCREEN 7, 0, 1, 0 x = 50: y = 50 x2 = 130: y2 = 150 pspeed = 5 xadj = 1: yadj = 1 delay = 1 DO press$ = INKEY$ LINE (0, 0)-(320, 200), 0, BF CIRCLE (x, y), 7 PAINT (x, y), 4, 15 LINE (x2, y2)-(x2 + 30, y2 + 7), 1, BF LINE (x2, y2)-(x2 + 30, y2 + 7), 8, B LOCATE 1, 1: PRINT score PCOPY 1, 0 IF y <= 20 THEN yadj = 1 IF y >= 180 THEN yadj = -1 IF x >= 300 THEN xadj = -1 IF x <= 20 THEN xadj = 1 IF press$ = "," AND x2 > 1 THEN x2 = x2 - pspeed IF press$ = "." AND x2 < 290 THEN x2 = x2 + pspeed x = x + xadj y = y + yadj IF y > y2 - 7 AND y2 < y2 + 2 AND x < x2 + 30 AND x > x2 THEN yadj = -1: score = score + 1 END IF IF y > y2 + 10 THEN FOR i = 1 TO 100 PRINT "GAME OVER!!" PCOPY 1, 0 NEXT END END IF FOR i = 1 TO delay: NEXT LOOP UNTIL press$ = "q" '--- This is the end of this tutorial... I really hope that this has helped you. When I first started out there were no tutorials out there like this... I had to learn directly from code... That is another good way to start, so look at others source code and learn from it... My current E-Mail address is RADIOHANDS@AOL.com If you are using this tutorial on your page, please leave the tutorial exactly as it is... please don't change anything, unless its spelling errors... Theres alot of them! I don't like using the backspace key... The original website that these were on is http://members.aol.com/radiohands/index.html Thank you Vic Luce Finished september 29 1999