Post by Azan on Apr 29, 2005 20:28:44 GMT -5
Credits
First let me thank Jiub (as most of this is his program, I will just be going through it and showing how it works)
What are types?
Well types are a way of storing variables for sets of things, this is really hard to explane, but emagine space invaders, each bullet you fire is a type, each bullet holds variables within it that hold its information like its location etc. etc.
Think of it this way, imagine a room filled with chairs, each chair is the same type "CHAIR" with in this type each chair holds its location.
Getting Started
Ok types will be one of the hardest things to get your head around, and for now we will just be getting into the basics of it, I will also be taking you through the process of making a simple space invaders game (well at least the basics of one).
First off lets define our global variables/screen here.
Now x and y will be used to determine our players location/starting location (as you can see is at the center of the screen)
playerspeed speaks for itself, it will be the players speed. Now lets move on this next part will be defining our types.
Now this is probably something you havent seen before (which is why you are reading this tutorial) but fields basicaly determine the variables that a type will hold. as you can see we have Projectile which will be our bullets and inside the type we will hold its location, next we have Ship, these will be our enemy ships. (Hitpoints im not sure what they are, ill have to ask Jiub)
Now what im about to put up next will probably confuse you, and they confuse me too, but this basicaly will create enemy ships in a certain pattern you might say, when this is done try changing the values and you can get an idea what they do.
Now you shouldve read Jiubs tutorial on types as he tells what all these commands do like enemy.Ship = New Ship and enemy\x=ex I can give a basic explanation enemy.Ship = New Ship does just what it says it makes a new thing using a types outline (in this case Ship) and then we can access the fields within the type with enemy\x = ex. Now lets move on.
This is our Main loop, most of this is self explanitory, but as we move on we will call the functions.
Once again this should be self explanitory, it creates a box that can be moved around the screen (if this confuses you look at my previous tutorials) but now lets move on to the fire function.
Ok is what this does is that it defines a new thing within a type in this case bullet and when we do bullet.Projectile=New Projectile its ssaying that this will follow Projectiles Fields, now this function could also be called CreateBullet() as all this really does is create a new bullet at the boxes x and y values, bullet\x is accessing Projectiles Field, this will hold its location, and when a new bullet is created its location will equal the players location. Next we move on to the part that controls a bullet.
Ok this For bullet.Projectile = Each Projectile is basicaly saying that for all bullets in existance and that each bullets why value is subtracted thus causing the bullet to move upwards. Then we create the bullets graphics here Rect bullet\x,bullet\y,5,5 the rest of the function is telling it if a bullet goes off screen it gets deleted, this is a must as too many bullets can cause any game to lag after many have been made. Now the next bit of code is just for dealing with collisions between a bullet and a enemy ship.
I wont explane this as it can be easily figured out, or just look in blitzs command refrence. Well our tutorial is nearing an end and the last part is to draw the enemies.
Once again self explanitory, well that ends my tutorial, I will be updating it every so often as it may need some correction in grammer and explanation.
Simple type programs
Heres a simple little program to help you further understand types (be sure to read the notes)
Copyright: This document may not be posted anywhere else but ragnarokcom.proboards33.com with out the authors consent, by posting anywhere else with out the authors consent you are agreeing to give me your soul mwahahaha (but feel free to post links to this site/tutorial on other sites).
First let me thank Jiub (as most of this is his program, I will just be going through it and showing how it works)
What are types?
Well types are a way of storing variables for sets of things, this is really hard to explane, but emagine space invaders, each bullet you fire is a type, each bullet holds variables within it that hold its information like its location etc. etc.
Think of it this way, imagine a room filled with chairs, each chair is the same type "CHAIR" with in this type each chair holds its location.
Getting Started
Ok types will be one of the hardest things to get your head around, and for now we will just be getting into the basics of it, I will also be taking you through the process of making a simple space invaders game (well at least the basics of one).
First off lets define our global variables/screen here.
Global x=640/2
Global y=480/2
Global playerspeed=3
Graphics 640,480
SetBuffer BackBuffer()
Now x and y will be used to determine our players location/starting location (as you can see is at the center of the screen)
playerspeed speaks for itself, it will be the players speed. Now lets move on this next part will be defining our types.
Type Projectile
Field x,y
End Type
Type Ship
Field x,y
Field hitpoints
End Type
Now this is probably something you havent seen before (which is why you are reading this tutorial) but fields basicaly determine the variables that a type will hold. as you can see we have Projectile which will be our bullets and inside the type we will hold its location, next we have Ship, these will be our enemy ships. (Hitpoints im not sure what they are, ill have to ask Jiub)
Now what im about to put up next will probably confuse you, and they confuse me too, but this basicaly will create enemy ships in a certain pattern you might say, when this is done try changing the values and you can get an idea what they do.
For ex = 100 To 500 Step 50
For ey = 100 To 200 Step 20
enemy.Ship = New Ship
enemy\x = ex
enemy\y = ey
enemy\hitpoints = 111
Next
Next
Now you shouldve read Jiubs tutorial on types as he tells what all these commands do like enemy.Ship = New Ship and enemy\x=ex I can give a basic explanation enemy.Ship = New Ship does just what it says it makes a new thing using a types outline (in this case Ship) and then we can access the fields within the type with enemy\x = ex. Now lets move on.
While Not KeyHit(1)
Flip
Cls
update_player()
update_bullets()
update_collisions()
update_enemys()
If KeyDown(200) up()
If KeyDown(208) down()
If KeyDown(203) le()
If KeyDown(205) ri()
If KeyHit(57) fire()
Wend
End
This is our Main loop, most of this is self explanitory, but as we move on we will call the functions.
Function update_player()
Color 0,255,0
Rect x,y,15,15
End Function
Function up()
y=y-playerspeed
End Function
Function down()
y=y+playerspeed
End Function
Function le()
x=x-playerspeed
End Function
Function ri()
x=x+playerspeed
End Function
Once again this should be self explanitory, it creates a box that can be moved around the screen (if this confuses you look at my previous tutorials) but now lets move on to the fire function.
Function fire()
bullet.Projectile = New Projectile
bullet\x = x
bullet\y = y
End Function
Ok is what this does is that it defines a new thing within a type in this case bullet and when we do bullet.Projectile=New Projectile its ssaying that this will follow Projectiles Fields, now this function could also be called CreateBullet() as all this really does is create a new bullet at the boxes x and y values, bullet\x is accessing Projectiles Field, this will hold its location, and when a new bullet is created its location will equal the players location. Next we move on to the part that controls a bullet.
Function update_bullets()
Color 0,64,255
For bullet.Projectile = Each Projectile
bullet\y = bullet\y - 4
Rect bullet\x,bullet\y,5,5
If bullet\y <= 0
Delete bullet.projectile
EndIf
Next
End Function
Ok this For bullet.Projectile = Each Projectile is basicaly saying that for all bullets in existance and that each bullets why value is subtracted thus causing the bullet to move upwards. Then we create the bullets graphics here Rect bullet\x,bullet\y,5,5 the rest of the function is telling it if a bullet goes off screen it gets deleted, this is a must as too many bullets can cause any game to lag after many have been made. Now the next bit of code is just for dealing with collisions between a bullet and a enemy ship.
Function update_collisions()
For enemy.Ship = Each Ship
del2=0
For bullet.Projectile = Each Projectile
del=0
If RectsOverlap(bullet\x,bullet\y,5,5,enemy\x-5,enemy\y-5,15,15)
del=1
del2=1
EndIf
If del=1 Then Delete bullet.projectile
Next
If del2=1 Then Delete enemy.ship
Next
End Function
I wont explane this as it can be easily figured out, or just look in blitzs command refrence. Well our tutorial is nearing an end and the last part is to draw the enemies.
Function update_enemys()
For enemy.Ship=Each ship
Color 255,0,0
Rect enemy\x,enemy\y,10,10
Next
End Function
Once again self explanitory, well that ends my tutorial, I will be updating it every so often as it may need some correction in grammer and explanation.
Simple type programs
Heres a simple little program to help you further understand types (be sure to read the notes)
Graphics 800,600
SetBuffer BackBuffer()
Type ball ;the type ball
Field x
Field y
End Type
While Not KeyHit(1)
If KeyHit(57) Then createball() ; if the space bar is hit create b
updateballs() ;increases each b why value and draws the circle
Text 200,200,"Hit the space key"
Flip
Cls
Wend
Function createball() ;creates a new instance called b following the type ball
b.ball=New ball
b\x=50 ;set b x and why values
b\y=50
End Function
Function updateballs()
For b.ball=Each ball ;targets all b
b\y=b\y+5 ;for each b it has its why value increased
Oval b\x,b\y,10,10 ;draws a circle at each b coordinates
Next
End Function
Copyright: This document may not be posted anywhere else but ragnarokcom.proboards33.com with out the authors consent, by posting anywhere else with out the authors consent you are agreeing to give me your soul mwahahaha (but feel free to post links to this site/tutorial on other sites).