unit GMTetris;

interface
  uses GMVars,Windows,Graphics,Math,IDX_sound;

type
  TTetr = Array [1..4, 1..4] of Byte;


const
  FigureList: Array [1..11, 1..4] of TTetr =
  (
  (((1,1,0,0),(0,1,1,0),(0,0,0,0),(0,0,0,0)),
   ((0,1,0,0),(1,1,0,0),(1,0,0,0),(0,0,0,0)),
   ((1,1,0,0),(0,1,1,0),(0,0,0,0),(0,0,0,0)),
   ((0,1,0,0),(1,1,0,0),(1,0,0,0),(0,0,0,0))),
  (((0,1,1,0),(1,1,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,0,0,0),(1,1,0,0),(0,1,0,0),(0,0,0,0)),
   ((0,1,1,0),(1,1,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,0,0,0),(1,1,0,0),(0,1,0,0),(0,0,0,0))),
  (((0,1,0,0),(1,1,1,0),(0,0,0,0),(0,0,0,0)),
   ((0,1,0,0),(0,1,1,0),(0,1,0,0),(0,0,0,0)),
   ((0,0,0,0),(1,1,1,0),(0,1,0,0),(0,0,0,0)),
   ((0,1,0,0),(1,1,0,0),(0,1,0,0),(0,0,0,0))),
  (((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
   ((0,1,0,0),(0,1,0,0),(0,1,0,0),(0,1,0,0)),
   ((0,0,0,0),(1,1,1,1),(0,0,0,0),(0,0,0,0)),
   ((0,1,0,0),(0,1,0,0),(0,1,0,0),(0,1,0,0))),
  (((1,1,0,0),(1,1,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,1,0,0),(1,1,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,1,0,0),(1,1,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,1,0,0),(1,1,0,0),(0,0,0,0),(0,0,0,0))),
  (((1,1,0,0),(0,1,0,0),(0,1,0,0),(0,0,0,0)),
   ((0,0,1,0),(1,1,1,0),(0,0,0,0),(0,0,0,0)),
   ((1,0,0,0),(1,0,0,0),(1,1,0,0),(0,0,0,0)),
   ((1,1,1,0),(1,0,0,0),(0,0,0,0),(0,0,0,0))),
  (((1,1,0,0),(1,0,0,0),(1,0,0,0),(0,0,0,0)),
   ((1,1,1,0),(0,0,1,0),(0,0,0,0),(0,0,0,0)),
   ((0,1,0,0),(0,1,0,0),(1,1,0,0),(0,0,0,0)),
   ((1,0,0,0),(1,1,1,0),(0,0,0,0),(0,0,0,0))),
  (((0,1,0,0),(1,1,1,0),(0,1,0,0),(0,0,0,0)),
   ((0,1,0,0),(1,1,1,0),(0,1,0,0),(0,0,0,0)),
   ((0,1,0,0),(1,1,1,0),(0,1,0,0),(0,0,0,0)),
   ((0,1,0,0),(1,1,1,0),(0,1,0,0),(0,0,0,0))),
  (((1,1,1,0),(1,0,1,0),(0,0,0,0),(0,0,0,0)),
   ((1,1,0,0),(0,1,0,0),(1,1,0,0),(0,0,0,0)),
   ((1,0,1,0),(1,1,1,0),(0,0,0,0),(0,0,0,0)),
   ((1,1,0,0),(1,0,0,0),(1,1,0,0),(0,0,0,0))),
  (((1,0,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,0,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,0,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,0,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0))),
  (((1,0,0,0),(1,0,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,1,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,0,0,0),(1,0,0,0),(0,0,0,0),(0,0,0,0)),
   ((1,1,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0)))
  );

  colors : Array [1..12] of Integer = (clAqua, clBlue, clFuchsia, clGreen,
  clLime, clMaroon, clNavy, clOlive, clPurple, clRed, clTeal, clYellow);

  SCORE_SINGLE  =1;
  SCORE_DOUBLE  =2;
  SCORE_TRIPLE  =3;
  SCORE_TETRIS  =4;

  level_linesa:array[0..9] of Byte =
  ( 4, 4+5,
    5, 5+6,
    6, 6+7,
    7, 7+8,
    8, 8+9 );

type
 TOEXPLOSION=object
  x, y, w, h:Integer;
  explo_id:Integer;
  time, ani_time:Integer;
  alive:Boolean;
  procedure Start(sx, sy:Integer; mega:Boolean);
  procedure Draw;
  procedure Procces;
end;

type
 TOFLYBLOCK=object
  alive:Boolean;
  x, y:Integer;
  startx:Integer;
  starty:Integer;
  block:Integer;
  speed:Single;
  angle:Single;
  draw_angle:Single;
  scale:Single;
  ani_time:Integer;
  procedure Start(sx, sy:Integer; bblock:Integer;super_speed:Integer);
  procedure Draw;
  procedure Procces;
  function calculate_pos:Integer ;
end;

type
  TOTPlayer=object
    tetr, nexttetr : TTetr;

    num, nextnum : Integer;
    gen, nextgen : Integer;
    fcl,nextfcl : Integer;

    sizex:Integer;
    sizey:Integer;

    FigureSet:Integer;
    GameMode:Byte;

    x,y : Integer;
    offpx:Integer;
    offpy:Integer;

    scores : Integer;
    lines : Integer;
    level : Integer;
    level_lines:Integer;

    lines_counter:Integer;

    panel :array[-3..30, 1..12] of Integer;

    Started:Boolean;
    BomberTimer:Integer;

    procedure setspeed(s : Byte);
    procedure showfigure;
    procedure drawsquare(i,j,c : Integer;isbomb:Boolean=False);
    function canrotate : Boolean;
    procedure rotatefigure;
    procedure gennextfigure;
    procedure nextfigure;
    procedure newgame(mode:Byte);

    function canmoveleft : Boolean;
    function canmoveright : Boolean;
    function canmovedown : Boolean;


    function gameover : Boolean;
    procedure checkpanel;
    procedure fixfigure;
    procedure stopmove;

    procedure moveleft;
    procedure moveright;
    procedure movedown;

    procedure Procces();
    procedure Draw();
    procedure Make_Megabombs();
    procedure Create_bomb_explo(sx,sy:Integer);
    procedure Destroy_block(sx, sy:Integer; mega:Boolean);
end;
var
 Player:TOTPlayer;
 MyTimer:Cardinal=0;
 ExplosionList:array of TOEXPLOSION;
 FlyBlocksList:array of TOFLYBLOCK;

 procedure AddExplosion(sx, sy:Integer; mega:Boolean);
 procedure proccesExplosion();
 procedure DrawExplosion();

 procedure AddFlyBlock(sx, sy:Integer; bblock:Integer;super_speed:Integer);
 procedure proccesFlyBlocks();
 procedure DrawFlyBlocks();

implementation
   uses GMUtilite;

procedure TMPTimerProcces();
begin
   if (Not PauseGame)and(StartGame)and(Not Endgame) then
   begin
     if player.canmovedown then player.movedown else player.stopmove;
   end;
end;

procedure AddFlyBlock(sx, sy:Integer; bblock:Integer;super_speed:Integer);
var
  i,k:Integer;
begin
  k:=-1;
  for i:=0 to High(FlyBlocksList) do
  if FlyBlocksList[i].alive=False then
  begin
   k:=i;
  end;

  if k=-1 then
  begin
    k:=High(FlyBlocksList)+1;
    SetLength(FlyBlocksList,k+1);
  end;

  FlyBlocksList[k].Start(sx, sy, bblock,super_speed);
end;

procedure proccesFlyBlocks();
var
  i:Integer;
begin
  for i:=0 to High(FlyBlocksList) do
  if FlyBlocksList[i].alive then
  begin
   FlyBlocksList[i].Procces;
  end;

end;

procedure DrawFlyBlocks();
var
  i:Integer;
begin
  for i:=0 to High(FlyBlocksList) do
  begin
   FlyBlocksList[i].Draw;
  end;

end;

procedure TOFLYBLOCK.Start(sx, sy:Integer; bblock:Integer;super_speed:Integer);
begin
  startx := sx;
  starty := sy;
  block  := bblock;
  angle:= random(900)/10.0 + 45.0;
  if super_speed>0 then speed:=100.0
  else
  speed:= random(200)/10.0 + 40.0 + ABS(angle - 90.0);
  draw_angle:= 0.0;
  scale:= Random(200)/1000.0 - 0.1;
  ani_time:= GetTickCount;
  alive:=True;
  calculate_pos;
end;

function TOFLYBLOCK.calculate_pos:Integer ;
var
  time:Single;
  sx,sy:Single;
begin
  time:= 2.5 * (GetTickCount - ani_time) / 1000;

  sx:= (speed * cos(PI*angle/180.0) * time) + ((0.5) * 0   * time * time);
  sy:= (speed * sin(PI*angle/180.0) * time) - ((0.5) * 60 * time * time);

  x:= Trunc( (sx)+startx);
  y:= Trunc(-(sy)+starty);

  result:=0;
  if ((x < -24) or (x >= SCRW) or (y >= SCRH)) then
    result:=-1;
end;

procedure TOFLYBLOCK.Draw;
begin
  if  alive=False then Exit;
  scale:= 1 + (0.5) * (GetTickCount - ani_time) /1000;
  scale:=scale - (0.25) * ABS(x - SCR2W) / (SCR2W);
  scale:=scale + scale;

  DrawRectangle2D(x,y,scale,scale,block,255,trunc(draw_angle),True,True);
end;

procedure TOFLYBLOCK.Procces;
begin
 if (calculate_pos <> 0) then
  alive:=False;

  draw_angle:=draw_angle + SIGN(x - scr2w) * ((speed) / (10.0));

end;

procedure proccesExplosion();
var
  i:Integer;
begin
  for i:=0 to High(ExplosionList) do
  if ExplosionList[i].alive then
  begin
   ExplosionList[i].Procces;
  end;
end;

procedure DrawExplosion();
var
  i:Integer;
begin
  for i:=0 to High(ExplosionList) do
  begin
   ExplosionList[i].Draw;
  end;
end;


procedure AddExplosion(sx, sy:Integer; mega:Boolean);
var
  i,k:Integer;
begin
  k:=-1;
  for i:=0 to High(ExplosionList) do
  if ExplosionList[i].alive=False then
  begin
   k:=i;
  end;

  if k=-1 then
  begin
    k:=High(ExplosionList)+1;
    SetLength(ExplosionList,k+1);
  end;

  ExplosionList[k].Start(sx, sy, mega);

end;
procedure TOEXPLOSION.Start(sx, sy:Integer; mega:Boolean);
begin
 x := sx;
 y := sy;
 w:=192;
 h:=192;
 if not mega then w:= random(64)+32;
 if not mega then h:= random(64)+32;
 explo_id:= random(4);
 time:=Trunc(1000*3/4);
 if not mega then  time:=Random(Trunc(1000/4));
 ani_time:= GetTickCount;
 alive:=True;

end;

procedure TOEXPLOSION.Draw;
var
  i:Integer;
begin
if alive=False then Exit;
 DrawCircle(x-w/2,y-h/2,w,$ffffff,150,26,True);
end;

procedure TOEXPLOSION.Procces;
begin
  if ((GetTickCount-ani_time)>time) then
  alive:=False;

end;

procedure TOTPlayer.destroy_block(sx, sy:Integer; mega:Boolean );
begin
  if ((sx < 1) or (sy < 1) or (sx >= sizey) or (sy >= sizex)) then Exit;

  if panel[sx,sy]>0 then
  begin
    if panel[sx,sy]<10 then
    begin
     create_bomb_explo(sx,sy);
    end
    else
    begin

      AddFlyBlock(offpx + (sy+1)*24,
                  offpy + sx*24,
                  panel[sx,sy],
                  Byte(mega));

      panel[sx,sy]:= 0;
    end;
  end;
end;

procedure TOTPlayer.create_bomb_explo(sx,sy:Integer);
var
  cx, cy:Integer;
  xx, yy:Integer;
begin

 PlaySoundFromMEM(1);

  if ((panel[sx,sy]) = 2) then
  begin
     AddExplosion(offpx + sy*24 + 24 div 2,
                  offpy + sx*24 + 24 div 2, FALSE);
     panel[sx,sy]:= 0;

     scores:=scores+10;

    for yy:=-1 to 1 do
      for xx:=-1 to 1 do
        destroy_block(sx+xx,sy+yy , FALSE);
  end
  else
  begin
    cx:=-1;
    cy:=-1;

    if panel[sx,sy] = 3 then
    begin
      cx:= 1;
      cy:= 1;
      panel[sx+0,sy+0]:=0;
      panel[sx+0,sy+1]:=0;
      panel[sx+0,sy+0]:=0;
      panel[sx+1,sy+1]:=0;
    end
    else
    if panel[sx,sy] = 6  then
    begin
      cx:= 0;
      cy:= 1;
      panel[sx+0,sy-1]:=0;
      panel[sx+0,sy+0]:=0;
      panel[sx+1,sy-1]:=0;
      panel[sx+1,sy+0]:= 0;
    end
    else
    if panel[sx,sy]= 4 then
    begin
      cx:= 1;
      cy:= 0;
      panel[sx-1,sy+0]:=0;
      panel[sx-1,sy+1]:=0;
      panel[sx+0,sy+0]:=0;
      panel[sx+0,sy+1]:=0;
    end
    else
    if panel[sx,sy]= 5 then
    begin
      cx:= 0;
      cy:= 0;
      panel[sx-1,sy-1]:=0;
      panel[sx-1,sy+0]:=0;
      panel[sx+0,sy-1]:=0;
      panel[sx+0,sy+0]:=0;
    end;

    if (cx >= 0) then
    begin

     AddExplosion(offpx + (sy+cx)*24 + 24 div 2,
                  offpy + (sx+cy)*24 + 24 div 2, True);

      scores:=scores+50;

      for yy:=-3 to 4 do
        for xx:=-3 to 4 do
          destroy_block( sx+cx+xx, sy+cy+yy, TRUE);
    end;
  end;
end;

procedure TOTPlayer.Make_Megabombs();
var
  dy,dx:Integer;
begin

if (gamemode <> TETRIS_MODE_BOMBER) then Exit;

  for dy:=1 to sizey-1 do
  for dx:=1 to sizex-1 do
      if (((panel[dy+0,dx+0] )  = 2) and
          ((panel[dy+1,dx+0] )  = 2) and
          ((panel[dy+1,dx+1] )  = 2) and
          ((panel[dy+0,dx+1] )  = 2)) then
      begin
        panel[dy+0,dx+0] := 3;
        panel[dy+1,dx+0] := 4;
        panel[dy+1,dx+1] := 5;
        panel[dy+0,dx+1] := 6;
      end;



end;

procedure TOTPlayer.Setspeed(s : Byte);
begin
  if (MyTimer<>0) then KillTimer(0,MyTimer);
   MyTimer:=SetTimer(0, 1, 1000 - (s-1) * 12, @TMPTimerProcces);
end;

procedure TOTPlayer.Drawsquare(i,j,c : Integer;isbomb:Boolean=False);
var
  dx,dy : Integer;
begin
  dx := (j-1)*24;
  dy := (i-1)*24;

// if isbomb then
//  DrawRectangle2D(offpx+dx,offpy+dy ,24,24,$0095D5)
// else
if c>10 then
  DrawTextureBox( offpx+dx,offpy+dy,
                24,24,
                MyTileGFX[0],
                0,
                False,
                c);

 if isbomb or (c=2) then
        DrawTextureBox( offpx+dx,offpy+dy,
               24,24,
                MyTileGFX[2]);

 if c=3 then
        DrawTextureBox( offpx+dx,offpy+dy,
               24*2,24*2,
                MyTileGFX[2]);
end;


procedure TOTPlayer.showfigure;
var
  i,j : Integer;
begin
  for i:=1 to 4 do
    for j:=1 to 4 do
     begin
      if tetr[i,j]>0 then
       drawsquare(i+y-1,j+x-1,fcl,tetr[i,j]=2);
     end;
end;

function TOTPlayer.canrotate : Boolean;
var
i,j,k : Integer;
t : TTetr;
begin
  result := true;
  k := num;
  if k < 4 then inc(k) else k := 1;
  t := FigureList[gen,k];
  for i := 1 to 4 do
    for j := 1 to 4 do
      if (t[i,j]>0) and ((panel[i+y-1,j+x-1]>0)
      or (j-1+x-1<0) or (j+x>sizex+1) or (i+y>sizey+1)) then
      begin
        result := false;
        exit;
      end;
end;


procedure TOTPlayer.rotatefigure;
var
  i,j,m,c,l:Integer;
  b:array[1..4,1..4] of Byte;
begin
  if num < 4 then inc(num) else num := 1;

if (GameMode = TETRIS_MODE_BOMBER) then
begin
  for j:=1 to 4 do
  for i:=1 to 4 do
  b[i,4-j+1]:=tetr[j,i];

      if (gen=3)or(gen=4) then m:=1
      else
      if (gen=10) then m:=4
      else
      m:=2;

      for j:=1 to m do
      begin
       c:=0;
        for i:=1 to 4 do
        begin
          if b[i,1]=0 then Inc(c);
        end;
        if c>3 then
        begin
          for l:=1 to 3 do
          for i:=1 to 4 do b[i,l]:=b[i,l+1];
          for i:=1 to 4 do b[i,4]:=0;
        end;

      end;

      for j:=1 to 4 do
      for i:=1 to 4 do
        tetr[j,i]:= b[j,i];
end
else
  tetr := FigureList[gen,num];



end;

procedure TOTPlayer.gennextfigure;
var
  i,j,k:Integer;
begin
  if FigureSet=0 then nextgen := random(7)+1
  else
  nextgen := random(11)+1;

  nextnum := random(4)+1;
  nexttetr := FigureList[nextgen,nextnum];
  nextfcl :=  colors[random(12)+1];

   if (GameMode = TETRIS_MODE_BOMBER) then
   begin
     k:=-1;
      for i := 1 to 4 do
      for j := 1 to 4 do
      begin
       inc(k);
        if nexttetr[i,j]=1 then
        begin
         if (k=nextgen) or (nextgen=6) then
         begin
           nexttetr[i,j]:=2;
         end;
        end;
      end;

   end;

end;

procedure TOTPlayer.nextfigure;
var i,j : Integer;
begin
  gen := nextgen;
  num := nextnum;
  tetr := nexttetr;

  fcl := nextfcl;
  for i := 4 downto 1 do
    for j := 1 to 4 do
      if tetr[i,j]>0 then
      begin
        y := -3+(4-i);
        x := 4;
        gennextfigure;
        Exit;
      end;
  x := 4;
  gennextfigure;
end;


procedure TOTPlayer.newgame(mode:Byte);
var i,j : Integer;
begin
  sizex:= 10;
  sizey:= 22;

  for i := -3 to sizey do
    for j := 1 to sizex do
      panel[i,j] := 0;

  GameMode:=mode;

  if mode=TETRIS_MODE_BOMBER then
  BomberTimer:=GetTickCount;

  scores := 0;
  lines :=0;
  level :=0;
  level_lines:=0;
  FigureSet:=0;

  setspeed(1);
  randomize;
  gennextfigure;
  nextfigure;

  StartGame:=True;
  PauseGame:=False;

  offpx:=SCR2W-(sizex div 2)*24;
  offpy:=24*2;
  Endgame:=False;
  lines_counter:=0;

end;

function TOTPlayer.canmoveleft : Boolean;
var i,j : Integer;
begin
  result := true;
  for i := 1 to 4 do
    for j := 1 to 4 do
      if (tetr[i,j]>0) and ((panel[i+y-1,j-1+x-1]>0) or (j-1+x-1=0)) then
      begin
        result := false;
        exit;
      end;
end;

function TOTPlayer.canmoveright : Boolean;
var i,j : Integer;
begin
  result := true;
  for i := 1 to 4 do
    for j := 1 to 4 do
      if (tetr[i,j]>0) and ((panel[i+y-1,j+x]>0) or (j+x=sizex+1)) then
      begin
        result := false;
        exit;
      end;
end;

function TOTPlayer.canmovedown : Boolean;
var i,j : Integer;
begin
  result := true;
  for i := 4 downto 1 do
    for j := 1 to 4 do
      if (tetr[i,j]>0) and ((panel[i+y,j+x-1]>0) or (i+y=sizey+1)) then
      begin
        PlaySoundFromMEM(0);
        Make_Megabombs;
        result := false;
        exit;
      end;
end;


function TOTPlayer.gameover : Boolean;
var
  i : Integer;
begin
  Result := False;
  for i := 1 to sizex do
    if panel[0,i]>0 then
    begin
      Result := True;
      Exit;
    end;
end;

procedure TOTPlayer.checkpanel;
var i,j,k,l,c : Integer;
begin
    l := 0;
    for i := 1 to sizey do
    begin
      c := 0;
      for j := 1 to sizex do
      if panel[i,j]>0 then inc(c);

      if c = sizex then
      begin
        Inc(l);

        PlaySoundFromMEM(Random(3)+2);

        for j := 1 to sizex do
        if (panel[i,j]>1) and (panel[i,j]<10)then Create_bomb_explo(i,j);

        for k := 1 to i-1 do
          for j := 1 to sizex do
          begin
            panel[i-k+1,j] := panel[i-k,j];
            panel[i-k,j] := 0;
          end;
      end;
    end;

   if l>0 then
   begin
    Inc(scores, l * 10+ Random(5));
    Inc(lines,l);
    Inc(level_lines,l);

    Inc(lines_counter,l);
   end;

    if (level_lines >= level_linesa[level mod  10]) then
    begin
      Inc(level);
      if (not((level mod 2)>0)) then level_lines:= 0;
      setspeed(level);
    end;

end;

procedure TOTPlayer.fixfigure;
var i,j : Integer;
begin
  for i := 1 to 4 do
    for j := 1 to 4 do
      if tetr[i,j]>0 then
      begin
       if tetr[i,j]=1 then panel[y+i-1,x+j-1] := fcl;
       if tetr[i,j]=2 then panel[y+i-1,x+j-1] := 2;
      end;
end;

procedure TOTPlayer.stopmove;
begin
  fixfigure;
  Make_Megabombs;
  checkpanel;
  if not gameover then
  begin
    nextfigure;
  end
  else
  begin
   Endgame:=True;
  end;
end;

procedure TOTPlayer.moveleft;
begin
  dec(x);
end;

procedure TOTPlayer.moveright;
begin
  inc(x);
end;

procedure TOTPlayer.movedown;
begin
  inc(y);
end;


procedure TOTPlayer.Procces();
var
  i,ix,iy,c,k:Integer;
begin

  if EndGame then
  begin
    Exit;
  end;

   if StartGame=False then
   begin
        if Keys[vk_space] then
        begin
         StartGame:=True;
         Keys[vk_space]:=false;
        end;
    Exit;
   end;


  if Keys[VK_Space] then
  begin
    while canmovedown do movedown;
          stopmove;
  end;


  if Keys[VK_Left] then
  if canmoveleft then moveleft;

  if Keys[VK_Right] then
  if canmoveright then moveright;

  if Keys[VK_Up] then
  if canrotate then rotatefigure;

  if Keys[VK_Down] then
  begin
    if canmovedown then movedown else stopmove;
  end;

  if GameMode=TETRIS_MODE_BOMBER then
  begin

    proccesExplosion;
    proccesFlyBlocks;
    
    if (GetTickCount-BomberTimer)> 10000 then
    begin

     k:=0;
     for i:=0 to k do
     begin
        for iy:=1 to sizey-1 do
          for ix:=1 to sizex do
          begin
            panel[iy,ix]:= panel[iy+1,ix];
          end;

        iy:= sizey;
        for ix:=1 to sizex do
        begin
          panel[iy,ix]:= colors[random(12)+1];
        end;

        for c:=0 to 2 do
        begin
          ix:= random(sizex)+1;
          panel[iy,ix]:= 0;
        end;
     end;

     BomberTimer:=GetTickCount;
     lines_counter:= 0;
    end;

  end;
end;

procedure TOTPlayer.Draw();
var
  i,j,k,c,l,m:Integer;
  dx,dy:Integer;
  tmp:TTetr;
  s:string;
  b:array[1..4,1..4] of Byte;
begin
    DrawRectangle2D(offpx,offpy+1 ,24*sizex,24*sizey,$0095D5);
    DrawRectangle2D(offpx+2,offpy+2 ,24*sizex-4,24*sizey-4,0,180,0,True);
    DrawRectangle2D(offpx-5,offpy+2,24*sizex+10,24*sizey+5,$0095D5);


    showfigure;

    for i := 1 to sizey do
    for j := 1 to sizex do
    begin
      if panel[i,j]>0 then
        drawsquare(i,j,panel[i,j]);
    end;

    DrawExplosion;
    DrawFlyBlocks;




  DrawRectangle2D(SCR2W+182-16,260-16,16*4,16*4,0,180,0,True);

   glBlend(1);
    for i := 1 to 4 do
    for j := 1 to 4 do
    if nexttetr[i,j]>0 then
    begin

       dx := SCR2W+182 +(j-1)*12;
       dy := 260  +(i-1)*12;

     DrawTextureBox( dx,dy,
                12,12,
                MyTileGFX[1],
                0,
                False,
                nextfcl);

       if nexttetr[i,j]=2 then
       DrawTextureBox( dx,dy,
                12,12,
                MyTileGFX[2]);

    end;

    DrawTextureBox( SCR2W-220,SCRH- 100, 128,128,MyPanelGFX[0]);
    DrawTextureBox( SCR2W-220,SCRH- 180, 128,128,MyPanelGFX[1]);

    DrawTextureBox( SCR2W+140,10, 128,64,MyPanelGFX[3]);
    DrawTextureBox( SCR2W+140,80, 128,64,MyPanelGFX[4]);



    SText2D(MySysfont[2], IntToStr(scores),SCR2W+160,60,Rgb(250,153,36),255);
    SText2D(MySysfont[2], IntToStr(lines), SCR2W-220+10,SCRH- 100+50,Rgb(104,250,240),255);
    SText2D(MySysfont[2], s+IntToStr(level),SCR2W-220+10,SCRH- 180+50,Rgb(108,217,250),255);

    SText2D(MySysfont[2], '0',SCR2W+160,60+ 68,Rgb(251,124,71),255);

   if GameMode=TETRIS_MODE_BOMBER then
   begin

     if EndGame then i:=0
     else
     i:= Trunc(10 - (GetTickCount - BomberTimer) / 1000);

     DrawTextureBox( SCR2W+140,150, 128,64,MyPanelGFX[2]);
     s:='';
     if i<10 then s:='0';
     SText2D(MySysfont[2], '00:00:'+s+IntToStr(i), SCR2W+166,60+ 64+75,Rgb(151,250,98),255);

   end;

  if Endgame then
  begin
    SText2D(MySysfont[0],'Игра закончена',10 , 20,Rgb(255,0,0),255);
  end;

end;

end.
