 |
| Author |
Message |
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
|
Posted: 2nd Jan 2007 04:33 Edited: 2nd Jan 2007 04:50 |
| link | toggle |
This Community Projects thread is dedicated to the fantastic DBPro community we have and to show that DBPro and the DBPro community can make decent games in a short amount of time.
The concept is simple and straight forward: Make simple games from contributions from the community in a given timeframe (one to two weeks). When the timeframe is up, the project is considered "complete" and we move on to the next project. Of course, folks can wrap up previous projects if they want.
The most important thing to remember is that the projects are intended to be a forum for the community to work together towards some common goals. These are not meant to be in-depth ground-breaking amaizing top-of-the-line projects, but rather little projects that provide folks with a distraction from their day-to-day projects that I know everyone is working so hard on. That said, these are meant to be fun, and can even get silly.
Feel free to contribute in any way you see fit. If you like to model, provide a model or a texture for the project. If you have a suggestion on the game design, post it. If you have some code to contribute, add it on in. And hopefully, along the way, some folks might learn a thing or two.
Some guidelines:
-Please respect other people's contributions: Not everyone is a master coder, modeler, or artist. If you have a suggestion to improve on someone else's work, then by all means make it politely and constructively.
-Please respect other people's advice and critisms about any contributions you have made: Take the opportunity to learn a different approach to achieving the same results.
-Move forward on the project: Avoid reworking someone else's work unless it is absolutely necessary. Remember, there is a time limit set on these projects to provide a challenge to see if we can get the project completed on time.
-Anyone can start a project as long as there is no project currently in progress: To start a project; you must show a significant start to the project, which should include code and some media; you also must have contributed significantly to the previous project. The community may reject any project idea submitted, so it is best to ask before posting a new project. If several projects are posted at roughly the same time, then the community will decide which project to complete first.
-If you start a project, be prepared to finish it: That means you are the leader for that project. If no one else contributes, then it would be up to you to finish it in the timeframe given. This is intended to prevent folks from posting outrageous project ideas. If you could not do it by yourself, please do not expect the community to do it for you.
-Extending a project deadline: Project timeframes should be established from the start for one to two weeks (please post the completion date, not just the number of weeks). A project may be extended by up to two days by the person that started the project. Project deadlines should not be simply extended because a project is not done. Rather, if the community has demonstrated interest and continued development of the project, then extend the deadline. Otherwise, let a new project get started.
-Media is encouraged; but should be limited and small (and compressed).
-Project files: should be zip files with folder structure, code, and media included. Please, no .exe files.
-If you are updating code, do not update the project zip file: Post the code in a code box in a thread. The next upload of the project zip file can then include previously submitted code. Basically, let's be respectful of TGC's storage space.
Note: I will be posting the first project challenge (Space Invaders) in a few minutes.

Open MMORPG: It's your game!
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
|
Posted: 2nd Jan 2007 04:44 Edited: 2nd Jan 2007 04:52 |
| link | toggle |
|
Back to top
 |
|
indi
User
Joined: Mon Aug 26th 2002
Location: Earth, Brisbane, Australia
|
|
Posted: 2nd Jan 2007 05:12 Edited: 2nd Jan 2007 05:14 |
| link | toggle |
splash - screen, with possible space for menu in the middle, or just first splash screen.
its a png so it retains its colours and quality, and about 600k so it can be implemented without losing jpgness-ness.
800 x 600, if you require another size just ask Ive retained the PSD file.
good work mate, its starting to feel like the older days.
|
Back to top
 |
|
Ric
User
Joined: Sun Jul 11th 2004
Location: object position x
|
Good concept Rii - I'll give it my support as and when time allows.
|
Back to top
 |
|
BatVink
TGC Newsletter Editor
 Joined: Fri Apr 4th 2003
Location: Chilling
|
|
Back to top
 |
|
Peter H
User
Joined: Fri Feb 20th 2004
Location: Witness Protection Program
|
Ok, i updated the code to add movement controls for the player.
I created a UDT for the player and really went overboard explaining UDTs because i wasn't sure how simplified we should make it (so i decided I'd rather be safe than sorry). If it's too much feel free to cut it up.
Also, i changed the file structure, putting all the media in a "data" folder (so it doesn't get messy)... i hope that's ok?
attached updated version
One man, one lawnmower, plenty of angry groundhogs.

|
Back to top
 |
|
Lukas W
User
Joined: Fri Sep 5th 2003
Location: Norway
|
sounds like fun!
i applied some rotation to the player ship while moving left or right.
i would've added some more features, but I'll take it easy. I'm not exactly one of the best programmers here..
|
Back to top
 |
|
Zotoaster
User
Joined: Mon Dec 20th 2004
Location: Scotland
|
Here's some nice firey particles from the ship
The particle engine can be used for anything so far.
P.S. Doesn't seem to like me uploading the zip - last try
|
Back to top
 |
|
Zotoaster
User
Joined: Mon Dec 20th 2004
Location: Scotland
|
|
Posted: 2nd Jan 2007 11:38 Edited: 2nd Jan 2007 11:58 |
| link | toggle |
Here's some nice firey particles from the ship
The particle engine can be used for anything so far.
P.S. Doesn't seem to like me uploading the zip - last try
Actually, no change in media, only code, so here it is
+ Code Snippet
`Standard Setup
Sync On: Sync Rate 60:Autocam Off
`The camera starts of at 0,0,0. Move it back and up a bit so the action can take center on 0,0,0
Position Camera 0,50,-100
`There will be 6 rows of 9 invaders to start with.
`The player's ship will be at the bottom, like so.
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` /
`Create an array to store the invader information. This is not needed now, but will be needed later.
`This is a 2-dimensional array. It is 6 by 9 in size (or 6x9=54). 5,8 was used instead of 53 to make
`it easier to determine rows and columns. It is normally better to use a single array of 53.
Dim Invaders(5,8)
`Load the aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
`Get a free object ID for our invader
obj=FreeObject()
`Store the Object ID in the Invaders array
Invaders(r,i)=obj
`Load the alien model
Load Object "DataAlien1.x",obj
`Next, we need to make sure our model is the right size. For this, the model needs to be scaled.
`Roughly, we want a model that is 6x6x6 (just what is picked and looks about right).
`To scale the model, we first need to know it's size ratio to a size of 6.
`Here's how to get that:
s#=6.0/Object Size X(obj)
`All that is left to do is scale the model. Remember that Scale Object uses an integer scale that represents
`percentage values (100 = 100% = 1, 50 = 50% = .5, 1 = 1% = .01, and so on). So, the scale (s#) needs to be
`multiplied by 100.
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
`Each invader is 6x6x6 in size. Our viewable area is a little less than 100 x 100
`In order to fit the invaders evenly, we can divide the area we have to work with by
`the number of colums - 9. Since 9 goes into 90 evenly, lets use that. The result is 10.
`Here is the formula to calculate the X-position of the invaders every 10 spaces from
`-45x to +45x
x=(i*10)-45
`Using the same spacing of 10 for the Y-position, and the invaders need to be well up off
`the ground, here is the formula to calculate the Y-position.
y=(r*10)+50
`Position the invader
Position Object obj,x,y,0
Next i
Next r
`Load the shield barriers (same process as above).
Dim Barriers(2)
For i = 0 to 2
obj=FreeObject()
Barriers(i)=obj
Load Object "DataBarrier.x",Barriers(i)
s#=12.0/Object Size X(obj)
Scale Object Barriers(i),s#*100.0,s#*100.0,s#*100.0
x#=(i*35)-35
Position Object Barriers(i),x#,20,0
Next i
`Load player's missile
`Use an array for the variable since it is global (A global variable could be used, but later you will see why
`there is a preference for Arrays over Global Variables).
Dim Missile(0)
`Assign a free object to Missile
Missile(0)=FreeObject()
`Load the missile model
Load Object "DataMissile.x",Missile(0)
`Calculate the scale
s#=6.0/Object Size Y(Missile(0))
`Scale the missile.
Scale Object Missile(0),s#*100.0,s#*100.0,s#*100.0
`Hide the missile
Hide Object Missile(0)
`Create a playertype "user defined array"
type PlayerType
`this will store the id of the object that is associated with the player, so we can move it later
ObjId as integer
`to keep track of the x position of the player, we are creating these as floats in order
`to maintain a smooth movement. With integers the player would "jump" from one integer to another
`adding the "#" to the end defines it as a float, to use this variable later you must use the "#"
xpos#
`we also need to keep track of the y position of the player, the player's "up and down"
`movement will be limited to keep them behind the bariers
ypos#
`this stores the distance that the player can move in one frame
speed#
` with the following variable stored, we can apply a neat rotation effect to the ship while
` it is moving left or right
yang#
endtype
REMSTART
Using "User Defined Types"
The code
type TheBestTypeEver
endtype
Creates a User Defined Type (reffered to as a UDT) with the name "TheBestTypeEver"
This by itself is a empty type, the entire point of using UDTs is to simplify code
by allowing sub variables for a "Parent" variable
to add sub variables to a UDT you simply type them out in between the 'type' and 'endtype'
type TheBestTypeEver
TheBestVariableEver as integer
TheOtherVariable as string
endtype
This adds two sub variables to the UDT, a integer (only "whole" numbers such as 1, 2, 3, 4, -17, 42,.. etc)
named "TheBestVariableEver", and a string named "TheOtherVariable"
Note: if you declare a variable without telling the compiler what type it is, it will
automatically be declared as an integer. So we could have left off the "as integer"
A UDT by itself does nothing at all, you can't assign something to it (such as TheBestTypeEver = 1337)
You have to declare a variable and define it as the type that your UDT is
A UDT is really just a fancy type of variable that you've created custom made for your game
It's used in exactly the same way that you would when you are declaring a variable as a string, float, or integer
bob as integer
bob as TheBestTypeEver
however, UDTs are different than normal variable types because they have sub variables
with "bob as int" you assign a value to it in the normal way, bob = 12
When you have a variable declared as a UDT you have to pick a sub variable to work with
It's easy to tell the compiler which sub variable you want, you just add a period (".")
to the end, then add the name of the sub variable.
so, going with our example, bob as TheBestTypeEver
bob.TheBestVariableEver = 20
bob.TheOtherVariable = "hello world"
That sets the two sub variables in bob to 20, and "hello world" (terribly original i know)
you can then access them just like normal variables
print bob.TheBestVariableEver
print bob.TheOtherVariable
Note: You CANNOT assign anything to the "parent" variable by itself. So bob = 20 would NOT work.
you need to pick a sub variable to assign to.
REMEND
`Create array Player() so it is global (A global variable can be used, but we will cover later why
`there is a preference for arrays over global variables).
`This array is created as a PlayerType array, and has all the attributes of the PlayerType User Defined Type
Dim Player(0) as PlayerType
`Get a free object for the player's ship
Player(0).ObjId = FreeObject()
`Load in the player's ship
Load Object "DataShip.x",Player(0).Objid
`Calculate the ratio of the ship to size 6 (making the player a little larger than the aliens)
s#=6.0/Object Size X(Player(0).ObjId)
`Scale the player's ship
Scale Object Player(0).ObjId,s#*100.0,s#*100.0,s#*100.0
`Place the player's ship
Position Object Player(0).ObjId,0,0,0
`-------------------------
`Player_Controls variables
`-------------------------
`set the player's speed
Player(0).speed# = 0.4
`sets the farthest position the player can travel to the right
rightside# = 77.0
`sets the farthest position the player can travel to the left
leftside# = -77.0
`sets the farthest position the player can travel upwards
topside# = 12.0
`sets the farthest position the player can travel downwards
bottomside# = -9.0
`-------------------------
`End Of Player_Controls variables
`-------------------------
`Particle Data
`Use this function to set up the particle data, otherwise they wont work.
setupParticles()
`Create a particle emmiter
prtShip=addParticles(0,0,0,60,60)
setParticleSize(prtShip,1.8,-0.03)
colorParticles(prtShip,255,0,0)
setParticleGhost(prtShip,1)
setParticleGravity(prtShip,0,-0.1,0)
`Call this function to actually make all the particles
createParticles()
`Main loop
Do
gosub player_controls
Set Cursor 0,0
Print Screen FPS()
Sync
Loop
End
`For more information on For/Next/Loops, refer to DBPro Help: Principles / Common Statements
`For more information on For/Next/Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`For more information on Formulas, refer to DBPro Help: Principles / Datatypes and variables
`A quick little function to check for the first free object number.
Function FreeObject()
`Start at object number 1
i=1
`A simple loop
Do
`If the object does not exist (=0), then exit the loop. "i" will be the valid object number
If Object Exist(i)=0 Then Exit
`Move on to the next object number
i=i+1
`Close the loop
Loop
`End the function and return the valid object number
Endfunction i
`For more information on Functions and returning values, refer to DBPro Help (press F1): Principles / Functions
`For more information on Loops, refer to DBPro Help: Principles / Common Statements
`For more information on Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`handles all the input from the player
player_controls:
` while not left or right key is pressed, we rotate the ship back to its original angle
Player(0).yang# = curvevalue( 0.0, object angle y( Player(0).ObjId ), 50.0 )
`check if the player is hitting the right key, if they are we want to let them move right
`as long as they are not hitting the right side of the screen
if rightkey() = 1
`move the player to the right at the preset speed
inc Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the right side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# > rightside# then Player(0).xpos# = rightside#
` apply rotation to the ship while we move right
Player(0).yang# = curvevalue( -40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check in the same way for the left key
if leftkey() = 1
`move the player to the left at the preset speed
dec Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the left side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# < leftside# then Player(0).xpos# = leftside#
` apply rotation to the ship while we move left
Player(0).yang# = curvevalue( 40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check if the player is hitting the upkey, if they are we want to let them move up
`but not past the barriers
if upkey() = 1
`move the player upwards at the preset speed
inc Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the barriers
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# > topside# then Player(0).ypos# = topside#
endif
`check if the player is hitting the downkey, if they are we want to let them move down
`but not past the bottom of the screen
if downkey() = 1
`move the player downwards at the preset speed
dec Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the bottom of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# < bottomside# then Player(0).ypos# = bottomside#
endif
`now that we've adjusted the position of the player accordingly, we need to update the object
`that is representing the player
position object Player(0).ObjId, Player(0).xpos#, Player(0).ypos#, 0
rotate object Player(0).ObjId, 0, Player(0).yang#, 0
`Update Particles
positionParticles(prtShip,Player(0).xpos#+plusmin(rnd(1.0)), Player(0).ypos#, 0)
controlParticles()
return
`Particle Data
function plusmin(num#)
rand=rnd(10)
if rand>5 then ret#=num#*-1 else ret#=num#
endfunction ret#
function setupParticles()
dim prtsys(0) as tprtsys
endfunction
function createParticles()
count=array count(prtsys())
cmax=0
for c=1 to count
if prtsys(c).count>cmax then cmax=prtsys(c).count
next c
dim prtdat(array count(prtsys()),cmax) as tprtdat
for c=1 to array count(prtsys())
for o=1 to prtsys(c).count
prtdat(c,o).obj=val(getToken(prtsys(c).objc,",",o))
next o
next c
endfunction count
function addParticles(x#,y#,z#,life,count)
array insert at bottom prtsys()
num=array count(prtsys())
prtsys(num).pos.x=x#+plusmin(rnd(0.2))
prtsys(num).pos.y=y#+plusmin(rnd(0.2))
prtsys(num).pos.z=z#+plusmin(rnd(0.2))
prtsys(num).life=life
prtsys(num).count=count
for c=1 to prtsys().count
obj=zp_freeobject() : make object plain obj,100,100
set object transparency obj,3
disable object zwrite obj
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
obj$=obj$+str$(obj)+","
lifep=rnd(life)
life$=life$+str$(lifep)+","
next c
prtsys(num).objc=obj$
prtsys(num).lifep=life$
endfunction num
function setParticleSpeed(num,speed#)
prtsys(num).speed=speed#
endfunction
function setParticleSize(num,size#,sizep#)
prtsys(num).sizei=size#
prtsys(num).sizep=sizep#
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
scale object obj,size#,size#,1
next c
endfunction
function positionParticles(num,x#,y#,z#)
prtsys(num).pos.x=x#
prtsys(num).pos.y=y#
prtsys(num).pos.z=z#
endfunction
function setParticleGravity(num,x#,y#,z#)
prtsys(num).grav.x=x#
prtsys(num).grav.y=y#
prtsys(num).grav.z=z#
endfunction
function colorParticles(num,r,g,b)
col=rgb(r,g,b)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
color object obj,col
next c
endfunction
function setParticleGhost(num,flag)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
if flag=1
ghost object on obj
else
ghost object off obj
endif
next c
endfunction
function controlParticles()
for num=1 to array count(prtsys())
for c=1 to prtsys(num).count
prtsys(num).lifec=prtsys(num).lifec+1
prtsys(num).lifecount=prtsys(num).life-prtsys(num).lifec
obj=prtdat(num,c).obj
angx#=angx#+((360/prtsys(num).count)*c)
angy#=angy#+((360/prtsys(num).count)*c)
sx#=object position x(obj)+(sin(angy#)*cos(angx#)*prtsys(num).speed)
sy#=object position y(obj)-(sin(angx#)*prtsys(num).speed)
sz#=object position z(obj)+(cos(angy#)*cos(angx#)*prtsys(num).speed)
sx#=sx#+prtsys(num).grav.x
sy#=sy#+prtsys(num).grav.y
sz#=sz#+prtsys(num).grav.z
size#=prtsys(num).sizei+(prtsys(num).sizep*prtsys(num).lifecount)
position object obj,sx#,sy#,sz#
scale object obj,size#,size#,1
if prtsys(num).lifec>prtsys(num).life
prtsys(num).lifec=prtsys(num).lifec*0
size#=prtsys(num).sizei
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
endif
next c
next num
endfunction
function getEmitterCount()
count=array count(prtsys())
endfunction count
function getParticleCount(num)
count=prtsys(num).count
endfunction count
`Functions
function zp_freeobject()
repeat
inc i
until object exist(i)=0
endfunction i
function getTokens(st$,sp$)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until counter>len(nst$)
tokens=splits-1
endfunction tokens
function getToken(st$,sp$,num)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until splits=num or counter>len(st$)
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$<>sp$ then token$=token$+cur$
until cur$=sp$ or counter>len(st$)
endfunction token$
`UDTs
type vec3
x as float
y as float
z as float
endtype
type tprtsys
objc as string
pos as vec3
life as integer
lifec as integer
lifecount as integer
lifep as string
count as integer
speed as float
sizei as float
sizep as float
grav as vec3
endtype
type tprtdat
obj as integer
endtype

|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
|
Posted: 2nd Jan 2007 11:56 Edited: 2nd Jan 2007 12:09 |
| link | toggle |
Great start!
Quote: "Is this a valid contribution?"
It certainly is. This is intended to be a learning process.
Quote: "Good concept Rii - I'll give it my support as and when time allows."
Thanks Ric! Inspired by the most famous community thread around.
@Indi: Thanks for the splash screen. We'll try get that in sometime soon. I think the size is perfect for the game.
@Peter H: Thanks for the detailed explanation on UDT's and updating the code. Also, good idea for some vertical movement.
@Lukas: Nice ship rotation! While the game is a 2D perspective, you just made great use of the 3D objects being used.

Open MMORPG: It's your game!
|
Back to top
 |
|
Zotoaster
User
Joined: Mon Dec 20th 2004
Location: Scotland
|
Oi! You forgot me!
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
|
Posted: 2nd Jan 2007 12:09 Edited: 2nd Jan 2007 12:14 |
| link | toggle |
No - didn't forget  . I was just posting while you were. Great particles effects! Here's with missiles added (press space to shoot).
Edit: Oh, and it seems the java uploader won't upload the zip files for some reason (they might be blocked for some reason).
Edit 2: Oops. Not following my own "rules". No media update, so deleting the attachment and posting the code.
+ Code Snippet`Standard Setup
Sync On: Sync Rate 60:Autocam Off
`The camera starts of at 0,0,0. Move it back and up a bit so the action can take center on 0,0,0
Position Camera 0,50,-100
`There will be 6 rows of 9 invaders to start with.
`The player's ship will be at the bottom, like so.
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` /\
`Create an array to store the invader information. This is not needed now, but will be needed later.
`This is a 2-dimensional array. It is 6 by 9 in size (or 6x9=54). 5,8 was used instead of 53 to make
`it easier to determine rows and columns. It is normally better to use a single array of 53.
Dim Invaders(5,8)
`Load the aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
`Get a free object ID for our invader
obj=FreeObject()
`Store the Object ID in the Invaders array
Invaders(r,i)=obj
`Load the alien model
Load Object "Data\Alien1.x",obj
`Next, we need to make sure our model is the right size. For this, the model needs to be scaled.
`Roughly, we want a model that is 6x6x6 (just what is picked and looks about right).
`To scale the model, we first need to know it's size ratio to a size of 6.
`Here's how to get that:
s#=6.0/Object Size X(obj)
`All that is left to do is scale the model. Remember that Scale Object uses an integer scale that represents
`percentage values (100 = 100% = 1, 50 = 50% = .5, 1 = 1% = .01, and so on). So, the scale (s#) needs to be
`multiplied by 100.
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
`Each invader is 6x6x6 in size. Our viewable area is a little less than 100 x 100
`In order to fit the invaders evenly, we can divide the area we have to work with by
`the number of colums - 9. Since 9 goes into 90 evenly, lets use that. The result is 10.
`Here is the formula to calculate the X-position of the invaders every 10 spaces from
`-45x to +45x
x=(i*10)-45
`Using the same spacing of 10 for the Y-position, and the invaders need to be well up off
`the ground, here is the formula to calculate the Y-position.
y=(r*10)+50
`Position the invader
Position Object obj,x,y,0
Next i
Next r
`Load the shield barriers (same process as above).
Dim Barriers(2)
For i = 0 to 2
obj=FreeObject()
Barriers(i)=obj
Load Object "Data\Barrier.x",Barriers(i)
s#=12.0/Object Size X(obj)
Scale Object Barriers(i),s#*100.0,s#*100.0,s#*100.0
x#=(i*35)-35
Position Object Barriers(i),x#,20,0
Next i
`Load player's missile
`Use an array for the variable since it is global (A global variable could be used, but later you will see why
`there is a preference for Arrays over Global Variables).
Dim Missile(0)
`Assign a free object to Missile
Missile(0)=FreeObject()
`Load the missile model
Load Object "Data\Missile.x",Missile(0)
`Calculate the scale
s#=6.0/Object Size Y(Missile(0))
`Scale the missile.
Scale Object Missile(0),s#*100.0,s#*100.0,s#*100.0
`Hide the missile
Hide Object Missile(0)
`Create a playertype "user defined array"
type PlayerType
`this will store the id of the object that is associated with the player, so we can move it later
ObjId as integer
`to keep track of the x position of the player, we are creating these as floats in order
`to maintain a smooth movement. With integers the player would "jump" from one integer to another
`adding the "#" to the end defines it as a float, to use this variable later you must use the "#"
xpos#
`we also need to keep track of the y position of the player, the player's "up and down"
`movement will be limited to keep them behind the bariers
ypos#
`this stores the distance that the player can move in one frame
speed#
` with the following variable stored, we can apply a neat rotation effect to the ship while
` it is moving left or right
yang#
endtype
REMSTART
Using "User Defined Types"
The code
type TheBestTypeEver
endtype
Creates a User Defined Type (reffered to as a UDT) with the name "TheBestTypeEver"
This by itself is a empty type, the entire point of using UDTs is to simplify code
by allowing sub variables for a "Parent" variable
to add sub variables to a UDT you simply type them out in between the 'type' and 'endtype'
type TheBestTypeEver
TheBestVariableEver as integer
TheOtherVariable as string
endtype
This adds two sub variables to the UDT, a integer (only "whole" numbers such as 1, 2, 3, 4, -17, 42,.. etc)
named "TheBestVariableEver", and a string named "TheOtherVariable"
Note: if you declare a variable without telling the compiler what type it is, it will
automatically be declared as an integer. So we could have left off the "as integer"
A UDT by itself does nothing at all, you can't assign something to it (such as TheBestTypeEver = 1337)
You have to declare a variable and define it as the type that your UDT is
A UDT is really just a fancy type of variable that you've created custom made for your game
It's used in exactly the same way that you would when you are declaring a variable as a string, float, or integer
bob as integer
bob as TheBestTypeEver
however, UDTs are different than normal variable types because they have sub variables
with "bob as int" you assign a value to it in the normal way, bob = 12
When you have a variable declared as a UDT you have to pick a sub variable to work with
It's easy to tell the compiler which sub variable you want, you just add a period (".")
to the end, then add the name of the sub variable.
so, going with our example, bob as TheBestTypeEver
bob.TheBestVariableEver = 20
bob.TheOtherVariable = "hello world"
That sets the two sub variables in bob to 20, and "hello world" (terribly original i know)
you can then access them just like normal variables
print bob.TheBestVariableEver
print bob.TheOtherVariable
Note: You CANNOT assign anything to the "parent" variable by itself. So bob = 20 would NOT work.
you need to pick a sub variable to assign to.
REMEND
`Create array Player() so it is global (A global variable can be used, but we will cover later why
`there is a preference for arrays over global variables).
`This array is created as a PlayerType array, and has all the attributes of the PlayerType User Defined Type
Dim Player(0) as PlayerType
`Get a free object for the player's ship
Player(0).ObjId = FreeObject()
`Load in the player's ship
Load Object "Data\Ship.x",Player(0).Objid
`Calculate the ratio of the ship to size 6 (making the player a little larger than the aliens)
s#=6.0/Object Size X(Player(0).ObjId)
`Scale the player's ship
Scale Object Player(0).ObjId,s#*100.0,s#*100.0,s#*100.0
`Place the player's ship
Position Object Player(0).ObjId,0,0,0
`-------------------------
`Player_Controls variables
`-------------------------
`set the player's speed
Player(0).speed# = 0.4
`sets the farthest position the player can travel to the right
rightside# = 77.0
`sets the farthest position the player can travel to the left
leftside# = -77.0
`sets the farthest position the player can travel upwards
topside# = 12.0
`sets the farthest position the player can travel downwards
bottomside# = -9.0
`-------------------------
`End Of Player_Controls variables
`-------------------------
`Particle Data
`Use this function to set up the particle data, otherwise they wont work.
setupParticles()
`Create a particle emmiter
prtShip=addParticles(0,0,0,60,60)
setParticleSize(prtShip,1.8,-0.03)
colorParticles(prtShip,255,0,0)
setParticleGhost(prtShip,1)
setParticleGravity(prtShip,0,-0.1,0)
`Call this function to actually make all the particles
createParticles()
`Main loop
Do
gosub player_controls
Control_Missile()
Set Cursor 0,0
Print Screen FPS()
Sync
Loop
End
`For more information on For/Next/Loops, refer to DBPro Help: Principles / Common Statements
`For more information on For/Next/Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`For more information on Formulas, refer to DBPro Help: Principles / Datatypes and variables
`A quick little function to check for the first free object number.
Function FreeObject()
`Start at object number 1
i=1
`A simple loop
Do
`If the object does not exist (=0), then exit the loop. "i" will be the valid object number
If Object Exist(i)=0 Then Exit
`Move on to the next object number
i=i+1
`Close the loop
Loop
`End the function and return the valid object number
Endfunction i
`For more information on Functions and returning values, refer to DBPro Help (press F1): Principles / Functions
`For more information on Loops, refer to DBPro Help: Principles / Common Statements
`For more information on Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`handles all the input from the player
player_controls:
` while not left or right key is pressed, we rotate the ship back to its original angle
Player(0).yang# = curvevalue( 0.0, object angle y( Player(0).ObjId ), 50.0 )
`check if the player is hitting the right key, if they are we want to let them move right
`as long as they are not hitting the right side of the screen
if rightkey() = 1
`move the player to the right at the preset speed
inc Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the right side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# > rightside# then Player(0).xpos# = rightside#
` apply rotation to the ship while we move right
Player(0).yang# = curvevalue( -40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check in the same way for the left key
if leftkey() = 1
`move the player to the left at the preset speed
dec Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the left side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# < leftside# then Player(0).xpos# = leftside#
` apply rotation to the ship while we move left
Player(0).yang# = curvevalue( 40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check if the player is hitting the upkey, if they are we want to let them move up
`but not past the barriers
if upkey() = 1
`move the player upwards at the preset speed
inc Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the barriers
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# > topside# then Player(0).ypos# = topside#
endif
`check if the player is hitting the downkey, if they are we want to let them move down
`but not past the bottom of the screen
if downkey() = 1
`move the player downwards at the preset speed
dec Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the bottom of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# < bottomside# then Player(0).ypos# = bottomside#
endif
`now that we've adjusted the position of the player accordingly, we need to update the object
`that is representing the player
position object Player(0).ObjId, Player(0).xpos#, Player(0).ypos#, 0
rotate object Player(0).ObjId, 0, Player(0).yang#, 0
`Update Particles
positionParticles(prtShip,Player(0).xpos#+plusmin(rnd(1.0)), Player(0).ypos#, 0)
controlParticles()
return
`Particle Data
function plusmin(num#)
rand=rnd(10)
if rand>5 then ret#=num#*-1 else ret#=num#
endfunction ret#
function setupParticles()
dim prtsys(0) as tprtsys
endfunction
function createParticles()
count=array count(prtsys())
cmax=0
for c=1 to count
if prtsys(c).count>cmax then cmax=prtsys(c).count
next c
dim prtdat(array count(prtsys()),cmax) as tprtdat
for c=1 to array count(prtsys())
for o=1 to prtsys(c).count
prtdat(c,o).obj=val(getToken(prtsys(c).objc,",",o))
next o
next c
endfunction count
function addParticles(x#,y#,z#,life,count)
array insert at bottom prtsys()
num=array count(prtsys())
prtsys(num).pos.x=x#+plusmin(rnd(0.2))
prtsys(num).pos.y=y#+plusmin(rnd(0.2))
prtsys(num).pos.z=z#+plusmin(rnd(0.2))
prtsys(num).life=life
prtsys(num).count=count
for c=1 to prtsys().count
obj=zp_freeobject() : make object plain obj,100,100
set object transparency obj,3
disable object zwrite obj
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
obj$=obj$+str$(obj)+","
lifep=rnd(life)
life$=life$+str$(lifep)+","
next c
prtsys(num).objc=obj$
prtsys(num).lifep=life$
endfunction num
function setParticleSpeed(num,speed#)
prtsys(num).speed=speed#
endfunction
function setParticleSize(num,size#,sizep#)
prtsys(num).sizei=size#
prtsys(num).sizep=sizep#
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
scale object obj,size#,size#,1
next c
endfunction
function positionParticles(num,x#,y#,z#)
prtsys(num).pos.x=x#
prtsys(num).pos.y=y#
prtsys(num).pos.z=z#
endfunction
function setParticleGravity(num,x#,y#,z#)
prtsys(num).grav.x=x#
prtsys(num).grav.y=y#
prtsys(num).grav.z=z#
endfunction
function colorParticles(num,r,g,b)
col=rgb(r,g,b)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
color object obj,col
next c
endfunction
function setParticleGhost(num,flag)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
if flag=1
ghost object on obj
else
ghost object off obj
endif
next c
endfunction
function controlParticles()
for num=1 to array count(prtsys())
for c=1 to prtsys(num).count
prtsys(num).lifec=prtsys(num).lifec+1
prtsys(num).lifecount=prtsys(num).life-prtsys(num).lifec
obj=prtdat(num,c).obj
angx#=angx#+((360/prtsys(num).count)*c)
angy#=angy#+((360/prtsys(num).count)*c)
sx#=object position x(obj)+(sin(angy#)*cos(angx#)*prtsys(num).speed)
sy#=object position y(obj)-(sin(angx#)*prtsys(num).speed)
sz#=object position z(obj)+(cos(angy#)*cos(angx#)*prtsys(num).speed)
sx#=sx#+prtsys(num).grav.x
sy#=sy#+prtsys(num).grav.y
sz#=sz#+prtsys(num).grav.z
size#=prtsys(num).sizei+(prtsys(num).sizep*prtsys(num).lifecount)
position object obj,sx#,sy#,sz#
scale object obj,size#,size#,1
if prtsys(num).lifec>prtsys(num).life
prtsys(num).lifec=prtsys(num).lifec*0
size#=prtsys(num).sizei
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
endif
next c
next num
endfunction
function getEmitterCount()
count=array count(prtsys())
endfunction count
function getParticleCount(num)
count=prtsys(num).count
endfunction count
`Functions
function zp_freeobject()
repeat
inc i
until object exist(i)=0
endfunction i
function getTokens(st$,sp$)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until counter>len(nst$)
tokens=splits-1
endfunction tokens
function getToken(st$,sp$,num)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until splits=num or counter>len(st$)
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$<>sp$ then token$=token$+cur$
until cur$=sp$ or counter>len(st$)
endfunction token$
`Player Missile Control
Function Control_Missile()
`Check to see of the Missile is hidden. If the missile is hidden, then it has not been launched.
If Object Visible(Missile(0))=0
`The missile has not been launched; check to see if the player is pressing the space bar.
If SpaceKey()
`The spacekey is pressed, so launch the missile
`Start by showing the missile
Show Object Missile(0)
`Next, position the missile at the ship's location
Position Object Missile(0),Object Position X(Player(0).ObjID),Object Position Y(Player(0).ObjID), Object Position Z(Player(0).ObjID)
`Make sure the missile is pointing in the correct direction - up.
Rotate Object Missile(0),0,0,0
Else
`The missile has not been launched and the player is not pressing the space key - so nothing left to do.
Exitfunction
Endif
Endif
`If the code gets to these lines in this function, the missile has been launched. So, let's move it.
Move Object Up Missile(0),3
`Next, check to see if the missile has gone off the screen.
If Object Screen Y(Missile(0))<0
`The missile has gone off the screen, so let's hide it again. Remember, hiding it will stop it and have
`it ready for re-launch.
Hide Object Missile(0)
Endif
Endfunction
`UDTs
type vec3
x as float
y as float
z as float
endtype
type tprtsys
objc as string
pos as vec3
life as integer
lifec as integer
lifecount as integer
lifep as string
count as integer
speed as float
sizei as float
sizep as float
grav as vec3
endtype
type tprtdat
obj as integer
endtype

Open MMORPG: It's your game!
|
Back to top
 |
|
RUCCUS
User
Joined: Sat Dec 11th 2004
Location: Canada
|
|
Posted: 2nd Jan 2007 14:50 Edited: 4th Jan 2007 08:34 |
| link | toggle |
Ill work on some media if you want,
threw these together, theyre a single mesh, not a bunch of boxes. Theyre also to scale
<removed>
Right now theyre pretty flat, but if you guys want I could give them a more 3D look, up to you.
Im going out now, Ill finish the models tonight or tomorrow morning and post em', shouldnt be long, those took me about 5 mins each.
<EDIT>
Also, indi, I was thinking, that splash screen looks great but maybe you could try making the title text in the same neon greenish colour as the planet? I think itd look better as a colour contrast, but either way it looks great as is too.
- RUC'
|
Back to top
 |
|
indi
User
Joined: Mon Aug 26th 2002
Location: Earth, Brisbane, Australia
|
alright i will make another version to evaluate this arvo.
|
Back to top
 |
|
RUCCUS
User
Joined: Sat Dec 11th 2004
Location: Canada
|
|
Posted: 2nd Jan 2007 20:05 Edited: 4th Jan 2007 08:34 |
| link | toggle |
Made progress on the models, Ill texture em soon. Ill also write a few functions to handle cycling through the objects to create animations. I might write the section of the game that handles moving the enemies as well, if people are ok with it, but no promises as of now.
<removed>
|
Back to top
 |
|
indi
User
Joined: Mon Aug 26th 2002
Location: Earth, Brisbane, Australia
|
thats perfect mate.
a series of primitive cubes stretched a little fat in the z dimension,
and built up to make the barricades would be an easy method for a collapsing barricade element.
As if the older 2d pixels are now distorted cubes, naturally a smaller amount of 3d objects, but still in the same old shape of the barriers, and yet still stay in tune with the overall look of pixels vs now using 3d distorted cubes being wiped out by alien fire. Alternatively it also allows for the player to make a hole and blast through it
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
That would work great! Let's get it going. I'll add on the splash screen. We can change it later if decided, though I am ready to go with that first splash screen (then again, I'm no aritst so color schemes elude me).

Open MMORPG: It's your game!
|
Back to top
 |
|
Lukas W
User
Joined: Fri Sep 5th 2003
Location: Norway
|
Those aliens are looking great ruccus!
it would be neat if they were coded in dbpro, that way we could detach the cubes and let it explode like some special effect.
|
Back to top
 |
|
RUCCUS
User
Joined: Sat Dec 11th 2004
Location: Canada
|
Ive got an idea Ill work up tonight. As opposed to actually modelling each of the characters, Im going to write up a character creator. Itll basically be 4 32x32 grids that you draw your character in, and wherever you place a pixel, a box is positioned. At the end it'll generate the code to create the monster by saving all of the data to a file and then can later be loaded in using the load array command. Im just stretching on indi's idea, I was thinking this way if even the enemies are built up of boxes, then as you shoot the enemies, their pixels or boxes could crumble as well. Im just thinking though, would that many boxes be a big fps drainer? If you figure you've got about i dunno, 60 enemies, and each enemy is made up of maybe 30 boxes.... thats a lot of boxes.
Ill still make the program but Im starting to doubt its practicality, itd probably be easier to just use particles.
- RUC'
|
Back to top
 |
|
BatVink
TGC Newsletter Editor
 Joined: Fri Apr 4th 2003
Location: Chilling
|
|
Posted: 3rd Jan 2007 11:50 Edited: 3rd Jan 2007 11:53 |
| link | toggle |
This is all looking very interesting. It could however, fall apart if there's no glue on the seams.
Can I suggest that for the next challenge, the owner creates a structure which people can volunteer to take a piece of. They in turn could further subdivide their bit, allowing others to jump in and contribute to one area, or take bits from different areas. You also need a system that facilitates such a development cycle, such as a State Machine design.
It could really do with a project management mini-site that allows allocation of jobs. It may sound a little overboard, but the results could be far more elaborate and complete.
For example, take Breakthrough. As the owner, I want to split it up as follows:
 Barebones coder - creating the game framework, in the style of a State Machine.
 Menu system, allowing game to be played, hi-score access and exit.
 Graphics - set of different coloured blocks to which I can apply different attributes. Backgrounds for game & menu, Paddle(s) and ball(s)
 Level designer - someone to create the levels (perhaps with a new tool they write), and a function for building the levels in-game
 Ball & Paddle control - function to control paddle movement and ball deflection.
 Integration manager - defining the interfaces between the different aspects (probably the project owner).
 Effects creation - explosions, sounds etc.
|
Back to top
 |
|
|
Google Ad
AdBot
Joined: Aug 26th 2002
Location: Everywhere
|
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
Sounds like a good plan there BatVink. If interested, please pick a section of code to work on from the ToDo list (x's are done):
X Add Player Ship Movement
X Add Missile firing from Player Ship
0 Add Alien Movement
0 Add Alien Missile Fire
0 Add Missile Collision; Player & Aliens
0 Add Missile Collision; Barriers
0 Add Scoring
0 Add Player Death and Lives
0 Add Game Over sequence
0 Add Bonuses (flying saucers)
0 Add Game Start and Options
0 Animate Aliens (and any other models)
0 Clean up and Debug
0 Optional: Add online high score tracker

Open MMORPG: It's your game!
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
I have added a Game Start with Indi's splash screen and a start button.
From the ToDo list, I will take the Alien Movement. If someone can add in some alien return fire, we will be pretty close to a game.

Open MMORPG: It's your game!
|
Back to top
 |
|
Lukas W
User
Joined: Fri Sep 5th 2003
Location: Norway
|
nice going RiiDii, that button is perfect for indi's background image.
Quote: "If someone can add in some alien return fire,"
i thought i'd try this so i attached the code to this post. (no need to upload a zip, when i didn't change any media)
i used the timer() + a random value of 20 seconds to decide when the alien would fire a bullet.
the problem is some aliens fire at the same time, causing two bullets to end up on top of each other etc.
if somebody could have a look at that, that would be great!
+ Code Snippet`Standard Setup
Sync On: Sync Rate 60:Autocam Off
`The camera starts of at 0,0,0. Move it back and up a bit so the action can take center on 0,0,0
Position Camera 0,50,-100
Load Image "data\space_invaders_splashscreen.png",1,1
Sprite 1,0,0,1
Size Sprite 1,Screen Width(),Screen Height()
Hide Sprite 1
Sprite 1,-800,-600,1
`Initialize the attached buttons functionality
Initialize_Buttons
`Make_Button(File as String,xpos as float, ypos as float,RVal as Integer,AnimFlag)
Make_Button("data\Start.png",320-32,200-16,1,1)
Do
Show Sprite 1
Paste Sprite 1,0,0
Hide Sprite 1
`Check to see if the button is "active" (mouse pointer is over sprite) and for left click
If Get_Button()=1 And MouseClick()=1 Then Exit
Sync
Loop
`Clean up the buttons.
Clear_Buttons
`There will be 6 rows of 9 invaders to start with.
`The player's ship will be at the bottom, like so.
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` /\
`Create an array to store the invader information. This is not needed now, but will be needed later.
`This is a 2-dimensional array. It is 6 by 9 in size (or 6x9=54). 5,8 was used instead of 53 to make
`it easier to determine rows and columns. It is normally better to use a single array of 53.
Dim Invaders(5,8)
`Load the aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
`Get a free object ID for our invader
obj=FreeObject()
`Store the Object ID in the Invaders array
Invaders(r,i)=obj
`Load the alien model
Load Object "Data\Alien1.x",obj
`Next, we need to make sure our model is the right size. For this, the model needs to be scaled.
`Roughly, we want a model that is 6x6x6 (just what is picked and looks about right).
`To scale the model, we first need to know it's size ratio to a size of 6.
`Here's how to get that:
s#=6.0/Object Size X(obj)
`All that is left to do is scale the model. Remember that Scale Object uses an integer scale that represents
`percentage values (100 = 100% = 1, 50 = 50% = .5, 1 = 1% = .01, and so on). So, the scale (s#) needs to be
`multiplied by 100.
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
`Each invader is 6x6x6 in size. Our viewable area is a little less than 100 x 100
`In order to fit the invaders evenly, we can divide the area we have to work with by
`the number of colums - 9. Since 9 goes into 90 evenly, lets use that. The result is 10.
`Here is the formula to calculate the X-position of the invaders every 10 spaces from
`-45x to +45x
x=(i*10)-45
`Using the same spacing of 10 for the Y-position, and the invaders need to be well up off
`the ground, here is the formula to calculate the Y-position.
y=(r*10)+50
`Position the invader
Position Object obj,x,y,0
Next i
Next r
` this variable will be used to store the objid of the first alien object
Global AlienObjIdStart
AlienObjIdStart = obj - 53
` *************************************************************************
` Prepare Enemy Bullets
` - last changed: 04.Jan.2007
` create a UDT for the Enemy Bullets
` (a description of UDT's and what they are, can be found further down.)
Type tEnemyBullet
` create a variable to store the bullets Object ID
ObjId AS INTEGER
` create a variable to store the alien Object ID
AlienId AS INTEGER
` create timer variables :: that way we can control how often
` the enemies are to fire a bullet.
TimeControl AS BOOLEAN ` here we will store a 1 for reset StartSecond or 0 for Start Counting Seconds
StartSecond AS INTEGER ` here we will store the starting position in time
CurSecond AS INTEGER ` here we will store the current position in time
FireHere AS INTEGER ` here we will store on which second the Alien should fire his bullet
` create a Active variable :: that way we can see if the bullet
` is destroyed. if it's destroyed, it means the alien
` connected to this bullet will be able to fire a new one.
Active AS INTEGER
` create position and rotation angle variables for the bullet
` this way we can easily control the movement and angle of the bullet
` --it would also be possible to make some sinus/cosinus effects
` if we want to make Easy - Hard difficulties. that would be neat.
xPos AS FLOAT
yPos AS FLOAT
zPos AS FLOAT
xAng AS FLOAT
yAng AS FLOAT
zAng AS FLOAT
EndType
` create the array where we store the Enemy Bullet information
` remember we have 6*9 aliens, yea? that means 54 enemies
Dim EnemyBullet(53) AS tEnemyBullet
` now create all the bullets
For bullet = 1 to 54
` we set ID to bullet-1 because our Array "EnemyBullet" is counting
` from 0-53, and not 1-54
arrayPosition = bullet -1
` get the object nr for this bullet
obj = FreeObject()
` make a box object for the bullet
Make Object Box obj, 2, 6, 2
Hide Object obj
` update the EnemyBullet array
alien = AlienObjIdStart + arrayPosition
EnemyBullet( arrayPosition ).ObjId = obj ` the object nr
EnemyBullet( arrayPosition ).AlienId = alien ` the alien object nr
Next bullet
` --------------------------------------- end of Prepare Enemy Bullets
` *************************************************************************
`Load the shield barriers (same process as above).
Dim Barriers(2)
For i = 0 to 2
obj=FreeObject()
Barriers(i)=obj
Load Object "Data\Barrier.x",Barriers(i)
s#=12.0/Object Size X(obj)
Scale Object Barriers(i),s#*100.0,s#*100.0,s#*100.0
x#=(i*35)-35
Position Object Barriers(i),x#,20,0
Next i
`Load player's missile
`Use an array for the variable since it is global (A global variable could be used, but later you will see why
`there is a preference for Arrays over Global Variables).
Dim Missile(0)
`Assign a free object to Missile
Missile(0)=FreeObject()
`Load the missile model
Load Object "Data\Missile.x",Missile(0)
`Calculate the scale
s#=6.0/Object Size Y(Missile(0))
`Scale the missile.
Scale Object Missile(0),s#*100.0,s#*100.0,s#*100.0
`Hide the missile
Hide Object Missile(0)
`Create a playertype "user defined array"
type PlayerType
`this will store the id of the object that is associated with the player, so we can move it later
ObjId as integer
`to keep track of the x position of the player, we are creating these as floats in order
`to maintain a smooth movement. With integers the player would "jump" from one integer to another
`adding the "#" to the end defines it as a float, to use this variable later you must use the "#"
xpos#
`we also need to keep track of the y position of the player, the player's "up and down"
`movement will be limited to keep them behind the bariers
ypos#
`this stores the distance that the player can move in one frame
speed#
` with the following variable stored, we can apply a neat rotation effect to the ship while
` it is moving left or right
yang#
endtype
REMSTART
Using "User Defined Types"
The code
type TheBestTypeEver
endtype
Creates a User Defined Type (reffered to as a UDT) with the name "TheBestTypeEver"
This by itself is a empty type, the entire point of using UDTs is to simplify code
by allowing sub variables for a "Parent" variable
to add sub variables to a UDT you simply type them out in between the 'type' and 'endtype'
type TheBestTypeEver
TheBestVariableEver as integer
TheOtherVariable as string
endtype
This adds two sub variables to the UDT, a integer (only "whole" numbers such as 1, 2, 3, 4, -17, 42,.. etc)
named "TheBestVariableEver", and a string named "TheOtherVariable"
Note: if you declare a variable without telling the compiler what type it is, it will
automatically be declared as an integer. So we could have left off the "as integer"
A UDT by itself does nothing at all, you can't assign something to it (such as TheBestTypeEver = 1337)
You have to declare a variable and define it as the type that your UDT is
A UDT is really just a fancy type of variable that you've created custom made for your game
It's used in exactly the same way that you would when you are declaring a variable as a string, float, or integer
bob as integer
bob as TheBestTypeEver
however, UDTs are different than normal variable types because they have sub variables
with "bob as int" you assign a value to it in the normal way, bob = 12
When you have a variable declared as a UDT you have to pick a sub variable to work with
It's easy to tell the compiler which sub variable you want, you just add a period (".")
to the end, then add the name of the sub variable.
so, going with our example, bob as TheBestTypeEver
bob.TheBestVariableEver = 20
bob.TheOtherVariable = "hello world"
That sets the two sub variables in bob to 20, and "hello world" (terribly original i know)
you can then access them just like normal variables
print bob.TheBestVariableEver
print bob.TheOtherVariable
Note: You CANNOT assign anything to the "parent" variable by itself. So bob = 20 would NOT work.
you need to pick a sub variable to assign to.
REMEND
`Create array Player() so it is global (A global variable can be used, but we will cover later why
`there is a preference for arrays over global variables).
`This array is created as a PlayerType array, and has all the attributes of the PlayerType User Defined Type
Dim Player(0) as PlayerType
`Get a free object for the player's ship
Player(0).ObjId = FreeObject()
`Load in the player's ship
Load Object "Data\Ship.x",Player(0).Objid
`Calculate the ratio of the ship to size 6 (making the player a little larger than the aliens)
s#=6.0/Object Size X(Player(0).ObjId)
`Scale the player's ship
Scale Object Player(0).ObjId,s#*100.0,s#*100.0,s#*100.0
`Place the player's ship
Position Object Player(0).ObjId,0,0,0
`-------------------------
`Player_Controls variables
`-------------------------
`set the player's speed
Player(0).speed# = 0.4
`sets the farthest position the player can travel to the right
rightside# = 77.0
`sets the farthest position the player can travel to the left
leftside# = -77.0
`sets the farthest position the player can travel upwards
topside# = 12.0
`sets the farthest position the player can travel downwards
bottomside# = -9.0
`-------------------------
`End Of Player_Controls variables
`-------------------------
`Particle Data
`Use this function to set up the particle data, otherwise they wont work.
setupParticles()
`Create a particle emmiter
prtShip=addParticles(0,0,0,60,60)
setParticleSize(prtShip,1.8,-0.03)
colorParticles(prtShip,255,0,0)
setParticleGhost(prtShip,1)
setParticleGravity(prtShip,0,-0.1,0)
`Call this function to actually make all the particles
createParticles()
`Main loop
Do
gosub player_controls
GoSub alien_fire_control
Control_Missile()
Set Cursor 0,0
Print Screen FPS()
Sync
Loop
End
`For more information on For/Next/Loops, refer to DBPro Help: Principles / Common Statements
`For more information on For/Next/Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`For more information on Formulas, refer to DBPro Help: Principles / Datatypes and variables
`A quick little function to check for the first free object number.
Function FreeObject()
`Start at object number 1
i=1
`A simple loop
Do
`If the object does not exist (=0), then exit the loop. "i" will be the valid object number
If Object Exist(i)=0 Then Exit
`Move on to the next object number
i=i+1
`Close the loop
Loop
`End the function and return the valid object number
Endfunction i
`For more information on Functions and returning values, refer to DBPro Help (press F1): Principles / Functions
`For more information on Loops, refer to DBPro Help: Principles / Common Statements
`For more information on Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`handles all the input from the player
player_controls:
` while not left or right key is pressed, we rotate the ship back to its original angle
Player(0).yang# = curvevalue( 0.0, object angle y( Player(0).ObjId ), 50.0 )
`check if the player is hitting the right key, if they are we want to let them move right
`as long as they are not hitting the right side of the screen
if rightkey() = 1
`move the player to the right at the preset speed
inc Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the right side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# > rightside# then Player(0).xpos# = rightside#
` apply rotation to the ship while we move right
Player(0).yang# = curvevalue( -40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check in the same way for the left key
if leftkey() = 1
`move the player to the left at the preset speed
dec Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the left side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# < leftside# then Player(0).xpos# = leftside#
` apply rotation to the ship while we move left
Player(0).yang# = curvevalue( 40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check if the player is hitting the upkey, if they are we want to let them move up
`but not past the barriers
if upkey() = 1
`move the player upwards at the preset speed
inc Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the barriers
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# > topside# then Player(0).ypos# = topside#
endif
`check if the player is hitting the downkey, if they are we want to let them move down
`but not past the bottom of the screen
if downkey() = 1
`move the player downwards at the preset speed
dec Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the bottom of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# < bottomside# then Player(0).ypos# = bottomside#
endif
`now that we've adjusted the position of the player accordingly, we need to update the object
`that is representing the player
position object Player(0).ObjId, Player(0).xpos#, Player(0).ypos#, 0
rotate object Player(0).ObjId, 0, Player(0).yang#, 0
`Update Particles
positionParticles(prtShip,Player(0).xpos#+plusmin(rnd(1.0)), Player(0).ypos#, 0)
controlParticles()
return
alien_fire_control:
` start a for-next loop :: go through each of the bullets
For arrayPos = 0 To 53
obj = EnemyBullet( arrayPos ).ObjId ` get object nr. for this bullet
alien = EnemyBullet( arrayPos ).AlienId ` get object nr. of the alien
` If TimeControl = 0 And the bullet is inactive..
If EnemyBullet( arrayPos ).TimeControl = 0 And EnemyBullet( arrayPos ).Active = 0
EnemyBullet( arrayPos ).StartSecond = Timer() ` reset StartSecond
EnemyBullet( arrayPos ).FireHere = 5 + rnd(20) ` set FireHere to 3 seconds + a random of 10 seconds
EnemyBullet( arrayPos ).TimeControl = 1 ` set TimeControl to 1
EndIf
` If TimeControl = 1..
If EnemyBullet( arrayPos ).TimeControl = 1
` increase .CurSecond by 1 each secound
EnemyBullet( arrayPos ).CurSecond = (Timer() - EnemyBullet( arrayPos ).StartSecond) / 1000
` if .CurSecond > FireHere, that means the alien should fire his bullet
If EnemyBullet( arrayPos ).CurSecond > EnemyBullet( arrayPos ).FireHere
EnemyBullet( arrayPos ).Active = 1
EnemyBullet( arrayPos ).TimeControl = 0
EndIf
EndIf
` if the bullet is active.. [stage 1] - update
If EnemyBullet( arrayPos ).Active = 1
Show Object obj
` update bullet position
EnemyBullet( arrayPos ).xPos = Object Position X( alien )
EnemyBullet( arrayPos ).yPos = Object Position Y( alien )
EnemyBullet( arrayPos ).zPos = Object Position Z( alien )
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
` go to stage 2 :: move the bullet
EnemyBullet( arrayPos ).Active = 2
EndIf
` if the bullet is active.. [stage 2] - move
If EnemyBullet( arrayPos ).Active = 2
` apply rotation to the bullet
EnemyBullet( arrayPos ).xAng = 0
EnemyBullet( arrayPos ).yAng = 0
EnemyBullet( arrayPos ).zAng = 180
` update position of the bullet
EnemyBullet( arrayPos ).xPos = EnemyBullet( arrayPos ).xPos + sin( EnemyBullet( arrayPos ).zAng ) * 1.3
EnemyBullet( arrayPos ).yPos = EnemyBullet( arrayPos ).yPos + cos( EnemyBullet( arrayPos ).zAng ) * 1.3
` check if the bullet is outside the screen
If EnemyBullet( arrayPos ).yPos < bottomside#
Hide Object obj
EnemyBullet( arrayPos ).Active = 0
EndIf
` update object
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
Rotate Object obj, EnemyBullet( arrayPos ).xAng, EnemyBullet( arrayPos ).yAng, EnemyBullet( arrayPos ).zAng
EndIf
Next arrayPos
return
`Particle Data
function plusmin(num#)
rand=rnd(10)
if rand>5 then ret#=num#*-1 else ret#=num#
endfunction ret#
function setupParticles()
dim prtsys(0) as tprtsys
endfunction
function createParticles()
count=array count(prtsys())
cmax=0
for c=1 to count
if prtsys(c).count>cmax then cmax=prtsys(c).count
next c
dim prtdat(array count(prtsys()),cmax) as tprtdat
for c=1 to array count(prtsys())
for o=1 to prtsys(c).count
prtdat(c,o).obj=val(getToken(prtsys(c).objc,",",o))
next o
next c
endfunction count
function addParticles(x#,y#,z#,life,count)
array insert at bottom prtsys()
num=array count(prtsys())
prtsys(num).pos.x=x#+plusmin(rnd(0.2))
prtsys(num).pos.y=y#+plusmin(rnd(0.2))
prtsys(num).pos.z=z#+plusmin(rnd(0.2))
prtsys(num).life=life
prtsys(num).count=count
for c=1 to prtsys().count
obj=zp_freeobject() : make object plain obj,100,100
set object transparency obj,3
disable object zwrite obj
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
obj$=obj$+str$(obj)+","
lifep=rnd(life)
life$=life$+str$(lifep)+","
next c
prtsys(num).objc=obj$
prtsys(num).lifep=life$
endfunction num
function setParticleSpeed(num,speed#)
prtsys(num).speed=speed#
endfunction
function setParticleSize(num,size#,sizep#)
prtsys(num).sizei=size#
prtsys(num).sizep=sizep#
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
scale object obj,size#,size#,1
next c
endfunction
function positionParticles(num,x#,y#,z#)
prtsys(num).pos.x=x#
prtsys(num).pos.y=y#
prtsys(num).pos.z=z#
endfunction
function setParticleGravity(num,x#,y#,z#)
prtsys(num).grav.x=x#
prtsys(num).grav.y=y#
prtsys(num).grav.z=z#
endfunction
function colorParticles(num,r,g,b)
col=rgb(r,g,b)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
color object obj,col
next c
endfunction
function setParticleGhost(num,flag)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
if flag=1
ghost object on obj
else
ghost object off obj
endif
next c
endfunction
function controlParticles()
for num=1 to array count(prtsys())
for c=1 to prtsys(num).count
prtsys(num).lifec=prtsys(num).lifec+1
prtsys(num).lifecount=prtsys(num).life-prtsys(num).lifec
obj=prtdat(num,c).obj
angx#=angx#+((360/prtsys(num).count)*c)
angy#=angy#+((360/prtsys(num).count)*c)
sx#=object position x(obj)+(sin(angy#)*cos(angx#)*prtsys(num).speed)
sy#=object position y(obj)-(sin(angx#)*prtsys(num).speed)
sz#=object position z(obj)+(cos(angy#)*cos(angx#)*prtsys(num).speed)
sx#=sx#+prtsys(num).grav.x
sy#=sy#+prtsys(num).grav.y
sz#=sz#+prtsys(num).grav.z
size#=prtsys(num).sizei+(prtsys(num).sizep*prtsys(num).lifecount)
position object obj,sx#,sy#,sz#
scale object obj,size#,size#,1
if prtsys(num).lifec>prtsys(num).life
prtsys(num).lifec=prtsys(num).lifec*0
size#=prtsys(num).sizei
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
endif
next c
next num
endfunction
function getEmitterCount()
count=array count(prtsys())
endfunction count
function getParticleCount(num)
count=prtsys(num).count
endfunction count
`Functions
function zp_freeobject()
repeat
inc i
until object exist(i)=0
endfunction i
function getTokens(st$,sp$)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until counter>len(nst$)
tokens=splits-1
endfunction tokens
function getToken(st$,sp$,num)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until splits=num or counter>len(st$)
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$<>sp$ then token$=token$+cur$
until cur$=sp$ or counter>len(st$)
endfunction token$
`Player Missile Control
Function Control_Missile()
`Check to see of the Missile is hidden. If the missile is hidden, then it has not been launched.
If Object Visible(Missile(0))=0
`The missile has not been launched; check to see if the player is pressing the space bar.
If SpaceKey()
`The spacekey is pressed, so launch the missile
`Start by showing the missile
Show Object Missile(0)
`Next, position the missile at the ship's location
Position Object Missile(0),Object Position X(Player(0).ObjID),Object Position Y(Player(0).ObjID), Object Position Z(Player(0).ObjID)
`Make sure the missile is pointing in the correct direction - up.
Rotate Object Missile(0),0,0,0
Else
`The missile has not been launched and the player is not pressing the space key - so nothing left to do.
Exitfunction
Endif
Endif
`If the code gets to these lines in this function, the missile has been launched. So, let's move it.
Move Object Up Missile(0),3
`Next, check to see if the missile has gone off the screen.
If Object Screen Y(Missile(0))<0
`The missile has gone off the screen, so let's hide it again. Remember, hiding it will stop it and have
`it ready for re-launch.
Hide Object Missile(0)
Endif
Endfunction
`UDTs
type vec3
x as float
y as float
z as float
endtype
type tprtsys
objc as string
pos as vec3
life as integer
lifec as integer
lifecount as integer
lifep as string
count as integer
speed as float
sizei as float
sizep as float
grav as vec3
endtype
type tprtdat
obj as integer
endtype
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
|
Posted: 4th Jan 2007 07:58 Edited: 4th Jan 2007 07:59 |
| link | toggle |
Thanks!
I've got the fire rate fixed in this snippet with the help of a variable called AlienFireFlag(0) that is set to 60 whenever an alien fires. The next alien cannot fire until the AlienFireFlag(0) counter reaches 0 again, 60 frames later (about 1 second).
I am still working on the alien movement.
+ Code Snippet`Standard Setup
Sync On: Sync Rate 60:Autocam Off
`The camera starts of at 0,0,0. Move it back and up a bit so the action can take center on 0,0,0
Position Camera 0,50,-100
Load Image "dataspace_invaders_splashscreen.png",1,1
Sprite 1,0,0,1
Size Sprite 1,Screen Width(),Screen Height()
Hide Sprite 1
Sprite 1,-800,-600,1
`Initialize the attached buttons functionality
Initialize_Buttons
`Make_Button(File as String,xpos as float, ypos as float,RVal as Integer,AnimFlag)
Make_Button("dataStart.png",320-32,200-16,1,1)
Do
Show Sprite 1
Paste Sprite 1,0,0
Hide Sprite 1
`Check to see if the button is "active" (mouse pointer is over sprite) and for left click
If Get_Button()=1 And MouseClick()=1 Then Exit
Sync
Loop
`Clean up the buttons.
Clear_Buttons
`There will be 6 rows of 9 invaders to start with.
`The player's ship will be at the bottom, like so.
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` /
`Create an array to store the invader information. This is not needed now, but will be needed later.
`This is a 2-dimensional array. It is 6 by 9 in size (or 6x9=54). 5,8 was used instead of 53 to make
`it easier to determine rows and columns. It is normally better to use a single array of 53.
Dim Invaders(5,8)
`Load the aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
`Get a free object ID for our invader
obj=FreeObject()
`Store the Object ID in the Invaders array
Invaders(r,i)=obj
`Load the alien model
Load Object "DataAlien1.x",obj
`Next, we need to make sure our model is the right size. For this, the model needs to be scaled.
`Roughly, we want a model that is 6x6x6 (just what is picked and looks about right).
`To scale the model, we first need to know it's size ratio to a size of 6.
`Here's how to get that:
s#=6.0/Object Size X(obj)
`All that is left to do is scale the model. Remember that Scale Object uses an integer scale that represents
`percentage values (100 = 100% = 1, 50 = 50% = .5, 1 = 1% = .01, and so on). So, the scale (s#) needs to be
`multiplied by 100.
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
`Each invader is 6x6x6 in size. Our viewable area is a little less than 100 x 100
`In order to fit the invaders evenly, we can divide the area we have to work with by
`the number of colums - 9. Since 9 goes into 90 evenly, lets use that. The result is 10.
`Here is the formula to calculate the X-position of the invaders every 10 spaces from
`-45x to +45x
x=(i*10)-45
`Using the same spacing of 10 for the Y-position, and the invaders need to be well up off
`the ground, here is the formula to calculate the Y-position.
y=(r*10)+50
`Position the invader
Position Object obj,x,y,0
Next i
Next r
` this variable will be used to store the objid of the first alien object
Global AlienObjIdStart
AlienObjIdStart = obj - 53
` *************************************************************************
` Prepare Enemy Bullets
` - last changed: 04.Jan.2007
` create a UDT for the Enemy Bullets
` (a description of UDT's and what they are, can be found further down.)
Type tEnemyBullet
` create a variable to store the bullets Object ID
ObjId AS INTEGER
` create a variable to store the alien Object ID
AlienId AS INTEGER
` create timer variables :: that way we can control how often
` the enemies are to fire a bullet.
TimeControl AS BOOLEAN ` here we will store a 1 for reset StartSecond or 0 for Start Counting Seconds
StartSecond AS INTEGER ` here we will store the starting position in time
CurSecond AS INTEGER ` here we will store the current position in time
FireHere AS INTEGER ` here we will store on which second the Alien should fire his bullet
` create a Active variable :: that way we can see if the bullet
` is destroyed. if it's destroyed, it means the alien
` connected to this bullet will be able to fire a new one.
Active AS INTEGER
` create position and rotation angle variables for the bullet
` this way we can easily control the movement and angle of the bullet
` --it would also be possible to make some sinus/cosinus effects
` if we want to make Easy - Hard difficulties. that would be neat.
xPos AS FLOAT
yPos AS FLOAT
zPos AS FLOAT
xAng AS FLOAT
yAng AS FLOAT
zAng AS FLOAT
EndType
` create the array where we store the Enemy Bullet information
` remember we have 6*9 aliens, yea? that means 54 enemies
Dim EnemyBullet(53) AS tEnemyBullet
`Counter to keep track of last bullet
Dim ALienFireFlag(0)
` now create all the bullets
For bullet = 1 to 54
` we set ID to bullet-1 because our Array "EnemyBullet" is counting
` from 0-53, and not 1-54
arrayPosition = bullet -1
` get the object nr for this bullet
obj = FreeObject()
` make a box object for the bullet
Load Object "DataMissile.x",obj
s#=6.0/Object Size Y(obj)
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
Set Object Emissive obj,Rgb(50,0,0)
Hide Object obj
` update the EnemyBullet array
alien = AlienObjIdStart + arrayPosition
EnemyBullet( arrayPosition ).ObjId = obj ` the object nr
EnemyBullet( arrayPosition ).AlienId = alien ` the alien object nr
EnemyBullet( arrayPos ).FireHere = rnd(20) ` set FireHere to a random of 20 seconds
Next bullet
` --------------------------------------- end of Prepare Enemy Bullets
` *************************************************************************
`Load the shield barriers (same process as above).
Dim Barriers(2)
For i = 0 to 2
obj=FreeObject()
Barriers(i)=obj
Load Object "DataBarrier.x",Barriers(i)
s#=12.0/Object Size X(obj)
Scale Object Barriers(i),s#*100.0,s#*100.0,s#*100.0
x#=(i*35)-35
Position Object Barriers(i),x#,20,0
Next i
`Load player's missile
`Use an array for the variable since it is global (A global variable could be used, but later you will see why
`there is a preference for Arrays over Global Variables).
Dim Missile(0)
`Assign a free object to Missile
Missile(0)=FreeObject()
`Load the missile model
Load Object "DataMissile.x",Missile(0)
`Calculate the scale
s#=6.0/Object Size Y(Missile(0))
`Scale the missile.
Scale Object Missile(0),s#*100.0,s#*100.0,s#*100.0
`Hide the missile
Hide Object Missile(0)
`Create a playertype "user defined array"
type PlayerType
`this will store the id of the object that is associated with the player, so we can move it later
ObjId as integer
`to keep track of the x position of the player, we are creating these as floats in order
`to maintain a smooth movement. With integers the player would "jump" from one integer to another
`adding the "#" to the end defines it as a float, to use this variable later you must use the "#"
xpos#
`we also need to keep track of the y position of the player, the player's "up and down"
`movement will be limited to keep them behind the bariers
ypos#
`this stores the distance that the player can move in one frame
speed#
` with the following variable stored, we can apply a neat rotation effect to the ship while
` it is moving left or right
yang#
endtype
REMSTART
Using "User Defined Types"
The code
type TheBestTypeEver
endtype
Creates a User Defined Type (reffered to as a UDT) with the name "TheBestTypeEver"
This by itself is a empty type, the entire point of using UDTs is to simplify code
by allowing sub variables for a "Parent" variable
to add sub variables to a UDT you simply type them out in between the 'type' and 'endtype'
type TheBestTypeEver
TheBestVariableEver as integer
TheOtherVariable as string
endtype
This adds two sub variables to the UDT, a integer (only "whole" numbers such as 1, 2, 3, 4, -17, 42,.. etc)
named "TheBestVariableEver", and a string named "TheOtherVariable"
Note: if you declare a variable without telling the compiler what type it is, it will
automatically be declared as an integer. So we could have left off the "as integer"
A UDT by itself does nothing at all, you can't assign something to it (such as TheBestTypeEver = 1337)
You have to declare a variable and define it as the type that your UDT is
A UDT is really just a fancy type of variable that you've created custom made for your game
It's used in exactly the same way that you would when you are declaring a variable as a string, float, or integer
bob as integer
bob as TheBestTypeEver
however, UDTs are different than normal variable types because they have sub variables
with "bob as int" you assign a value to it in the normal way, bob = 12
When you have a variable declared as a UDT you have to pick a sub variable to work with
It's easy to tell the compiler which sub variable you want, you just add a period (".")
to the end, then add the name of the sub variable.
so, going with our example, bob as TheBestTypeEver
bob.TheBestVariableEver = 20
bob.TheOtherVariable = "hello world"
That sets the two sub variables in bob to 20, and "hello world" (terribly original i know)
you can then access them just like normal variables
print bob.TheBestVariableEver
print bob.TheOtherVariable
Note: You CANNOT assign anything to the "parent" variable by itself. So bob = 20 would NOT work.
you need to pick a sub variable to assign to.
REMEND
`Create array Player() so it is global (A global variable can be used, but we will cover later why
`there is a preference for arrays over global variables).
`This array is created as a PlayerType array, and has all the attributes of the PlayerType User Defined Type
Dim Player(0) as PlayerType
`Get a free object for the player's ship
Player(0).ObjId = FreeObject()
`Load in the player's ship
Load Object "DataShip.x",Player(0).Objid
`Calculate the ratio of the ship to size 6 (making the player a little larger than the aliens)
s#=6.0/Object Size X(Player(0).ObjId)
`Scale the player's ship
Scale Object Player(0).ObjId,s#*100.0,s#*100.0,s#*100.0
`Place the player's ship
Position Object Player(0).ObjId,0,0,0
`-------------------------
`Player_Controls variables
`-------------------------
`set the player's speed
Player(0).speed# = 0.4
`sets the farthest position the player can travel to the right
rightside# = 77.0
`sets the farthest position the player can travel to the left
leftside# = -77.0
`sets the farthest position the player can travel upwards
topside# = 12.0
`sets the farthest position the player can travel downwards
bottomside# = -9.0
`-------------------------
`End Of Player_Controls variables
`-------------------------
`Particle Data
`Use this function to set up the particle data, otherwise they wont work.
setupParticles()
`Create a particle emmiter
prtShip=addParticles(0,0,0,60,60)
setParticleSize(prtShip,1.8,-0.03)
colorParticles(prtShip,255,0,0)
setParticleGhost(prtShip,1)
setParticleGravity(prtShip,0,-0.1,0)
`Call this function to actually make all the particles
createParticles()
`Main loop
Do
gosub player_controls
GoSub alien_fire_control
Control_Missile()
Set Cursor 0,0
Print Screen FPS()
Sync
Loop
End
`For more information on For/Next/Loops, refer to DBPro Help: Principles / Common Statements
`For more information on For/Next/Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`For more information on Formulas, refer to DBPro Help: Principles / Datatypes and variables
`A quick little function to check for the first free object number.
Function FreeObject()
`Start at object number 1
i=1
`A simple loop
Do
`If the object does not exist (=0), then exit the loop. "i" will be the valid object number
If Object Exist(i)=0 Then Exit
`Move on to the next object number
i=i+1
`Close the loop
Loop
`End the function and return the valid object number
Endfunction i
`For more information on Functions and returning values, refer to DBPro Help (press F1): Principles / Functions
`For more information on Loops, refer to DBPro Help: Principles / Common Statements
`For more information on Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`handles all the input from the player
player_controls:
` while not left or right key is pressed, we rotate the ship back to its original angle
Player(0).yang# = curvevalue( 0.0, object angle y( Player(0).ObjId ), 50.0 )
`check if the player is hitting the right key, if they are we want to let them move right
`as long as they are not hitting the right side of the screen
if rightkey() = 1
`move the player to the right at the preset speed
inc Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the right side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# > rightside# then Player(0).xpos# = rightside#
` apply rotation to the ship while we move right
Player(0).yang# = curvevalue( -40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check in the same way for the left key
if leftkey() = 1
`move the player to the left at the preset speed
dec Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the left side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# < leftside# then Player(0).xpos# = leftside#
` apply rotation to the ship while we move left
Player(0).yang# = curvevalue( 40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check if the player is hitting the upkey, if they are we want to let them move up
`but not past the barriers
if upkey() = 1
`move the player upwards at the preset speed
inc Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the barriers
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# > topside# then Player(0).ypos# = topside#
endif
`check if the player is hitting the downkey, if they are we want to let them move down
`but not past the bottom of the screen
if downkey() = 1
`move the player downwards at the preset speed
dec Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the bottom of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# < bottomside# then Player(0).ypos# = bottomside#
endif
`now that we've adjusted the position of the player accordingly, we need to update the object
`that is representing the player
position object Player(0).ObjId, Player(0).xpos#, Player(0).ypos#, 0
rotate object Player(0).ObjId, 0, Player(0).yang#, 0
`Update Particles
positionParticles(prtShip,Player(0).xpos#+plusmin(rnd(1.0)), Player(0).ypos#, 0)
controlParticles()
return
alien_fire_control:
`Decrease the AlienFireFlag counter if it is greater than 0
AlienFireFlag(0)=AlienFireFlag(0)-(AlienFireFlag(0)>0)
` start a for-next loop :: go through each of the bullets
For arrayPos = 0 To 53
obj = EnemyBullet( arrayPos ).ObjId ` get object nr. for this bullet
alien = EnemyBullet( arrayPos ).AlienId ` get object nr. of the alien
` If TimeControl = 0 And the bullet is inactive..
If EnemyBullet( arrayPos ).TimeControl = 0 And EnemyBullet( arrayPos ).Active = 0
EnemyBullet( arrayPos ).StartSecond = Timer() ` reset StartSecond
EnemyBullet( arrayPos ).FireHere = 5 + rnd(20) ` set FireHere to 3 seconds + a random of 10 seconds
EnemyBullet( arrayPos ).TimeControl = 1 ` set TimeControl to 1
EndIf
` If TimeControl = 1..
If EnemyBullet( arrayPos ).TimeControl = 1 and AlienFireFlag(0)=0
` increase .CurSecond by 1 each secound
EnemyBullet( arrayPos ).CurSecond = (Timer() - EnemyBullet( arrayPos ).StartSecond) / 1000
` if .CurSecond > FireHere, that means the alien should fire his bullet
If EnemyBullet( arrayPos ).CurSecond > EnemyBullet( arrayPos ).FireHere
EnemyBullet( arrayPos ).Active = 1
EnemyBullet( arrayPos ).TimeControl = 0
`Set the AlienFireFlag counter
AlienFireFlag(0)=60:`Sets the fire rate to approx. 1 second (60 frames at 60 FPS)
EndIf
EndIf
` if the bullet is active.. [stage 1] - update
If EnemyBullet( arrayPos ).Active = 1
Show Object obj
` update bullet position
EnemyBullet( arrayPos ).xPos = Object Position X( alien )
EnemyBullet( arrayPos ).yPos = Object Position Y( alien )
EnemyBullet( arrayPos ).zPos = Object Position Z( alien )
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
` go to stage 2 :: move the bullet
EnemyBullet( arrayPos ).Active = 2
EndIf
` if the bullet is active.. [stage 2] - move
If EnemyBullet( arrayPos ).Active = 2
` apply rotation to the bullet
EnemyBullet( arrayPos ).xAng = 0
EnemyBullet( arrayPos ).yAng = 0
EnemyBullet( arrayPos ).zAng = 180
` update position of the bullet
EnemyBullet( arrayPos ).xPos = EnemyBullet( arrayPos ).xPos + sin( EnemyBullet( arrayPos ).zAng ) * 1.3
EnemyBullet( arrayPos ).yPos = EnemyBullet( arrayPos ).yPos + cos( EnemyBullet( arrayPos ).zAng ) * 1.3
` check if the bullet is outside the screen
If EnemyBullet( arrayPos ).yPos < bottomside#
Hide Object obj
EnemyBullet( arrayPos ).Active = 0
EndIf
` update object
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
Rotate Object obj, EnemyBullet( arrayPos ).xAng, EnemyBullet( arrayPos ).yAng, EnemyBullet( arrayPos ).zAng
EndIf
Next arrayPos
return
Function Move_Aliens()
`Start by counting how many aliens still are alive. This will determine alien speed.
AlienCount=0
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
AlienCount=AlienCount+Object Exist(Invaders(5,8))
Next i
Next r
Endfunction
`Particle Data
function plusmin(num#)
rand=rnd(10)
if rand>5 then ret#=num#*-1 else ret#=num#
endfunction ret#
function setupParticles()
dim prtsys(0) as tprtsys
endfunction
function createParticles()
count=array count(prtsys())
cmax=0
for c=1 to count
if prtsys(c).count>cmax then cmax=prtsys(c).count
next c
dim prtdat(array count(prtsys()),cmax) as tprtdat
for c=1 to array count(prtsys())
for o=1 to prtsys(c).count
prtdat(c,o).obj=val(getToken(prtsys(c).objc,",",o))
next o
next c
endfunction count
function addParticles(x#,y#,z#,life,count)
array insert at bottom prtsys()
num=array count(prtsys())
prtsys(num).pos.x=x#+plusmin(rnd(0.2))
prtsys(num).pos.y=y#+plusmin(rnd(0.2))
prtsys(num).pos.z=z#+plusmin(rnd(0.2))
prtsys(num).life=life
prtsys(num).count=count
for c=1 to prtsys().count
obj=zp_freeobject() : make object plain obj,100,100
set object transparency obj,3
disable object zwrite obj
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
obj$=obj$+str$(obj)+","
lifep=rnd(life)
life$=life$+str$(lifep)+","
next c
prtsys(num).objc=obj$
prtsys(num).lifep=life$
endfunction num
function setParticleSpeed(num,speed#)
prtsys(num).speed=speed#
endfunction
function setParticleSize(num,size#,sizep#)
prtsys(num).sizei=size#
prtsys(num).sizep=sizep#
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
scale object obj,size#,size#,1
next c
endfunction
function positionParticles(num,x#,y#,z#)
prtsys(num).pos.x=x#
prtsys(num).pos.y=y#
prtsys(num).pos.z=z#
endfunction
function setParticleGravity(num,x#,y#,z#)
prtsys(num).grav.x=x#
prtsys(num).grav.y=y#
prtsys(num).grav.z=z#
endfunction
function colorParticles(num,r,g,b)
col=rgb(r,g,b)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
color object obj,col
next c
endfunction
function setParticleGhost(num,flag)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
if flag=1
ghost object on obj
else
ghost object off obj
endif
next c
endfunction
function controlParticles()
for num=1 to array count(prtsys())
for c=1 to prtsys(num).count
prtsys(num).lifec=prtsys(num).lifec+1
prtsys(num).lifecount=prtsys(num).life-prtsys(num).lifec
obj=prtdat(num,c).obj
angx#=angx#+((360/prtsys(num).count)*c)
angy#=angy#+((360/prtsys(num).count)*c)
sx#=object position x(obj)+(sin(angy#)*cos(angx#)*prtsys(num).speed)
sy#=object position y(obj)-(sin(angx#)*prtsys(num).speed)
sz#=object position z(obj)+(cos(angy#)*cos(angx#)*prtsys(num).speed)
sx#=sx#+prtsys(num).grav.x
sy#=sy#+prtsys(num).grav.y
sz#=sz#+prtsys(num).grav.z
size#=prtsys(num).sizei+(prtsys(num).sizep*prtsys(num).lifecount)
position object obj,sx#,sy#,sz#
scale object obj,size#,size#,1
if prtsys(num).lifec>prtsys(num).life
prtsys(num).lifec=prtsys(num).lifec*0
size#=prtsys(num).sizei
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
endif
next c
next num
endfunction
function getEmitterCount()
count=array count(prtsys())
endfunction count
function getParticleCount(num)
count=prtsys(num).count
endfunction count
`Functions
function zp_freeobject()
repeat
inc i
until object exist(i)=0
endfunction i
function getTokens(st$,sp$)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until counter>len(nst$)
tokens=splits-1
endfunction tokens
function getToken(st$,sp$,num)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until splits=num or counter>len(st$)
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$<>sp$ then token$=token$+cur$
until cur$=sp$ or counter>len(st$)
endfunction token$
`Player Missile Control
Function Control_Missile()
`Check to see of the Missile is hidden. If the missile is hidden, then it has not been launched.
If Object Visible(Missile(0))=0
`The missile has not been launched; check to see if the player is pressing the space bar.
If SpaceKey()
`The spacekey is pressed, so launch the missile
`Start by showing the missile
Show Object Missile(0)
`Next, position the missile at the ship's location
Position Object Missile(0),Object Position X(Player(0).ObjID),Object Position Y(Player(0).ObjID), Object Position Z(Player(0).ObjID)
`Make sure the missile is pointing in the correct direction - up.
Rotate Object Missile(0),0,0,0
Else
`The missile has not been launched and the player is not pressing the space key - so nothing left to do.
Exitfunction
Endif
Endif
`If the code gets to these lines in this function, the missile has been launched. So, let's move it.
Move Object Up Missile(0),3
`Next, check to see if the missile has gone off the screen.
If Object Screen Y(Missile(0))<0
`The missile has gone off the screen, so let's hide it again. Remember, hiding it will stop it and have
`it ready for re-launch.
Hide Object Missile(0)
Endif
Endfunction
`UDTs
type vec3
x as float
y as float
z as float
endtype
type tprtsys
objc as string
pos as vec3
life as integer
lifec as integer
lifecount as integer
lifep as string
count as integer
speed as float
sizei as float
sizep as float
grav as vec3
endtype
type tprtdat
obj as integer
endtype

Open MMORPG: It's your game!
|
Back to top
 |
|
RUCCUS
User
Joined: Sat Dec 11th 2004
Location: Canada
|
Looks good Rii/Lukas, just thought Id point out Rii, remember to allow the aliens to move farther as each column of aliens is destroyed.
I think Im done modelling, just wonderin if anyone sees any aliens missing from this pic?
If not then Ill map em' and upload them.
- RUC'
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
Looks good to me Ruccus. Thanks for the reminder. I think I have it working so that it will stop on the last collumns, but we will have to see when we get some collision and slimey alien destruction going on.
Here's the movement. We nearly have a game! Anyone want to take collision and slimey alien destruction? Could add in some basic scoring too. I would not worry about player collision and death until we have everything else down. It can be a pain to try and debug something if you keep dying.
+ Code Snippet`Standard Setup
Sync On: Sync Rate 60:Autocam Off
`The camera starts of at 0,0,0. Move it back and up a bit so the action can take center on 0,0,0
Position Camera 0,50,-100
Load Image "data\space_invaders_splashscreen.png",1,1
Sprite 1,0,0,1
Size Sprite 1,Screen Width(),Screen Height()
Hide Sprite 1
Sprite 1,-800,-600,1
`Initialize the attached buttons functionality
Initialize_Buttons
`Make_Button(File as String,xpos as float, ypos as float,RVal as Integer,AnimFlag)
Make_Button("data\Start.png",320-32,200-16,1,1)
Do
Show Sprite 1
Paste Sprite 1,0,0
Hide Sprite 1
`Check to see if the button is "active" (mouse pointer is over sprite) and for left click
If Get_Button()=1 And MouseClick()=1 Then Exit
Sync
Loop
`Clean up the buttons.
Clear_Buttons
`There will be 6 rows of 9 invaders to start with.
`The player's ship will be at the bottom, like so.
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` /\
`Create an array to store the invader information. This is not needed now, but will be needed later.
`This is a 2-dimensional array. It is 6 by 9 in size (or 6x9=54). 5,8 was used instead of 53 to make
`it easier to determine rows and columns. It is normally better to use a single array of 53.
Dim Invaders(5,8)
`Load the aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
`Get a free object ID for our invader
obj=FreeObject()
`Store the Object ID in the Invaders array
Invaders(r,i)=obj
`Load the alien model
Load Object "Data\Alien1.x",obj
`Next, we need to make sure our model is the right size. For this, the model needs to be scaled.
`Roughly, we want a model that is 6x6x6 (just what is picked and looks about right).
`To scale the model, we first need to know it's size ratio to a size of 6.
`Here's how to get that:
s#=6.0/Object Size X(obj)
`All that is left to do is scale the model. Remember that Scale Object uses an integer scale that represents
`percentage values (100 = 100% = 1, 50 = 50% = .5, 1 = 1% = .01, and so on). So, the scale (s#) needs to be
`multiplied by 100.
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
`Each invader is 6x6x6 in size. Our viewable area is a little less than 100 x 100
`In order to fit the invaders evenly, we can divide the area we have to work with by
`the number of colums - 9. Since 9 goes into 90 evenly, lets use that. The result is 10.
`Here is the formula to calculate the X-position of the invaders every 10 spaces from
`-45x to +45x
x=(i*10)-45
`Using the same spacing of 10 for the Y-position, and the invaders need to be well up off
`the ground, here is the formula to calculate the Y-position.
y=(r*10)+50
`Position the invader
Position Object obj,x,y,0
Next i
Next r
` this variable will be used to store the objid of the first alien object
Global AlienObjIdStart
AlienObjIdStart = obj - 53
` *************************************************************************
` Prepare Enemy Bullets
` - last changed: 04.Jan.2007
` create a UDT for the Enemy Bullets
` (a description of UDT's and what they are, can be found further down.)
Type tEnemyBullet
` create a variable to store the bullets Object ID
ObjId AS INTEGER
` create a variable to store the alien Object ID
AlienId AS INTEGER
` create timer variables :: that way we can control how often
` the enemies are to fire a bullet.
TimeControl AS BOOLEAN ` here we will store a 1 for reset StartSecond or 0 for Start Counting Seconds
StartSecond AS INTEGER ` here we will store the starting position in time
CurSecond AS INTEGER ` here we will store the current position in time
FireHere AS INTEGER ` here we will store on which second the Alien should fire his bullet
` create a Active variable :: that way we can see if the bullet
` is destroyed. if it's destroyed, it means the alien
` connected to this bullet will be able to fire a new one.
Active AS INTEGER
` create position and rotation angle variables for the bullet
` this way we can easily control the movement and angle of the bullet
` --it would also be possible to make some sinus/cosinus effects
` if we want to make Easy - Hard difficulties. that would be neat.
xPos AS FLOAT
yPos AS FLOAT
zPos AS FLOAT
xAng AS FLOAT
yAng AS FLOAT
zAng AS FLOAT
EndType
` create the array where we store the Enemy Bullet information
` remember we have 6*9 aliens, yea? that means 54 enemies
Dim EnemyBullet(53) AS tEnemyBullet
`Counter to keep track of last bullet
Dim ALienFireFlag(0)
` now create all the bullets
For bullet = 1 to 54
` we set ID to bullet-1 because our Array "EnemyBullet" is counting
` from 0-53, and not 1-54
arrayPosition = bullet -1
` get the object nr for this bullet
obj = FreeObject()
` make a box object for the bullet
Load Object "Data\Missile.x",obj
s#=6.0/Object Size Y(obj)
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
Set Object Emissive obj,Rgb(50,0,0)
Hide Object obj
` update the EnemyBullet array
alien = AlienObjIdStart + arrayPosition
EnemyBullet( arrayPosition ).ObjId = obj ` the object nr
EnemyBullet( arrayPosition ).AlienId = alien ` the alien object nr
EnemyBullet( arrayPos ).FireHere = rnd(20) ` set FireHere to a random of 20 seconds
Next bullet
` --------------------------------------- end of Prepare Enemy Bullets
` *************************************************************************
`Load the shield barriers (same process as above).
Dim Barriers(2)
For i = 0 to 2
obj=FreeObject()
Barriers(i)=obj
Load Object "Data\Barrier.x",Barriers(i)
s#=12.0/Object Size X(obj)
Scale Object Barriers(i),s#*100.0,s#*100.0,s#*100.0
x#=(i*35)-35
Position Object Barriers(i),x#,20,0
Next i
`Load player's missile
`Use an array for the variable since it is global (A global variable could be used, but later you will see why
`there is a preference for Arrays over Global Variables).
Dim Missile(0)
`Assign a free object to Missile
Missile(0)=FreeObject()
`Load the missile model
Load Object "Data\Missile.x",Missile(0)
`Calculate the scale
s#=6.0/Object Size Y(Missile(0))
`Scale the missile.
Scale Object Missile(0),s#*100.0,s#*100.0,s#*100.0
`Hide the missile
Hide Object Missile(0)
`Create a playertype "user defined array"
type PlayerType
`this will store the id of the object that is associated with the player, so we can move it later
ObjId as integer
`to keep track of the x position of the player, we are creating these as floats in order
`to maintain a smooth movement. With integers the player would "jump" from one integer to another
`adding the "#" to the end defines it as a float, to use this variable later you must use the "#"
xpos#
`we also need to keep track of the y position of the player, the player's "up and down"
`movement will be limited to keep them behind the bariers
ypos#
`this stores the distance that the player can move in one frame
speed#
` with the following variable stored, we can apply a neat rotation effect to the ship while
` it is moving left or right
yang#
endtype
REMSTART
Using "User Defined Types"
The code
type TheBestTypeEver
endtype
Creates a User Defined Type (reffered to as a UDT) with the name "TheBestTypeEver"
This by itself is a empty type, the entire point of using UDTs is to simplify code
by allowing sub variables for a "Parent" variable
to add sub variables to a UDT you simply type them out in between the 'type' and 'endtype'
type TheBestTypeEver
TheBestVariableEver as integer
TheOtherVariable as string
endtype
This adds two sub variables to the UDT, a integer (only "whole" numbers such as 1, 2, 3, 4, -17, 42,.. etc)
named "TheBestVariableEver", and a string named "TheOtherVariable"
Note: if you declare a variable without telling the compiler what type it is, it will
automatically be declared as an integer. So we could have left off the "as integer"
A UDT by itself does nothing at all, you can't assign something to it (such as TheBestTypeEver = 1337)
You have to declare a variable and define it as the type that your UDT is
A UDT is really just a fancy type of variable that you've created custom made for your game
It's used in exactly the same way that you would when you are declaring a variable as a string, float, or integer
bob as integer
bob as TheBestTypeEver
however, UDTs are different than normal variable types because they have sub variables
with "bob as int" you assign a value to it in the normal way, bob = 12
When you have a variable declared as a UDT you have to pick a sub variable to work with
It's easy to tell the compiler which sub variable you want, you just add a period (".")
to the end, then add the name of the sub variable.
so, going with our example, bob as TheBestTypeEver
bob.TheBestVariableEver = 20
bob.TheOtherVariable = "hello world"
That sets the two sub variables in bob to 20, and "hello world" (terribly original i know)
you can then access them just like normal variables
print bob.TheBestVariableEver
print bob.TheOtherVariable
Note: You CANNOT assign anything to the "parent" variable by itself. So bob = 20 would NOT work.
you need to pick a sub variable to assign to.
REMEND
`Create array Player() so it is global (A global variable can be used, but we will cover later why
`there is a preference for arrays over global variables).
`This array is created as a PlayerType array, and has all the attributes of the PlayerType User Defined Type
Dim Player(0) as PlayerType
`Get a free object for the player's ship
Player(0).ObjId = FreeObject()
`Load in the player's ship
Load Object "Data\Ship.x",Player(0).Objid
`Calculate the ratio of the ship to size 6 (making the player a little larger than the aliens)
s#=6.0/Object Size X(Player(0).ObjId)
`Scale the player's ship
Scale Object Player(0).ObjId,s#*100.0,s#*100.0,s#*100.0
`Place the player's ship
Position Object Player(0).ObjId,0,0,0
`-------------------------
`Player_Controls variables
`-------------------------
`set the player's speed
Player(0).speed# = 0.4
`sets the farthest position the player can travel to the right
rightside# = 77.0
`sets the farthest position the player can travel to the left
leftside# = -77.0
`sets the farthest position the player can travel upwards
topside# = 12.0
`sets the farthest position the player can travel downwards
bottomside# = -9.0
`-------------------------
`End Of Player_Controls variables
`-------------------------
`Particle Data
`Use this function to set up the particle data, otherwise they wont work.
setupParticles()
`Create a particle emmiter
prtShip=addParticles(0,0,0,60,60)
setParticleSize(prtShip,1.8,-0.03)
colorParticles(prtShip,255,0,0)
setParticleGhost(prtShip,1)
setParticleGravity(prtShip,0,-0.1,0)
`Call this function to actually make all the particles
createParticles()
`Main loop
Do
Move_Aliens()
gosub player_controls
GoSub alien_fire_control
Control_Missile()
Set Cursor 0,0
Print Screen FPS()
Sync
Loop
End
`For more information on For/Next/Loops, refer to DBPro Help: Principles / Common Statements
`For more information on For/Next/Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`For more information on Formulas, refer to DBPro Help: Principles / Datatypes and variables
`A quick little function to check for the first free object number.
Function FreeObject()
`Start at object number 1
i=1
`A simple loop
Do
`If the object does not exist (=0), then exit the loop. "i" will be the valid object number
If Object Exist(i)=0 Then Exit
`Move on to the next object number
i=i+1
`Close the loop
Loop
`End the function and return the valid object number
Endfunction i
`For more information on Functions and returning values, refer to DBPro Help (press F1): Principles / Functions
`For more information on Loops, refer to DBPro Help: Principles / Common Statements
`For more information on Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`handles all the input from the player
player_controls:
` while not left or right key is pressed, we rotate the ship back to its original angle
Player(0).yang# = curvevalue( 0.0, object angle y( Player(0).ObjId ), 50.0 )
`check if the player is hitting the right key, if they are we want to let them move right
`as long as they are not hitting the right side of the screen
if rightkey() = 1
`move the player to the right at the preset speed
inc Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the right side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# > rightside# then Player(0).xpos# = rightside#
` apply rotation to the ship while we move right
Player(0).yang# = curvevalue( -40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check in the same way for the left key
if leftkey() = 1
`move the player to the left at the preset speed
dec Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the left side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# < leftside# then Player(0).xpos# = leftside#
` apply rotation to the ship while we move left
Player(0).yang# = curvevalue( 40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check if the player is hitting the upkey, if they are we want to let them move up
`but not past the barriers
if upkey() = 1
`move the player upwards at the preset speed
inc Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the barriers
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# > topside# then Player(0).ypos# = topside#
endif
`check if the player is hitting the downkey, if they are we want to let them move down
`but not past the bottom of the screen
if downkey() = 1
`move the player downwards at the preset speed
dec Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the bottom of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# < bottomside# then Player(0).ypos# = bottomside#
endif
`now that we've adjusted the position of the player accordingly, we need to update the object
`that is representing the player
position object Player(0).ObjId, Player(0).xpos#, Player(0).ypos#, 0
rotate object Player(0).ObjId, 0, Player(0).yang#, 0
`Update Particles
positionParticles(prtShip,Player(0).xpos#+plusmin(rnd(1.0)), Player(0).ypos#, 0)
controlParticles()
return
alien_fire_control:
`Decrease the AlienFireFlag counter if it is greater than 0
AlienFireFlag(0)=AlienFireFlag(0)-(AlienFireFlag(0)>0)
` start a for-next loop :: go through each of the bullets
For arrayPos = 0 To 53
obj = EnemyBullet( arrayPos ).ObjId ` get object nr. for this bullet
alien = EnemyBullet( arrayPos ).AlienId ` get object nr. of the alien
` If TimeControl = 0 And the bullet is inactive..
If EnemyBullet( arrayPos ).TimeControl = 0 And EnemyBullet( arrayPos ).Active = 0
EnemyBullet( arrayPos ).StartSecond = Timer() ` reset StartSecond
EnemyBullet( arrayPos ).FireHere = 5 + rnd(20) ` set FireHere to 3 seconds + a random of 10 seconds
EnemyBullet( arrayPos ).TimeControl = 1 ` set TimeControl to 1
EndIf
` If TimeControl = 1..
If EnemyBullet( arrayPos ).TimeControl = 1 and AlienFireFlag(0)=0
` increase .CurSecond by 1 each secound
EnemyBullet( arrayPos ).CurSecond = (Timer() - EnemyBullet( arrayPos ).StartSecond) / 1000
` if .CurSecond > FireHere, that means the alien should fire his bullet
If EnemyBullet( arrayPos ).CurSecond > EnemyBullet( arrayPos ).FireHere
EnemyBullet( arrayPos ).Active = 1
EnemyBullet( arrayPos ).TimeControl = 0
`Set the AlienFireFlag counter
AlienFireFlag(0)=60:`Sets the fire rate to approx. 1 second (60 frames at 60 FPS)
EndIf
EndIf
` if the bullet is active.. [stage 1] - update
If EnemyBullet( arrayPos ).Active = 1
Show Object obj
` update bullet position
EnemyBullet( arrayPos ).xPos = Object Position X( alien )
EnemyBullet( arrayPos ).yPos = Object Position Y( alien )
EnemyBullet( arrayPos ).zPos = Object Position Z( alien )
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
` go to stage 2 :: move the bullet
EnemyBullet( arrayPos ).Active = 2
EndIf
` if the bullet is active.. [stage 2] - move
If EnemyBullet( arrayPos ).Active = 2
` apply rotation to the bullet
EnemyBullet( arrayPos ).xAng = 0
EnemyBullet( arrayPos ).yAng = 0
EnemyBullet( arrayPos ).zAng = 180
` update position of the bullet
EnemyBullet( arrayPos ).xPos = EnemyBullet( arrayPos ).xPos + sin( EnemyBullet( arrayPos ).zAng ) * 1.3
EnemyBullet( arrayPos ).yPos = EnemyBullet( arrayPos ).yPos + cos( EnemyBullet( arrayPos ).zAng ) * 1.3
` check if the bullet is outside the screen
If EnemyBullet( arrayPos ).yPos < bottomside#
Hide Object obj
EnemyBullet( arrayPos ).Active = 0
EndIf
` update object
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
Rotate Object obj, EnemyBullet( arrayPos ).xAng, EnemyBullet( arrayPos ).yAng, EnemyBullet( arrayPos ).zAng
EndIf
Next arrayPos
return
Function Move_Aliens()
Dim NextAlienMove(0):`Tracks how long until the next alien move
Dim AlienMoveDirection(0):`Tracks which direction the aliens are moving in
Dim AliensXY(1):`Tracks the Aliens position as a group. 0=X, 1=Y
`Start by moving to the right.
If AlienMoveDirection(0)=0 Then AlienMoveDirection(0)=1
`Set the initial Alien Group position if it is not already set
If AliensXY(1)=0
AliensXY(0)=-45
AliensXY(1)=50
Endif
`Decrease NextAlienMove(0) if it is greater than 0
NextAlienMove(0)=NextAlienMove(0)-(NextAlienMove(0)>0)
`If NextAlienMove(0)>0 then exit the function, since no move will be made
If NextAlienMove(0)>0 Then ExitFunction
`Start by counting how many aliens still are alive. This will determine alien speed.
AlienCount=0
`Set the left & right invader trackers so we can determine the left & right most invaders
LeftInvader=8:RightInvader=0
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
If Object Exist(Invaders(r,i))
AlienCount=AlienCount+1
if i<LeftInvader then LeftInvader=i
if i>RightInvader then RightInvader=i
Endif
Next i
Next r
`A move will be processed, so set the NextAlienMove(0) counter now
NextAlienMove(0)=AlienCount
MoveDown=0
`Check to see if the aliens are to the far right.
If AlienMoveDirection(0)=1
If (RightInvader*10)+AliensXY(0)>70
MoveDown=1
Endif
Endif
`Check to see if the aliens are to the far left
If AlienMoveDirection(0)=-1
If (LeftInvader*10)+AliensXY(0)<-70
MoveDown=1
Endif
Endif
`Calculate the new alien group position
If MoveDown>0
`Move the alien group down
AliensXY(1)=AliensXY(1)-2
`And change directions
AlienMoveDirection(0)=-AlienMoveDirection(0)
Else
AliensXY(0)=AliensXY(0)+AlienMoveDirection(0)*2
Endif
`Reposition Aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
x=AliensXY(0)+(i*10)
y=AliensXY(1)+(r*10)
If Object Exist(Invaders(r,i))
Position Object Invaders(r,i),x,y,0
Endif
Next i
Next r
Endfunction
`Particle Data
function plusmin(num#)
rand=rnd(10)
if rand>5 then ret#=num#*-1 else ret#=num#
endfunction ret#
function setupParticles()
dim prtsys(0) as tprtsys
endfunction
function createParticles()
count=array count(prtsys())
cmax=0
for c=1 to count
if prtsys(c).count>cmax then cmax=prtsys(c).count
next c
dim prtdat(array count(prtsys()),cmax) as tprtdat
for c=1 to array count(prtsys())
for o=1 to prtsys(c).count
prtdat(c,o).obj=val(getToken(prtsys(c).objc,",",o))
next o
next c
endfunction count
function addParticles(x#,y#,z#,life,count)
array insert at bottom prtsys()
num=array count(prtsys())
prtsys(num).pos.x=x#+plusmin(rnd(0.2))
prtsys(num).pos.y=y#+plusmin(rnd(0.2))
prtsys(num).pos.z=z#+plusmin(rnd(0.2))
prtsys(num).life=life
prtsys(num).count=count
for c=1 to prtsys().count
obj=zp_freeobject() : make object plain obj,100,100
set object transparency obj,3
disable object zwrite obj
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
obj$=obj$+str$(obj)+","
lifep=rnd(life)
life$=life$+str$(lifep)+","
next c
prtsys(num).objc=obj$
prtsys(num).lifep=life$
endfunction num
function setParticleSpeed(num,speed#)
prtsys(num).speed=speed#
endfunction
function setParticleSize(num,size#,sizep#)
prtsys(num).sizei=size#
prtsys(num).sizep=sizep#
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
scale object obj,size#,size#,1
next c
endfunction
function positionParticles(num,x#,y#,z#)
prtsys(num).pos.x=x#
prtsys(num).pos.y=y#
prtsys(num).pos.z=z#
endfunction
function setParticleGravity(num,x#,y#,z#)
prtsys(num).grav.x=x#
prtsys(num).grav.y=y#
prtsys(num).grav.z=z#
endfunction
function colorParticles(num,r,g,b)
col=rgb(r,g,b)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
color object obj,col
next c
endfunction
function setParticleGhost(num,flag)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
if flag=1
ghost object on obj
else
ghost object off obj
endif
next c
endfunction
function controlParticles()
for num=1 to array count(prtsys())
for c=1 to prtsys(num).count
prtsys(num).lifec=prtsys(num).lifec+1
prtsys(num).lifecount=prtsys(num).life-prtsys(num).lifec
obj=prtdat(num,c).obj
angx#=angx#+((360/prtsys(num).count)*c)
angy#=angy#+((360/prtsys(num).count)*c)
sx#=object position x(obj)+(sin(angy#)*cos(angx#)*prtsys(num).speed)
sy#=object position y(obj)-(sin(angx#)*prtsys(num).speed)
sz#=object position z(obj)+(cos(angy#)*cos(angx#)*prtsys(num).speed)
sx#=sx#+prtsys(num).grav.x
sy#=sy#+prtsys(num).grav.y
sz#=sz#+prtsys(num).grav.z
size#=prtsys(num).sizei+(prtsys(num).sizep*prtsys(num).lifecount)
position object obj,sx#,sy#,sz#
scale object obj,size#,size#,1
if prtsys(num).lifec>prtsys(num).life
prtsys(num).lifec=prtsys(num).lifec*0
size#=prtsys(num).sizei
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
endif
next c
next num
endfunction
function getEmitterCount()
count=array count(prtsys())
endfunction count
function getParticleCount(num)
count=prtsys(num).count
endfunction count
`Functions
function zp_freeobject()
repeat
inc i
until object exist(i)=0
endfunction i
function getTokens(st$,sp$)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until counter>len(nst$)
tokens=splits-1
endfunction tokens
function getToken(st$,sp$,num)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until splits=num or counter>len(st$)
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$<>sp$ then token$=token$+cur$
until cur$=sp$ or counter>len(st$)
endfunction token$
`Player Missile Control
Function Control_Missile()
`Check to see of the Missile is hidden. If the missile is hidden, then it has not been launched.
If Object Visible(Missile(0))=0
`The missile has not been launched; check to see if the player is pressing the space bar.
If SpaceKey()
`The spacekey is pressed, so launch the missile
`Start by showing the missile
Show Object Missile(0)
`Next, position the missile at the ship's location
Position Object Missile(0),Object Position X(Player(0).ObjID),Object Position Y(Player(0).ObjID), Object Position Z(Player(0).ObjID)
`Make sure the missile is pointing in the correct direction - up.
Rotate Object Missile(0),0,0,0
Else
`The missile has not been launched and the player is not pressing the space key - so nothing left to do.
Exitfunction
Endif
Endif
`If the code gets to these lines in this function, the missile has been launched. So, let's move it.
Move Object Up Missile(0),3
`Next, check to see if the missile has gone off the screen.
If Object Screen Y(Missile(0))<0
`The missile has gone off the screen, so let's hide it again. Remember, hiding it will stop it and have
`it ready for re-launch.
Hide Object Missile(0)
Endif
Endfunction
`UDTs
type vec3
x as float
y as float
z as float
endtype
type tprtsys
objc as string
pos as vec3
life as integer
lifec as integer
lifecount as integer
lifep as string
count as integer
speed as float
sizei as float
sizep as float
grav as vec3
endtype
type tprtdat
obj as integer
endtype

Open MMORPG: It's your game!
|
Back to top
 |
|
RUCCUS
User
Joined: Sat Dec 11th 2004
Location: Canada
|
|
Posted: 4th Jan 2007 09:10 Edited: 4th Jan 2007 09:18 |
| link | toggle |
Ill take a stab at it for the next hour - hour and a half, if I dont get anywhere Ill post back incase anyone else wants to try.
<edit>
sorry scratch that, Ive got to go now. If nobody's tried it when I get back Ill start programming.
|
Back to top
 |
|
BatVink
TGC Newsletter Editor
 Joined: Fri Apr 4th 2003
Location: Chilling
|
Quote: "remember to allow the aliens to move farther as each column of aliens is destroyed"
In my tutorial I had a box on each side of the screen. I did a simple collision test against the boxes for the range of alien objects, and switched direction when it happened.
You should also monitor the direction and check only against the relevant box, otherwise you can get a jiggy thing going on when more than one hit at the same time.
Likewise, you can use the same theory for check the aliens reaching the bottom. You can't rely on hitting bases, because they may be completely destroyed.
|
Back to top
 |
|
Peter H
User
Joined: Fri Feb 20th 2004
Location: Witness Protection Program
|
Quote: "You should also monitor the direction and check only against the relevant box, otherwise you can get a jiggy thing going on when more than one hit at the same time."
heh... yeah, i had that problem at first with my space invaders game a while back (the snail one)
One man, one lawnmower, plenty of angry groundhogs.

|
Back to top
 |
|
indi
User
Joined: Mon Aug 26th 2002
Location: Earth, Brisbane, Australia
|
|
Posted: 5th Jan 2007 06:57 Edited: 5th Jan 2007 07:04 |
| link | toggle |
I update the code.
[1] Cleaned it up a bit firstly so its a little easier to manage.
[2] Added a loading bar to the start so its a little more user friendly and responsive after the button start click
this code will work with the download RiiDii supplied above and worked from Riidii's last code snippet in the list.
+ Code Snippet
REM -----------------------------------------------------------------------------------
REM SPACE INVADERS
REM Community Based Project
REM Language : DBP version 6.3
REM Jan 05 2007
REM Authors
REM Ridii
REM indi
REM Ruccus
REM Lukas W
REM
REM Program Notes
REM Added Loading bar
REM Cleaned up the code a little with clear titles
REM
REM -----------------------------------------------------------------------------------
REM -----------------------------------------------------------------------------------
REM SETUP GAME SHELL
REM -----------------------------------------------------------------------------------
set display mode 800,600,32 : Sync On : Sync Rate 60 : Autocam Off
backdrop off : color backdrop rgb(0,0,0)
REM -----------------------------------------------------------------------------------
REM CREATE LOADING BAR
REM -----------------------------------------------------------------------------------
type LoadBar
name$ as string
x1 as integer
y1 as integer
x2 as integer
y2 as integer
bdr as integer
width as integer
height as integer
length as integer
endtype
dim LoadBar(1) as LoadBar
LoadBar(1).height = 8
LoadBar(1).width = 256
LoadBar(1).x1 = 10
LoadBar(1).y1 = 30
LoadBar(1).x2 = LoadBar(1).x1 + LoadBar(1).width
LoadBar(1).y2 = LoadBar(1).y1 + LoadBar(1).height
LoadBar(1).bdr = 1
LoadBar(1).name$ = "Loading Data"
LoadBar(1).length = 0
REM -----------------------------------------------------------------------------------
REM INIT SCREEN
REM -----------------------------------------------------------------------------------
Load Image "dataspace_invaders_splashscreen.png",1,1
Sprite 1,0,0,1
Size Sprite 1,Screen Width(),Screen Height()
Sprite 1,-800,-600,1
Hide Sprite 1
`Initialize the attached buttons functionality
Initialize_Buttons
`Make_Button(File as String,xpos as float, ypos as float,RVal as Integer,AnimFlag)
Make_Button("dataStart.png",screen width()/2-32,screen height()/2-66,1,1)
Show Sprite 1
Do
Paste Sprite 1,0,0
`Check to see if the button is "active" (mouse pointer is over sprite) and for left click
If Get_Button()=1 And MouseClick()=1 Then Exit
fastSync
Loop
hide sprite 1
`Clean up the buttons.
Clear_Buttons
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 10
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM
REM CREATE ALIENS
REM
REM -----------------------------------------------------------------------------------
`There will be 6 rows of 9 invaders to start with.
`The player's ship will be at the bottom, like so.
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` /
`Create an array to store the invader information. This is not needed now, but will be needed later.
`This is a 2-dimensional array. It is 6 by 9 in size (or 6x9=54). 5,8 was used instead of 53 to make
`it easier to determine rows and columns. It is normally better to use a single array of 53.
Dim Invaders(5,8)
`Load the aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
`Get a free object ID for our invader
obj=FreeObject()
`Store the Object ID in the Invaders array
Invaders(r,i)=obj
`Load the alien model
Load Object "DataAlien1.x",obj
`Next, we need to make sure our model is the right size. For this, the model needs to be scaled.
`Roughly, we want a model that is 6x6x6 (just what is picked and looks about right).
`To scale the model, we first need to know it's size ratio to a size of 6.
`Here's how to get that:
s#=6.0/Object Size X(obj)
`All that is left to do is scale the model. Remember that Scale Object uses an integer scale that represents
`percentage values (100 = 100% = 1, 50 = 50% = .5, 1 = 1% = .01, and so on). So, the scale (s#) needs to be
`multiplied by 100.
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
`Each invader is 6x6x6 in size. Our viewable area is a little less than 100 x 100
`In order to fit the invaders evenly, we can divide the area we have to work with by
`the number of colums - 9. Since 9 goes into 90 evenly, lets use that. The result is 10.
`Here is the formula to calculate the X-position of the invaders every 10 spaces from
`-45x to +45x
x=(i*10)-45
`Using the same spacing of 10 for the Y-position, and the invaders need to be well up off
`the ground, here is the formula to calculate the Y-position.
y=(r*10)+50
`Position the invader
Position Object obj,x,y,0
Next i
Next r
` this variable will be used to store the objid of the first alien object
Global AlienObjIdStart
AlienObjIdStart = obj - 53
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 30
Display_LoadBar(1)
` *************************************************************************
` Prepare Enemy Bullets
` - last changed: 04.Jan.2007
` create a UDT for the Enemy Bullets
` (a description of UDT's and what they are, can be found further down.)
Type tEnemyBullet
` create a variable to store the bullets Object ID
ObjId AS INTEGER
` create a variable to store the alien Object ID
AlienId AS INTEGER
` create timer variables :: that way we can control how often
` the enemies are to fire a bullet.
TimeControl AS BOOLEAN ` here we will store a 1 for reset StartSecond or 0 for Start Counting Seconds
StartSecond AS INTEGER ` here we will store the starting position in time
CurSecond AS INTEGER ` here we will store the current position in time
FireHere AS INTEGER ` here we will store on which second the Alien should fire his bullet
` create a Active variable :: that way we can see if the bullet
` is destroyed. if it's destroyed, it means the alien
` connected to this bullet will be able to fire a new one.
Active AS INTEGER
` create position and rotation angle variables for the bullet
` this way we can easily control the movement and angle of the bullet
` --it would also be possible to make some sinus/cosinus effects
` if we want to make Easy - Hard difficulties. that would be neat.
xPos AS FLOAT
yPos AS FLOAT
zPos AS FLOAT
xAng AS FLOAT
yAng AS FLOAT
zAng AS FLOAT
EndType
` create the array where we store the Enemy Bullet information
` remember we have 6*9 aliens, yea? that means 54 enemies
Dim EnemyBullet(53) AS tEnemyBullet
`Counter to keep track of last bullet
Dim ALienFireFlag(0)
` now create all the bullets
For bullet = 1 to 54
` we set ID to bullet-1 because our Array "EnemyBullet" is counting
` from 0-53, and not 1-54
arrayPosition = bullet -1
` get the object nr for this bullet
obj = FreeObject()
` make a box object for the bullet
Load Object "DataMissile.x",obj
s#=6.0/Object Size Y(obj)
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
Set Object Emissive obj,Rgb(50,0,0)
Hide Object obj
` update the EnemyBullet array
alien = AlienObjIdStart + arrayPosition
EnemyBullet( arrayPosition ).ObjId = obj ` the object nr
EnemyBullet( arrayPosition ).AlienId = alien ` the alien object nr
EnemyBullet( arrayPos ).FireHere = rnd(20) ` set FireHere to a random of 20 seconds
Next bullet
` --------------------------------------- end of Prepare Enemy Bullets
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 50
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM BARRIERS
REM -----------------------------------------------------------------------------------
`Load the shield barriers (same process as above).
Dim Barriers(2)
For i = 0 to 2
obj=FreeObject()
Barriers(i)=obj
Load Object "DataBarrier.x",Barriers(i)
s#=12.0/Object Size X(obj)
Scale Object Barriers(i),s#*100.0,s#*100.0,s#*100.0
x#=(i*35)-35
Position Object Barriers(i),x#,20,0
Next i
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 60
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM CREATE PLAYER
REM -----------------------------------------------------------------------------------
`Load player's missile
`Use an array for the variable since it is global (A global variable could be used, but later you will see why
`there is a preference for Arrays over Global Variables).
Dim Missile(0)
`Assign a free object to Missile
Missile(0)=FreeObject()
`Load the missile model
Load Object "DataMissile.x",Missile(0)
`Calculate the scale
s#=6.0/Object Size Y(Missile(0))
`Scale the missile.
Scale Object Missile(0),s#*100.0,s#*100.0,s#*100.0
`Hide the missile
Hide Object Missile(0)
`Create a playertype "user defined array"
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 70
Display_LoadBar(1)
type PlayerType
`this will store the id of the object that is associated with the player, so we can move it later
ObjId as integer
`to keep track of the x position of the player, we are creating these as floats in order
`to maintain a smooth movement. With integers the player would "jump" from one integer to another
`adding the "#" to the end defines it as a float, to use this variable later you must use the "#"
xpos#
`we also need to keep track of the y position of the player, the player's "up and down"
`movement will be limited to keep them behind the bariers
ypos#
`this stores the distance that the player can move in one frame
speed#
` with the following variable stored, we can apply a neat rotation effect to the ship while
` it is moving left or right
yang#
endtype
REMSTART
Using "User Defined Types"
The code
type TheBestTypeEver
endtype
Creates a User Defined Type (reffered to as a UDT) with the name "TheBestTypeEver"
This by itself is a empty type, the entire point of using UDTs is to simplify code
by allowing sub variables for a "Parent" variable
to add sub variables to a UDT you simply type them out in between the 'type' and 'endtype'
type TheBestTypeEver
TheBestVariableEver as integer
TheOtherVariable as string
endtype
This adds two sub variables to the UDT, a integer (only "whole" numbers such as 1, 2, 3, 4, -17, 42,.. etc)
named "TheBestVariableEver", and a string named "TheOtherVariable"
Note: if you declare a variable without telling the compiler what type it is, it will
automatically be declared as an integer. So we could have left off the "as integer"
A UDT by itself does nothing at all, you can't assign something to it (such as TheBestTypeEver = 1337)
You have to declare a variable and define it as the type that your UDT is
A UDT is really just a fancy type of variable that you've created custom made for your game
It's used in exactly the same way that you would when you are declaring a variable as a string, float, or integer
bob as integer
bob as TheBestTypeEver
however, UDTs are different than normal variable types because they have sub variables
with "bob as int" you assign a value to it in the normal way, bob = 12
When you have a variable declared as a UDT you have to pick a sub variable to work with
It's easy to tell the compiler which sub variable you want, you just add a period (".")
to the end, then add the name of the sub variable.
so, going with our example, bob as TheBestTypeEver
bob.TheBestVariableEver = 20
bob.TheOtherVariable = "hello world"
That sets the two sub variables in bob to 20, and "hello world" (terribly original i know)
you can then access them just like normal variables
print bob.TheBestVariableEver
print bob.TheOtherVariable
Note: You CANNOT assign anything to the "parent" variable by itself. So bob = 20 would NOT work.
you need to pick a sub variable to assign to.
REMEND
`Create array Player() so it is global (A global variable can be used, but we will cover later why
`there is a preference for arrays over global variables).
`This array is created as a PlayerType array, and has all the attributes of the PlayerType User Defined Type
Dim Player(0) as PlayerType
`Get a free object for the player's ship
Player(0).ObjId = FreeObject()
`Load in the player's ship
Load Object "DataShip.x",Player(0).Objid
`Calculate the ratio of the ship to size 6 (making the player a little larger than the aliens)
s#=6.0/Object Size X(Player(0).ObjId)
`Scale the player's ship
Scale Object Player(0).ObjId,s#*100.0,s#*100.0,s#*100.0
`Place the player's ship
Position Object Player(0).ObjId,0,0,0
`-------------------------
`Player_Controls variables
`-------------------------
`set the player's speed
Player(0).speed# = 0.4
`sets the farthest position the player can travel to the right
rightside# = 77.0
`sets the farthest position the player can travel to the left
leftside# = -77.0
`sets the farthest position the player can travel upwards
topside# = 12.0
`sets the farthest position the player can travel downwards
bottomside# = -9.0
`-------------------------
`End Of Player_Controls variables
`-------------------------
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 80
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM PARTICLES
REM -----------------------------------------------------------------------------------
`Particle Data
`Use this function to set up the particle data, otherwise they wont work.
setupParticles()
`Create a particle emmiter
prtShip=addParticles(0,0,0,60,60)
setParticleSize(prtShip,1.8,-0.03)
colorParticles(prtShip,255,0,0)
setParticleGhost(prtShip,1)
setParticleGravity(prtShip,0,-0.1,0)
`Call this function to actually make all the particles
createParticles()
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 90
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM PRE MAIN LOOP
REM -----------------------------------------------------------------------------------
Position Camera 0,50,-100
ink rgb(255,255,255),1
backdrop on
REM -----------------------------------------------------------------------------------
REM MAIN GAME LOOP
REM -----------------------------------------------------------------------------------
disable escapekey : while escapekey()=0
gosub player_controls
Move_Aliens()
GoSub alien_fire_control
Control_Missile()
text 1,1,"fps :"+STR$(screen FPS())
fastSync
endwhile
End
`For more information on For/Next/Loops, refer to DBPro Help: Principles / Common Statements
`For more information on For/Next/Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`For more information on Formulas, refer to DBPro Help: Principles / Datatypes and variables
REM -----------------------------------------------------------------------------------
REM
REM FUNCTIONS DECLARATION
REM
REM -----------------------------------------------------------------------------------
`A quick little function to check for the first free object number.
Function FreeObject()
`Start at object number 1
i=1
`A simple loop
Do
`If the object does not exist (=0), then exit the loop. "i" will be the valid object number
If Object Exist(i)=0 Then Exit
`Move on to the next object number
i=i+1
`Close the loop
Loop
`End the function and return the valid object number
Endfunction i
`For more information on Functions and returning values, refer to DBPro Help (press F1): Principles / Functions
`For more information on Loops, refer to DBPro Help: Principles / Common Statements
`For more information on Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`handles all the input from the player
player_controls:
` while not left or right key is pressed, we rotate the ship back to its original angle
Player(0).yang# = curvevalue( 0.0, object angle y( Player(0).ObjId ), 50.0 )
`check if the player is hitting the right key, if they are we want to let them move right
`as long as they are not hitting the right side of the screen
if rightkey() = 1
`move the player to the right at the preset speed
inc Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the right side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# > rightside# then Player(0).xpos# = rightside#
` apply rotation to the ship while we move right
Player(0).yang# = curvevalue( -40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check in the same way for the left key
if leftkey() = 1
`move the player to the left at the preset speed
dec Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the left side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# < leftside# then Player(0).xpos# = leftside#
` apply rotation to the ship while we move left
Player(0).yang# = curvevalue( 40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check if the player is hitting the upkey, if they are we want to let them move up
`but not past the barriers
if upkey() = 1
`move the player upwards at the preset speed
inc Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the barriers
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# > topside# then Player(0).ypos# = topside#
endif
`check if the player is hitting the downkey, if they are we want to let them move down
`but not past the bottom of the screen
if downkey() = 1
`move the player downwards at the preset speed
dec Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the bottom of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# < bottomside# then Player(0).ypos# = bottomside#
endif
`now that we've adjusted the position of the player accordingly, we need to update the object
`that is representing the player
position object Player(0).ObjId, Player(0).xpos#, Player(0).ypos#, 0
rotate object Player(0).ObjId, 0, Player(0).yang#, 0
`Update Particles
positionParticles(prtShip,Player(0).xpos#+plusmin(rnd(1.0)), Player(0).ypos#, 0)
controlParticles()
return
alien_fire_control:
`Decrease the AlienFireFlag counter if it is greater than 0
AlienFireFlag(0)=AlienFireFlag(0)-(AlienFireFlag(0)>0)
` start a for-next loop :: go through each of the bullets
For arrayPos = 0 To 53
obj = EnemyBullet( arrayPos ).ObjId ` get object nr. for this bullet
alien = EnemyBullet( arrayPos ).AlienId ` get object nr. of the alien
` If TimeControl = 0 And the bullet is inactive..
If EnemyBullet( arrayPos ).TimeControl = 0 And EnemyBullet( arrayPos ).Active = 0
EnemyBullet( arrayPos ).StartSecond = Timer() ` reset StartSecond
EnemyBullet( arrayPos ).FireHere = 5 + rnd(20) ` set FireHere to 3 seconds + a random of 10 seconds
EnemyBullet( arrayPos ).TimeControl = 1 ` set TimeControl to 1
EndIf
` If TimeControl = 1..
If EnemyBullet( arrayPos ).TimeControl = 1 and AlienFireFlag(0)=0
` increase .CurSecond by 1 each secound
EnemyBullet( arrayPos ).CurSecond = (Timer() - EnemyBullet( arrayPos ).StartSecond) / 1000
` if .CurSecond > FireHere, that means the alien should fire his bullet
If EnemyBullet( arrayPos ).CurSecond > EnemyBullet( arrayPos ).FireHere
EnemyBullet( arrayPos ).Active = 1
EnemyBullet( arrayPos ).TimeControl = 0
`Set the AlienFireFlag counter
AlienFireFlag(0)=60:`Sets the fire rate to approx. 1 second (60 frames at 60 FPS)
EndIf
EndIf
` if the bullet is active.. [stage 1] - update
If EnemyBullet( arrayPos ).Active = 1
Show Object obj
` update bullet position
EnemyBullet( arrayPos ).xPos = Object Position X( alien )
EnemyBullet( arrayPos ).yPos = Object Position Y( alien )
EnemyBullet( arrayPos ).zPos = Object Position Z( alien )
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
` go to stage 2 :: move the bullet
EnemyBullet( arrayPos ).Active = 2
EndIf
` if the bullet is active.. [stage 2] - move
If EnemyBullet( arrayPos ).Active = 2
` apply rotation to the bullet
EnemyBullet( arrayPos ).xAng = 0
EnemyBullet( arrayPos ).yAng = 0
EnemyBullet( arrayPos ).zAng = 180
` update position of the bullet
EnemyBullet( arrayPos ).xPos = EnemyBullet( arrayPos ).xPos + sin( EnemyBullet( arrayPos ).zAng ) * 1.3
EnemyBullet( arrayPos ).yPos = EnemyBullet( arrayPos ).yPos + cos( EnemyBullet( arrayPos ).zAng ) * 1.3
` check if the bullet is outside the screen
If EnemyBullet( arrayPos ).yPos < bottomside#
Hide Object obj
EnemyBullet( arrayPos ).Active = 0
EndIf
` update object
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
Rotate Object obj, EnemyBullet( arrayPos ).xAng, EnemyBullet( arrayPos ).yAng, EnemyBullet( arrayPos ).zAng
EndIf
Next arrayPos
return
Function Move_Aliens()
Dim NextAlienMove(0):`Tracks how long until the next alien move
Dim AlienMoveDirection(0):`Tracks which direction the aliens are moving in
Dim AliensXY(1):`Tracks the Aliens position as a group. 0=X, 1=Y
`Start by moving to the right.
If AlienMoveDirection(0)=0 Then AlienMoveDirection(0)=1
`Set the initial Alien Group position if it is not already set
If AliensXY(1)=0
AliensXY(0)=-45
AliensXY(1)=50
Endif
`Decrease NextAlienMove(0) if it is greater than 0
NextAlienMove(0)=NextAlienMove(0)-(NextAlienMove(0)>0)
`If NextAlienMove(0)>0 then exit the function, since no move will be made
If NextAlienMove(0)>0 Then ExitFunction
`Start by counting how many aliens still are alive. This will determine alien speed.
AlienCount=0
`Set the left & right invader trackers so we can determine the left & right most invaders
LeftInvader=8:RightInvader=0
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
If Object Exist(Invaders(r,i))
AlienCount=AlienCount+1
if i<LeftInvader then LeftInvader=i
if i>RightInvader then RightInvader=i
Endif
Next i
Next r
`A move will be processed, so set the NextAlienMove(0) counter now
NextAlienMove(0)=AlienCount
MoveDown=0
`Check to see if the aliens are to the far right.
If AlienMoveDirection(0)=1
If (RightInvader*10)+AliensXY(0)>70
MoveDown=1
Endif
Endif
`Check to see if the aliens are to the far left
If AlienMoveDirection(0)=-1
If (LeftInvader*10)+AliensXY(0)<-70
MoveDown=1
Endif
Endif
`Calculate the new alien group position
If MoveDown>0
`Move the alien group down
AliensXY(1)=AliensXY(1)-2
`And change directions
AlienMoveDirection(0)=-AlienMoveDirection(0)
Else
AliensXY(0)=AliensXY(0)+AlienMoveDirection(0)*2
Endif
`Reposition Aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
x=AliensXY(0)+(i*10)
y=AliensXY(1)+(r*10)
If Object Exist(Invaders(r,i))
Position Object Invaders(r,i),x,y,0
Endif
Next i
Next r
Endfunction
`Particle Data
function plusmin(num#)
rand=rnd(10)
if rand>5 then ret#=num#*-1 else ret#=num#
endfunction ret#
function setupParticles()
dim prtsys(0) as tprtsys
endfunction
function createParticles()
count=array count(prtsys())
cmax=0
for c=1 to count
if prtsys(c).count>cmax then cmax=prtsys(c).count
next c
dim prtdat(array count(prtsys()),cmax) as tprtdat
for c=1 to array count(prtsys())
for o=1 to prtsys(c).count
prtdat(c,o).obj=val(getToken(prtsys(c).objc,",",o))
next o
next c
endfunction count
function addParticles(x#,y#,z#,life,count)
array insert at bottom prtsys()
num=array count(prtsys())
prtsys(num).pos.x=x#+plusmin(rnd(0.2))
prtsys(num).pos.y=y#+plusmin(rnd(0.2))
prtsys(num).pos.z=z#+plusmin(rnd(0.2))
prtsys(num).life=life
prtsys(num).count=count
for c=1 to prtsys().count
obj=zp_freeobject() : make object plain obj,100,100
set object transparency obj,3
disable object zwrite obj
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
obj$=obj$+str$(obj)+","
lifep=rnd(life)
life$=life$+str$(lifep)+","
next c
prtsys(num).objc=obj$
prtsys(num).lifep=life$
endfunction num
function setParticleSpeed(num,speed#)
prtsys(num).speed=speed#
endfunction
function setParticleSize(num,size#,sizep#)
prtsys(num).sizei=size#
prtsys(num).sizep=sizep#
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
scale object obj,size#,size#,1
next c
endfunction
function positionParticles(num,x#,y#,z#)
prtsys(num).pos.x=x#
prtsys(num).pos.y=y#
prtsys(num).pos.z=z#
endfunction
function setParticleGravity(num,x#,y#,z#)
prtsys(num).grav.x=x#
prtsys(num).grav.y=y#
prtsys(num).grav.z=z#
endfunction
function colorParticles(num,r,g,b)
col=rgb(r,g,b)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
color object obj,col
next c
endfunction
function setParticleGhost(num,flag)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
if flag=1
ghost object on obj
else
ghost object off obj
endif
next c
endfunction
function controlParticles()
for num=1 to array count(prtsys())
for c=1 to prtsys(num).count
prtsys(num).lifec=prtsys(num).lifec+1
prtsys(num).lifecount=prtsys(num).life-prtsys(num).lifec
obj=prtdat(num,c).obj
angx#=angx#+((360/prtsys(num).count)*c)
angy#=angy#+((360/prtsys(num).count)*c)
sx#=object position x(obj)+(sin(angy#)*cos(angx#)*prtsys(num).speed)
sy#=object position y(obj)-(sin(angx#)*prtsys(num).speed)
sz#=object position z(obj)+(cos(angy#)*cos(angx#)*prtsys(num).speed)
sx#=sx#+prtsys(num).grav.x
sy#=sy#+prtsys(num).grav.y
sz#=sz#+prtsys(num).grav.z
size#=prtsys(num).sizei+(prtsys(num).sizep*prtsys(num).lifecount)
position object obj,sx#,sy#,sz#
scale object obj,size#,size#,1
if prtsys(num).lifec>prtsys(num).life
prtsys(num).lifec=prtsys(num).lifec*0
size#=prtsys(num).sizei
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
endif
next c
next num
endfunction
function getEmitterCount()
count=array count(prtsys())
endfunction count
function getParticleCount(num)
count=prtsys(num).count
endfunction count
function zp_freeobject()
repeat
inc i
until object exist(i)=0
endfunction i
function getTokens(st$,sp$)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until counter>len(nst$)
tokens=splits-1
endfunction tokens
function getToken(st$,sp$,num)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until splits=num or counter>len(st$)
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$<>sp$ then token$=token$+cur$
until cur$=sp$ or counter>len(st$)
endfunction token$
`Player Missile Control
Function Control_Missile()
`Check to see of the Missile is hidden. If the missile is hidden, then it has not been launched.
If Object Visible(Missile(0))=0
`The missile has not been launched; check to see if the player is pressing the space bar.
If SpaceKey()
`The spacekey is pressed, so launch the missile
`Start by showing the missile
Show Object Missile(0)
`Next, position the missile at the ship's location
Position Object Missile(0),Object Position X(Player(0).ObjID),Object Position Y(Player(0).ObjID), Object Position Z(Player(0).ObjID)
`Make sure the missile is pointing in the correct direction - up.
Rotate Object Missile(0),0,0,0
Else
`The missile has not been launched and the player is not pressing the space key - so nothing left to do.
Exitfunction
Endif
Endif
`If the code gets to these lines in this function, the missile has been launched. So, let's move it.
Move Object Up Missile(0),3
`Next, check to see if the missile has gone off the screen.
If Object Screen Y(Missile(0))<0
`The missile has gone off the screen, so let's hide it again. Remember, hiding it will stop it and have
`it ready for re-launch.
Hide Object Missile(0)
Endif
Endfunction
`UDTs
type vec3
x as float
y as float
z as float
endtype
type tprtsys
objc as string
pos as vec3
life as integer
lifec as integer
lifecount as integer
lifep as string
count as integer
speed as float
sizei as float
sizep as float
grav as vec3
endtype
type tprtdat
obj as integer
endtype
function Display_LoadBar(Num)
box LoadBar(Num).x1,LoadBar(Num).y1,LoadBar(Num).x2,LoadBar(Num).y2,rgb(192,192,192),rgb(92,92,92),rgb(192,192,192),rgb(92,92,92)
ink rgb(32,32,32),1
box LoadBar(Num).x1+LoadBar(Num).bdr,LoadBar(Num).y1+LoadBar(Num).bdr,LoadBar(Num).x2-LoadBar(Num).bdr,LoadBar(Num).y2-LoadBar(Num).bdr
if LoadBar(Num).length > LoadBar(Num).width
box LoadBar(Num).x1+LoadBar(Num).bdr+1,LoadBar(Num).y1+LoadBar(Num).bdr+1,LoadBar(Num).x1+LoadBar(Num).width-LoadBar(Num).bdr-1,LoadBar(Num).y2-LoadBar(Num).bdr-1,rgb(255,255,0),rgb(255,255,0),rgb(0,255,0),rgb(0,255,0)
else
box LoadBar(Num).x1+LoadBar(Num).bdr+1,LoadBar(Num).y1+LoadBar(Num).bdr+1,LoadBar(Num).x1+LoadBar(Num).length-1,LoadBar(Num).y2-LoadBar(Num).bdr-1,rgb(255,255,0),rgb(255,255,0),rgb(0,255,0),rgb(0,255,0)
endif
ink rgb(255,255,55),1
text LoadBar(Num).x1+LoadBar(Num).bdr+2,LoadBar(Num).y1 + LoadBar(Num).height+2,LoadBar(Num).name$
fastsync
endfunction

|
Back to top
 |
|
indi
User
Joined: Mon Aug 26th 2002
Location: Earth, Brisbane, Australia
|
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
|
Posted: 5th Jan 2007 08:00 Edited: 5th Jan 2007 08:04 |
| link | toggle |
Thanks for the update and cleanup Indi! The link is great too.
Here are some of the things that should get done today or tomorrow:
Collision and alien death.
Add sound effects.
I will get to the sound effects tonight if no one else has by then.
@Batvink: I am hoping the movement in there now works. If not, good ol' collision box will definitely do the trick. Thanks.

Open MMORPG: It's your game!
|
Back to top
 |
|
indi
User
Joined: Mon Aug 26th 2002
Location: Earth, Brisbane, Australia
|
cheers mate, it looks really good so far.
are we going to use the 3d blocks for the barriers?
when is ruccus delivering the awesome models?
we need a score board as well.
I think a simple text statement would work for that little bit of text vs 3d.
what was the font they all used for the score back in the day?
this website appears to have the font matched perfectly.
http://www.spaceinvaders.de/
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
|
Posted: 7th Jan 2007 00:12 Edited: 7th Jan 2007 00:13 |
| link | toggle |
I've added a bit of collision and some sound effects. We nearly have a game. Only three more days to go.
Indi, I was playing with the progress bar a bit and it's a bit messed up. Can you take a look at the mods I made and fix it up some? I think you can see what I was going for. I did not change the function itself, just the function calls.
At this point, the code is a bit tough to read with all the comments in there, so I am going to thin them out quite a bit. Anyone that wants to know what was going on before can simply download a previous version of the code.
Somebody want to code resetting the aliens when they are all dead?

Open MMORPG: It's your game!
|
Back to top
 |
|
indi
User
Joined: Mon Aug 26th 2002
Location: Earth, Brisbane, Australia
|
It was the length of the defined bars width, set to higher then 100.
I reset it to 100 and then reshuffled the areas it displays in.
If you want it to be smoother, or more accurate as things load, perhaps create another bar under that one for specific loading areas you had it in.
I removed the smooth loading areas in this instance.
It loads right up to 100% across the bar in this source code below.
Define the bar as 2 types within the one, if you want that feature and use the 2nd bar in those areas.
It looks really good so far mate. The sounds really help make it.
I'm heading off for a holiday tomorrow night so I wont have much time to see the result until i'm back from holidays.
+ Code Snippet
REM -----------------------------------------------------------------------------------
REM SPACE INVADERS
REM Community Based Project
REM Language : DBP version 6.3
REM Jan 05 2007
REM Authors
REM Ridii
REM indi
REM Ruccus
REM Lukas W
REM
REM Program Notes
REM Added Loading bar
REM Cleaned up the code a little with clear titles
REM
REM -----------------------------------------------------------------------------------
REM -----------------------------------------------------------------------------------
REM SETUP GAME SHELL
REM -----------------------------------------------------------------------------------
Sync On : Sync Rate 60 : Autocam Off
color backdrop rgb(0,0,0)
REM -----------------------------------------------------------------------------------
REM CREATE LOADING BAR
REM -----------------------------------------------------------------------------------
type LoadBar
name$ as string
x1 as integer
y1 as integer
x2 as integer
y2 as integer
bdr as integer
width as integer
height as integer
length as integer
endtype
dim LoadBar(1) as LoadBar
LoadBar(1).height = 8
LoadBar(1).width = 100
LoadBar(1).x1 = 15
LoadBar(1).y1 = 15
LoadBar(1).x2 = LoadBar(1).x1 + LoadBar(1).width
LoadBar(1).y2 = LoadBar(1).y1 + LoadBar(1).height
LoadBar(1).bdr = 1
LoadBar(1).name$ = "Loading Data"
LoadBar(1).length = 0
REM -----------------------------------------------------------------------------------
REM INIT SCREEN
REM -----------------------------------------------------------------------------------
Load Image "data\space_invaders_splashscreen.png",1,1
Sprite 1,0,0,1
Size Sprite 1,Screen Width(),Screen Height()
Sprite 1,-800,-600,1
Hide Sprite 1
`Initialize the attached buttons functionality
Initialize_Buttons
`Make_Button(File as String,xpos as float, ypos as float,RVal as Integer,AnimFlag)
Make_Button("data\Start.png",screen width()/2-32,screen height()/2-66,1,1)
Show Sprite 1
Do
Paste Sprite 1,0,0
`Check to see if the button is "active" (mouse pointer is over sprite) and for left click
If Get_Button()=1 And MouseClick()=1 Then Exit
fastSync
Loop
hide sprite 1
`Clean up the buttons.
Clear_Buttons
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 10
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM
REM CREATE ALIENS
REM
REM -----------------------------------------------------------------------------------
`There will be 6 rows of 9 invaders to start with.
`The player's ship will be at the bottom, like so.
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` X X X X X X X X X
` /
`Create an array to store the invader information. This is not needed now, but will be needed later.
`This is a 2-dimensional array. It is 6 by 9 in size (or 6x9=54). 5,8 was used instead of 53 to make
`it easier to determine rows and columns. It is normally better to use a single array of 53.
Dim Invaders(5,8)
`Load the aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
`Get a free object ID for our invader
obj=FreeObject()
`Store the Object ID in the Invaders array
Invaders(r,i)=obj
`Load the alien model
Load Object "Data\Alien1.x",obj
`Next, we need to make sure our model is the right size. For this, the model needs to be scaled.
`Roughly, we want a model that is 6x6x6 (just what is picked and looks about right).
`To scale the model, we first need to know it's size ratio to a size of 6.
`Here's how to get that:
s#=5.0/Object Size X(obj)
`All that is left to do is scale the model. Remember that Scale Object uses an integer scale that represents
`percentage values (100 = 100% = 1, 50 = 50% = .5, 1 = 1% = .01, and so on). So, the scale (s#) needs to be
`multiplied by 100.
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
`Each invader is 6x6x6 in size. Our viewable area is a little less than 100 x 100
`In order to fit the invaders evenly, we can divide the area we have to work with by
`the number of colums - 9. Since 9 goes into 90 evenly, lets use that. The result is 10.
`Here is the formula to calculate the X-position of the invaders every 10 spaces from
`-45x to +45x
x=(i*10)-45
`Using the same spacing of 10 for the Y-position, and the invaders need to be well up off
`the ground, here is the formula to calculate the Y-position.
y=(r*10)+50
`Position the invader
Position Object obj,x,y,0
Set Object Collision To Boxes obj
Next i
Next r
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 20
Display_LoadBar(1)
Load Sound "data\fastinvader1.wav",1
Load Sound "data\invaderkilled.wav",3
` this variable will be used to store the objid of the first alien object
Global AlienObjIdStart
AlienObjIdStart = obj - 53
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 30
Display_LoadBar(1)
` *************************************************************************
` Prepare Enemy Bullets
` - last changed: 04.Jan.2007
` create a UDT for the Enemy Bullets
` (a description of UDT's and what they are, can be found further down.)
Type tEnemyBullet
` create a variable to store the bullets Object ID
ObjId AS INTEGER
` create a variable to store the alien Object ID
AlienId AS INTEGER
` create timer variables :: that way we can control how often
` the enemies are to fire a bullet.
TimeControl AS BOOLEAN ` here we will store a 1 for reset StartSecond or 0 for Start Counting Seconds
StartSecond AS INTEGER ` here we will store the starting position in time
CurSecond AS INTEGER ` here we will store the current position in time
FireHere AS INTEGER ` here we will store on which second the Alien should fire his bullet
` create a Active variable :: that way we can see if the bullet
` is destroyed. if it's destroyed, it means the alien
` connected to this bullet will be able to fire a new one.
Active AS INTEGER
` create position and rotation angle variables for the bullet
` this way we can easily control the movement and angle of the bullet
` --it would also be possible to make some sinus/cosinus effects
` if we want to make Easy - Hard difficulties. that would be neat.
xPos AS FLOAT
yPos AS FLOAT
zPos AS FLOAT
xAng AS FLOAT
yAng AS FLOAT
zAng AS FLOAT
EndType
` create the array where we store the Enemy Bullet information
` remember we have 6*9 aliens, yea? that means 54 enemies
Dim EnemyBullet(53) AS tEnemyBullet
`Counter to keep track of last bullet
Dim ALienFireFlag(0)
` now create all the bullets
For bullet = 1 to 54
` we set ID to bullet-1 because our Array "EnemyBullet" is counting
` from 0-53, and not 1-54
arrayPosition = bullet -1
` get the object nr for this bullet
obj = FreeObject()
` make a box object for the bullet
Load Object "Data\Missile.x",obj
s#=4.0/Object Size Y(obj)
Scale Object obj,s#*100.0,s#*100.0,s#*100.0
Set Object Emissive obj,Rgb(50,0,0)
Hide Object obj
Set Object Collision To Boxes obj
` update the EnemyBullet array
alien = AlienObjIdStart + arrayPosition
EnemyBullet( arrayPosition ).ObjId = obj ` the object nr
EnemyBullet( arrayPosition ).AlienId = alien ` the alien object nr
EnemyBullet( arrayPos ).FireHere = rnd(20) ` set FireHere to a random of 20 seconds
Next bullet
` --------------------------------------- end of Prepare Enemy Bullets
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 40
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM BARRIERS
REM -----------------------------------------------------------------------------------
`Load the shield barriers (same process as above).
Dim Barriers(2)
For i = 0 to 2
obj=FreeObject()
Barriers(i)=obj
Load Object "Data\Barrier.x",Barriers(i)
s#=12.0/Object Size X(obj)
Scale Object Barriers(i),s#*100.0,s#*100.0,s#*100.0
x#=(i*35)-35
Position Object Barriers(i),x#,20,0
Set Object Collision To Boxes Barriers(i)
Next i
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 50
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM CREATE PLAYER
REM -----------------------------------------------------------------------------------
`Load player's missile
`Use an array for the variable since it is global (A global variable could be used, but later you will see why
`there is a preference for Arrays over Global Variables).
Dim Missile(1)
`Assign a free object to Missile
Missile(0)=FreeObject()
`Load the missile model
Load Object "Data\Missile.x",Missile(0)
`Calculate the scale
s#=6.0/Object Size Y(Missile(0))
`Scale the missile.
Scale Object Missile(0),s#*100.0,s#*100.0,s#*100.0
`Hide the missile
Hide Object Missile(0)
Set Object Collision To Boxes Missile(0)
Create Animated Sprite 11,"Data\Explode.bmp",4,1,11
Sprite 11,-512,0,11
Size Sprite 11,50,50
Set Image Colorkey 0,0,0
Set Sprite 11,1,1
`Create a playertype "user defined array"
Load Sound "Data\shoot.wav",2
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 60
Display_LoadBar(1)
type PlayerType
`this will store the id of the object that is associated with the player, so we can move it later
ObjId as integer
`to keep track of the x position of the player, we are creating these as floats in order
`to maintain a smooth movement. With integers the player would "jump" from one integer to another
`adding the "#" to the end defines it as a float, to use this variable later you must use the "#"
xpos#
`we also need to keep track of the y position of the player, the player's "up and down"
`movement will be limited to keep them behind the bariers
ypos#
`this stores the distance that the player can move in one frame
speed#
` with the following variable stored, we can apply a neat rotation effect to the ship while
` it is moving left or right
yang#
`FireControl is how long before the player can fire again.
FireControl
endtype
REMSTART
Using "User Defined Types"
The code
type TheBestTypeEver
endtype
Creates a User Defined Type (reffered to as a UDT) with the name "TheBestTypeEver"
This by itself is a empty type, the entire point of using UDTs is to simplify code
by allowing sub variables for a "Parent" variable
to add sub variables to a UDT you simply type them out in between the 'type' and 'endtype'
type TheBestTypeEver
TheBestVariableEver as integer
TheOtherVariable as string
endtype
This adds two sub variables to the UDT, a integer (only "whole" numbers such as 1, 2, 3, 4, -17, 42,.. etc)
named "TheBestVariableEver", and a string named "TheOtherVariable"
Note: if you declare a variable without telling the compiler what type it is, it will
automatically be declared as an integer. So we could have left off the "as integer"
A UDT by itself does nothing at all, you can't assign something to it (such as TheBestTypeEver = 1337)
You have to declare a variable and define it as the type that your UDT is
A UDT is really just a fancy type of variable that you've created custom made for your game
It's used in exactly the same way that you would when you are declaring a variable as a string, float, or integer
bob as integer
bob as TheBestTypeEver
however, UDTs are different than normal variable types because they have sub variables
with "bob as int" you assign a value to it in the normal way, bob = 12
When you have a variable declared as a UDT you have to pick a sub variable to work with
It's easy to tell the compiler which sub variable you want, you just add a period (".")
to the end, then add the name of the sub variable.
so, going with our example, bob as TheBestTypeEver
bob.TheBestVariableEver = 20
bob.TheOtherVariable = "hello world"
That sets the two sub variables in bob to 20, and "hello world" (terribly original i know)
you can then access them just like normal variables
print bob.TheBestVariableEver
print bob.TheOtherVariable
Note: You CANNOT assign anything to the "parent" variable by itself. So bob = 20 would NOT work.
you need to pick a sub variable to assign to.
REMEND
`Create array Player() so it is global (A global variable can be used, but we will cover later why
`there is a preference for arrays over global variables).
`This array is created as a PlayerType array, and has all the attributes of the PlayerType User Defined Type
Dim Player(0) as PlayerType
`Get a free object for the player's ship
Player(0).ObjId = FreeObject()
`Load in the player's ship
Load Object "Data\Ship.x",Player(0).Objid
`Calculate the ratio of the ship to size 6 (making the player a little larger than the aliens)
s#=6.0/Object Size X(Player(0).ObjId)
`Scale the player's ship
Scale Object Player(0).ObjId,s#*100.0,s#*100.0,s#*100.0
`Place the player's ship
Position Object Player(0).ObjId,0,0,0
Set Object Collision To Boxes Player(0).ObjID
Set Object Light Player(0).ObjId,0
`-------------------------
`Player_Controls variables
`-------------------------
`set the player's speed
Player(0).speed# = 0.4
`sets the farthest position the player can travel to the right
rightside# = 77.0
`sets the farthest position the player can travel to the left
leftside# = -77.0
`sets the farthest position the player can travel upwards
topside# = 12.0
`sets the farthest position the player can travel downwards
bottomside# = -9.0
`-------------------------
`End Of Player_Controls variables
`-------------------------
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 60
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM PARTICLES
REM -----------------------------------------------------------------------------------
`Particle Data
`Use this function to set up the particle data, otherwise they wont work.
setupParticles()
`Create a particle emmiter
prtShip=addParticles(0,0,0,60,60)
setParticleSize(prtShip,1.8,-0.03)
colorParticles(prtShip,255,0,0)
setParticleGhost(prtShip,1)
setParticleGravity(prtShip,0,-0.1,0)
`Call this function to actually make all the particles
createParticles()
REM -----------------------------------------------------------------------------------
REM UPDATING LOAD BAR
REM -----------------------------------------------------------------------------------
LoadBar(1).length = 100
Display_LoadBar(1)
REM -----------------------------------------------------------------------------------
REM PRE MAIN LOOP
REM -----------------------------------------------------------------------------------
Position Camera 0,50,-100
ink rgb(255,255,255),1
backdrop on
REM -----------------------------------------------------------------------------------
REM MAIN GAME LOOP
REM -----------------------------------------------------------------------------------
disable escapekey : while escapekey()=0
gosub player_controls
Move_Aliens()
GoSub alien_fire_control
Control_Missile()
text 1,1,"fps :"+STR$(screen FPS())
fastSync
endwhile
End
`For more information on For/Next/Loops, refer to DBPro Help: Principles / Common Statements
`For more information on For/Next/Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`For more information on Formulas, refer to DBPro Help: Principles / Datatypes and variables
REM -----------------------------------------------------------------------------------
REM
REM FUNCTIONS DECLARATION
REM
REM -----------------------------------------------------------------------------------
`A quick little function to check for the first free object number.
Function FreeObject()
`Start at object number 1
i=1
`A simple loop
Do
`If the object does not exist (=0), then exit the loop. "i" will be the valid object number
If Object Exist(i)=0 Then Exit
`Move on to the next object number
i=i+1
`Close the loop
Loop
`End the function and return the valid object number
Endfunction i
`For more information on Functions and returning values, refer to DBPro Help (press F1): Principles / Functions
`For more information on Loops, refer to DBPro Help: Principles / Common Statements
`For more information on Loops, refer to DBPro Help: Commands / Core Commands
`For more information on Object Commands, refer to DBPro Help: Commands / Basic3D Commands
`handles all the input from the player
player_controls:
` while not left or right key is pressed, we rotate the ship back to its original angle
Player(0).yang# = curvevalue( 0.0, object angle y( Player(0).ObjId ), 50.0 )
`check if the player is hitting the right key, if they are we want to let them move right
`as long as they are not hitting the right side of the screen
if rightkey() = 1
`move the player to the right at the preset speed
inc Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the right side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# > rightside# then Player(0).xpos# = rightside#
` apply rotation to the ship while we move right
Player(0).yang# = curvevalue( -40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check in the same way for the left key
if leftkey() = 1
`move the player to the left at the preset speed
dec Player(0).xpos#, Player(0).speed#
`make sure they haven't passed the left side of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).xpos# < leftside# then Player(0).xpos# = leftside#
` apply rotation to the ship while we move left
Player(0).yang# = curvevalue( 40.0, object angle y( Player(0).ObjId ), 80.0 )
endif
`check if the player is hitting the upkey, if they are we want to let them move up
`but not past the barriers
if upkey() = 1
`move the player upwards at the preset speed
inc Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the barriers
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# > topside# then Player(0).ypos# = topside#
endif
`check if the player is hitting the downkey, if they are we want to let them move down
`but not past the bottom of the screen
if downkey() = 1
`move the player downwards at the preset speed
dec Player(0).ypos#, Player(0).speed#
`make sure they haven't passed the bottom of the screen
`if they have, then re-position them so that they can't pass it
if Player(0).ypos# < bottomside# then Player(0).ypos# = bottomside#
endif
`now that we've adjusted the position of the player accordingly, we need to update the object
`that is representing the player
position object Player(0).ObjId, Player(0).xpos#, Player(0).ypos#, 0
rotate object Player(0).ObjId, 0, Player(0).yang#, 0
`Update Particles
positionParticles(prtShip,Player(0).xpos#+plusmin(rnd(1.0)), Player(0).ypos#, 0)
controlParticles()
return
alien_fire_control:
`Decrease the AlienFireFlag counter if it is greater than 0
AlienFireFlag(0)=AlienFireFlag(0)-(AlienFireFlag(0)>0)
` start a for-next loop :: go through each of the bullets
For arrayPos = 0 To 53
obj = EnemyBullet( arrayPos ).ObjId ` get object nr. for this bullet
alien = EnemyBullet( arrayPos ).AlienId ` get object nr. of the alien
If Object Exist(alien)
` If TimeControl = 0 And the bullet is inactive..
If EnemyBullet( arrayPos ).TimeControl = 0 And EnemyBullet( arrayPos ).Active = 0
EnemyBullet( arrayPos ).StartSecond = Timer() ` reset StartSecond
EnemyBullet( arrayPos ).FireHere = 5 + rnd(20) ` set FireHere to 3 seconds + a random of 10 seconds
EnemyBullet( arrayPos ).TimeControl = 1 ` set TimeControl to 1
EndIf
` If TimeControl = 1..
If EnemyBullet( arrayPos ).TimeControl = 1 and AlienFireFlag(0)=0
` increase .CurSecond by 1 each secound
EnemyBullet( arrayPos ).CurSecond = (Timer() - EnemyBullet( arrayPos ).StartSecond) / 1000
` if .CurSecond > FireHere, that means the alien should fire his bullet
If EnemyBullet( arrayPos ).CurSecond > EnemyBullet( arrayPos ).FireHere
EnemyBullet( arrayPos ).Active = 1
EnemyBullet( arrayPos ).TimeControl = 0
`Set the AlienFireFlag counter
AlienFireFlag(0)=60:`Sets the fire rate to approx. 1 second (60 frames at 60 FPS)
EndIf
EndIf
Endif
` if the bullet is active.. [stage 1] - update
If EnemyBullet( arrayPos ).Active = 1
Show Object obj
` update bullet position
EnemyBullet( arrayPos ).xPos = Object Position X( alien )
EnemyBullet( arrayPos ).yPos = Object Position Y( alien )
EnemyBullet( arrayPos ).zPos = Object Position Z( alien )
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
` go to stage 2 :: move the bullet
EnemyBullet( arrayPos ).Active = 2
EndIf
` if the bullet is active.. [stage 2] - move
If EnemyBullet( arrayPos ).Active = 2
` apply rotation to the bullet
EnemyBullet( arrayPos ).xAng = 0
EnemyBullet( arrayPos ).yAng = 0
EnemyBullet( arrayPos ).zAng = 180
` update position of the bullet
EnemyBullet( arrayPos ).xPos = EnemyBullet( arrayPos ).xPos + sin( EnemyBullet( arrayPos ).zAng ) * 1.3
EnemyBullet( arrayPos ).yPos = EnemyBullet( arrayPos ).yPos + cos( EnemyBullet( arrayPos ).zAng ) * 1.3
` check if the bullet is outside the screen
If EnemyBullet( arrayPos ).yPos < bottomside#
Hide Object obj
EnemyBullet( arrayPos ).Active = 0
EndIf
` update object
Position Object obj, EnemyBullet( arrayPos ).xPos, EnemyBullet( arrayPos ).yPos, EnemyBullet( arrayPos ).zPos
Rotate Object obj, EnemyBullet( arrayPos ).xAng, EnemyBullet( arrayPos ).yAng, EnemyBullet( arrayPos ).zAng
EndIf
Next arrayPos
return
Function Move_Aliens()
Dim NextAlienMove(0):`Tracks how long until the next alien move
Dim AlienMoveDirection(0):`Tracks which direction the aliens are moving in
Dim AliensXY(1):`Tracks the Aliens position as a group. 0=X, 1=Y
`Start by moving to the right.
If AlienMoveDirection(0)=0 Then AlienMoveDirection(0)=1
`Set the initial Alien Group position if it is not already set
If AliensXY(1)=0
AliensXY(0)=-45
AliensXY(1)=50
Endif
`Decrease NextAlienMove(0) if it is greater than 0
NextAlienMove(0)=NextAlienMove(0)-(NextAlienMove(0)>0)
`If NextAlienMove(0)>0 then exit the function, since no move will be made
If NextAlienMove(0)>0 Then ExitFunction
`Start by counting how many aliens still are alive. This will determine alien speed.
AlienCount=0
`Set the left & right invader trackers so we can determine the left & right most invaders
LeftInvader=8:RightInvader=0
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
If Object Exist(Invaders(r,i))
AlienCount=AlienCount+1
if i<LeftInvader then LeftInvader=i
if i>RightInvader then RightInvader=i
Endif
Next i
Next r
`A move will be processed, so set the NextAlienMove(0) counter now
NextAlienMove(0)=AlienCount+3
MoveDown=0
`Check to see if the aliens are to the far right.
If AlienMoveDirection(0)=1
If (RightInvader*10)+AliensXY(0)>70
MoveDown=1
Endif
Endif
`Check to see if the aliens are to the far left
If AlienMoveDirection(0)=-1
If (LeftInvader*10)+AliensXY(0)<-70
MoveDown=1
Endif
Endif
`Calculate the new alien group position
If MoveDown>0
`Move the alien group down
AliensXY(1)=AliensXY(1)-2
`And change directions
AlienMoveDirection(0)=-AlienMoveDirection(0)
Else
AliensXY(0)=AliensXY(0)+AlienMoveDirection(0)*2
Endif
`Reposition Aliens
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
x=AliensXY(0)+(i*10)
y=AliensXY(1)+(r*10)
If Object Exist(Invaders(r,i))
Position Object Invaders(r,i),x,y,0
Endif
Next i
Next r
Play Sound 1
Endfunction
`Particle Data
function plusmin(num#)
rand=rnd(10)
if rand>5 then ret#=num#*-1 else ret#=num#
endfunction ret#
function setupParticles()
dim prtsys(0) as tprtsys
endfunction
function createParticles()
count=array count(prtsys())
cmax=0
for c=1 to count
if prtsys(c).count>cmax then cmax=prtsys(c).count
next c
dim prtdat(array count(prtsys()),cmax) as tprtdat
for c=1 to array count(prtsys())
for o=1 to prtsys(c).count
prtdat(c,o).obj=val(getToken(prtsys(c).objc,",",o))
next o
next c
endfunction count
function addParticles(x#,y#,z#,life,count)
array insert at bottom prtsys()
num=array count(prtsys())
prtsys(num).pos.x=x#+plusmin(rnd(0.2))
prtsys(num).pos.y=y#+plusmin(rnd(0.2))
prtsys(num).pos.z=z#+plusmin(rnd(0.2))
prtsys(num).life=life
prtsys(num).count=count
for c=1 to prtsys().count
obj=zp_freeobject() : make object plain obj,100,100
set object transparency obj,3
disable object zwrite obj
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
obj$=obj$+str$(obj)+","
lifep=rnd(life)
life$=life$+str$(lifep)+","
next c
prtsys(num).objc=obj$
prtsys(num).lifep=life$
endfunction num
function setParticleSpeed(num,speed#)
prtsys(num).speed=speed#
endfunction
function setParticleSize(num,size#,sizep#)
prtsys(num).sizei=size#
prtsys(num).sizep=sizep#
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
scale object obj,size#,size#,1
next c
endfunction
function positionParticles(num,x#,y#,z#)
prtsys(num).pos.x=x#
prtsys(num).pos.y=y#
prtsys(num).pos.z=z#
endfunction
function setParticleGravity(num,x#,y#,z#)
prtsys(num).grav.x=x#
prtsys(num).grav.y=y#
prtsys(num).grav.z=z#
endfunction
function colorParticles(num,r,g,b)
col=rgb(r,g,b)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
color object obj,col
next c
endfunction
function setParticleGhost(num,flag)
for c=1 to prtsys(num).count
obj=val(getToken(prtsys(num).objc,",",c))
if flag=1
ghost object on obj
else
ghost object off obj
endif
next c
endfunction
function controlParticles()
for num=1 to array count(prtsys())
for c=1 to prtsys(num).count
prtsys(num).lifec=prtsys(num).lifec+1
prtsys(num).lifecount=prtsys(num).life-prtsys(num).lifec
obj=prtdat(num,c).obj
angx#=angx#+((360/prtsys(num).count)*c)
angy#=angy#+((360/prtsys(num).count)*c)
sx#=object position x(obj)+(sin(angy#)*cos(angx#)*prtsys(num).speed)
sy#=object position y(obj)-(sin(angx#)*prtsys(num).speed)
sz#=object position z(obj)+(cos(angy#)*cos(angx#)*prtsys(num).speed)
sx#=sx#+prtsys(num).grav.x
sy#=sy#+prtsys(num).grav.y
sz#=sz#+prtsys(num).grav.z
size#=prtsys(num).sizei+(prtsys(num).sizep*prtsys(num).lifecount)
position object obj,sx#,sy#,sz#
scale object obj,size#,size#,1
if prtsys(num).lifec>prtsys(num).life
prtsys(num).lifec=prtsys(num).lifec*0
size#=prtsys(num).sizei
position object obj,prtsys(num).pos.x,prtsys(num).pos.y,prtsys(num).pos.z
endif
next c
next num
endfunction
function getEmitterCount()
count=array count(prtsys())
endfunction count
function getParticleCount(num)
count=prtsys(num).count
endfunction count
function zp_freeobject()
repeat
inc i
until object exist(i)=0
endfunction i
function getTokens(st$,sp$)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until counter>len(nst$)
tokens=splits-1
endfunction tokens
function getToken(st$,sp$,num)
if left$(st$,1)<>sp$ then nst$=sp$+st$
if right$(st$,1)<>sp$ then nst$=nst$+sp$
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$=sp$ then inc splits
until splits=num or counter>len(st$)
repeat
inc counter
cur$=mid$(nst$,counter)
if cur$<>sp$ then token$=token$+cur$
until cur$=sp$ or counter>len(st$)
endfunction token$
`Player Missile Control
Function Control_Missile()
If Sprite Frame(11)>3
Set Sprite Frame 11,1
Sprite 11,-512,0,11
Endif
If Sprite X(11)>0
Play Sprite 11,1,4,50
Endif
`Check to see of the Missile is hidden. If the missile is hidden, then it has not been launched.
Player(0).FireControl=Player(0).FireControl-(Player(0).FireControl>0)
If Object Visible(Missile(0))=0 and Player(0).FireControl<1
`The missile has not been launched; check to see if the player is pressing the space bar.
If SpaceKey()
`The spacekey is pressed, so launch the missile
`Start by showing the missile
Show Object Missile(0)
`Next, position the missile at the ship's location
Position Object Missile(0),Object Position X(Player(0).ObjID),Object Position Y(Player(0).ObjID), Object Position Z(Player(0).ObjID)
`Make sure the missile is pointing in the correct direction - up.
Rotate Object Missile(0),0,0,0
Player(0).FireControl=40
Play Sound 2
Else
`The missile has not been launched and the player is not pressing the space key - so nothing left to do.
Exitfunction
Endif
Endif
`If the code gets to these lines in this function, the missile has been launched. So, let's move it.
Move Object Up Missile(0),3
mc=Object Collision(Missile(0),0)
if mc=Player(0).ObjID Then Exitfunction
For r=0 to 5: `0 to 5 is 6 rows (0,1,2, 3,4,5)
For i = 0 to 8: `0 to 8 is 9 invaders per row
If mc=Invaders(r,i)
Delete Object Invaders(r,i)
Sprite 11,Object Screen X(Missile(0))-25,Object Screen Y(Missile(0))-45,11
Hide Object Missile(0)
Position Object Missile(0),0,-500,0
Play Sound 3
Endif
Next i
Next r
`Next, check to see if the missile has gone off the screen.
If Object Screen Y(Missile(0))<0
`The missile has gone off the screen, so let's hide it again. Remember, hiding it will stop it and have
`it ready for re-launch.
Hide Object Missile(0)
Endif
Endfunction
function Display_LoadBar(Num)
box LoadBar(Num).x1,LoadBar(Num).y1,LoadBar(Num).x2,LoadBar(Num).y2,rgb(192,192,192),rgb(92,92,92),rgb(192,192,192),rgb(92,92,92)
ink rgb(32,32,32),1
box LoadBar(Num).x1+LoadBar(Num).bdr,LoadBar(Num).y1+LoadBar(Num).bdr,LoadBar(Num).x2-LoadBar(Num).bdr,LoadBar(Num).y2-LoadBar(Num).bdr
if LoadBar(Num).length > LoadBar(Num).width
box LoadBar(Num).x1+LoadBar(Num).bdr+1,LoadBar(Num).y1+LoadBar(Num).bdr+1,LoadBar(Num).x1+LoadBar(Num).width-LoadBar(Num).bdr-1,LoadBar(Num).y2-LoadBar(Num).bdr-1,rgb(255,255,0),rgb(255,255,0),rgb(0,255,0),rgb(0,255,0)
else
box LoadBar(Num).x1+LoadBar(Num).bdr+1,LoadBar(Num).y1+LoadBar(Num).bdr+1,LoadBar(Num).x1+LoadBar(Num).length-1,LoadBar(Num).y2-LoadBar(Num).bdr-1,rgb(255,255,0),rgb(255,255,0),rgb(0,255,0),rgb(0,255,0)
endif
ink rgb(255,255,55),1
text LoadBar(Num).x1+LoadBar(Num).bdr+2,LoadBar(Num).y1 + LoadBar(Num).height+2,LoadBar(Num).name$
sync
endfunction
`UDTs
type vec3
x as float
y as float
z as float
endtype
type tprtsys
objc as string
pos as vec3
life as integer
lifec as integer
lifecount as integer
lifep as string
count as integer
speed as float
sizei as float
sizep as float
grav as vec3
endtype
type tprtdat
obj as integer
endtype

|
Back to top
 |
|
BMacZero
User
Joined: Fri Dec 30th 2005
Location: E:/N_America/USA/ California/South
|
I think it would be cool if you could incorporate GeeCee's Zoom Starfield effect (Newsletter Issue 43). It gives a nice spacey feel. It would probably look kind of weird while you were fighting but it would be great for a transition between levels.
Bmaczero
The upside to not thinking about the consequences is that you'll always surprise those who do.
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
If you want to add it in, go right ahead. That's what makes this a community project.
Thanks Indi. Enjoy your holiday.

Open MMORPG: It's your game!
|
Back to top
 |
|
blobby 101
User
Joined: Sat Jun 17th 2006
Location: England, UK
|
i really like the 'pixely' look on those models ruccus!

perfection is not achived, it is earned.
|
Back to top
 |
|
Aaron Miller
User
Joined: Sat Feb 25th 2006
Location: Behind the trusty dumpster.
|
I would like to contribute some stuff:
First, I have several premade textures, they might not be usefull to the Space Invaders project in particular, but they are various textures which may be usefull in the future, they are included in the archive.
Next, I have 2 plugins made, one allows a console box, they other allows a messagebox system, the console box is good for debugging, the messagebox system is good for handling errors (displaying them at least). Both are relatively easy to use.  (They are the first plugins I have ever made  )
In addition, I am taking dll requests and would not mind contributing the finished DLLs here. (Makes coding easier)
I have made several models, and wouldnt mind making more, one model is a "Death" model, usefull for Game Overs, another is meant to be a Teleportation Pod, usefull for, well, probably not the Space Invaders game, but still there.
I have made several code snippets I will share when they become appropriate for use. As of currently, nothing would appear usefull for the current working game, however, I can contribute my GUI, if anyone would like. (Title menu, pause menu, etc)
Console Plugin Example:
+ Code Snippet
`NOTE, this uses the messagebox plugin
console_Init
nl$=chr$(13)
cnl$=chr$(10)+chr$(13)
_TRY_INIT:
created=console_Create()
if created = 0
result=displayMessage("The console window could not be created!"+nl$+"What do you want to do?","Error",iconError(),btnAbortRetryIgnore())
if result=resultAbort() then end
if result=resultRetry() then goto _TRY_INIT
if result=resultIgnore() then goto MAIN
else
console_Write "Console created succesfully!"+cnl$+"Type 'end' to end the program."+cnl$
endif
console_SetTitle "DBP Console!"
console_SetColor "0F"
MAIN:
console_Write "Hello World!"+cnl$
console_Write "Nice day, ain't it?"+cnl$
console_Write "Well, I really don't care."+cnl$
console_Write str$(rnd(999))
console_Write "Woohooo!"
print "Press any key to end."
suspend for key
end
Messagebox Plugin Example:
+ Code Snippet
x=displayMessage("Will you continue?","Question Of Mine",iconQuestion(),btnYesNo())
nl$=chr$(13)
if x=resultYes()
a#=rnd(999.9)
b#=rnd(999.9)
c#=rnd(100.0)
d#=rnd(5.0)
e#=rnd(999.9)
_SET_POINT:
if d#=0.0
x=displayMessage("The division variable was set to zero.","Error",iconError(),btnRetryCancel())
if x=resultRetry() then d#=rnd(5.0)
if x=resultCancel() then end
goto _SET_POINT
endif
s$=str$(a#)+"+"+str$(b#)+"*"+str$(c#)+"/"+str$(d#)+"-"+str$(e#)+"="+str$(a#+b#*c#/d#-e#)
x=displayMessage(s$,"Did you know?",iconInfo(),btnOk())
if x<>resultOk()
x=displayMessage("For some reason, the OK button wasn't pressed!"+nl$+"How the hell did that happen?","Message",iconError(),btnOk())
endif
endif
end
Notice cnl$, and nl$
cnl$=chr$(10)+chr$(13) `CR and NL tokens used to simulate ENTER key press
nl$=chr$(13) `NL token used to simulate a New Line
And so, enjoy I guess.
-db
|
Back to top
 |
|
RiiDii
User
Joined: Thu Jan 20th 2005
Location: Inatincan
|
Thanks for the contributions DB user 2006+.
===
At this point, we are past the deadline for this project, so we bring it to an incomplete close. Overall, it's a good looking game. I like the particles and the splash screen - some great contributions there. Anyone wants to, they can wrap it up and post the finished product.
Does anyone have any suggestions for the next project? Should it be more challenging? Easier (Pong?)?

Open MMORPG: It's your game!
|
Back to top
 |
|
This is a multi-page thread older than 30 days. Go to the last page to check if you can reply to it.
Enter a word or phrase to search our Forum for:
|
|
 |