backdrop on
color backdrop 0
sync on
sync rate 60
 
randomize timer()
 
Global ASTEROID_SIZE = 50
Global ASTEROID_COUNT = 10
 
type asteroid
   pt1 as integer
   pt2 as integer
   pt3 as integer
   pt4 as integer
   pt5 as integer
   pt6 as integer
   x as integer
   y as integer
   speed as float
   angle as float
endtype
 
rem array to hold all asteroids
dim rocks(0) as asteroid
 
rem initialize the asteroids
for t = 1 to ASTEROID_COUNT
   array insert at bottom rocks()
   makeRandomAsteroid(t)
   resetAsteroid(t)
   rocks(t).x = rnd(640)
next t
 
 
DO
 
 
   gosub _player_controls
   gosub _update_asteroids
   gosub _handle_bullets
   gosub _draw_player
   gosub _draw_asteroids
 
 
   sync
LOOP
 
 
 
 
 
 
 
REM PLAYER CONTROLS
_player_controls:
  if upkey()=1 then dec py#,3.0+veloc#
  if downkey()=1 then inc py#,3.0+veloc#
  if leftkey()=1 then dec px#,4.0+veloc#
  if rightkey()=1 then inc px#,4.0+veloc#
  if controlkey() = 1 and bdelay >= waiting and ammo>0 then fireshot=1:bdelay=0
  if shiftkey()=1 then lazer=1
return
 
 
 
REM UPDATE PLAYER
_draw_player:
   rem ship boundaries
   remstart
   if px# < 1130 then px#=1130
   if px# > 1840 then px#=1840
   if py# < 20 then py#=20
   if py# > 590 then py#=590
   remend
 
   ink rgb(0,0,255),0
   line px#,py#,px#+40,py#
   line px#+40,py#,px#+50,py#+20
   line px#,py#,px#,py#+20
   line px#,py#+20,px#+50,py#+20
 
return
 
 
 
REM UPDATES POSITION OF ASTEROIDS
_update_asteroids:
   for t = 1 to ASTEROID_COUNT
      rocks(t).x = rocks(t).x - rocks(t).speed
      rocks(t).angle = rocks(t).angle + rocks(t).speed
      if rocks(t).x < 0-ASTEROID_SIZE then resetAsteroid(t)
   next t
return
 
 
 
REM DRAWS THE ASTEROIDS ON THE SCREEN
_draw_asteroids:
   for t = 1 to ASTEROID_COUNT
      remstart
      mx = mousex()
      my = mousey()
      if pointInAsteroid(mx,my,t) = 1
         ink rgb(255,0,0),0
      else
         ink rgb(255,255,255),0
      endif
      remend
      ink rgb(0,255,0),0
      x1 = sin(rocks(t).angle)*rocks(t).pt1
      y1 = cos(rocks(t).angle)*rocks(t).pt1
      x2 = sin(rocks(t).angle+60)*rocks(t).pt2
      y2 = cos(rocks(t).angle+60)*rocks(t).pt2
      x3 = sin(rocks(t).angle+120)*rocks(t).pt3
      y3 = cos(rocks(t).angle+120)*rocks(t).pt3
      x4 = sin(rocks(t).angle+180)*rocks(t).pt4
      y4 = cos(rocks(t).angle+180)*rocks(t).pt4
      x5 = sin(rocks(t).angle+240)*rocks(t).pt5
      y5 = cos(rocks(t).angle+240)*rocks(t).pt5
      x6 = sin(rocks(t).angle+300)*rocks(t).pt6
      y6 = cos(rocks(t).angle+300)*rocks(t).pt6
      line rocks(t).x + x1, rocks(t).y + y1,rocks(t).x + x2, rocks(t).y + y2
      line rocks(t).x + x2, rocks(t).y + y2,rocks(t).x + x3, rocks(t).y + y3
      line rocks(t).x + x3, rocks(t).y + y3,rocks(t).x + x4, rocks(t).y + y4
      line rocks(t).x + x4, rocks(t).y + y4,rocks(t).x + x5, rocks(t).y + y5
      line rocks(t).x + x5, rocks(t).y + y5,rocks(t).x + x6, rocks(t).y + y6
      line rocks(t).x + x6, rocks(t).y + y6,rocks(t).x + x1, rocks(t).y + y1
   next t
 
return
 
 
REM UPDATES BULLET POSITIONS AND FIRING
_handle_bullets:
 
 
return
 
REM MAKES A RANDOM ASTEROID
function makeRandomAsteroid(index as integer )
   rocks(index).pt1 = rnd(ASTEROID_SIZE-5)+5
   rocks(index).pt2 = rnd(ASTEROID_SIZE-5)+5
   rocks(index).pt3 = rnd(ASTEROID_SIZE-5)+5
   rocks(index).pt4 = rnd(ASTEROID_SIZE-5)+5
   rocks(index).pt5 = rnd(ASTEROID_SIZE-5)+5
   rocks(index).pt6 = rnd(ASTEROID_SIZE-5)+5
   `rocks(index).pt1 = ASTEROID_SIZE
   `rocks(index).pt2 = ASTEROID_SIZE
   `rocks(index).pt3 = ASTEROID_SIZE
   `rocks(index).pt4 = ASTEROID_SIZE
   `rocks(index).pt5 = ASTEROID_SIZE
   `rocks(index).pt6 = ASTEROID_SIZE
endfunction
 
 
function resetAsteroid(index as integer)
   makeRandomAsteroid(index)
   rocks(index).x = screen width() + ASTEROID_SIZE
   rocks(index).y = rnd(screen height())
   rocks(index).speed = (rnd(400)+100)/100.0
   rocks(index).angle = 0
endfunction
 
 
REM IF POINT IS INSIDE OF ASTEROID (OUTLINE EXCLUSIVE)
function pointInAsteroid(x as float, y as float, index as integer)
   count = 0
   answer = 0
   x1 = sin(rocks(index).angle)*rocks(index).pt1
   y1 = cos(rocks(index).angle)*rocks(index).pt1
   x2 = sin(rocks(index).angle+60)*rocks(index).pt2
   y2 = cos(rocks(index).angle+60)*rocks(index).pt2
   x3 = sin(rocks(index).angle+120)*rocks(index).pt3
   y3 = cos(rocks(index).angle+120)*rocks(index).pt3
   x4 = sin(rocks(index).angle+180)*rocks(index).pt4
   y4 = cos(rocks(index).angle+180)*rocks(index).pt4
   x5 = sin(rocks(index).angle+240)*rocks(index).pt5
   y5 = cos(rocks(index).angle+240)*rocks(index).pt5
   x6 = sin(rocks(index).angle+300)*rocks(index).pt6
   y6 = cos(rocks(index).angle+300)*rocks(index).pt6
   inc count, point_line(x,y,rocks(index).x + x1, rocks(index).y + y1,rocks(index).x + x2, rocks(index).y + y2)
   inc count, point_line(x,y,rocks(index).x + x2, rocks(index).y + y2,rocks(index).x + x3, rocks(index).y + y3)
   inc count, point_line(x,y,rocks(index).x + x3, rocks(index).y + y3,rocks(index).x + x4, rocks(index).y + y4)
   inc count, point_line(x,y,rocks(index).x + x4, rocks(index).y + y4,rocks(index).x + x5, rocks(index).y + y5)
   inc count, point_line(x,y,rocks(index).x + x5, rocks(index).y + y5,rocks(index).x + x6, rocks(index).y + y6)
   inc count, point_line(x,y,rocks(index).x + x6, rocks(index).y + y6,rocks(index).x + x1, rocks(index).y + y1)
   if count = 0 then answer = 1
endfunction answer
 
 
REM returns 1 if point is to the right of line, else 0
REM points must be in clockwise order
function point_line(px#,py#,x1#,y1#,x2#,y2#)
   dp# = (x2# - x1#) * (py# - y1#) - (px# - x1#) * (y2# - y1#)
   answer = (dp#>0)
endfunction answer