Hey guys I'm all new to the programming world and have been studying basic programming at college I have developed a basic 2d asteroids game but can not get a collision and death splash screen to work what id to happen is the asteroid and ship collide and the death screen appears here is my code any help would be appreciated.
My code:
#constant ship 1
#constant firstasteroid 51
#constant firstmissile 101
global keyspressed as byte
global angle as integer
global lastfired as integer
global dim missilemoves(9)
global missileno as integer
InitialiseGame()
do
paste image 4,0,0
handlekeyboard()
handleship()
handlemissiles()
handleasteroids()
handleCheckForCollision()
loop
end
Function InitialiseGame()
Sync rate 100
Set display mode 1280,1024,32
Set image colorkey 255,255,255
Load image "spaceship.png",2
sprite ship, screen width()/2,screen Height()/2,2
offset sprite ship,sprite width(ship)/2,sprite height(ship)/2
lastfired=timer()
set image colorkey 255, 255, 255
load image "missile.jpg",3
missileno=firstmissile
for c= 0 to 9
missilemoves(c)=0
next c
randomize timer()
Createasteroids()
StartPositionForAsteroids()
Load image "background.jpg",4
endfunction
Function handlekeyboard()
keyspressed=0
if keystate(44)=1
keyspressed = keyspressed ||%001
else
if keystate(45)=1
keyspressed = keyspressed ||%010
endif
endif
if keystate(57)=1
keyspressed = keyspressed ||%100
endif
if keystate(208)=1
keyspressed = keyspressed ||%101
endif
endfunction
Function handleship()
if (keyspressed && %001)> 0
angle = wrapvalue(angle-5);
endif
if (keyspressed && %010)> 0
angle = wrapvalue(angle +5);
endif
if (keyspressed && %100) > 0 and timer() - lastfired > 100
launchmissile()
endif
rotate sprite ship, angle
endfunction
function Launchmissile()
if sprite exist(missileno)
exitfunction
endif
sprite missileno,sprite x(ship),sprite Y(ship),3
offset sprite missileno,sprite width(missileno)/2, sprite height(missileno)/2
rotate sprite missileno, sprite angle(ship)
move sprite missileno, 30
lastfired = timer()
missileno=missileno mod 10 + firstmissile
endfunction
function HandleMissiles
for spriteno = firstmissile to firstmissile +10
if sprite exist(spriteno)
move sprite spriteno,15
spritehit = sprite hit(spriteno,0)
if spritehit >1
delete sprite spritehit
delete sprite spriteno
post = spriteno - firstmissile
missilemoves(post)= 0
else
post = spriteno - firstmissile
inc missilemoves(post)
if missilemoves(post)>=150
delete sprite spriteno
missilemoves(post)=0
endif
endif
endif
next spriteno
endfunction
function createasteroids()
create animated sprite firstasteroid, "asteroid.bmp", 5,1,1
offset sprite firstasteroid, sprite width(firstasteroid)/2,sprite height (firstasteroid)/2
for spriteno = firstasteroid + 1 to firstasteroid + 9
clone sprite firstasteroid, spriteno
next spriteno
endfunction
function startpositionforAsteroids()
for spriteno = firstasteroid to firstasteroid + 9
side = rnd(3)
select side
case 0
x = rnd(screen width())
y=0
angle = rnd(180) + 90
endcase
case 1
x = screen width()
y = rnd(screen height())
angle = rnd (180)+ 180
endcase
case 2
x = rnd(screen width())
y = screen height()
angle = rnd (180)- 90
endcase
case 3
x = 0
y = rnd(screen height())
angle = rnd(180)
endcase
endselect
rotate sprite spriteno, angle
sprite spriteno, x, y, 1
next spriteno
endfunction
function handleasteroids()
for spriteno = firstasteroid to firstasteroid + 9
if sprite exist(spriteno)
move sprite spriteno,1
if sprite Y(spriteno)<0
sprite spriteno,sprite x (spriteno),screen height (),1
endif
if sprite y(spriteno)>screen height()
sprite spriteno, sprite x(spriteno),0,1
endif
if sprite x(spriteno)<0
sprite spriteno,screen width(), sprite y(spriteno),1
endif
if sprite x(spriteno)>screen width ()
sprite spriteno, 0,sprite y(spriteno),1
endif
play sprite spriteno,5,1,40
endif
next spriteno
endfunction
function handleCheckForCollision()
Rem This will check to see if you have been hit by nme Ships
for spriteNo = firstasteroid to firstasteroid + 9
if sprite hit(spriteNo , ship)
handleendgame()
endif
if sprite hit (ship , firstasteroid + (spriteNo - 100))
handleendgame()
endif
next spriteNo
endFunction
function handleendgame()
for spriteNo = firstasteroid to firstasteroid + 9
if sprite exist (spriteNo)
rem delete sprite spriteNo
`delete sprite ship
endif
if sprite exist (firstasteroid + (spriteNo - 100))
delete sprite (firstasteroid + (spriteNo - 100))
endif
next spriteNo
if sprite exist (ship)
delete sprite ship
endif
endFunction