Vic's QBasic Programming Tutorial Basic Tutorial V BITMAPS !!!! ----------------------------------------------------------------------------- No, this will not tell you how to use the windowns *.bmp picture format... This is way more important than that... Using bitmaps will change your programming life forever. I know it really changed the way I program. It is very very easy to understand and to use. Basically, this is a bitmap 111110000220003333333330022000000220000333330000111111000 110011000000000003300000022200000220003300033000110001100 111110000220000003300000022220022220033000003300110001100 110011000220000003300000022022220220033333333300111110000 110011000220000003300000022002200220033000003300110000000 111110000220000003300000022000000220033000003300110000000 If you look close you can see that I have drawn BITMAP in the bitmap above. I bet your thinking, "Big deal, how the heck does that revolutionize the way you program?" The answer to that is how you wish to use it... 1. You could use it to draw BITMAP on the screen... 2. You could use it as a map file to make an RPG game... 3. You could use your imagination... so what, I ran out of ideas... This tecnique is allmost vital to game programming. It allows you to make more detailed graphics and allows you to map out where you want things to go... The only hard thing to understand is how you use it... ---------------------------------------------------------------------------- I will once again start with an example... '--- Start copying SCREEN 13 FOR y = 1 TO 5 FOR x = 1 TO 5 READ clr IF clr = 1 THEN PSET (x, y), 1 IF clr = 0 THEN PSET (x, y), 4 NEXT NEXT DATA 1,0,0,0,1 DATA 0,1,0,1,0 DATA 0,0,1,0,0 DATA 0,1,0,1,0 DATA 1,0,0,0,1 '--- STOP! Remember from a few tutorials back that X goes left and right and Y goes up and down, and they go in the order X then Y. But if you look in the above program in the two commands... FOR y = 1 TO 5 FOR x = 1 TO 5 you notice that they are reversed... Remember to do this, If you put it the othere way everything will be screwed up, and you will most likely not notice it when you debug... what the previous commands do (FOR y = 1 TO 5 & FOR x = 1 TO 5) is tell the program to read x1,y1 then x2,y1 then x3,y1... until it gets to the one after x5,y1 then it goes to x1,y2 then x2,y1 and on and on until it finally gets to the last one x5,y5. If you don't understand this right now it will eventually fall in and make sense when you get to programming your own games using this. If you don't know where two 5's come from look at what is declared in the data. there are 5 numbers on the top going left to right and 5 numbers on the left going up and down. The "READ clr" command reads the next number that was declared in the data statement. once that one was read it reads the next one after it loops (when it gets to the NEXT command) The PSET command puts a pixel at the x and y locations with the correct bitmaped color... ------------------- Please tell me you understand the above... If you really don't then just keep looking at it and experimenting (hope thats spelled right)... what I teach you next won't make much sense at first. You might think Why is he teaching me this? This is important though... If you want to spread something out what should you use? For a bitmap you should use multiplication. ( the * character ) heres an example of how it works. '--- Start copying SCREEN 13 FOR y = 1 TO 5 FOR x = 1 TO 5 READ clr IF clr = 1 THEN PSET (x * 5, y * 5), 1 IF clr = 0 THEN PSET (x * 5, y * 5), 4 NEXT NEXT DATA 1,0,0,0,1 DATA 0,1,0,1,0 DATA 0,0,1,0,0 DATA 0,1,0,1,0 DATA 1,0,0,0,1 '--- STOP! If you try this out you should notice that the pixels end up 5 spaces from each other. That is set by the "x * 5" Now if you wanted to change the pixel size you would use the command Line (x * 5, y * 5)-(x * 5 + 5, y * 5 + 5),color,bf That should draw a square the correct size... So here is the program again with bigger pixels... '--- Start copying SCREEN 13 FOR y = 1 TO 5 FOR x = 1 TO 5 READ clr IF clr = 1 THEN LINE (x * 5, y * 5)-(x * 5 + 5, y * 5 + 5), 1, BF IF clr = 0 THEN LINE (x * 5, y * 5)-(x * 5 + 5, y * 5 + 5), 4, BF NEXT NEXT DATA 1,0,0,0,1 DATA 0,1,0,1,0 DATA 0,0,1,0,0 DATA 0,1,0,1,0 DATA 1,0,0,0,1 '--- STOP! ------------------------------------ LOADING OBJECTS ONTO SCREEN FROM BITMAP... To do this you do the same thing as in the last one... only put an object instead of a line(x,y)-(x+xx,y+yy),clr,bf... (if you understand that, if not, don't worry...) Here is an example to learn from... it uses the GET & put commands, I havn't discussed them yet, but I will in the future and don't worry if you don't get it. '----- Start copying! DIM bground1(1000), bground2(1000) 'Dim the background pics before the 'Get commands... SCREEN 13 FOR y = 1 TO 10 ' the first bitmap is 10 x 10 FOR x = 1 TO 10 READ clr PSET (x, y), clr NEXT NEXT GET (1, 1)-(10, 10), bground1 FOR y = 1 TO 10 ' the second bitmap is 10 x 10 FOR x = 1 TO 10 READ clr PSET (x, y), clr NEXT NEXT GET (1, 1)-(10, 10), bground2 CLS FOR y = 1 TO 5 'This reads the 5 by 5 map and FOR x = 1 TO 5 'puts the tiles where they belong READ tilenumber IF tilenumber = 1 THEN PUT (x * 10, y * 10), bground1, PSET 'This puts the IF tilenumber = 2 THEN PUT (x * 10, y * 10), bground2, PSET 'tiles down NEXT NEXT 'This is the first background pic, not the map yet... 'This is pseted on the screen and then saved as bground1 DATA 1,1,1,7,7,7,7,2,2,2 DATA 1,1,7,7,7,7,7,7,2,2 DATA 1,8,1,7,7,7,7,2,8,2 DATA 8,8,8,1,7,7,2,8,8,8 DATA 8,8,8,8,1,2,8,8,8,8 DATA 8,8,8,8,4,3,8,8,8,8 DATA 8,8,8,4,7,7,3,8,8,8 DATA 4,8,4,7,7,7,7,3,8,3 DATA 4,4,7,7,7,7,7,7,3,3 DATA 4,4,4,7,7,7,7,3,3,3 'This is the second background pic, still not the map... 'This is pseted on the screen and then saved as bground2 DATA 9,9,9,9,9,9,9,9,9,9 DATA 9,7,7,7,1,4,8,8,8,9 DATA 9,7,7,7,1,4,8,8,8,9 DATA 9,7,7,7,1,4,8,8,8,9 DATA 9,1,1,1,1,4,4,4,4,9 DATA 9,3,3,3,3,2,2,2,2,9 DATA 9,8,8,8,3,2,7,7,7,9 DATA 9,8,8,8,3,2,7,7,7,9 DATA 9,8,8,8,3,2,7,7,7,9 DATA 9,9,9,9,9,9,9,9,9,9 'THIS IS THE MAP!! it is 5 x 5 DATA 1,1,1,1,1 DATA 1,2,2,2,1 DATA 1,2,1,2,1 DATA 1,2,2,2,1 DATA 1,1,1,1,1 '------ DONE!!! Remember, Don't type this in by hand, what you can do is Start Qbasic in Windows 95 or 98 or whatever, and press ALT & ENTER to make it in a box, then, copy the program from here. Go back to the dos box and push the Paste button on the dos box toolbar, This should type everything for you. If you don't understand what exactly is going on, just look at it and experiment... This will help in many programs. ---------------------------------------------------------------------------- Does your head hurt as much as mine? One more quick thing and I can call it a night... PUTTING A BITMAP INTO AN ARRAY... First you have to DIM the ARRAY like this DIM bitmapname(max x value, max y value) to put a number into it do this. READ bitmapname(x,y) The only time you would want to use an array for a bitmap is if you are using it over and over. If you use it more than once you might want to store it in an array... I don't want to but what the heck, another example '----- START! DIM tile(10, 10) 'Dim the array SCREEN 13 FOR y = 1 TO 10 FOR x = 1 TO 10 READ tile(x, y) NEXT NEXT 'All that gets the information into the array FOR i = 1 TO 10 'Do this five times FOR y = 1 TO 10 FOR x = 1 TO 10 PSET (x + i * 10, y), tile(x, y) NEXT NEXT NEXT DATA 1,7,7,7,7,7,7,7,7,4 DATA 7,1,8,8,8,8,8,8,4,7 DATA 7,8,1,8,8,8,8,4,8,7 DATA 7,8,8,1,8,8,4,8,8,7 DATA 7,8,8,8,1,4,8,8,8,7 DATA 7,8,8,8,4,1,8,8,8,7 DATA 7,8,8,4,8,8,1,8,8,7 DATA 7,8,4,8,8,8,8,1,8,7 DATA 7,4,8,8,8,8,8,8,1,7 DATA 4,7,7,7,7,7,7,7,7,1 '----- STOP!! If you have allready tried to write your own program and come up with the error "You have run out of DATA" or something like that that is because you cant use the READ function more than once (unless you use a certain command, but that gets complicated). This will solve that problem... ----------------------------------------------------------------------------- Thats it for this tutorial, If I didn't get into enough detail in the explanations then just look at the source code and try to figure it out on your own. All else fails E-Mail Me... 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 November 2 (Yes, one day after I finished Tutorial IV!) 1999 If you want to be notified when a new tutorial is out.. Send An E-mail to RADIOHANDS@AOL.com with the subject saying VQBLIST and then your E-mail address(check website)