Vic's QBasic Programming Tutorial Basic Tutorial III GAME TEQUINIQUES!-----------------------------------------------------------------------------since the last tutorial (GRAPHICS), you are probably wondering how youcan incorperate the graphics into a game...In this tutorial I will teach you how to give the computer input and askfor output in many different ways...The first thing that you will need to learn is...HOW TO MOVE THINGS ON THE SCREENRemember in the last tutorial when I told you about the locations ofX and Y ? If I wanted to put a pixel at 27, 24 then I would use the commandPSET (27,24)in which X = 27 and Y = 24 RIGHT?RememberX = left and rightY = up and downIf 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 tothe right)In order to do this in a program you would do it like this---SCREEN 13LET X = 27LET Y = 24PSET (X,Y),1LET X = X + 1PSET (X,Y),1---In the beggining you tell x that it now equalls 27 and y = 24NOTICE: (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 itequalls 28 (27 + 1)...If you want the point to go left instead of right you would sayLET X = X - 1x 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 sayY = Y + 1Up you would sayY = Y - 1Down and to the right you would sayX = X + 1Y = Y + 1Down and to the left you would sayX = X - 1Y = Y + 1Do 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 commandINKEY$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 isDO...programming stuff...LOOPok, here we go...You could use the inkey$ command like this...DOIf INKEY$ = "a" print "Huh, you pressed the letter a":ENDLOOPBut it would work 10 times better if you do it like thisDOPress$ = inkey$If Press$ = "a" the print "This will catch the keypresses better":ENDLOOPyou 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 fromreviewing other peoples source code.. so here is an example...w = ups = downa = leftd = rightq = quit'----SCREEN 13x = 50y = 50DOpress$ = INKEY$PSET (x, y), xIF press$ = "w" THEN y = y - 1IF press$ = "s" THEN y = y + 1IF press$ = "d" THEN x = x + 1IF press$ = "a" THEN x = x - 1LOOP 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 thatnow...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 importantconcepts to get...Remember, to move down you say y = y + 1to go up you say y = y - 1left you say x = x - 1right you say x = x + 1Ok... Thats that...----------------------------------------------------------------------------CLIPINGIn the last programm you may have noticed that you can move the pixel off thescreen and it will disappear. In order to stop that you must use cliping...here is a programming example of no clipping...'---SCREEN 13x = 50y = 50DOpress$ = INKEY$WAIT &H3DA, 8LINE (x - 8, y - 8)-(x + 8, y + 8), 0, BFCIRCLE (x, y), 7PAINT (x, y), 4, 15IF press$ = "w" THEN y = y - 1IF press$ = "s" THEN y = y + 1IF press$ = "d" THEN x = x + 1IF press$ = "a" THEN x = x - 1LOOP UNTIL press$ = "q"'---In this example (which if you look hard enough you can understand) it allowsyou to move a circle around the screen, here you can move the circle off thescreen. ( you don't have to understand the WAIT &H3DA,8 or the PAINT commandthey just make the program look good... They would work without them, butThe graphics would suck...)Now, here is an example of the same program but with clipping...'---SCREEN 13x = 50y = 50DOpress$ = INKEY$WAIT &H3DA, 8LINE (x - 8, y - 8)-(x + 8, y + 8), 0, BFCIRCLE (x, y), 7PAINT (x, y), 4, 15IF y > 20 THEN IF press$ = "w" THEN y = y - 1IF y < 180 THEN IF press$ = "s" THEN y = y + 1IF x < 300 THEN IF press$ = "d" THEN x = x + 1IF x > 20 THEN IF press$ = "a" THEN x = x - 1LOOP 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 commandIF y > 20 THEN IF press$ = "w" THEN y = y - 1it says that unless y is greater than... just a minute...> this is the greater than sign< this is the less than signAn 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 thanthey are kind of like arrows pointing down or up the line.The greater than arrow points UP the lineThe less than arrow points DOWN the lineIf you allready know this then it will help you out a lot.ok, lets get back!In the commandIF y > 20 THEN IF press$ = "w" THEN y = y - 1it 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 commandIF y > 20 THEN IF press$ = "w" THEN y = y - 1it says that unless y is greater than 20, look and compare the key that ispressed. 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 ofthe screen...If you don't get this, look at the programm again and again, you may changethings and see what happens, eventually you will learn what it I'm talkingabout...--------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 13x = 50: y = 50xadj = 1: yadj = 1DOpress$ = INKEY$WAIT &H3DA, 8LINE (x - 8, y - 8)-(x + 8, y + 8), 0, BFCIRCLE (x, y), 7PAINT (x, y), 4, 15IF y <= 20 THEN yadj = 1IF y >= 180 THEN yadj = -1IF x >= 300 THEN xadj = -1IF x <= 20 THEN xadj = 1x = x + xadjy = y + yadjLOOP UNTIL press$ = "q"'---In this example, a ball bounces around the screen. When it gets about readyto go off the screen it changes direction, and looks like it bounced.We will use this later to make a pong like game...-----------------------------------------------------------------------------COLLISIONFor the longest time this was something that I could just not understand orfigure out on my own, I had to be taught this. And still I admit, this isprobably not the most reliable method.This will be your first oficial game!!!Use asdw as keysW = upS = downA = leftD = rightQ = quit'--- START COPYING HERE...x1 = 1: y1 = 1x2 = 20: y2 = 10DOpress$ = INKEY$IF oldx1 <> x1 OR oldy1 <> y1 THENCLSoldx1 = x1: oldy1 = y1LOCATE oldy1, oldx1: PRINT " "LOCATE y1, x1: PRINT "O"LOCATE y2, x2: PRINT "X"END IFIF y1 >= 2 THEN IF press$ = "w" THEN y1 = y1 - 1IF y1 <= 22 THEN IF press$ = "s" THEN y1 = y1 + 1IF x1 >= 2 THEN IF press$ = "a" THEN x1 = x1 - 1IF x1 <= 70 THEN IF press$ = "d" THEN x1 = x1 + 1IF x1 = x2 AND y1 = y2 THEN : PRINT "YOU WIN!!!": ENDLOOP 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!!!": ENDyou could also say it like thisIf x1 = x2 thenif y1 = y2 then print "YOU WIN!!!"ENDEND IFEND IFBut 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 drink2. a fairy / Gym TeacherWell, maybe not your gym teacher, you can use mine as an example, (he shavesHis 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 sameX value and the Same Y value it has to be touching, so print YOU WIN andend the game...---That works great when you are in text mode and the sprites take up only onespot, 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 13x = 60: y = 15x2 = 120: y2 = 100xadj = 1: yadj = 1LINE (x2, y2)-(x2 + 10, y2 + 10), 1, BFDOpress$ = INKEY$: WAIT &H3DA, 8PSET (x, y), 4IF y <= 20 THEN yadj = 1IF y >= 180 THEN yadj = -1IF x >= 300 THEN xadj = -1IF x <= 20 THEN xadj = 1x = x + xadj: y = y + yadjIF x > x2 AND x < x2 + 10 AND y > y2 AND y < y2 + 10 THEN PRINT "HIT"ENDEND IFLOOP UNTIL press$ = "q"---In this example a pixel goes around the screen until it hits the blue squarein 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, 0x = 100: y = 0x2 = 120: y2 = 100xadj = 1: yadj = 1delay = 1DOpress$ = INKEY$LINE (0, 0)-(320, 200), 0, BFPSET (x, y), 4LINE (x2, y2)-(x2 + 10, y2 + 10), 1, BFPCOPY 1, 0IF y <= 20 THEN yadj = 1IF y >= 180 THEN yadj = -1IF x >= 300 THEN xadj = -1IF x <= 20 THEN xadj = 1x = x + xadj: y = y + yadjIF x > x2 AND x < x2 + 10 AND y > y2 AND y < y2 + 10 THEN : PRINT "HIT": ENDIF press$ = "w" THEN y2 = y2 - 1IF press$ = "s" THEN y2 = y2 + 1IF press$ = "a" THEN x2 = x2 - 1IF press$ = "d" THEN x2 = x2 + 1FOR i = 1 TO delay: NEXTLOOP UNTIL press$ = "q"---In this game the objective is to move the box around with the keysW = upS = downA = leftD = rightQ = quitRemember... 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 changingthe 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 seeSCREEN 7,0,1,0This means that you are entering SCREEN 7the ,0,1,0 means that you are using one page, you don't have to understandthis, just use it...If you try drawing on the screen in this mode you won't be able to seeanything... you have to use the command PCOPY 1,0...Here is an example'---SCREEN 7,0,1,0print "Hello World!"'---If you try this in qbasic nothing will show up...'---SCREEN 7,0,1,0print "Hello World!"PCOPY 1,0'---This example will show something...The next thing is theLINE (0,0)-(320,200),0,bfThis is the same as CLS only it is much faster...---The other thing is the FOR I = 1 to DELAYthis 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 tequniqueslike 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 startingup QBASIC, pushing ALT + ENTER (to shrink it) and push the paste button onthe tool bar (looks like clipboard and a peice of paper)...HERE WE GO !!'THE PONG EXAMPLE...'---SCREEN 7, 0, 1, 0x = 50: y = 50x2 = 130: y2 = 150pspeed = 5xadj = 1: yadj = 1delay = 1DOpress$ = INKEY$LINE (0, 0)-(320, 200), 0, BFCIRCLE (x, y), 7PAINT (x, y), 4, 15LINE (x2, y2)-(x2 + 30, y2 + 7), 1, BFLINE (x2, y2)-(x2 + 30, y2 + 7), 8, BLOCATE 1, 1: PRINT scorePCOPY 1, 0IF y <= 20 THEN yadj = 1IF y >= 180 THEN yadj = -1IF x >= 300 THEN xadj = -1IF x <= 20 THEN xadj = 1IF press$ = "," AND x2 > 1 THEN x2 = x2 - pspeedIF press$ = "." AND x2 < 290 THEN x2 = x2 + pspeedx = x + xadjy = y + yadjIF y > y2 - 7 AND y < y2 + 2 AND x < x2 + 30 AND x > x2 THENyadj = -1: score = score + 1END IFIF y > y2 + 10 THENFOR i = 1 TO 100PRINT "GAME OVER!!"PCOPY 1, 0NEXTENDEND IFFOR i = 1 TO delay: NEXTLOOP 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.comIf you are using this tutorial on your page, please leave the tutorial exactly as it is... please don't change anything, unless its spellingerrors... Theres alot of them! I don't like using the backspace key...The original website that these were on ishttp://members.aol.com/radiohands/index.htmlThank youVic LuceFinishedseptember 291999