asteriods 1

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global winx , winy
winx = WindowWidth
winy = WindowHeight
global px , py , pst , lastkey$ , pi , q , d , joyYN$ , firetel
global points , lives , state , ufohasfired
global true , false
false = 0
true = not( false )
pi = atn( 1 ) * 4
px = winx / 2
py = winy / 2
lives = 3
state = rnd( 0 ) * ( winx / 2 ) + winx / 4
ufohasfired = false
for i = 0 to 31
  loadbmp "p";i , DefaultDir$ + "\BMP\player" + nr$( i ) + ".bmp"
  anim$ = anim$ + " p";i
next i
loadbmp "fire" , DefaultDir$ + "\BMP\dot.bmp"
loadbmp "rock" , DefaultDir$ + "\BMP\rock.bmp"
loadbmp "ufo"  , DefaultDir$ + "\BMP\ufo.bmp"
loadbmp "star" , DefaultDir$ + "\BMP\star.bmp"
notice  "Blua tigro presents :" + chr$( 13 ) _
       + "ASTERIODS ."           + chr$( 13 ) _
       + "use cursor keys"       + chr$( 13 ) _
       + "to move your ship"     + chr$( 13 ) _
       + "spacebar : fire ."     + chr$( 13 ) _
       + "[esc] : quit game ."   + chr$( 13 ) _
       + "rock : 2 points ."     + chr$( 13 ) _
       + "ufo : 7 points ."
readjoystick 1
if Joy1x > 2 then
  confirm "Use joystick ?" ; joyYN$
end if
''graphicbox #m.g , 0 , 0 , winx , winy
nomainwin
open "asteriods" for graphics as #m
  #m "trapclose [quit]"
  #m "when characterInput [key]"
  #m "fill black"
  #m "backcolor white"
  #m "color white"
  #m "getbmp back 0 0 " ; winx ; " " ; winy
  #m "background back"
  for i = 0 to 100
    #m "addsprite star" ; i ; " star"
    #m "spritexy star"; i ; " " _
    ; rnd( 0 ) * winx ; " " ; rnd( 0 ) * winy
    #m "spritescale star"; i ; " " ; rnd( 0 ) ^ 3 * 100
    #m "spritemovexy star" ; i ; " 0 1"
  next i
  for i = 0 to 4
    #m "addsprite live" ; i ; " p" ; 0
    #m "spritexy live" ; i ; " " ; i * 50 ; " " ; winy - 50
    #m "spritescale live"; i ; " 50"
  next i
  for i = 0 to 20
    #m "addsprite playerfire" ; i ; " fire"
    #m "spritexy playerfire" ; i ; " -100 -100"
  next i
  #m "addsprite rock rock"
  #m "spritemovexy rock 3 1"
  #m "addsprite ufo ufo"
  #m "spritexy ufo winx 50"
  #m "spritemovexy ufo -2 0"
  #m "addsprite ufofire fire"
  #m "spritexy ufofire -100 -100"
  #m "setfocus"
  #m "addsprite player " ; anim$
  #m "spritexy player " ; px ; " " ; py
  call drawpoints
  timer 40 , [tmr]
wait
function nr$( no )
  nr$ = right$( "00" + str$( no ) , 2 )
end function
[tmr]
  q = q - 1
  if q < 0 then
    q = int( rnd( 0 ) * 20 )
    d = int( rnd( 0 ) * 3 )
  end if
  if joyYN$ = "yes" then
    if Joy1x < 256 * 256 / 3 then
        pst = ( pst + 1 )
        if pst > 31 then pst = 0
    end if
    if Joy1x > 256 * 256 * 2 / 3 then
        pst = ( pst - 1 )
        if pst < 0 then pst = 31
    end if
    if Joy1y < 256 * 256 / 3 then
        px = ( px - sin( strad( pst ) ) * 5 )
        if px < -100 then px = winx
        if px > winx then px = -100
        py = ( py - cos( strad( pst ) ) * 5 )
        if py < -100 then py = winy
        if py > winy then py = -100
    end if
    if Joy1y > 256 * 256 * 2 / 3 then
      ''here i wil place a hyperjump in the furture
    end if
  else
    select case lower$( lastkey$ )
      case chr$( _VK_LEFT )
        pst = ( pst + 1 )
        if pst > 32 then pst = 0
      case chr$( _VK_RIGHT )
        pst = ( pst - 1 )
        if pst < 0 then pst = 32
      case chr$( _VK_UP )
        px = ( px - sin( strad( pst ) ) * 5 )
        if px < -100 then px = winx
        if px > winx then px = -100
        py = ( py - cos( strad( pst ) ) * 5 )
        if py < -100 then py = winy
        if py > winy then py = -100
      case chr$( _VK_DOWN )
        ''hyperjump
      case chr$( 27 ) ''escape
        goto [quit]
        wait
      case " "
        #m "spritexy playerfire" ; firetel ; " " _
        ; px - 16 - sin( strad( pst ) ) * 45 ; " " _
        ; py - 16 - cos( strad( pst ) ) * 45
        #m "spritemovexy playerfire" ; firetel ; " " _
        ; sin( strad( pst ) ) * -6 ; " " _
        ; cos( strad( pst ) ) * -6
        firetel = firetel + 1
        if firetel > 20 then firetel = 0
    end select
  end if
  lastkey$ = ""
  #m "spritexy player " ; px - 50 ; " " ; py - 50
  #m "spriteimage player p" ; pst
  for i = 0 to 10
    #m "spritexy? playerfire" ; i ; " x  y"
    if x < -100 or x > winx + 100 _
    or y < -100 or y > winy + 100 then
      #m "spritemovexy playerfire" ; i ; " 0 0"
    end if
  next i
  #m "spritexy? ufo ufox ufoy"
  if ufox < -100 then
    ufox = winx
    ufohasfired = false
    state = rnd( 0 ) * ( winx / 2 ) + winx / 4
  end if
  state = state - 1
  if state < 0 and not( ufohasfired ) then
    ufohasfired = true
    dx = px - ufox
    dy = py - ufoy
    af = sqr( dx ^ 2 + dy ^ 2 )
    #m "spritexy ufofire " ; ufox ; " " ; ufoy
    #m "spritemovexy ufofire " ; dx / af * 6 ; " " ; dy / af * 6
  end if
  #m "spritexy? ufofire x y"
  if x < -100 then #m "spritemovexy ufofire 0 0"
  if x > winx then #m "spritemovexy ufofire 0 0"
  if y < -100 then #m "spritemovexy ufofire 0 0"
  if y > winy then #m "spritemovexy ufofire 0 0"
  #m "spritexy ufo " ; ufox ; " " ; ufoy
  #m "spritexy? rock x y"
  if x < -100 then x = winx
  if x > winx then x = -100
  if y < -100 then y = winy
  if y > winy then y = -100
  #m "spritexy rock " ; x ; " " ; y
  ''colisiondetecion and respons
  #m "spritecollides player playerhit$"
  if instr( playerhit$ , "rock" ) <> 0 _
  or instr( playerhit$ , "ufo" ) <> 0 then
    lives = lives - 1
    call resetscene
  end if
  #m "spritecollides rock rockhit$"
  if instr( rockhit$ , "playerfire" ) then
    points = points + 2
    #m "spritexy rock -100 " ; rnd( 0 ) * winy - 50
    call drawpoints
  end if
  #m "spritecollides ufo ufohit$"
  if instr( ufohit$ , "playerfire" ) then
    points = points + 7
    #m "spritexy ufo " ; winx ; " " _
    ; rnd( 0 ) * ( winy / 2 ) + winy / 4 - 50
    state = rnd( 0 ) * ( winx / 2 ) + winx / 4
    ufohasfired = false
    call drawpoints
  end if
  for i = 0 to 100
    #m "spritexy? star" ;  i ; " x y"
    if y > winy then
      #m "spritexy star" ; i ; " " ; rnd( 0 ) * winx ; " " ; -30
      #m "spritescale star" ; i ; " " _
      ; rnd( 0 ) ^ 3 * 100
    end if
  next i
  #m "drawsprites"
wait
sub resetgame
  level = 0
  lives = 3
  points = 0
  ufohasfired = false
  state = rnd( 0 ) * ( winx / 2 ) + winx / 4
  call resetscene
  notice "Game Over !!"
end sub
sub resetscene
  timer 0
  call drawpoints
  for i = 1 to 4
    #m "spritexy live" ; i ; " " ; i * 50 ; " " ; winy - 50
  next i
  for i = 0 to 20
    #m "spritexy playerfire" ; i ; " -100 -100"
  next i
  #m "spritemovexy rock 3 1"
  #m "spritexy ufo winx 50"
  #m "spritemovexy ufo -2 0"
  #m "spritexy ufofire -100 -100"
  px = winx / 2
  py = winy / 2
  #m "spritexy player " ; px ; " " ; py
  if lives < 0 then call resetgame
  timer 40 , [tmr]
end sub
sub drawpoints
  #m "fill black"
  #m "font Corier_New 50 bold"
  #m "color green"
  #m "backcolor black"
  #m "goto 0 50"
  #m "\POINTS : " ; right$( "        " + str$( points ) , 8 )
  #m "getbmp back 0 0 " ; winx ; " " ; winy
  #m "background back"
  for i = 0 to lives
    #m "spritexy live" ; i ; " " ; i * 50 ; " " ; 50
  next i
  for i = lives to 3
    #m "spritexy live" ; i ; " -100 -100"
  next i
end sub
function strad( st )
  strad = st * pi / 16
end function
[key]
  lastkey$ = right$( Inkey$ , 1 )
wait
[quit]
  unloadbmp "fire"
  unloadbmp "ufo"
  unloadbmp "star"
  for i = 0 to 31
    unloadbmp "p" ; i
  next i
  close #m
end