[ re bullets ]
bloodlocust wrote this for me ages ago but made it public so its ok to share.
its a bullet tutorial with extended remarks in his own wit.
`
` learn arrays. and bullets! A tutorial for beginners
`
` by Rob Cummings (aka bloodlocust)
`
sync on
sync rate 60
autocam off
hide mouse
`make a box for the spaceship.
make object cube 20,10
`this line controls how many bullets are stored in the array
`obviously it means also the amount of objects you use and which ones.
maxbullets=10
`so thats 10 bullets right?
`now a FOR loop. For loops make a number go from =1 to whatever
`number is in "maxbullets". in this case, 1 to 10. but its not fixed,see?
for i=1 to maxbullets
make object sphere i , 5
`to make 3d into 2d for this demo
next i
`we've just made 10 bullets unless you change the maxbullets variable
`at the top
`now using the same maxbullets variable means good ****. why? because it means
`we can use it for our arrays to make a number in the array for each of those 10 bullets
dim bullet_x( maxbullets )
dim bullet_y( maxbullets )
dim bullet_used( maxbullets )
`what have we done? yeah we've made a bullet x and bullet y 10 TIMES! remember this. 10 bullets
`means we need 10 x's and 10 y's get it?
`but what does bullet_used() do? well that becomes clear later. we're gonna use
`that thing for testing if a bullet is free to use in the list.
`we can't suddenly use a bullet that is already travelling towards
`an enemy can we? well this thing is 0 if the bullet is free to use or
` 1 if it is currently flying along.
`now for the loop. the game is dead simple. you move the mouse left and right and shoot these bullets.
rem pre main or pre game loop
disable escapekey
rem start of game loop
while escapekey()=0
`simple stuff for demo. so you can see whats going on.
position camera 0,0,-200
point camera 0,0,0
`do a simple spaceship. this is our cube. it's object 20 ok? make sure you remember that object 20
`is being used.
spaceship_x = spaceship_x+mousemovex()
spaceship_y = -100
`
`position the silly thing.
position object 20,spaceship_x,spaceship_y,0
`we need this variable later. To help find an unused bullet. we need to reset it now though.
available_bullet=0
`loop through all existing bullets and decide their AI yes bullets have to make decisions.
`now the important part. we need to draw 10 bullets.
`this for loop goes through all the bullets from 1 to 10
for i=1 to maxbullets
`
`FIRST AI check.
`if our bullets are off screen (or has hit an alien? a wall? or ran out of steam?) -
`you decide, but for now, we choose if its off screen (in this demo understand?)
`we check em all in one tight for loop. yes check all 10 bullets and decide the ai
`for all our bullets. without these comments it would be very small
`
if bullet_y(i) > 200
`YES! the bullet has gone far enough etc to kill it. it is now an unused bullet watch
`this one closely.
bullet_used(i)=0
`see? bullet used is zero, ie unused. we will use this later to decide if a bullet
`is available. for now, we've told it it's dead lets hide it.
hide object i
else
`ELSE! otherwise we move it 10 units minus cos it isn't dead! ie.. its travellin'
bullet_y(i)=bullet_y(i)+6
`it really doesnt matter which object is used. they are all bullets.
`lets position it now.
position object i,bullet_x(i),bullet_y(i),0
endif
`final ai check save an unused bullet in case we wanna shoot one use the
`available_bullet variable to make a new one
`
if bullet_used(i)=0 then available_bullet=i
next i
`and thats it!
`
`if the timer reaches 0 then its been long enough to let
`you fire another bullet. Why? because otherwise you would
`just shoot all the bullets at once and not see a damn thing apart
`from a bunch of bullets huddled togeather like fags.
dec bullet_timer
if bullet_timer<0
`reset the clock first otherwise this thing will not work.
bullet_timer=8
`click the mouse to shoot. finally we might want to make a new bullet now.
`but only if there is an available bullet.
if mouseclick() and available_bullet>0
`show our shiny new bullet.
show object available_bullet
`position our bullet to start off where we are, in this case, a spaceship.
bullet_x(available_bullet) = spaceship_x
bullet_y(available_bullet) = spaceship_y
`finally tell the program and the for loop above that its a USED bullet!
bullet_used(available_bullet)=1
endif
endif
sync
rem end game loop
endwhile
rem cleanup
for i = 1 to 100
if object exist(i)=1
delete object i
endif
next i
end
[ re object management ]
There are a few ways to manage this.
The easiest way I have found is to encapsulate the 3d media integer or number into a variable thats inside your objects instancing.
so how ever u create a series of creatures whether its hardcoded,via a loop,inside an array,inside a type,inside a typed array will depend on how your code will flow on in construction.
I have found typed arrays to be the easiest way of managing groups of objects.
The NUM variable will always handle the 3d medias number so there is only change required and thats at its declaration.
An easier way to try to explain is if u set the varaible
Monster1 = 140
and used this reference throughout your code when ever the 3d media location was asked for then u only need to change the variable number and it will affect all representations of this varaible.
now thats a bit sloppy so to work around it we declare a typed array
imagine a type thats similar to a class.
in this instance we create one bugbear
managing the code this way saves a tonne of array usage.
cleaning up is a breeze also
Monster(2).NUM would be a 2nd monsters 3d media location if u created more than one monster
Monster(1).NAME would equal his name in a string variable
` This source code was distributed via DBDN
` The Dark Basic Developer Network (http://dbdn.darkbasic.com)
` It is copyright David Smith and is used on DBDN with full permission
sync on
sync rate 0
set text size 20
set text font "verdana"
ink rgb(255,255,255),1
TYPE Monster
NUM AS INTEGER
NAME AS STRING
HPS AS INTEGER
x# as FLOAT
y# as FLOAT
z# as FLOAT
ENDTYPE
dim Monster(1) as Monster
Monster(1).NUM=1
Monster(1).NAME="BugBear"
Monster(1).HPS=35
Monster(1).x#=0.0
Monster(1).y#=0.0
Monster(1).z#=0.0
make object cube Monster(1).NUM,10
position object Monster(1).NUM,Monster(1).x#,Monster(1).y#,Monster(1).z#
position camera 0,30,-60
point camera 0,0,0
do
center text object screen x(1),object screen y(1),""+Monster(1).NAME
if escapekey()=1
delete object Monster(1).NUM
undim Monster(1)
end
endif
sync
loop
I spaced object groups by 100 in my game so If i need to create more than 100 of each object Ill have to simply change the NUM locations to suit.
I hope that helps, it always pays off to have your media list in a notepad next to you.