sync on : sync rate 50 : hide mouse
autocam off : randomize timer()
 
`d2l3=1
`load music "d2l3.mid",d2l3
`loop music d2l3
 
stars=1
for i=0 to 10000
   dot rnd(screen width()),rnd(screen height()),rgb(255,255,255)
next i
get image stars,0,0,screen width(),screen height(),0
make object plain stars,120,120
xrotate object stars,-90
texture object stars,stars
position object stars,0,-10,0
set object cull stars,0
 
global Plr=2
global Plrx#=0.0
global Plrz#=0.0
global Plrs#=0.0
global Plra#=0.0
global Fire
global Gun=2
global Score=0
global Lives=3
global SpawnTime
Fire=timer()
SpawnTime=timer()
 
make object cone Plr,5
scale object Plr,100,100,30
make object cone 3,5
scale object 3,150,90,30
make mesh from object 1,3
delete object 3
add limb Plr,1,1
delete mesh 1
offset limb Plr,1,0,-2,-0.5
color object Plr,rgb(0,0,255)
color limb Plr,1,rgb(50,255,50)
make object cylinder 3,5
scale object 3,12,60,12
make mesh from object 1,3
make mesh from object 2,3
delete object 3
add limb Plr,2,1
add limb Plr,3,2
delete mesh 1
delete mesh 2
offset limb Plr,2,2,-1,0
offset limb Plr,3,-2,-1,0
color limb Plr,2,rgb(100,100,100)
color limb Plr,3,rgb(100,100,100)
set object cull Plr,0
xrotate object Plr,90
fix object pivot Plr
set object collision on Plr
 
make object sphere 3,6
scale object 3,100,150,50
xrotate object 3,90
fix object pivot 3
color object 3,rgb(0,255,0)
ghost object on 3,4
hide object 3
 
position camera 0,50,0
point camera 0,0,0
 
do
   if rnd(30)=1 then MakeFreakAsteroid(rnd(360),rnd(7)+3)
   if mouseclick()=2 then inc Plrs#,0.05
   if mouseclick()<>2 then dec Plrs#,0.025
   if Plrs# >= 1.0 then Plrs#=1.0
   if Plrs# <= 0 then Plrs#=0
   Plra#=wrapvalue(Plra#+(mousemovex()*0.2))
   inc Plrx#,sin(Plra#)*Plrs#
   inc Plrz#,cos(Plra#)*Plrs#
   if mouseclick()=1 and timer()-Fire >= 200 then MakeFreakBullet() : Fire=timer()
   if mouseclick()=3
      inc Plrs#,0.05
      if timer()-Fire >= 200 then MakeFreakBullet() : Fire=timer()
   endif
   if timer()-SpawnTime >= 2000 then hide object 3
   if timer()-SpawnTime < 2000 then show object 3
   for i=4 to 1000 : ControlTehAsteroids(i) : next i
   for i=1001 to 2000 : ControlTehBullets(i) : next i
   CheckPlayerDeath()
   WrapPlayerPosition()
   position object Plr,Plrx#,0,Plrz#
   position object 3,Plrx#,0,Plrz#
   yrotate object Plr,Plra#
   yrotate object 3,Plra#
   yrotate object stars,object angle y(stars)+0.5
   text screen width()-text width("Score: "+str$(Score))-5,5,"Score: "+str$(Score)
   for i=1 to Lives : circle i*20,20,10 : next i
   sync
loop
 
function MakeFreakAsteroid(angle,size)
   Asteroid=3
   while object exist(Asteroid)=1
      inc Asteroid,1
   endwhile
   make object sphere Asteroid,size
   yrotate object Asteroid,angle
   if angle >= 45 and angle < 135 then x=-object size x(1) : y=rnd(object size y(1))-object size y(1)/2
   if angle >= 135 and angle < 225 then x=rnd(object size x(1))-object size x(1)/2 : y=-object size y(1)
   if angle >= 225 and angle < 315 then x=object size x(1) : y=rnd(object size y(1))-object size y(1)/2
   if angle >= 315 or angle < 45 then x=rnd(object size x(1))-object size x(1)/2 : y=object size y(1)
   position object Asteroid,x,0,y
   color object Asteroid,rgb(150,0,0)
   set object collision to spheres Asteroid
endfunction
 
function ControlTehAsteroids(Asteroid)
   newx=0
   newy=0
   change=0
   if object exist(Asteroid)=1
      move object Asteroid,0.3
      if object position x(Asteroid) > 50 then newx=-49 : change=1
      if object position x(Asteroid) < -50 then newx=49 : change=1
      if object position z(Asteroid) > 40 then newy=-39 : change=1
      if object position z(Asteroid) < -40 then newy=39 : change=1
      if change=1
         if newx<>0 then position object Asteroid,newx,0,object position z(Asteroid)
         if newy<>0 then position object Asteroid,object position x(Asteroid),0,newy
      endif
   endif
endfunction
 
function MakeFreakBullet()
   Flack=1001
   while object exist(Flack)=1
      inc Flack,1
   endwhile
   make object sphere Flack,0.3
   scale object Flack,100,100,1000
   set object diffuse Flack,rgb(255,0,0)
   set object specular Flack,rgb(255,150,150)
   set object emissive Flack,rgb(255,0,0)
   position object Flack,limb position x(Plr,Gun),0,limb position z(Plr,Gun)
   yrotate object Flack,Plra#
   set object collision to spheres Flack
   Gun=(-Gun)+5
endfunction
 
function ControlTehBullets(Flack)
   if object exist(Flack)=1
      move object Flack,1.2
      if object position x(Flack) < -object size x(1)/2 or object position x(Flack) > object size x(1)/2 or object position z(Flack) < -object size y(1)/2 or object position z(Flack) > object size y(1)/2 then delete object Flack : exitfunction
      if object collision(Flack,0)<>0 and object collision(Flack,0)<>Plr and object collision(Flack,0)<>1 and object collision(Flack,0)<>3
         delete object object collision(Flack,0)
         inc Score,1
         delete object Flack
         exitfunction
      endif
   endif
endfunction
 
function CheckPlayerDeath()
   if object collision(Plr,0)<>0 and object collision(Plr,0)<>3 and object collision(Plr,0)<1001
      if timer()-SpawnTime >= 2000
         if Lives>0
            hide object Plr
            ink rgb(200,200,200),0
            center text screen width()/2,screen height()/2,"YOU DIED" : sync
            suspend for key
            ink rgb(255,255,255),0
            dec Lives
            Plrx#=0.0
            Plrz#=0.0
            Plra#=0.0
            Plrs#=0.0
            delete object object collision(Plr,0)
            for i=1 to 50 : if object collision(3,0)<>0 and object collision(3,0)<>Plr and object collision(3,0)<>1 then delete object object collision(3,0)
            next i
            show object Plr
            SpawnTime=timer()
         else
            hide object Plr
            ink rgb(200,200,200),0
            center text screen width()/2,screen height()/2,"GAME OVER" : sync
            suspend for key
            ink rgb(255,255,255),0
            Lives=3
            Score=0
            Plrx#=0.0
            Plrz#=0.0
            Plra#=0.0
            Plrs#=0.0
            delete object object collision(Plr,0)
            for i=1 to 50 : if object collision(3,0)<>0 and object collision(3,0)<>Plr and object collision(3,0)<>1 then delete object object collision(3,0)
            next i
            show object Plr
            SpawnTime=timer()
         endif
      endif
   endif
endfunction
 
function WrapPlayerPosition()
   if Plrx# < -50 then Plrx#=49
   if Plrx# > 50 then Plrx#=-49
   if Plrz# < -40 then Plrz#=39
   if Plrz# > 40 then Plrz#=-39
endfunction