Vic's QBasic Programming Tutorial Basic Tutorial XVIII Multiple instances how to program more than one nude chick sprite to move in your next game!! Just kidding... (I am againt Porno in games) (unless its cheap) ----------------------------------------------------------------------------- so, you have youre brand new RPG game running in QB... or maybe you don't... so, you have youre brand new 3d space game running in QB... or maybe you don't... Either way, you might have run into a problem... but allways remember... don't forget about the suntan lotion... --- one of the big problems you might have run into, is that it is very hard to get more than one sprite to work at a time... if you have read many of my tutors you might have seen this... most sprites are done like this... x1 = 50 y1 = 50 x2 = ... y2 = ... that is an easy way to do it if you don't need very many sprites... But, if you do, your in need of a new tecnique... And I'm here to teach it to you... ------- lets start off with an example that you yourself might have thought up if you needed to... 'BEGIN READ x1: READ y1: READ x2: READ y2 READ x3: READ y3: READ x4: READ y4 READ x5: READ y5: READ x6: READ y6 READ x7: READ y7: READ x8: READ y8 SCREEN 13 PSET (x1, y1), INT(RND * 16 + 1) PSET (x2, y2), INT(RND * 16 + 1) PSET (x3, y3), INT(RND * 16 + 1) PSET (x4, y4), INT(RND * 16 + 1) PSET (x5, y5), INT(RND * 16 + 1) PSET (x6, y6), INT(RND * 16 + 1) PSET (x7, y7), INT(RND * 16 + 1) PSET (x8, y8), INT(RND * 16 + 1) DATA 50,20,10,20,15,60,23,23,20,23,23,62,26,73,23,43 'END such a simple purpose, but so complicated and long... lets shorten it up a bit... ''' if youre feeling a bit daring, try to shorten it up before looking... 'begin... DIM x(8), y(8) FOR i = 1 TO 8: READ x(i): READ y(i): NEXT SCREEN 13 FOR i = 1 TO 8 PSET (x(i), y(i)), INT(RND * 16 + 1) NEXT DATA 50,20,10,20,15,60,23,23,20,23,23,62,26,73,23,43 'end!!! your not impressed??? Imagine, if you had to handle not 8 sprites, but 800 sprites!! which one would you choose? ---- Ok, now we know that it can make programs shorter and more orginized, but what use would it be in my game??? ... A question that I was asked recently, is how to make more than one 3D Poly-gon on the screen... A good question for someone just starting out... And I hope to answer that question here... --- before we go into 3D,,, lets look at a few 2d examples... this first example will be pretty basic, and just handle one sprite, but it will have more than one instance of itself, and each one will have a mind of its own... 'BEGIN!!!!! DIM x(1000), y(1000), xadj(1000), yadj(1000) FOR i = 1 TO 1000: x(i) = INT(RND * 320 + 1): y(i) = INT(RND * 200 + 1) xadj(i) = 1: yadj(i) = 1: NEXT spritenum = 100 ' this can be from 1 to 1000... SCREEN 7, 0, 1, 0 DO: press$ = INKEY$: CLS FOR i = 1 TO spritenum IF x(i) >= 320 THEN xadj(i) = INT(RND * -3) IF x(i) <= 0 THEN xadj(i) = INT(RND * 3) IF y(i) >= 200 THEN yadj(i) = INT(RND * -3) IF y(i) <= 0 THEN yadj(i) = INT(RND * 3) x(i) = x(i) + xadj(i): y(i) = y(i) + yadj(i) CIRCLE (x(i), y(i)), 5, 1 NEXT PCOPY 1, 0 LOOP UNTIL press$ = CHR$(27) 'LET THE BUBBLE INSANITY END!!! --- pretty cool huh? (that is if you havn't tried this stuff before...) ------ so, once agian I see what it can do, but how does it work??? the main reason this all works is how you handle the ARRAYS... At the beginning of the program you probly noticed this... DIM x(100),y(100) what this does is set the array X to have more than one place to store the data... (101 places to be exact... 0-100 ) if you wanted to store data indavidually to each X value you would do it like this.. x(13) = 53 what this does is set the 13th value of X to 53... so, if you did this... PRINT x(13) 53 would be printed out... ITS THAT SIMPLE!!! try it... just remember.. Make sure to DIM the arrays!!! (or else it won't work) you can do it like this DIM ARRAY(#) or you can do it like this... DIM SHARED ARRAY(#) when you put the SHARED, you can use this value in the SUB's... ---- Ok, before I continue lets take the above code and cut it up for better understanding (hopefully) DIM x(1000), y(1000), xadj(1000), yadj(1000) First you of coarse have to dim the arrays... X is the X values... y is the y values... xadj is the direction that sprite is traveling x direction... yadj is the direction that the sprite is traveling y direction... (this uses your basic steps for moving a bouncing ball...) --- FOR i = 1 TO 1000: x(i) = INT(RND * 320 + 1): y(i) = INT(RND * 200 + 1) xadj(i) = 1: yadj(i) = 1: NEXT this can be adjusted to look like this (withought the Colons) FOR i = 1 TO 1000 x(i) = INT(RND * 320 + 1) y(i) = INT(RND * 200 + 1) xadj(i) = 1 yadj(i) = 1 NEXT what this does is set up each sprite location to start at thier own place... i is set to go from 1 to 1000... x(i) = INT(RND * 320 + 1) this sets up the i sprites x value... y(i) = INT(RND * 320 + 1) this sets up the i sprites y value... xadj(i) = 1 yadj(i) = 1 this sets up the beggining values for all xadj and yadj to 1... so if i was currently 6 then this is what it would look like... x(6) = INT(RND * 320 + 1) y(6) = INT(RND * 200 + 1) xadj(6) = 1 yadj(6) = 1 --- spritenum = 100 ' this can be from 1 to 1000... this is simple... sets up how many circles to make... --- SCREEN 7, 0, 1, 0 this sets up the screen mode... --- DO: press$ = INKEY$: CLS this starts the loop --- FOR i = 1 TO spritenum starts the i for loop... (remember that each time this goes through 1 is added to i ) --- IF x(i) >= 320 THEN xadj(i) = INT(RND * -3) IF x(i) <= 0 THEN xadj(i) = INT(RND * 3) IF y(i) >= 200 THEN yadj(i) = INT(RND * -3) IF y(i) <= 0 THEN yadj(i) = INT(RND * 3) once again, this is the same code used to make a basic bouncing ball routine... IF x(i) >= 320 THEN xadj(i) = INT(RND * -3) if the current x value is off the screen than make the xadj value a negative so it will back up... (the int(rnd * -3) sets it up so its not constant) you could have just xadj(i) = -1 if you wanted...) I think you can figure the others out... --- x(i) = x(i) + xadj(i): y(i) = y(i) + yadj(i) this could look like this (withought the colons) x(i) = x(i) + xadj(i) y(i) = y(i) + yadj(i) this just keeps the ball rolling... --- CIRCLE (x(i), y(i)), 5, 1 draw the current i # circle... --- NEXT run the loop... --- PCOPY 1, 0 copy the current screen to visible screen... --- LOOP UNTIL press$ = CHR$(27) loop... --- and thats that... -------------------------- ########################## -------------------------- OK OK... wonderfull,,, but in your 3D tutor you allready covered that!!! How can I use this when I am allready using them!!! for this, you need to know another way to set up the same arrays... like this... DIM X(10,10) or this DIM X(10,10,10,10,10,10) (how many you want) this is exaclty the same when used... X(5,3) = 20 print x(5,3) the only difference is that you can set it up differently... -------------------------- Ok... heres a cool example... (If you can't tell, I ripped it from tutor 8 and edited it...) 'BEGIN... DIM c!(360), s!(360) 'Now dim the array for Eight points... 'Remember, a cube has Eight Points in it... DIM x(8), y(8), Z(8), x2(8), y2(8), Z2(8), x3(8), y3(8) DIM xv(100), yv(100), phiv(100), thetav(100), speedv(100) FOR i = 1 TO 100 xv(i) = INT(RND * 310) yv(i) = INT(RND * 190) phiv(i) = INT(RND * 350) thetav(i) = INT(RND * 350) speedv(i) = INT(RND * 10 + 1) NEXT FOR i = 1 TO 360 c!(i) = COS(i * 3.14 / 180): s!(i) = SIN(i * 3.14 / 180) NEXT FOR i = 1 TO 8: READ x(i): READ y(i): READ Z(i): NEXT phi = 1: theta = 1 xcenter = 150: ycenter = 100: zcenter = 256 delay = 10000 'You know what this is for... change for speed SCREEN 7, 0, 1, 0 DO press$ = INKEY$ CLS FOR d = 1 TO 10 FOR i = 1 TO 8 'This makes you do this 8 times... 'Remember, these are right from the paper you printed up... x2(i) = -x(i) * s!(thetav(d)) + y(i) * c!(thetav(d)) y2(i) = -x(i) * c!(thetav(d)) * s!(phiv(d)) - y(i) * s!(thetav(d)) * s!(phiv(d)) - Z(i) * c!(phiv(d)) + p Z2(i) = -x(i) * c!(thetav(d)) * c!(phiv(d)) - y(i) * s!(thetav(d)) * c!(phiv(d)) + Z(i) * s!(phiv(d)) x3(i) = 256 * (x2(i) / (Z2(i) + zcenter)) + xv(d) y3(i) = 256 * (y2(i) / (Z2(i) + zcenter)) + yv(d) PSET (x3(i), y3(i)), 15 NEXT LINE (x3(1), y3(1))-(x3(3), y3(3)), 15 LINE (x3(1), y3(1))-(x3(4), y3(4)), 15 LINE (x3(1), y3(1))-(x3(5), y3(5)), 15 LINE (x3(2), y3(2))-(x3(3), y3(3)), 15 LINE (x3(2), y3(2))-(x3(4), y3(4)), 15 LINE (x3(2), y3(2))-(x3(6), y3(6)), 15 LINE (x3(7), y3(7))-(x3(6), y3(6)), 15 LINE (x3(7), y3(7))-(x3(3), y3(3)), 15 LINE (x3(7), y3(7))-(x3(5), y3(5)), 15 LINE (x3(8), y3(8))-(x3(5), y3(5)), 15 LINE (x3(8), y3(8))-(x3(4), y3(4)), 15 LINE (x3(8), y3(8))-(x3(6), y3(6)), 15 NEXT 'All these lines just make the box by connecting the points... 'If you erase all the lines you will get just pixels drawn on the screen... PCOPY 1, 0 FOR i = 1 TO 10 phiv(i) = phiv(i) + speedv(i): thetav(i) = thetav(i) + speedv(i) IF phiv(i) > 360 THEN phiv(i) = phiv(i) - 360 IF thetav(i) > 360 THEN thetav(i) = thetav(i) - 360 NEXT FOR i = 1 TO delay: NEXT LOOP UNTIL press$ = CHR$(27) 'These are all of the points... predetermined... 'Remember, there are 8 points in a cube... 'These first four are the square farthest 'from the screen DATA 10,10,-10 DATA -10,-10,-10 DATA -10,10,-10 DATA 10,-10,-10 'These are the closest... DATA 10,10,10 DATA -10,-10,10 DATA -10,10,10 DATA 10,-10,10 'END... --------------- I just used the same tecnique... Edit this code so you have each square bouncing around the screen... (kind of like a 3d asteroids program!!) its posible... you might run into problems when you try to use more than one type of polygon, but with this information, I'm sure you can figure it out... ---------------------------------------------------------------------------- **************************************************************************** ---------------------------------------------------------------------------- File Edit View Search Run Debug Calls Options Help +--------------------------------- Untitled -------------------------------¦+-+ ¦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... I want to make these tutorials _ ¦as easy to understand as posible!!! _ ¦ _ ¦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 _ ¦May 23 _ ¦2000 _ ¦  ¦ ___________________________________________________________________________¦ ¦ N 00001:008 ... 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)