-- Space Wars by mathfacts@cox.net
-- Created Aug, 2006.  Posetd Nov, 2007.
-- members.cox.net/mathfacts

math.randomseed( math.random(os.time()) )
shot = Sound.load("sounds/shot.wav") ; explode = Sound.load("sounds/explode.wav")
shipexplode = Sound.load("sounds/shipexplode.wav") ; retro = Sound.load("sounds/retro.wav")
back1 = Image.load("sounds/back1.png") ; back2 = Image.load("sounds/back2.png")
back3 = Image.load("sounds/back3.png")
myfont = Font.load("sounds/uni-sol.ttf") ; myfont:setPixelSizes(0, 50)
myfont2 = Font.load("sounds/tabletro.ttf") ; myfont2:setPixelSizes(0, 20)
myfont3 = Font.load("sounds/tabletro.ttf") ; myfont3:setPixelSizes(0, 14)
green = Color.new(0, 255, 0) ; white = Color.new(255, 255, 255) ;red = Color.new(255, 0, 0)
yellow = Color.new(255, 255, 0);grey = Color.new(75,75,75) ; ltblue = Color.new (0,255,255)
black = Color.new (0,0,0) ; colors = {ltblue,white,red,green} ; players = 2 ; solo_on = 1
max_score = 1; help = -1
star_on = -1 ; gravity_on = -1 ; edges_on = -1

function init()
    bsx = { } ; bsy = { } ;  xn = {} ; yn = {} ; sx = {} ; sy = {} ; hx = {} ; hy = {}
    s1x = {-1,-1,0,1,1,3,3,2,-2,-3,-3,-1,-2,0,2}
    s1y = {5,0,2,0,5,0,-3,-5,-5,-3,0,5,-4,-8,-4}
    s2x = {0,1,3,3,2,2,1,0,-1,-2,-2,-3,-3,-1,0,-2,0,2}
    s2y = {5,1,-1,-2,-3,-5,-5,-3,-5,-5,-3,-2,-1,1,5,-4,-8,-4}
    s3x = {-0,1,3,1,3,0,-3,-1,-3,-1,0,-2,0,2}
    s3y = {5,3,2,-1,-5,-2,-5,-1,2,3,5,-4,-8,-4}
    s4x = {-1,1,2,2,1,1,3,0,-3,-1,-1,-2,-2,-1,-1,0,1}
    s4y = {5,5,3,1,0,-2,-5,-3,-5,-2,0,1,3,5,-4,-7,-4}
    max_points = {12,15,11,14}
    text = ""
    draw_bullet = {} ; bullet_xoff = {} ; bullet_yoff = {} ; bullet_vel = {4,4} ; bullet_ang = {}
    crashed ={0,0} ; testx = {} ; testy = {} ; col = 0
    cur_angle = {}; xoff = {}; yoff = {}; ship_scale = {}; vel = {}; turn = {}
    thrust = {}; thrust_dir = {}; thrust_chg = {}; decay = {}; thrust_step = {}
    max_p ={} ; ship_type = {}
    last_angle = {}; new_thrust_angle = {}; boost = {} ; no_ship = {0,0} ; score = {0,0,0,0}
    for a = 1, players do
        cur_angle[a] = 180 ; xoff[a] = 56 ; yoff[a] = 130 ; ship_scale[a] = 3 ; vel[a] = 3.5
        turn[a] = 6 ; thrust[a] = 0 ; thrust_dir[a] = 0 ; thrust_chg[a] = 0 ; decay[a] = .02
        thrust_step[a] = .15 ;  last_angle[a] = 180 ; new_thrust_angle[a] = 180 ; boost[a] = 0
        max_p[a] = max_points[a] ; ship_type[a] = a
    end
        xoff[2] = 416; yoff[2] = 130
    star_points = 17 ; star_scale = 2 ; star_x = 245 ; star_y = 130 ; star_mod = 1  ; back = 1 ; background = back1
    stx = {0,1,3,2,3,2,3,1,0,-1,-3,-2,-3,-2,-3,-1,0} ; sty = {5,2,3,1,0,-1,-3,-2,-5,-2,-3,-1,0,1,3,2,5}
    ship_star_ang = 0 ; star_min = 3; star_max = 3; timer = Timer.new() ; timer:start()
    ship_to_ship_ang = 0 ; ship_mod = 0
end

function options()
   bt = 0
   while bt == 0 do
      pad = Controls.read()
      if pad ~= oldpad then
         if pad:up() then star_on = star_on * -1; end
         if pad:left() then edges_on = edges_on * -1; end
         if pad:right() then gravity_on = gravity_on * -1;  end
         if pad:down() then
            solo_on = solo_on + 1 ; if solo_on > 2 then solo_on = 1 end
            if solo_on == 1 then ship_type[2]=math.random(4); max_p[2] = max_points[ship_type[2]];end
         end
         if pad:l() then ship_type[1] = ship_type[1] + 1
            if ship_type[1] > 4 then ship_type[1] = 1 end
            max_p[1] = max_points[ship_type[1]] ;
         end
         if pad:r() then ship_type[2] = ship_type[2] + 1
            if ship_type[2] > 4 then ship_type[2] = 1 end
            max_p[2] = max_points[ship_type[2]]
         end
         if pad:cross() then max_score = max_score +1;
            if max_score > 3 then max_score = 1 ; end
         end
            if pad:circle() then back = back + 1 ; if back > 4 then back = 1 end
            if back == 1 then background = back1 end
            if back == 2 then background = back2 end
            if back == 3 then background = back3 end
            if back == 4 then background = Image.createEmpty(480, 272) end
         end
         if pad:start() then bt = 1; end
         if pad:square() then help = -help; end
      oldpad = pad
      end
       screen:clear()
							screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)
    			if help == 1 then

          screen:fontPrint(myfont2,5,20,"Controls",yellow)
          screen:fontPrint(myfont2,185,20,"Player 1",ltblue)
          screen:fontPrint(myfont2,345,20,"Player 2",white)
          screen:fontPrint(myfont3,8,40,"Turn Left",green);screen:fontPrint(myfont3,190,40,"Left",green);screen:fontPrint(myfont3,350,40,"Square",green)
          screen:fontPrint(myfont3,8,56,"Turn Right",green); screen:fontPrint(myfont3,190,56,"Right",green);screen:fontPrint(myfont3,350,56,"Circle",green)
          screen:fontPrint(myfont3,8,72,"Thrust",green); screen:fontPrint(myfont3,190,72,"Left Trigger",green);screen:fontPrint(myfont3,350,72,"Right Trigger",green)
          screen:fontPrint(myfont3,8,88,"Fire",green); screen:fontPrint(myfont3,190,88,"Down",green);screen:fontPrint(myfont3,350,88,"Cross",green)
          screen:fontPrint(myfont3,8,104,"Respawn",green); screen:fontPrint(myfont3,190,104,"Up",green);screen:fontPrint(myfont3,350,104,"Triangle",green)
          screen:fontPrint(myfont3,8,130,"Restart",green); screen:fontPrint(myfont3,190,130,"SELECT",green)

          screen:fontPrint(myfont2,5,170,"Options",yellow)
          screen:fontPrint(myfont3,8,185,"Down",green); screen:fontPrint(myfont3,90,185,"1P / 2P",green)
          screen:fontPrint(myfont3,8,200,"Right",green); screen:fontPrint(myfont3,90,200,"Black Hole (2P only)",green)
          screen:fontPrint(myfont3,8,215,"Left",green); screen:fontPrint(myfont3,90,215,"Edges (2P only)",green)
          screen:fontPrint(myfont3,8,230,"Up",green); screen:fontPrint(myfont3,90,230,"Sun (2P only)",green)

          screen:fontPrint(myfont3,235,185,"Circle",green); screen:fontPrint(myfont3,345,185,"Toggle Background",green)
          screen:fontPrint(myfont3,235,200,"Cross",green); screen:fontPrint(myfont3,345,200,"Play to 5/10/15",green)
          screen:fontPrint(myfont3,235,215,"Left Trigger",green); screen:fontPrint(myfont3,345,215,"Select P1 Ship Type",green)
          screen:fontPrint(myfont3,235,230,"Right Trigger",green); screen:fontPrint(myfont3,345,230,"Select P2 Ship Type",green)
          screen:fontPrint(myfont3,160,260,"Press SQUARE to return",green)

		     else

          screen:fontPrint(myfont, 80,30,"SPACE WARS",green)
          screen:fontPrint(myfont2,60,170,"Human",yellow)
             if solo_on == 1 then screen:fontPrint(myfont2,364,170,"PSP",yellow); star_on = -1; edges_on = -1; gravity_on = -1 end
             if solo_on == 2 then screen:fontPrint(myfont2,348,170,"Human",yellow) end
          if star_on == 1 then draw_star() end
          if edges_on == 1 then draw_edges() end
          if gravity_on == 1 then screen:fontPrint(myfont3, 210,180,"Black Hole",green) end
          screen:fontPrint(myfont2, 238,70,max_score*5,yellow)
          screen:fontPrint(myfont2, 55,150,"Player 1",red)
          screen:fontPrint(myfont2, 340,150,"Player 2",red)
          for p = 1, players do
             rotate(p) ; t1 = xoff[p]; t2  = yoff[p] ;
             xoff[p] = 95*p*p; yoff[p] = 115 ; draw_ship(p,colors[p])
             xoff[p] = t1; yoff[p] = t2;
          end
          screen:fontPrint(myfont2, 175,240,"START to play",green)
          screen:fontPrint(myfont2, 165,265,"SQUARE for help",green)
        end
					   screen.waitVblankStart()
        screen.flip()
   end
end

function play()
   pad = Controls.read()
   if pad:right() then
     if no_ship[1] ~= 1 then cur_angle[1] = cur_angle[1] - turn[1]  end
   end
   if pad:left() then
      if no_ship[1] ~= 1 then cur_angle[1] = cur_angle[1] + turn[1] end
   end
   if pad:l() then
     if no_ship[1] ~= 1 then
        thrust_chg[1] =  thrust_step[1] ; new_thrust_angle[1] = cur_angle[1] ; boost[1] = 1
     end
   end
   if pad:down() then
       if no_ship[1] ~= 1 then
         if draw_bullet[1] ~= 1 then
            shot:play() ; bullet_xoff[1] = xoff[1] ; bullet_yoff[1] = yoff[1]
            draw_bullet[1] = 1 ; bullet_ang[1] = cur_angle[1]
            bullet_xoff[1] = bullet_xoff[1] + ship_scale[1]*5*math.sin(math.rad(bullet_ang[1]))
            bullet_yoff[1] = bullet_yoff[1] + ship_scale[1]*5*math.cos(math.rad(bullet_ang[1]))
         end
      end
   end
   if pad:up() then
     if no_ship[1] == 1 then
        no_ship[1] = 0 ; cur_angle[1] = math.random (360) ; xoff[1] = (math.random(450)+20)
        while xoff[1] > 210 and xoff[1] < 280 do
           xoff[1] = (math.random(450)+20);
        end
        yoff[1] = math.random(200)+20 ; thrust[1] = 0; last_angle[1] = cur_angle[1]; boost[1] = 0
     end
   end

   if pad:circle() then
      if no_ship[2] ~= 1 then cur_angle[2] = cur_angle[2] - turn[2] ; end
   end
   if pad:square() then
      if no_ship[2] ~= 1 then cur_angle[2] = cur_angle[2] + turn[2] ;  end
   end
   if pad:r() then
     if no_ship[2] ~= 1 then
        thrust_chg[2] =  thrust_step[2] ; new_thrust_angle[2] = cur_angle[2] ; boost[2] = 1
     end
   end
   if pad:cross() then
       if no_ship[2] ~= 1 then
         if draw_bullet[2] ~= 1 then
            shot:play() ; bullet_xoff[2] = xoff[2] ; bullet_yoff[2] = yoff[2]
            draw_bullet[2] = 1 ; bullet_ang[2] = cur_angle[2]
            bullet_xoff[2] = bullet_xoff[2] + ship_scale[2]*5*math.sin(math.rad(bullet_ang[2]))
            bullet_yoff[2] = bullet_yoff[2] + ship_scale[2]*5*math.cos(math.rad(bullet_ang[2]))
         end
      end
   end
   if pad:triangle() then
     if no_ship[2] == 1 then
        no_ship[2] = 0 ; cur_angle[2] = math.random (360); xoff[2] = (math.random(450)+20)
        while xoff[2] < 280 and xoff[2] > 210 do
           xoff[2] = (math.random(450)+20);
        end
        yoff[2] = math.random(200)+20 ; thrust[2] = 0; last_angle[2] = cur_angle[2]; boost[2] = 0
     end
   end

   if solo_on == 1 then
      if no_ship[2] == 1 then
         if timer:time()  > 2500 then
            no_ship[2] = 0 ; cur_angle[2] = math.random (360); xoff[2] = (math.random(450)+20)
               while xoff[2] < 280 and xoff[2] > 210 do
                  xoff[2] = (math.random(450)+20);
               end
            yoff[2] = math.random(200)+20 ; thrust[2] = 0; last_angle[2] = cur_angle[2]; boost[2] = 0
         end
      end

     if no_ship[1] ~= 1 and no_ship[2] ~= 1 then
          if math.random(5) == 5 then
             ship_to_ship_ang = (180/math.pi)*math.atan2((xoff[2]-xoff[1]), (yoff[2]-yoff[1]))
             ship_mod = 1
             if math.abs(cur_angle[2] - ship_to_ship_ang) >= 180 then ship_mod = -1 end
             cur_angle[2] = cur_angle[2] + turn[2]*ship_mod
          end

          if math.random(3) == 1 then
             thrust_chg[2] =  thrust_step[2] ; new_thrust_angle[2] = cur_angle[2] ; boost[2] = 1
          end

          if draw_bullet[2] ~= 1 then
             if math.random(10) == 1 then
                shot:play() ; bullet_xoff[2] = xoff[2] ; bullet_yoff[2] = yoff[2]
                draw_bullet[2] = 1 ; bullet_ang[2] = cur_angle[2]
                bullet_xoff[2] = bullet_xoff[2] + ship_scale[2]*5*math.sin(math.rad(bullet_ang[2]))
                bullet_yoff[2] = bullet_yoff[2] + ship_scale[2]*5*math.cos(math.rad(bullet_ang[2]))
             end
          end
       end
   end
   if pad:select() then
      init() ; options()
   end

   for q = 1,players do
      if cur_angle[q] > 360 then cur_angle[q] = cur_angle[q] - 360 end
      if cur_angle[q] < 0 then cur_angle[q] = cur_angle[q] + 360 end
   end
end

function disp()
  screen:clear()
  screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)
  if text ~= "" then
    screen:fontPrint(myfont3,190,100,text, colors[1])
    screen:fontPrint(myfont3,150,140,"Press select to play again.", colors[1])
    screen.flip()
    screen.waitVblankStart()
    pad = Controls.read()
        while not pad:select() do
           pad = Controls.read()
        end
    init()
    options()
  end

  check_crash(1)
  for p = 1, players do
     rotate(p)
     if gravity_on == 1 then set_thrust(p) end
     if gravity_on == 1 then gravity(p) end
     draw_ship(p,colors[p])
     if star_on == 1 then draw_star() end
     if edges_on == 1 then draw_edges() end
     draw_bull(p)
     check_hits(p)
     if star_on == 1 then check_star_hits(p) end
     if star_on == 1 then check_ship_hits(p) end
  end

  for c = 1, players do
    screen:fontPrint(myfont3,430*(c-1)+15,12,score[c], colors[c])
  end
  screen.waitVblankStart()
  screen.flip()
end

function check_hits(p)
  if draw_bullet[p] == 1 then
     ship_to_hit = p + 1
     if ship_to_hit > players then ship_to_hit = 1; end
     if no_ship[ship_to_hit] ~= 1 then
        rotate(ship_to_hit)
        bxp = bullet_xoff[p] ;  byp = bullet_yoff[p] ; crashed[p] = 0
           for t = 1, max_p[ship_to_hit] do
              testx[t] = ((ship_scale[ship_to_hit]+1) * xn[t]) + xoff[ship_to_hit]
              testy[t] = ((ship_scale[ship_to_hit]+1) * yn[t]) + yoff[ship_to_hit]
           end
           check (max_p[ship_to_hit], testx, testy, bxp, byp)
              if col == 1 then
                 explode:play() ; no_ship[ship_to_hit]=1; draw_bullet[p] = 0 ; score[p] = score[p] +1
                 timer:reset() ; timer:start()
              end
     end
  end
end

function check_star_hits(p)
  if draw_bullet[p] == 1 then
     bxp = bullet_xoff[p] ;  byp = bullet_yoff[p]
     for t = 1, star_points do
        testx[t] = ((star_scale+1) * stx[t]) + star_x
        testy[t] = ((star_scale+1) * sty[t]) + star_y
     end
     check (star_points, testx, testy, bxp, byp)
        if col == 1 then
           draw_bullet[p] = 0
        end
  end
end

function check_ship_hits(p)
   if no_ship[p] ~= 1 then
      star_hit_ship = 0 ; pp = p
      rotate(pp)
      ship_to_hit = p + 1
      if ship_to_hit > players then ship_to_hit = 1; end
      for b = 1,max_p[p] - 1 do
         bxp = (xn[b]*ship_scale[p]+1)+xoff[p] ;  byp = (yn[b]*ship_scale[p])+yoff[p]
         if star_hit_ship == 0 then
            for t = 1, star_points do
               testx[t] = ((star_scale+1) * stx[t]) + star_x
               testy[t] = ((star_scale+1) * sty[t]) + star_y
            end
            check (star_points, testx, testy, bxp, byp)
            if col == 1 then
               no_ship[p] = 1; star_hit_ship = 1; score[ship_to_hit] = score[ship_to_hit] +1
               timer:reset() ; timer:start()
            end
        end
     end
   end
end

function check_crash(p)
   if no_ship[p] ~= 1 then
      hit_other_ship = 0 ; pp = p
      ship_to_hit = p + 1
      if ship_to_hit > players then ship_to_hit = 1; end
      rotate(pp) ; sx = xn ; sy = yn
      rotate(ship_to_hit) ; hx = xn ; hy = yn
        if no_ship[ship_to_hit] ~= 1 then
          for b = 1,max_p[p] - 1 do
             bxp = (sx[b]*ship_scale[p])+xoff[p] ;  byp = (sy[b]*ship_scale[p])+yoff[p]
             if hit_other_ship == 0 then
                for t = 1, max_p[ship_to_hit] do
                   testx[t] = ((ship_scale[ship_to_hit]) * hx[t]) + xoff[ship_to_hit]
                   testy[t] = ((ship_scale[ship_to_hit]) * hy[t]) + yoff[ship_to_hit]
                end
                check (max_p[ship_to_hit], testx, testy, bxp, byp)
                if col == 1 then
                   no_ship[p] = 1; no_ship[ship_to_hit] = 1 ; hit_other_ship = 1;
                   timer:reset() ; timer:start()
                end
             end
          end
        end
   end
end

function rotate(p)
   if ship_type[p] == 1 then x = s1x ; y = s1y end
   if ship_type[p] == 2 then x = s2x ; y = s2y end
   if ship_type[p] == 3 then x = s3x ; y = s3y end
   if ship_type[p] == 4 then x = s4x ; y = s4y end
      for a = 1,max_p[p] do
         r1 = math.sqrt((x[a]*x[a])+(y[a]*y[a]))
         angle = math.atan2(x[a],y[a])
         angle = angle + math.rad(cur_angle[p])
         xn[a] = r1 * math.sin(angle)
         yn[a] = r1 * math.cos(angle)
      end
   end

function gravity(p)
  ship_star_ang = (180/math.pi)*math.atan2((star_x-xoff[p]), (star_y-yoff[p]))
  new_thrust_angle[p] = ship_star_ang
		thrust_chg[p] =  .0175
end

function draw_edges()
screen:fillRect(0,0, 480,2,red)
screen:fillRect(478,0, 2,270,red)
screen:fillRect(0,270, 480,2,red)
screen:fillRect(0,0, 2,270,red)
end

function draw_points(pointsx,pointsy,maxpoints,to_scale,x_offset,y_offset,invx,invy,color,is_ufo)
   for a = 1,maxpoints-1 do
      screen:drawLine((invx*pointsx[a])*to_scale+x_offset, (invy*pointsy[a])*to_scale+y_offset,(invx*pointsx[a+1])*to_scale+x_offset, (invy*pointsy[a+1])*to_scale+y_offset, color)
   end
end

function draw_ship(p,color)
   pp = p
   if no_ship[p] ~= 1 then
      set_thrust(pp)
      thrust[p] = thrust[p] - decay[p]
      if thrust[p] < 0 then thrust[p] = 0 end
 
      if edges_on == 1 then
        if xoff[p] > 465 then xoff[p] = 465; thrust[p] = 0 end
        if xoff[p] < 15 then xoff[p] = 15; thrust[p] = 0 end
        if yoff[p] > 255 then yoff[p] = 255; thrust[p] = 0 end
        if yoff[p] < 15 then yoff[p] = 15; thrust[p] = 0 end
      else
        if xoff[p] > 465 then xoff[p] = 15 end
        if xoff[p] < 15 then xoff[p] = 465 end
        if yoff[p] > 255 then yoff[p] = 15 end
        if yoff[p] < 15 then yoff[p] = 255 end
      end
      draw_points(xn,yn,max_p[p],ship_scale[p],xoff[p],yoff[p],1,1,color,0)
      if boost[p] == 1 then
         retro:play()
         boost[p] = 0; tmp_max = max_p[p]; max_p[p] = max_p[p] +3;
         rotate(pp);
         max_p[p] = tmp_max
          for a = max_p[p], max_p[p] + 3 do
             bsx[a-max_p[p]] = xn[a]; bsy[a-max_p[p]] = yn[a] ;
          end
          draw_points(bsx,bsy,3,ship_scale[p],xoff[p],yoff[p],1,1,yellow,0);
      end
   end
   if no_ship[p] == 1 then
      screen:fontPrint(myfont3,150,200+(p *11),"Player "..p.." Needs to respawn",colors[p])
   end
end

function draw_star()
      draw_points(stx,sty,star_points,star_scale,star_x,star_y,1,1,yellow,0)
   if timer:time()  > 100 then
      star_scale = star_scale + star_mod
      timer:reset() ; timer:start()
      if star_scale < star_min then star_mod = star_mod * -1 end
      if star_scale > star_max then star_mod = star_mod * -1 end
   end
end

function set_thrust(p)
   thrust[p] = thrust[p] + thrust_chg[p]
   xdelta = 0; ydelta = 0
   if new_thrust_angle[p] ~= last_angle[p] then
      xnew = thrust_chg[p]*math.sin(math.rad(new_thrust_angle[p]))
      ynew = thrust_chg[p]*math.cos(math.rad(new_thrust_angle[p]))
      xold = (thrust[p]-thrust_chg[p])*math.sin(math.rad(last_angle[p]))
      yold = (thrust[p]-thrust_chg[p])*math.cos(math.rad(last_angle[p]))
      xdelta = xnew + xold ; ydelta = ynew + yold
      thrust[p] = math.sqrt((xdelta*xdelta)+(ydelta*ydelta))
      last_angle[p] = math.deg(math.atan2(xdelta,ydelta))
    else
       xdelta = thrust[p]*math.sin(math.rad(last_angle[p]))
       ydelta = thrust[p]*math.cos(math.rad(last_angle[p]))
    end
    xoff[p] = xoff[p] + xdelta ; yoff[p] = yoff[p] + ydelta
    thrust_chg[p] = 0
    if thrust[p] > vel[p] then thrust[p] = vel[p]; end
    if thrust[p] < 0 then thrust[p] = 0; end
end

function draw_bull(p)
  if draw_bullet[p] == 1 then
      screen:fillRect(bullet_xoff[p],bullet_yoff[p], 2,2,colors[p])
      bullet_xoff[p] = bullet_xoff[p] + bullet_vel[p]*math.sin(math.rad(bullet_ang[p]))
      bullet_yoff[p] = bullet_yoff[p] + bullet_vel[p]*math.cos(math.rad(bullet_ang[p]))
      if bullet_xoff[p] < 0 then draw_bullet[p] = 0 end
      if bullet_yoff[p] < 0 then draw_bullet[p] = 0 end
      if bullet_xoff[p] > 480 then draw_bullet[p] = 0 end
      if bullet_yoff[p] > 272 then draw_bullet[p] = 0 end
  end
end

function check(npol, xp, yp, xp1, yp1)
   col = 0 ;  j = npol
   for i = 1,npol do
      if ((((yp[i] <= yp1) and (yp1 <yp[j])) or ((yp[j] <= yp1) and (yp1 < yp[i]))) and (xp1<((((xp[j]-xp[i])*(yp1-yp[i]))/(yp[j]-yp[i]))+xp[i]))) then
        if col == 0 then col = 1 else col = 0  end
      end
      j = i
   end
end

function game_over()
ply = {} ; win = 0
   for c = 1, players do
      ply[c] = 0
      if score[c] > max_score * 5 - 1 then ply[c] = 1
         text = "Player " .. c .. " Wins!" ; win = win + 1
         if c == 2 and solo_on == 1 then text = "You lose, I Win!!" end
         if c == 1 and solo_on == 1 then text = "You Win, I lose!!" end
      end
   end
   if win > 1 then text = "It's a Tie!!" end
end

function debug()
   screen:print(200,70,"Help = "  .. help, white)
end

init()
options()

while true do
      play()
      disp()
      game_over()
end