REM **************************
REM *         PS2tris        *
REM *                        *
REM *     by Peter Stock     *
REM *  www.peterstock.co.uk  *
REM **************************
 
KEY_REPEAT_TIME = 15 : SLIDE_TIME = 30

dim Port(1) : dim OldPort(1) : dim KeyRepeat(1)

dim Shapes(6,3,3,1)
dim Grid(1,12,25) : dim StackHeight(1)
dim CurrentShape(1,1) : dim NextShape(1,1)
dim Coords(1,2) : dim SpeedLevel(1) : dim SpeedCount(1)
dim FallDelta(1) : dim SlideTimer(1) : dim FastFall(1)

dim LinesFalling(1) : dim FallOffset(1,25)
dim LinesRising(1) : dim RiseOffset(1)

dim Score(1) : dim WinRecord(1)

dim DrawBackground(1) : dim DrawScore(1,1)
dim DrawBlocksFalling(1,1) : dim DrawBlocksFallingYMin(1)
dim DrawBlocksRising(1,1) : dim DrawNext(1,1)
dim DrawAddedBlock(1,1) : dim AddedBlockCoords(1,1)
dim AddedBlockShape(1,1) : dim DrawCurrentBlock(1)
dim EraseCurrentBlock(1,1) : dim OldCoords(1,2)
dim OldShape(1,1) : dim OldRiseOffset(1)

dim FallSpeeds(2) : dim SpeedIncreases(2)
dim OptionChoice(1) : dim NumSubOptions(2)
dim Options(1,2) : dim OptionNames$(2,3)
goto START


label GAME_START
poke "textalign", "rb"
clear window
for p=0 to NumPlayers
for i=0 to 12 : for j=0 to 25
  if (i = 0 or i >= 11 or j = 0) then Grid(p,i,j) = 1
  else Grid(p,i,j) = 0 endif
next j : next i
StackHeight(p) = 0
CurrentShape(p,0) = int(ran(7)) : CurrentShape(p,1) = 0
NextShape(p,0) = int(ran(7)) : NextShape(p,1) = 0
Coords(p,0) = 5 : Coords(p,1) = 22 : Coords(p,2) = 0
SpeedLevel(p) = Options(p,0) : SpeedCount(p) = 0
FallDelta(p) = FallSpeeds(SpeedLevel(p))
SlideTimer(p) = SLIDE_TIME : FastFall(p) = 0
LinesFalling(p) = 0 : LinesRising(p) = 0
for i=0 to 25 : FallOffset(p,i) = 0 : next i
RiseOffset(p) = 0 : Score(p) = 0
DrawScore(p,0) = 1 : DrawScore(p,1) = 1
DrawBlocksFalling(p,0) = 0 : DrawBlocksFalling(p,1) = 0
DrawBlocksRising(p,0) = 0 : DrawBlocksRising(p,1) = 0
DrawNext(p,0) = 1 : DrawNext(p,1) = 1
DrawAddedBlock(p,0) = 0 : DrawAddedBlock(p,1) = 0
DrawCurrentBlock(p) = 1
EraseCurrentBlock(p,0) = 0 : EraseCurrentBlock(p,1) = 0
next p
DrawBackground(0) = 1 : DrawBackground(1) = 1
repeat
REM *** read the controller ports ***
OldPort(0) = Port(0) : OldPort(1) = Port(1)
Port(0) = peek("port1") : Port(1) = peek("port2")
for p=0 to NumPlayers
  if (LinesFalling(p) = 0) then
   Port = Port(p) : OldPort = OldPort(p)
   if (and(Port,32) <> 0) then
    if (and(OldPort,32) = 0 or KeyRepeat(p) = 0) then
     x = Coords(p,0) : y = Coords(p,1)
     s = CurrentShape(p,0) : r = CurrentShape(p,1)
     if ((Grid(p,x+1+Shapes(s,r,0,0),y+Shapes(s,r,0,1)) = 0 and Grid(p,x+1+Shapes(s,r,1,0),y+Shapes(s,r,1,1)) = 0 and Grid(p,x+1+Shapes(s,r,2,0),y+Shapes(s,r,2,1)) = 0 and Grid(p,x+1+Shapes(s,r,3,0),y+Shapes(s,r,3,1)) = 0) and ((Grid(p,x+1+Shapes(s,r,0,0),y+1+Shapes(s,r,0,1)) = 0 and Grid(p,x+1+Shapes(s,r,1,0),y+1+Shapes(s,r,1,1)) = 0 and Grid(p,x+1+Shapes(s,r,2,0),y+1+Shapes(s,r,2,1)) = 0 and Grid(p,x+1+Shapes(s,r,3,0),y+1+Shapes(s,r,3,1)) = 0) or Coords(p,2) = 0)) then
      Coords(p,0) = Coords(p,0)+1
      KeyRepeat(p) = KEY_REPEAT_TIME
     else KeyRepeat(p) = 0 endif
    elsif (KeyRepeat(p) > 0) then
     KeyRepeat(p) = KeyRepeat(p)-1
    endif
   endif
   if (and(Port,128) <> 0) then
    if (and(OldPort,128) = 0 or KeyRepeat(p) = 0) then
     x = Coords(p,0) : y = Coords(p,1)
     s = CurrentShape(p,0) : r = CurrentShape(p,1)
     if ((Grid(p,x-1+Shapes(s,r,0,0),y+Shapes(s,r,0,1)) = 0 and Grid(p,x-1+Shapes(s,r,1,0),y+Shapes(s,r,1,1)) = 0 and Grid(p,x-1+Shapes(s,r,2,0),y+Shapes(s,r,2,1)) = 0 and Grid(p,x-1+Shapes(s,r,3,0),y+Shapes(s,r,3,1)) = 0) and ((Grid(p,x-1+Shapes(s,r,0,0),y+1+Shapes(s,r,0,1)) = 0 and Grid(p,x-1+Shapes(s,r,1,0),y+1+Shapes(s,r,1,1)) = 0 and Grid(p,x-1+Shapes(s,r,2,0),y+1+Shapes(s,r,2,1)) = 0 and Grid(p,x-1+Shapes(s,r,3,0),y+1+Shapes(s,r,3,1)) = 0) or Coords(p,2) = 0)) then
      Coords(p,0) = Coords(p,0)-1
      KeyRepeat(p) = KEY_REPEAT_TIME
     else KeyRepeat(p) = 0 endif
    elsif (KeyRepeat(p) > 0) then
     KeyRepeat(p) = KeyRepeat(p)-1
    endif
   endif

   if (and(Port,16384)<>0 and and(OldPort,16384)=0) then
    x = Coords(p,0) : y = Coords(p,1)
    s = CurrentShape(p,0) : r = CurrentShape(p,1)+1
    if (r > 3) r = 0
    if ((Grid(p,x+Shapes(s,r,0,0),y+Shapes(s,r,0,1)) = 0 and Grid(p,x+Shapes(s,r,1,0),y+Shapes(s,r,1,1)) = 0 and Grid(p,x+Shapes(s,r,2,0),y+Shapes(s,r,2,1)) = 0 and Grid(p,x+Shapes(s,r,3,0),y+Shapes(s,r,3,1)) = 0) and ((Grid(p,x+Shapes(s,r,0,0),y+1+Shapes(s,r,0,1)) = 0 and Grid(p,x+Shapes(s,r,1,0),y+1+Shapes(s,r,1,1)) = 0 and Grid(p,x+Shapes(s,r,2,0),y+1+Shapes(s,r,2,1)) = 0 and Grid(p,x+Shapes(s,r,3,0),y+1+Shapes(s,r,3,1)) = 0) or Coords(p,2) = 0)) 
CurrentShape(p,1) = r
   endif
   if (and(Port,32768)<>0 and and(OldPort,32768)=0) then
    x = Coords(p,0) : y = Coords(p,1)
    s = CurrentShape(p,0) : r = CurrentShape(p,1)-1
    if (r < 0) r = 3
    if ((Grid(p,x+Shapes(s,r,0,0),y+Shapes(s,r,0,1)) = 0 and Grid(p,x+Shapes(s,r,1,0),y+Shapes(s,r,1,1)) = 0 and Grid(p,x+Shapes(s,r,2,0),y+Shapes(s,r,2,1)) = 0 and Grid(p,x+Shapes(s,r,3,0),y+Shapes(s,r,3,1)) = 0) and ((Grid(p,x+Shapes(s,r,0,0),y+1+Shapes(s,r,0,1)) = 0 and Grid(p,x+Shapes(s,r,1,0),y+1+Shapes(s,r,1,1)) = 0 and Grid(p,x+Shapes(s,r,2,0),y+1+Shapes(s,r,2,1)) = 0 and Grid(p,x+Shapes(s,r,3,0),y+1+Shapes(s,r,3,1)) = 0) or Coords(p,2) = 0)) 
CurrentShape(p,1) = r
   endif

   if (and(Port,64) <> 0) then
    if (FastFall(p) = 0) FastFall(p) = 1
   else
    if (FastFall(p) = 1) FastFall(p) = 0
   endif
   if (and(Port,8192) <> 0) FastFall(p) = 2
   if (and(Port,8) <> 0 and and(OldPort,8) = 0) then
    repeat
     OldPort = Port
     if (p = 0) then Port = peek("port1")
     else Port = peek("port2") endif
     if (and(Port,4096)<>0 and and(OldPort,4096)=0) then
      GameOver = 0 : goto GAME_OVER
     endif
    until (and(Port,8) <> 0 and and(OldPort,8) = 0)
   endif

   REM *** move the block down ***
   if (FastFall(p) = 0) then
    Coords(p,2) = Coords(p,2)-FallDelta(p)
   else Coords(p,2) = 0 endif
   if (Coords(p,2) <= 0) then
    x = Coords(p,0) : y = Coords(p,1)
    s = CurrentShape(p,0) : r = CurrentShape(p,1)
    if (Grid(p,x+Shapes(s,r,0,0),y-1+Shapes(s,r,0,1)) = 0 and Grid(p,x+Shapes(s,r,1,0),y-1+Shapes(s,r,1,1)) = 0 and Grid(p,x+Shapes(s,r,2,0),y-1+Shapes(s,r,2,1)) = 0 and Grid(p,x+Shapes(s,r,3,0),y-1+Shapes(s,r,3,1)) = 0) then
     Coords(p,1) = Coords(p,1)-1
     if (FastFall(p) = 0) then
      Coords(p,2) = 19+Coords(p,2)
      if (Coords(p,2) < 0) Coords(p,2) = 0
     endif
     SlideTimer(p) = SLIDE_TIME
    else
     Coords(p,2) = 0
     SlideTimer(p) = SlideTimer(p)-1
     if (SlideTimer(p) = 0) then
      DrawBlocksFallingYMin(p) = 24
      for i=0 to 3
       u = x+Shapes(s,r,i,0) : v = y+Shapes(s,r,i,1)
       Grid(p,u,v) = 1
       if (v > StackHeight(p)) StackHeight(p) = v
       if (Grid(p,1,v) <> 0 and Grid(p,2,v) <> 0 and Grid(p,3,v) <> 0 and Grid(p,4,v) <> 0 and Grid(p,5,v) <> 0 and Grid(p,6,v) <> 0 and Grid(p,7,v) <> 0 and Grid(p,8,v) <> 0 and Grid(p,9,v) <> 0 and Grid(p,10,v) <> 0) then
        LinesFalling(p) = LinesFalling(p)+1
        for k=v to StackHeight(p)
         for j=1 to 10
          Grid(p,j,k) = Grid(p,j,k+1)
         next j
         FallOffset(p,k) = FallOffset(p,k+1)+19
        next k
        StackHeight(p) = StackHeight(p)-1
        if (v < DrawBlocksFallingYMin(p)) DrawBlocksFallingYMin(p) = v
       endif
      next i
      AddedBlockCoords(p,0) = Coords(p,0)
      AddedBlockCoords(p,1) = Coords(p,1)
      AddedBlockShape(p,0) = CurrentShape(p,0)
      AddedBlockShape(p,1) = CurrentShape(p,1)
      DrawAddedBlock(p,0) = 1 : DrawAddedBlock(p,1) = 1
      if (LinesFalling(p) > 0) then
       DrawCurrentBlock(p) = 0
       if (LinesFalling(p) = 1) then Points = 1
       elsif (LinesFalling(p) = 2) then Points = 3
       elsif (LinesFalling(p) = 3) then Points = 6
       else Points = 10 endif
       Score(p) = Score(p)+Points
       DrawScore(p,0) = 1 : DrawScore(p,1) = 1
       if (NumPlayers = 1) then
        i = LinesFalling(p)-Options(p,2)
        if (i > 0) then
         Coords(1-p,1) = Coords(1-p,1)+i
         if (Coords(1-p,1) > 22) then
          Coords(1-p,2) = Coords(1-p,2)+19*(Coords(1-p,1)-22)
          Coords(1-p,1) = 22
         endif
         for k=25 to i+1 step -1
          for j=1 to 10
           Grid(1-p,j,k) = Grid(1-p,j,k-i)
          next j
         next k
         for k=1 to i
          v = int(ran(10))+1
          for j=1 to 10
           if (j = v) then Grid(1-p,j,k) = 0
           else Grid(1-p,j,k) = 1 endif
          next j
         next k
         StackHeight(1-p) = StackHeight(1-p)+i
         RiseOffset(1-p) = RiseOffset(1-p)+19*i
         LinesRising(1-p) = 1
        endif
       endif
      else
       if (StackHeight(p) > 20) then
        GameOver = 2-p : goto GAME_OVER
       endif
       CurrentShape(p,0) = NextShape(p,0)
       CurrentShape(p,1) = NextShape(p,1)
       NextShape(p,0) = int(ran(7)) : NextShape(p,1) = 0
       Coords(p,0) = 5 : Coords(p,1) = 22 : Coords(p,2)=0
       SlideTimer(p) = SLIDE_TIME : FastFall(p) = 0
       DrawNext(p,0) = 1 : DrawNext(p,1) = 1
       DrawCurrentBlock(p) = 1
      endif
      SpeedCount(p) = SpeedCount(p)+SpeedIncreases(Options(p,1))
      if (SpeedCount(p) >= 1) then
       SpeedLevel(p) = SpeedLevel(p)+1
       if (SpeedLevel(p) <= 2) then
        FallDelta(p) = FallSpeeds(SpeedLevel(p))
       else FallDelta(p) = SpeedLevel(p)-1 endif
       SpeedCount(p) = 0
      endif
     endif
    endif
   endif
  else
   REM *** drop any lines ***
   DrawBlocksFalling(p,0) = 1 : DrawBlocksFalling(p,1) = 1
   LinesFalling(p) = 0
   j = 6
   for i=1 to 22
    if (FallOffset(p,i) > 0) then
     LinesFalling(p) = 1
     FallOffset(p,i) = FallOffset(p,i)-j
     if (FallOffset(p,i) < 0) FallOffset(p,i) = 0
     j = j-1 : if (j < 0) j = 0
    endif
   next i
   if (LinesFalling(p) = 0) then
    if (StackHeight(p) > 20) then
     GameOver = 2-p : goto GAME_OVER
    endif
    CurrentShape(p,0) = NextShape(p,0)
    CurrentShape(p,1) = NextShape(p,1)
    NextShape(p,0) = int(ran(7)) : NextShape(p,1) = 0
    Coords(p,0) = 5 : Coords(p,1) = 22 : Coords(p,2) = 0
    SlideTimer(p) = SLIDE_TIME : FastFall(p) = 0
    DrawNext(p,0) = 1 : DrawNext(p,1) = 1
    DrawCurrentBlock(p) = 1
   endif
  endif
  if (LinesRising(p) <> 0) then
   DrawBlocksRising(p,0) = 1 : DrawBlocksRising(p,1) = 1
   RiseOffset(p) = RiseOffset(p)-6
   if (RiseOffset(p) < 0) RiseOffset(p) = 0
   if (RiseOffset(p) = 0) then
    LinesRising(p) = 0
    if (StackHeight(p) > 20) then
     GameOver = 2-p : goto GAME_OVER
    endif
   endif
  endif
next p

REM *** redraw the screen ***
if (DrawBackground(CurrentBuffer) <> 0) then
  clear window
  setrgb 1,20,20,20
  for p=0 to NumPlayers
   fill rectangle 48+p*340,48 to 250+p*340,506
  next p
  DrawBackground(CurrentBuffer) = 0
endif
for p=0 to NumPlayers
  if (DrawScore(p,CurrentBuffer) <> 0) then
   setrgb 1,250,250,250
   x = 306+p*64
   y = 180+p*340
   if (NumPlayers = 1) then
    clear fill rectangle x-43,55 to x+5,75
    text x,60, str$(WinRecord(p))
   endif
   clear fill rectangle y-63,15 to y+5,35
   text y,20,str$(Score(p))
   DrawScore(p,CurrentBuffer) = 0
  endif
  setrgb 1,100,100,100
  line 48+p*340,430 to 250+p*340,430
  setrgb 1,200,200,200
  if (DrawNext(p,CurrentBuffer) <> 0) then
   u = 270+p*60
   clear fill rectangle u,390 to u+39,429
   ns = NextShape(p,0) : nr = NextShape(p,1)
   for i=0 to 3
    u = Shapes(ns,nr,i,0)*10+280+p*60
    v = Shapes(ns,nr,i,1)*10+400
    fill rectangle u,v to u+9,v+9
   next i
   DrawNext(p,CurrentBuffer) = 0
  endif
  if (DrawAddedBlock(p,CurrentBuffer) <> 0) then
   x = AddedBlockCoords(p,0) : y = AddedBlockCoords(p,1)
   s = AddedBlockShape(p,0) : r = AddedBlockShape(p,1)
   for i=0 to 3
    u = (x+Shapes(s,r,i,0))*20+30+p*340
    v = (y+Shapes(s,r,i,1))*19+31-RiseOffset(p)
    fill rectangle u,v to u+18,v+17
   next i
   DrawAddedBlock(p,CurrentBuffer) = 0
  endif
  if (DrawBlocksFalling(p,CurrentBuffer) <> 0 or DrawBlocksRising(p,CurrentBuffer) <> 0) then
   if (DrawBlocksRising(p,CurrentBuffer) <> 0) then yMin=1
   else yMin = DrawBlocksFallingYMin(p) endif
   y = 31+19*yMin
   setrgb 1,20,20,20
   fill rectangle 50+p*340,y to 248+p*340,485
   setrgb 1,100,100,100
   line 48+p*340,430 to 250+p*340,430
   setrgb 1,200,200,200
   for j=yMin to 22
    u = 50+p*340
    v = y+FallOffset(p,j)-RiseOffset(p)
    w = v+17
    if (w > 50) then
     if (v < 50) v = 50
     for i=1 to 10
      if (Grid(p,i,j) <> 0) fill rectangle u,v to u+18,w
      u = u+20
     next i
    endif
    y = y+19
   next j
   DrawBlocksFalling(p,CurrentBuffer) = 0
   DrawBlocksRising(p,CurrentBuffer) = 0
  endif
  if (DrawCurrentBlock(p) <> 0) then
   x = Coords(p,0) : y = Coords(p,1) : z = Coords(p,2)
   s = CurrentShape(p,0) : r = CurrentShape(p,1)
   for i=0 to 3
    u = (x+Shapes(s,r,i,0))*20+30+p*340
    v = (y+Shapes(s,r,i,1))*19+z+31-RiseOffset(p)
    w = v+17
    if (v < 504) then
     if (w > 504) w = 504
     fill rectangle u,v to u+18,w
    endif
   next i
   EraseCurrentBlock(p,CurrentBuffer) = 1
  else
   EraseCurrentBlock(p,CurrentBuffer) = 0
  endif
next p

setdispbuf CurrentBuffer
CurrentBuffer = 1-CurrentBuffer
setdrawbuf CurrentBuffer

setrgb 1,20,20,20
for p=0 to NumPlayers
  if (EraseCurrentBlock(p,CurrentBuffer) <> 0) then
   x=OldCoords(p,0) : y=OldCoords(p,1) : z=OldCoords(p,2)
   s = OldShape(p,0) : r = OldShape(p,1)
   for i=0 to 3
    u = (x+Shapes(s,r,i,0))*20+30+p*340
    v = (y+Shapes(s,r,i,1))*19+z+31-OldRiseOffset(p)
    w = v+17
    if (v < 504) then
     if (w > 504) w = 504
     fill rectangle u,v to u+18,w
    endif
   next i
  endif
  OldCoords(p,0) = Coords(p,0)
  OldCoords(p,1) = Coords(p,1)
  OldCoords(p,2) = Coords(p,2)
  OldShape(p,0) = CurrentShape(p,0)
  OldShape(p,1) = CurrentShape(p,1)
  OldRiseOffset(p) = RiseOffset(p)
next p
until (0 <> 0)

label GAME_OVER
if (GameOver = 0) then
goto MENU
else
poke "textalign", "cb"
setdrawbuf 1-CurrentBuffer
setrgb 1,250,250,250
if (NumPlayers = 1) then
  Winner$ = "Player x"
  right$(Winner$,1) = str$(GameOver)
  text 320,300, Winner$ : text 320,280, "Won"
  WinRecord(GameOver-1) = WinRecord(GameOver-1)+1
else text 320,300, "Game Over" endif
setrgb 1,150,150,150
text 320,220, "Press X" : text 320,200, "to play"
text 320,180, "again" : text 320,150, "Press"
text 320,130, "triangle" : text 320,110, "for menu"
setdrawbuf CurrentBuffer
repeat
  OldPort(0) = Port(0) : OldPort(1) = Port(1)
  Port(0) = peek("port1") : Port(1) = peek("port2")
  for p=0 to NumPlayers
   Port = Port(p) : OldPort = OldPort(p)
   if (and(Port,16384) <> 0 and and(OldPort,16384) = 0) goto GAME_START
   if (and(Port,4096) <> 0 and and(OldPort,4096) = 0) goto MENU
  next p
until (0 <> 0)
endif


label START
open window 640,512
window origin "lb"
CurrentBuffer = 0
setdrawbuf CurrentBuffer
setdispbuf 1-CurrentBuffer
setrgb 0,0,0,0
WinRecord(0) = 0 : WinRecord(1) = 0

data 0,1, 1,1, 0,0, 1,0,  0,1, 1,1, 0,0, 1,0
data 0,1, 1,1, 0,0, 1,0,  0,1, 1,1, 0,0, 1,0
data -1,0, 0,0, 1,0, 2,0,  0,2, 0,1, 0,0, 0,-1
data -1,0, 0,0, 1,0, 2,0,  0,2, 0,1, 0,0, 0,-1
data 1,1, -1,0, 0,0, 1,0,  0,1, 0,0, 0,-1, 1,-1
data -1,0, 0,0, 1,0, -1,-1,  -1,1, 0,1, 0,0, 0,-1
data -1,1, -1,0, 0,0, 1,0,  0,1, 1,1, 0,0, 0,-1
data -1,0, 0,0, 1,0, 1,-1,  0,1, 0,0, -1,-1, 0,-1
data -1,1, 0,1, 0,0, 1,0,  1,1, 0,0, 1,0, 0,-1
data -1,1, 0,1, 0,0, 1,0,  1,1, 0,0, 1,0, 0,-1
data 0,1, 1,1, -1,0, 0,0,  -1,1, -1,0, 0,0, 0,-1
data 0,1, 1,1, -1,0, 0,0,  -1,1, -1,0, 0,0, 0,-1
data 0,1, -1,0, 0,0, 1,0,  0,1, 0,0, 1,0, 0,-1
data -1,0, 0,0, 1,0, 0,-1,  0,1, -1,0, 0,0, 0,-1
for i=0 to 6 : for j=0 to 3 : for k=0 to 3
read Shapes(i,j,k,0) : read Shapes(i,j,k,1)
next k : next j : next i

data 0.25,0.5,1, 0,0.005,0.02, 2,2,2, 1,1,1
data "Fall speed", "slow", "medium", "fast"
data "Speed increase", "none", "low", "high"
data "2-player handicap", "easy", "medium", "hard"
for i=0 to 2 : read FallSpeeds(i) : next i
for i=0 to 2 : read SpeedIncreases(i) : next i
for i=0 to 2 : read NumSubOptions(i) : next i
for i=0 to 2
read Options(0,i) : Options(1,i) = Options(0,i)
next i
for i=0 to 2 : for j=0 to NumSubOptions(i)+1
read OptionNames$(i,j)
next j : next i


label MENU
poke "textalign", "cb"
repeat
OldPort(0) = Port(0) : Port(0) = peek("port1")
Port = Port(0) : OldPort = OldPort(0)
if (and(Port,16) <> 0 and and(OldPort,16) = 0) then
  MenuChoice = MenuChoice-1
  if (MenuChoice < 0) MenuChoice = 2
endif
if (and(Port,64) <> 0 and and(OldPort,64) = 0) then
  MenuChoice = MenuChoice+1
  if (MenuChoice > 2) MenuChoice = 0
endif
if (and(Port,16384) <> 0 and and(OldPort,16384) = 0) then
  if (MenuChoice = 0) then
   NumPlayers = 0 : goto GAME_START
  elsif (MenuChoice = 1) then
   NumPlayers = 1 : goto GAME_START
  else goto OPTIONS endif
endif

clear window
setrgb 1,0,80,220
fill rectangle 218,410 to 254,408
fill rectangle 218,400 to 254,398
fill rectangle 218,400 to 220,388
fill rectangle 252,410 to 254,398
fill rectangle 275,410 to 294,408
fill rectangle 258,390 to 277,388
fill rectangle 275,410 to 277,388
fill rectangle 298,410 to 334,408
fill rectangle 298,400 to 334,398
fill rectangle 298,390 to 334,388
fill rectangle 298,400 to 300,388
fill rectangle 332,410 to 334,398
fill rectangle 338,405 to 364,403
fill rectangle 350,410 to 352,388
fill rectangle 368,405 to 384,403
fill rectangle 368,405 to 370,388
fill rectangle 390,405 to 392,388
fill rectangle 410,405 to 424,403
fill rectangle 398,390 to 412,388
fill rectangle 410,405 to 412,388

if (MenuChoice = 0) then setrgb 1,250,250,250
else setrgb 1,150,150,150 endif
text 320,300, "1 Player Game"
if (MenuChoice = 1) then setrgb 1,250,250,250
else setrgb 1,150,150,150 endif
text 320,250, "2 Player Game"
if (MenuChoice = 2) then setrgb 1,250,250,250
else setrgb 1,150,150,150 endif
text 320,200, "Options"
setrgb 1,100,100,100
text 320,100, "Highlight choice and press X"

setdispbuf CurrentBuffer
CurrentBuffer = 1-CurrentBuffer
setdrawbuf CurrentBuffer
until (0 <> 0)


label OPTIONS
OptionChoice(0) = 0 : OptionChoice(1) = 0
repeat
OldPort(0) = Port(0) : OldPort(1) = Port(1)
Port(0) = peek("port1") : Port(1) = peek("port2")
for p=0 to 1
  Port = Port(p) : OldPort = OldPort(p)
  if (and(Port,16) <> 0 and and(OldPort,16) = 0) then
   OptionChoice(p) = OptionChoice(p)-1
   if (OptionChoice(p) < 0) OptionChoice(p) = 2
  endif
  if (and(Port,64) <> 0 and and(OldPort,64) = 0) then
   OptionChoice(p) = OptionChoice(p)+1
   if (OptionChoice(p) > 2) OptionChoice(p) = 0
  endif
  if (and(Port,32) <> 0 and and(OldPort,32) = 0 and Options(p,OptionChoice(p)) < NumSubOptions(OptionChoice(p))) 
Options(p,OptionChoice(p)) = Options(p,OptionChoice(p))+1
  if (and(Port,128) <> 0 and and(OldPort,128) = 0 and Options(p,OptionChoice(p)) > 0)
 Options(p,OptionChoice(p)) = Options(p,OptionChoice(p))-1
  if (and(Port,4096)<>0 and and(OldPort,4096)=0) goto MENU
next p

clear window
setrgb 1,250,250,250
text 320,430, "Options"
setrgb 1,200,200,200
text 130,390, "Player 1"
text 510,390, "Player 2"
for i=0 to 2
  setrgb 1,250,250,250
  text 320,350-i*40, OptionNames$(i,0)
  x = NumSubOptions(i)*-35
  for j=0 to NumSubOptions(i)
   for p=0 to 1
    if (i = OptionChoice(p)) then
     if (j = Options(p,i)) then setrgb 1,255,255,0
     else setrgb 1,150,150,150 endif
    else
     if (j = Options(p,i)) then setrgb 1,250,250,250
     else setrgb 1,100,100,100 endif
    endif
    text 130+p*380+x,350-i*40, OptionNames$(i,j+1)
   next p
   x = x+70
  next j
next i
setrgb 1,100,100,100
text 320,100, "Use the directional buttons to change the settings"
text 320,75, "Press triangle to go back"

setdispbuf CurrentBuffer
CurrentBuffer = 1-CurrentBuffer
setdrawbuf CurrentBuffer
until (0 <> 0)



