sync on
randomize timer()
 
create bitmap 1,640,480
 
ink rgb(255,0,0),0
box 65,1,128,64
ink rgb(0,255,0),0
box 1,65,64,128
ink rgb(0,0,255),0
box 65,65,128,128
 
get image 1,1,1,129,129
 
cls
 
ink rgb(255,255,255),0
for i=1 to 1000
   dot rnd(128),rnd(128)
next i
blur bitmap 1,1
 
get image 2,0,0,128,128
 
set current bitmap 0
 
backdrop on
texture backdrop 2
 
make matrix 1,1000,2000,10,20
prepare matrix texture 1,1,2,2
fill matrix 1,1,1
 
dim mat(10,20)
 
dim flashing(9,19)
dim flashingcol(9,19)
 
position camera 500,900,-95
rotate camera 54,0,0
 
randomizecols(8)
update matrix 1
 
gaptime=5000
gapstart=timer()
 
playerx=5
 
currentcol=rnd(2)+1
nextcol=rnd(2)+1
matchange=1
 
flashing=0
 
shot=0
 
score=0
 
set text font "arial"
 
repeat
 
   set cursor 0,0
 
   for x=0 to 9
      for z=2 to 19
         set matrix tile 1,x,z,mat(x,z)+1
      next z
   next x
   update matrix 1
 
   rightcount=rightcount-1
   if rightkey()=1 and playerx<9 and rightcount<0
      playerx=playerx+1
      rightcount=5
      rightkey=1
   else
      rightkey=0
   endif
   leftcount=leftcount-1
   if leftkey()=1 and playerx>0 and leftcount<0
      playerx=playerx-1
      leftcount=5
      leftkey=1
   else
      leftkey=0
   endif
 
   spacecount=spacecount-1
   if spacekey()=1 and shot=0 and spacecount<0
      shotx=playerx
      shotz=1
      shotcol=currentcol
      shot=1
      currentcol=nextcol
      nextcol=rnd(2)+1
      spacecount=20
   endif
 
   if timer()>=gapstart+gaptime and shot=0
      shift matrix up 1
      shiftarray()
      randomizecols(1)
      update matrix 1 : matchange=1
      gapstart=timer()
      for x=0 to 9
         if mat(x,2)>0 then death=1
      next x
      gaptime=gaptime-((gaptime-2500)/60)
   endif
 
   if shot=1
      shotz=shotz+1
      set matrix tile 1,shotx,shotz,shotcol+1
      set matrix tile 1,shotx,shotz-1,1
      update matrix 1 : matchange=1
      if mat(shotx,shotz+1)>0 or shotz=19
         mat(shotx,shotz)=shotcol
         scoreinc=clearcol(shotx,shotz)
         if scoreinc>2 then score=score+scoreinc*100
         flashing=1
         update matrix 1
         if mat(shotx,shotz)>0 and shotz<=2 then death=1
         shot=0
      endif
   endif
 
   if flashing=1
      flashing=0
      for x=0 to 9
         for z=0 to 19
            flash=flashing(x,z)
            if flash>0
               unit$=right$(str$(flash),1)
               if unit$="0" or unit$="1" or unit$="2" or unit$="3" or unit$="4"
                  col=flashingcol(x,z)
                  set matrix tile 1,x,z,col+1
               else
                  set matrix tile 1,x,z,1
               endif
               update matrix 1
               flashing(x,z)=flash-1
               if flashing(x,z)<1
                  set matrix tile 1,x,z,1
                  scoreinc=findislands()
                  if scoreinc>2 then score=score+scoreinc*150
               endif
               flashing=1
            endif
         next z
      next x
   endif
 
   backtexy=backtexy-1
   scroll backdrop backtexx,backtexy
 
   if rightkey=1
      set matrix tile 1,playerx-1,1,1
      set matrix tile 1,playerx-1,0,1
      update matrix 1 : matchange=1
   endif
   if leftkey=1
      set matrix tile 1,playerx+1,1,1
      set matrix tile 1,playerx+1,0,1
      update matrix 1 : matchange=1
   endif
 
   if matchange=1
      set matrix tile 1,playerx,1,currentcol+1
      set matrix tile 1,playerx,0,nextcol+1
      update matrix 1
      matchange=0
   endif
 
   set text size 30
   ink rgb(0,255,0),0
   text 0,0,"Score: "+str$(score)
 
   sync
 
until death=1
 
fog on
fog distance 0
 
ink rgb(255,0,0),0
 
repeat
   cls
   set text size 50
   center text 320,100,"YOU LOSE"
   set text size 30
   center text 320,165,"Final score: "+str$(score)
   center text 320,195,"Press 'e' to exit."
   sync
until keystate(18)>0
 
end
 
function shiftarray()
 
   dim firstline(9)
 
   for z=0 to 18
      for x=0 to 9
         mat(x,z)=mat(x,z+1)
      next x
   next z
 
   for x=0 to 9
      mat(x,19)=firstline(x)
   next x
 
   undim firstline(9)
 
endfunction
 
function randomizecols(amount)
 
   for z=19 to 20-amount step -1
      for x=0 to 9
         col=rnd(2)+1
         set matrix tile 1,x,z,col+1
         mat(x,z)=col
      next x
   next z
 
endfunction
 
function clearcol(tilex,tilez)
 
   coltofind=mat(tilex,tilez)
 
   dim fillx(200)
   dim filly(200)
   dim fill(10,20)
 
   fills=1
   fillx(1)=tilex
   filly(1)=tilez
   fill(tilex,tilez)=1
   endfill=0
 
   while endfill=0
 
      endfill=1
 
      fill2=fills
      for i=1 to fills
         fillx=fillx(i)
         filly=filly(i)
 
         if fillx<9
            if fill(fillx+1,filly)=0 : rem Right
               if mat(fillx+1,filly)=coltofind
                  fill2=fill2+1
                  fillx(fill2)=fillx+1
                  filly(fill2)=filly
                  fill(fillx+1,filly)=1
                  endfill=0
               endif
            endif
         endif
 
         if fillx>0
            if fill(fillx-1,filly)=0 : rem Left
               if mat(fillx-1,filly)=coltofind
                  fill2=fill2+1
                  fillx(fill2)=fillx-1
                  filly(fill2)=filly
                  fill(fillx-1,filly)=1
                  endfill=0
               endif
            endif
         endif
 
         if filly>0
            if fill(fillx,filly-1)=0 : rem Up
               if mat(fillx,filly-1)=coltofind
                  fill2=fill2+1
                  fillx(fill2)=fillx
                  filly(fill2)=filly-1
                  fill(fillx,filly-1)=1
                  endfill=0
               endif
            endif
         endif
 
         if filly<19
            if fill(fillx,filly+1)=0 : rem Down
               if mat(fillx,filly+1)=coltofind
                  fill2=fill2+1
                  fillx(fill2)=fillx
                  filly(fill2)=filly+1
                  fill(fillx,filly+1)=1
                  endfill=0
               endif
            endif
         endif
      next i
      fills=fill2
 
      if fills>=200 then endfill=1
 
   endwhile
 
   originalcol=mat(tilex,tilez)
   if fills>=3
      for x=0 to 9
         for z=0 to 19
            if fill(x,z)=1
               flashing(x,z)=20
               flashingcol(x,z)=originalcol
               mat(x,z)=0
            endif
         next i
      next i
   endif
 
   undim fillx(gridwidth*gridheight)
   undim filly(gridwidth*gridheight)
   undim fill(gridwidth,gridheight)
 
endfunction fills
 
function findislands()
 
   dim island(10,20)
   island=0
   islands=0
 
   for x=0 to 9
      for z=0 to 19
 
         if mat(x,z)>0 and island(x,z)=0
 
            island=island+1
 
            dim fillx(200)
            dim filly(200)
            dim fill(10,20)
 
            fills=1
            fillx(1)=x
            filly(1)=z
            fill(x,z)=1
            endfill=0
 
            while endfill=0
 
               endfill=1
 
               fill2=fills
               for i=1 to fills
                  fillx=fillx(i)
                  filly=filly(i)
 
                  if fillx<9
                     if fill(fillx+1,filly)=0 : rem Right
                        if mat(fillx+1,filly)>0
                           fill2=fill2+1
                           fillx(fill2)=fillx+1
                           filly(fill2)=filly
                           fill(fillx+1,filly)=1
                           endfill=0
                        endif
                     endif
                  endif
 
                  if fillx>0
                     if fill(fillx-1,filly)=0 : rem Left
                        if mat(fillx-1,filly)>0
                           fill2=fill2+1
                           fillx(fill2)=fillx-1
                           filly(fill2)=filly
                           fill(fillx-1,filly)=1
                           endfill=0
                        endif
                     endif
                  endif
 
                  if filly>0
                     if fill(fillx,filly-1)=0 : rem Up
                        if mat(fillx,filly-1)>0
                           fill2=fill2+1
                           fillx(fill2)=fillx
                           filly(fill2)=filly-1
                           fill(fillx,filly-1)=1
                           endfill=0
                        endif
                     endif
                  endif
 
                  if filly<19
                     if fill(fillx,filly+1)=0 : rem Down
                        if mat(fillx,filly+1)>0
                           fill2=fill2+1
                           fillx(fill2)=fillx
                           filly(fill2)=filly+1
                           fill(fillx,filly+1)=1
                           endfill=0
                        endif
                     endif
                  endif
               next i
               fills=fill2
 
               if fills>=200 then endfill=1
 
            endwhile
 
            for x1=0 to 9
               for z1=0 to 19
                  if fill(x1,z1)=1
                     island(x1,z1)=island
                  endif
               next i
            next i
 
            undim fillx(gridwidth*gridheight)
            undim filly(gridwidth*gridheight)
            undim fill(gridwidth,gridheight)
 
         endif
 
      next z
   next x
 
   scoreinc=0
 
   for i=1 to island
      secure=0
      for x=0 to 9
         if island(x,19)=i then secure=1
      next x
      if secure=0
         for x1=0 to 9
            for z1=0 to 19
               if island(x1,z1)=i
                  mat(x1,z1)=0
                  set matrix tile 1,x1,z1,1
                  scoreinc=scoreinc+1
               endif
            next i
         next i
      endif
   next i
 
   undim island(10,20)
 
endfunction scoreinc