include "win.inc"
int i
sys SetTimer = CallDll(user,"SetTimer")
sys KillTimer= CallDll(user,"KillTimer")

Screen 800,600,1
SetCaption "UFFO"
SetColorKey 255,0,255
ShowMouse 0

Type uBomb
  Dim x as long
  Dim y as long
  Dim z as Single
End Type

Type Uffo
  Dim x as long
  Dim y as long
  Dim z as long
  Dim r as long
  Dim i as long
  Dim n as long
End Type 

Type Rocket
  Dim x as long
  Dim y as long
  Dim r as long
End Type

Type Explosion
  Dim x as long
  Dim y as long
  Dim i as long
  Dim z as single
End Type

Dim Meg(40) as uBomb
Dim Ufo(40) as Uffo 
Dim Bum(40) as Explosion 
Dim Rok(40) as Rocket
sys xRak,yRak,rRak,zRak,iBx,iDx,iCx,iAx,zFrame,z1,z2,z3,px,sc,uz,jx,drop,Ok
sys ufos,miss,ship,bomb expl,bknd

'bknd= LoadBmp "media/bground.bmp",1
ufos= LoadBmp "media/ufos.bmp",8
miss= LoadBmp "media/missile.bmp",8
ship= LoadBmp "media/ships.bmp",8
bomb= LoadBmp "media/bomb1.bmp",8
expl= LoadBmp "media/explode.bmp",8

Function Timer1() as long callback
zFrame +=1
iF zFrame =8 Then zFrame =0
End Function

Function Timer2() as long callback
jx +=1
iF jx =21 Then jx=0
End Function

Function Timer3() as long callback
Drop = Rand(0,40) 
End Function

z1= call SetTimer hwnd,1,140,&Timer1 
z2= call SetTimer hwnd,2,200,&Timer2 
z3= call SetTimer hwnd,3,150,&Timer3 

Sub SetExplos(sys x, y, i)
For iDx=0 To 39
iF Bum(iDx).y =0
   Bum(iDx).x = x
   Bum(iDx).y = y
   Bum(iDx).i = i
   Exit Sub
End iF
Next
End Sub

Sub ShowExplos()
For iDx=0 To 39
iF Bum(iDx).i =1
   SetBmp expl,Bum(iDx).x,Bum(iDx).y,96,96,Bum(iDx).z
   Bum(iDx).z +=.1
   iF Bum(iDx).z >=8.0
      Bum(iDx).z =0
      Bum(iDx).y =0
      Bum(iDx).i =0
   End iF
End iF
Next
End Sub

Sub SetRocket(sys x, y)
For iAx=0 To 39
iF Rok(iAx).y =0
   Rok(iAx).x = x
   Rok(iAx).y = y
   Rok(iAx).r = 1
   Exit Sub
End iF
Next
End Sub

Sub ScanRocket()
For iAx=0 To 39
iF Rok(iAx).r =1 and Rok(iAx).y <=-32 
   Rok(iAx).r =0: Rok(iAx).y =0
End iF
Next
End Sub

Sub ShowRocket()
For iAx=0 To 39
iF Rok(iAx).r =1
   SetBmp miss,Rok(iAx).x,Rok(iAx).y,64,64,zFrame
   Rok(iAx).y -=1
   iF Rok(iAx).y =400 Then zRak=0
End iF
Next
End Sub

Sub ScanRakete()
iF rRak=5 Then Exit Sub
iF Key(32)<>0 and Key(39)<>0 and zRak =0
   SetRocket xRak,yRak
   rRak =1
   zRak =1
   call PlayWav "media/shoot.wav",0,1
ElseiF Key(32)<>0 and Key(37)<>0 and zRak=0
   SetRocket xRak,yRak
   rRak =2
   zRak =1
   call PlayWav "media/shoot.wav",0,1
ElseiF Key(32)<>0 and zRak=0
   SetRocket xRak,yRak
   zRak =1
   call PlayWav "media/shoot.wav",0,1
ElseiF Key(39)<>0 and xRak<736
   rRak =1
ElseiF Key(37)<>0 and xRak>0
   rRak =2
Else
   rRak =0
End iF
End Sub

Sub ShowRakete()
iF rRak =0
   SetBmp ship,xRak,yRak,64,64,zFrame
ElseiF rRak =1
   SetBmp ship,xRak,yRak,64,64,zFrame
   xRak +=1
ElseiF rRak =2
   SetBmp ship,xRak,yRak,64,64,zFrame
   xRak -=1
End If
End Sub

Function Collision(sys x1, y1, r1, x2, y2, r2) as sys
iF (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)<(r1+r2)*(r1+r2)
   Return 1
End iF
End Function

Sub RocketCollision()
For iBx=0 To uz
For iCx=0 To 39
iF Rok(iCx).r >0 And Ufo(iBx).r >0  
   iF Collision Rok(iCx).x+32,Rok(iCx).y+32,16,Ufo(iBx).x+32,Ufo(iBx).y+32,16
      SetExplos Rok(iCx).x,Rok(iCx).y,1
      SetExplos Ufo(iBx).x,Ufo(iBx).y,1 
      Rok(iCx).r =0: Rok(iCx).y =0
      Ufo(iBx).r =0: Ufo(iBx).y =0
      call PlayWav "media/plode.wav",0,1
      sc +=25 
      Exit For
   End iF
End iF
Next
Next
End Sub

Sub RaketeCollision()
iF Ok=1 Then Exit Sub
For iAx=0 To 40
iF Meg(iAx).y >0
   iF Collision xRak,yRak,16,Meg(iAx).x,Meg(iAx).y,16
      SetExplos xRak,yRak,1
      rRak=5: Ok=1
      'call PlayWav "media/xship.wav",0,1
      call PlayWav "../GDIWindow/cosmobumm.wav",0,1
      Exit Sub
   End iF
End iF
Next
End Sub
 
Sub SetUfos() 
For iAx=0 To uz
Ufo(iAx).x = Rand(16,736)
Ufo(iAx).y = Rand(16,300)
Ufo(iAx).r = Rand(1,4)
Ufo(iAx).z = 0
Ufo(iAX).n = 0
Next
End Sub

Sub ScanUfos()
For iBx=0 To uz
iF Ufo(iBx).r =1 and Ufo(iBx).x >=736
   Ufo(iBx).r =2 
ElseiF Ufo(iBx).r =2 and Ufo(iBx).x <=0
   Ufo(iBx).r =1 
ElseiF Ufo(iBx).r =3 and Ufo(iBx).y <=0
   Ufo(iBx).r =4 
ElseiF Ufo(iBx).r =4 and Ufo(iBx).y >=350
   Ufo(iBx).r =3
ElseiF jx =5 and Ufo(iBx).r =4
   Ufo(iBx).r = Rand(1,4)
ElseiF jx =10 and Ufo(iBx).r =3
   Ufo(iBx).r = Rand(1,4)
ElseiF jx =15 and Ufo(iBx).r =2
   Ufo(iBx).r = Rand(1,4)
ElseiF jx =20 and Ufo(iBx).r =1
   Ufo(iBx).r = Rand(1,4)
End iF
Next
End Sub

Sub ShowUfos()
For iBx=0 To uz
iF Ufo(iBx).r =1
   SetBmp ufos,Ufo(iBx).x,Ufo(iBx).y,64,64,zFrame
   Ufo(iBx).x +=1
ElseiF Ufo(iBx).r =2
   SetBmp ufos,Ufo(iBx).x,Ufo(iBx).y,64,64,zFrame
   Ufo(iBx).x -=1
ElseiF Ufo(iBx).r =3
   SetBmp ufos,Ufo(iBx).x,Ufo(iBx).y,64,64,zFrame
   Ufo(iBx).y -=1
ElseiF Ufo(iBx).r =4
   SetBmp ufos,Ufo(iBx).x,Ufo(iBx).y,64,64,zFrame
   Ufo(iBx).y +=1
End iF
Next
End Sub

Sub SetData
xRak=384:yRak=536:rRak=0
uz=40:sc=0:Ok=0 
SetUfos
For iBx=0 To 40
Meg(iBx).y =0
Meg(iBx).x =0
Next
End Sub

Sub TestUfos()
For iAx=0 To 40
iF Ufo(iAx).y >0 or Ok=1 Then Exit Sub
Next
Ok=1
End Sub

Sub ScanUfoBombs()
For iAx=0 To drop Step 2  
iF Ufo(iAx).r >0
   iF Meg(iAx).y=0
      Meg(iAx).x = Ufo(iAx).x
      Meg(iAx).y = Ufo(iAx).y+32
   End iF 
End iF
Next
End Sub

Sub UfoBombs()
For iDx=0 To 40
iF Meg(iDx).y >0
   iF iDx <20
      SetBmp bomb,Meg(iDx).x,Meg(iDx).y,64,64,zFrame
   ElseiF iDx >=20
      SetBmp bomb,Meg(iDx).x,Meg(iDx).y,64,64,zFrame
   End iF
   Meg(iDx).y +=1
   iF Meg(iDx).y >=600 Then Meg(iDx).y =0
End iF
Next
End Sub

'Main
'====
SetData()
While Key(27)=0 
'SetBmp bknd,0,0,800,600,0
'ClsColor 10,0,90
for i=0 to 599
  line 0,i,799,i,1,0,0,i >>2 'shaded sky
next
SetText 600,0,"SCORE:" + sc,255,255,255
ScanUfos
ShowUfos
ScanRocket
ShowRocket
ScanRakete
ShowRakete
ScanUfoBombs
UfoBombs
ShowExplos
RocketCollision
RaketeCollision
TestUfos
iF Ok=1
   SetText 328,300,"Once again?  ",255,250,245
   SetText 328,320,"Hit [c] Key",200,245,255
   iF Key(0x43) <>0 Then SetData
End iF
Flip
SetFps 80 'select your speed :)
Wend
call KillTimer hwnd,z1
call KillTimer hwnd,z2
call KillTimer hwnd,z3
Ends 