Wave's~ BlitzMax Tutorial~ July 20, 2005 ~ Version 10
Beginners guide to BlitzMaxversion 7

Sounds
Sounds is loaded the same way as images, YourSound = LoadSound("boom.wav"). Then you can play the sound with PlaySound(YourSound). You can also use different channels for your sounds. You can load .ogg and wav files atm. I hope sounds are pretty self-explanatory after you have dealt with images. Read about it in the BMax reference and make your own example to try them.

Short Lesson On Collision Detection
Collision: To detect when something overlaps or is about to overlap something else.
 
Let's say you want to know when your bullet hits the enemy.

One way to do it is to use this simple function which I snapped from the forums:
Function RectsOverlap(x0, y0, w0, h0, x2, y2, w2, h2)
If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return False
If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return False
Return True
End Function
This checks a rectangle at coordinate x0,y0 with a rectangle at x2,y2. w and h is the width and height of the rectangles, returns true if they overlap else false. This works perfect for enemy vs enemy or mouse-over-button collisions.
 
Let's say you want to make sure that people don't walk thru walls, how would you do that?

Consider the following example:
Graphics 800,600,0
Global Xvel# = 0.5, X# = 50
Repeat
LastX = X 'Save X
X=X+Xvel 'Update X
'Draw the wall
SetColor 255,255,255 'White
DrawRect( 500,0,500,800 )
'Draw moving object
SetColor 255,0,0 'Red
DrawRect( X, 50, 300, 55 )
If X+300 > 500 X=LastX 'We collided so reset to last saved X
Flip ; Cls
Until KeyDown(Key_Escape)
The above shows how a simple wall check would work.
 
The RectsOverlap Function above checks all sides of the square.
There is another quite smart way to check collision and that is by distance.

In the end checking the distance is the same as checking against circular targets.
Strict
Function Distance#( X1, Y1, X2, Y2 )
Local DX# = X2 - X1
Local DY# = Y2 - Y1
Return Sqr( Dx*DX + Dy*Dy )
EndFunction
Graphics 800,600,0
Local LastX, LastY, MR = 20 ' MR Short for MouseRadius
Local TargetX = 400 , TargetY = 400
Local TR = 300 ' TR Short for TargetRadius
Repeat
'Draw Mouse
SetColor 0,155,0 ' Green
DrawOval MouseX()-MR, MouseY()-MR, MR*2, MR*2
'Draw Target
SetColor 0,0,255 ' Blue
DrawOval TargetX-TR, TargetY-TR, TR*2, TR*2
If Distance( MouseX(), MouseY(), TargetX, TargetY ) < TR+MR
MoveMouse( LastX, LastY ) ' Move it back
SetColor 255,0,0
DrawText "CIRCULAR COLLISION DETECTED!",20,20
EndIf
LastX = MouseX() ; LastY = MouseY()
Flip ; Cls
Until KeyDown(Key_Escape)

The distance function is very handy. It gets a little more advanced if you want to stop, bounce or slide at surfaces. You can loop all your objects in a list and check each one against each other. Collision detection is very dependant on which type of game you are making. Blitz also has CollideImage functions, which can check your images for pixel perfect collisions. They are advanced and slow so I don't recommend any beginners to use them before they have proper documentation with good examples.
 
To Index | Next Page page 21