Original Post
I'm writing a space invaders clone. I'm trying to test collision between my bullets and the bonus ufo that flies across the top of the screen. I can't seem to get this collision to work. I tested collision between the player ship and the bonus ufo and that worked, so there's a problem with the bullets but I don't know where the problem lies. If anyone could help me out that would be great. If you wish to converse on aim my screen name is the same as my name here. [Edited by - ZGalhardo on February 18, 2009 6:09:25 PM]
Graphics 800, 600
;how big the star field is
size = 600
;number of stars in each image
num = 150
;set stars y coord
sy = 0
;create starfield images
sf1 = CreateImage(size,size)
sf2 = CreateImage(size,size)
sf3 = CreateImage(size,size)
;draw stars to first field
SetBuffer ImageBuffer(sf1)
For z = 1 To num
temp = Rand(150, 225)
Color temp, temp, temp
Rect Rand(size), Rand(size) ,2,2
Next
;draw stars to second field
SetBuffer ImageBuffer(sf2)
For z = 1 To num
temp = Rand(150, 200)
Color temp, temp, temp
Rect Rand(size), Rand(size) ,2,2
Next
;draw stars to third field
SetBuffer ImageBuffer(sf3)
For z = 1 To num
temp = Rand(100, 180)
Color temp, temp, temp
Rect Rand(size), Rand(size) ,2,2
Next
timer = CreateTimer(30)
;create a backbuffer
SetBuffer BackBuffer()
;set the handle to center of image
AutoMidHandle True
;-------------------------------
;Constants
;-------------------------------
Const LEFTKEY = 203
Const RIGHTKEY = 205
Const SPACEBAR = 57
Const STARTX = 400
Const STARTY = 550
Const PAUSEKEY = 25
;types
Type player
Field x,y,score,lives
End Type
Type bullet
Field x,y
End Type
Type enemy
Field x,y
End Type
Type bomb
Field x,y
End Type
Type bonusufo
Field x,y
End Type
AutoMidHandle True
;images
Global playerimage = LoadImage("player.bmp")
Global bulletimage = LoadImage("bullet.bmp")
Global enemyimage = LoadImage("enemy.bmp")
Global bombimage = LoadImage("bomb.bmp")
Global gameoverscreen = LoadImage("gameoverscreen.bmp")
Global pausescreen = LoadImage("pausescreen.bmp")
Global bonusufoimage = LoadImage("bonusufo.bmp")
;sounds
Global enemyhit = LoadSound("enemyhit.wav")
Global bulletshoot = LoadSound("bulletshoot.wav")
Global playerhit = LoadSound("playerhit.wav")
Global gameover = LoadSound("gameover.wav")
Global nextlevel = LoadSound("nextlevel.wav")
Global p.player = New player
p\x = STARTX
p\y = STARTY
Global b.bullet = New bullet
b\x = p\x
b\y = p\y - 8
Global numenemies = 0
;set score and lives
p\score = 0
p\lives = 3
Global level = 0
;set enemy speed
Global enspeed = 2
Global enmx = enspeed
Global dirchange = False
;-------------------------------
;main loop
;-------------------------------
While Not KeyDown(1)
If numenemies = 0 Then
level = level + 1
PlaySound nextlevel
For b.bullet = Each bullet
Delete b
Next
For bmb.bomb = Each bomb
Delete bmb
Next
Delay 500
For z = 1 To 6
For r = 1 To 4
en.enemy = New enemy
en\x = 300 + 75 * z
en\y = 20 + 50 * r
Next
Next
EndIf
;clear the screen
Cls
;draw images
DrawImage playerimage, p\x, p\y
MaskImage playerimage,255,255,255
;draw each field but alter y coord
TileImage sf1, 0, sy
TileImage sf2, 0, sy/2
TileImage sf3, 0, sy/3
;increment sy to move stars down
sy = sy + 1
DrawHud()
DrawAndUpdateEnemies()
DrawAndUpdateBombs()
DrawAndUpdateBonusUfo()
DrawAndUpdateBullets()
TestInput()
WaitTimer(timer)
Flip
Wend
;------------------------------------
;functions
;------------------------------------
Function TestInput()
If KeyHit(PAUSEKEY)
Cls
DrawImage pausescreen, 400, 300
Flip
While Not KeyHit(PAUSEKEY)
Wend
EndIf
If KeyDown(LEFTKEY)
p\x = p\x - 5
If p\x <=0
p\x = 0
EndIf
EndIf
If KeyDown(RIGHTKEY)
p\x = p\x + 5
If p\x >= 800
p\x = 800
EndIf
EndIf
If KeyHit(SPACEBAR) Then
PlaySound(bulletshoot)
b.bullet = New bullet
b\x = p\x
b\y = p\y - 8
EndIf
End Function
Function DrawHud()
Text 20, 20, "Score: " + p\score
Text 20, 30, "Lives: " + p\lives
Text 20, 40, "Level: " + level
End Function
Function DrawAndUpdateBombs()
For bmb.bomb = Each bomb
bmb\y = bmb\y + 5
DrawImage bombimage, bmb\x, bmb\y
If ImagesCollide(bombimage, bmb\x, bmb\y, 0, playerimage, p\x, p\y, 0) Then
PlaySound(playerhit)
Delete bmb
p\lives = p\lives - 1
Exit
ElseIf p\lives <= 0 Then
PlaySound(gameover)
Delete p
Cls
DrawImage gameoverscreen, 400, 300
Flip
Delay 1300
End
ElseIf bmb\y >800
Delete bmb
EndIf
Next
End Function
Function DrawAndUpdateEnemies()
;change enemy direction
If dirchange = True Then
enmx = -enmx
EndIf
dirchange = False
numenemies = 0
;update and draw enemies
For en.enemy = Each enemy
;count enemies
numenemies = numenemies + 1
en\x = en\x + enmx
If en\x >= 780 Then dirchange = True
If en\x <= 20 Then dirchange = True
DrawImage enemyimage, en\x,en\y
;generate bombs
If Rand(550) = 1 Then
bmb.bomb = New bomb
bmb\x = en\x
bmb\y = en\y
EndIf
For b.bullet = Each bullet
If ImagesCollide(bulletimage, b\x, b\y, 0, enemyimage, en\x, en\y, 0) Then
Delete b
Delete en
PlaySound(enemyhit)
p\score = p\score + 50
Exit
EndIf
Next
Next
End Function
Function DrawAndUpdateBullets()
For b.bullet = Each bullet
b\y = b\y - 12
DrawImage bulletimage, b\x, b\y
If b\y < 0 Then Delete b
Next
End Function
Function DrawAndUpdateBonusUfo()
If Rand(120) = 10 Then
bufo.bonusufo = New bonusufo
bufo\x = 780
bufo\y = 10
EndIf
For bufo.bonusufo = Each bonusufo
bufo\x = bufo\x - 4
DrawImage bonusufoimage, bufo\x, bufo\y
If bufo\x <= 0 Then Delete bufo
If ImagesCollide(bonusufoimage, bufo\x, bufo\y, 0, bulletimage, b\x, b\y, 0) Then
Delete bufo
Delete b
p\score = p\score + 500
Exit
EndIf
Next
End Function