set display mode 640,480,32
sync on
sync rate 50
hide mouse
 
randomize timer()
 
global dim x#(360)
global dim y#(360)
 
for i=0 to 359
   x#(i) = sin(i)
   y#(i) = cos(i+180)
next i
 
sx# = 320         ` X Position of ship
sy# = 240         ` Y Position of ship
angle = 0         ` Ships angle
thrustx# = 0      ` Thrust for the ship
thrusty# = 0
movementx# = 0    ` The current momentum
movementy# = 0
 
dim rockx#(50)    ` Set up variables for the asteroids...
dim rocky#(50)
dim rmovex#(50)
dim rmovey#(50)
dim rangle(50)
dim rspeed#(50)
numrocks = 10
 
gosub CreateRocks
 
repeat
   DrawShip(sx#,sy#,angle)
   gosub DrawRocks
   gosub ScoreBoard
 
   sync
   cls
 
   gosub Controls
   gosub MoveRocks
   gosub MoveShip
until inkey$()="q"
end
 
 
REM // Subroutines and Functions
 
ScoreBoard:
   ink rgb(0,255,0),rgb(0,0,0)
   text 0,0,"Asteroids: "+str$(numrocks)
return
 
CreateRocks:
   for i=1 to numrocks
      rockx#(i) = int(rnd(640))
      rocky#(i) = int(rnd(480))
      rangle(i) = int(rnd(360))
      rspeed#(i) = rnd(1.5)
      rmovex#(i) = x#(rangle(i))*rspeed#(i)
      rmovey#(i) = y#(rangle(i))*rspeed#(i)
   next i
return
 
Controls:
   if leftkey()=1 then angle=angle-2
   if rightkey()=1 then angle=angle+2
   angle = wrapvalue(angle)
   if upkey()=1
      thrustx# = x#(angle)*0.4
      thrusty# = y#(angle)*0.4
      movementx# = movementx# + thrustx#
      movementy# = movementy# + thrusty#
   endif
return
 
MoveShip:
   sx# = sx# + movementx#
   sy# = sy# + movementy#
   if sx# < 0 then sx# = 639
   if sx# > 639 then sx# = 0
   if sy# < 0 then sy# = 479
   if sy# > 479 then sy# = 0
return
 
MoveRocks:
   for i=1 to numrocks
      rockx#(i) = rockx#(i) + rmovex#(i)
      rocky#(i) = rocky#(i) + rmovey#(i)
      rangle(i) = rangle(i) + 1
      rangle(i) = wrapvalue(rangle(i))
      if rockx#(i) < 0 then rockx#(i) = 639
      if rockx#(i) > 639 then rockx#(i) = 0
      if rocky#(i) < 0 then rocky#(i) = 479
      if rocky#(i) > 479 then rocky#(i) = 0
   next i
return
 
DrawRocks:
   rs = 20           `` Size of the Rock
 
   ink rgb(128,128,128),rgb(0,0,0)
   for i=1 to numrocks
      line rockx#(i)+x#(int(wrapvalue(rangle(i))))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i))))*rs,rockx#(i)+x#(int(wrapvalue(rangle(i)+72)))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i)+72)))*rs
      line rockx#(i)+x#(int(wrapvalue(rangle(i)+72)))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i)+72)))*rs,rockx#(i)+x#(int(wrapvalue(rangle(i)+144)))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i)+144)))*rs
      line rockx#(i)+x#(int(wrapvalue(rangle(i)+144)))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i)+144)))*rs,rockx#(i)+x#(int(wrapvalue(rangle(i)+216)))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i)+216)))*rs
      line rockx#(i)+x#(int(wrapvalue(rangle(i)+216)))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i)+216)))*rs,rockx#(i)+x#(int(wrapvalue(rangle(i)+288)))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i)+288)))*rs
      line rockx#(i)+x#(int(wrapvalue(rangle(i)+288)))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i)+288)))*rs,rockx#(i)+x#(int(wrapvalue(rangle(i))))*rs,rocky#(i)+y#(int(wrapvalue(rangle(i))))*rs
   next i
return
 
function DrawShip(sx#,sy#,angle)
   sn = 20           ` Distance to ship nose from the actual ship position
   sr = int(sn*0.8)  ` Distance to each Rear corner of the ship
 
   ink rgb(255,255,0),rgb(0,0,0)
   line sx#+x#(int(wrapvalue(angle)))*sn,sy#+y#(int(wrapvalue(angle)))*sn,sx#+x#(int(wrapvalue(angle+135)))*sr,sy#+y#(int(wrapvalue(angle+135)))*sr
   line sx#+x#(int(wrapvalue(angle+135)))*sr,sy#+y#(int(wrapvalue(angle+135)))*sr,sx#+x#(int(wrapvalue(angle+225)))*sr,sy#+y#(int(wrapvalue(angle+225)))*sr
   line sx#+x#(int(wrapvalue(angle+225)))*sr,sy#+y#(int(wrapvalue(angle+225)))*sr,sx#+x#(int(wrapvalue(angle)))*sn,sy#+y#(int(wrapvalue(angle)))*sn
endfunction