unit uGame;

interface
uses
  Omega, Classes, SysUtils;

type
  TScene = ( sLogo, sLoad, sGame, sMenu, sPause );

type
  TPause_Scene = ( sPausse, sLose, sLevel );

type
  TFigure = record
  bytes: array [0..3, 0..3] of byte;
  tex: array [0..3, 0..3] of PTexture;
  end;

type
  TGreed = record
  byte: byte;
  rect: oerect;
  tex: ptexture
  end;

type
  quad = record
  byte: byte;
  delbyte: byte;
  openleft, openright, opentop, opendown: boolean;
  rect: oerect;
  tex: ptexture;
  move: boolean;
  full: boolean;
  delete, delete2: boolean;
  alpha, delalpha: integer;
  text_alpha, text_y: single;
  end;

type
  tplay = record
  x, y, f, a, nextf: integer;
  speed, stopr, stopl, stopro, down: integer;
  stop: single;
  enable, canl, canr, canro, cand, cand2, cans: boolean;
  end;

type
  tstrings = record
  y, alpha: single;
  x: integer;
  text: string[20];
  alpha_dir: byte;
  end;

var
  Pause_Scene: TPause_Scene;
  Blocks: array [2..12] of PTexture;
  Blocks2: array [2..12] of PTexture;
  greed: array [0..20, 0..17] of quad;
  greed2: array [0..20, 0..17] of quad;
  delgreed: array [0..20, 0..17] of quad;
  testt: array [0..3, 0..3] of TGreed;
  labelpanels: array [0..3] of PTexture;
  preview: array [1..12] of PTexture;
  preview2, preview1: ptexture;
  strings: array[0..17] of tstrings;
  strings2: array[0..15] of tstrings;
  play: tplay;
  F : TFileStream;
  bytes: array [0..3, 0..3] of byte;
  Figures: array [0..12, 0..3] of TFigure;
  Scene, Next_Scene2: TScene;
  Nick: String[10];
  Save_Nick: String[10];
  Font: array [0..3] of PFont2D;
  Main_Timer: POTimer;
  Cursors: array [0..5] of PTexture;
  Cursor, Cursor_Color, Black, White, Back, Title, Logo, Block, Block2, Panel, big_gear, Black_pause: PTexture;
  Cursor_ColorCoff, logo_count, preview_alpha, preview_alpha2, pause_BlackDir,
  pause_Blackalpha, pause_alpha, Points, Add_Points, Level, Pipes_Left, Minus_Pipes: Integer;
  Cursor_ColorAlpha, logo_alpha, alpha, Black_Alpha, gear_angle: Single;
  Black_Screen, game_over, game_over2, show_greed: boolean;
  rotate_snd, Move_snd, Down_snd, Check_snd, Button_snd, Click_snd, Pipe_snd: PSound;
  fullscreen: boolean;
  procedure Game_Process;
  procedure Game_Paint;
  procedure Logo_Process;
  procedure Logo_Paint;
  function BoolToStr( Bool: boolean ): String;
  function StrToBool( Str: String ): Boolean;
  procedure Open( N: integer );
  procedure load_top;
  procedure save_top;
  procedure add_score;
  procedure scroll_text;
  procedure scroll_text2;
  procedure pausegame;
  procedure pausegame_paint;

implementation
uses
  uInit, uMenu;

procedure add_score;
begin
  // Первое место!)
  if (points > top_scores[0].points) then
  begin
    top_scores[4].points := top_scores[3].points;
    top_scores[4].nick := top_scores[3].nick;
    top_scores[3].points := top_scores[2].points;
    top_scores[3].nick := top_scores[2].nick;
    top_scores[2].points := top_scores[1].points;
    top_scores[2].nick := top_scores[1].nick;
    top_scores[1].points := top_scores[0].points;
    top_scores[1].nick := top_scores[0].nick;
    top_scores[0].points := points;
    top_scores[0].nick := save_nick;
  end;
  // Второе место!)
  if (points > top_scores[1].points) and (points < top_scores[0].points) then
  begin
    top_scores[4].points := top_scores[3].points;
    top_scores[4].nick := top_scores[3].nick;
    top_scores[3].points := top_scores[2].points;
    top_scores[3].nick := top_scores[2].nick;
    top_scores[2].points := top_scores[1].points;
    top_scores[2].nick := top_scores[1].nick;
    top_scores[1].points := points;
    top_scores[1].nick := save_nick;
  end;
  // Третье место!)
  if (points > top_scores[2].points) and (points < top_scores[1].points) then
  begin
    top_scores[4].points := top_scores[3].points;
    top_scores[4].nick := top_scores[3].nick;
    top_scores[3].points := top_scores[2].points;
    top_scores[3].nick := top_scores[2].nick;
    top_scores[2].points := points;
    top_scores[2].nick := save_nick;
  end;
  // Четвёртое место!)
  if (points > top_scores[3].points) and (points < top_scores[2].points) then
  begin
    top_scores[4].points := top_scores[3].points;
    top_scores[4].nick := top_scores[3].nick;
    top_scores[3].points := points;
    top_scores[3].nick := save_nick;
  end;
  // Пятое место!)
  if (points > top_scores[4].points) and (points < top_scores[3].points) then
  begin
    top_scores[4].points := points;
    top_scores[4].nick := save_nick;
  end;
end;

// Сохраняем Top
procedure save_top;
var
i: integer;
begin
 f := TFileStream.Create('data\save\scores'+'.top', fmCreate);
 for i := 0 to 4 do
 begin
   f.Write(Top_Scores[i].nick,10);
   f.Write(Top_Scores[i].points,30);
 end;
 f.Free;
end;

// Загружаем Top
procedure load_top;
var
i: integer;
begin
  f := TFileStream.Create('data\save\scores'+'.top', fmOpenRead);
  for i := 0 to 4 do
  begin
    f.Read(Top_Scores[i].nick,10);
    f.Read(Top_Scores[i].points,30);
  end;
  f.Free;
end;

procedure pausegame;
var
i, j: integer;
begin
  gear_angle := gear_angle + 0.25;
  if gear_angle >= 360 then gear_angle := 0;
  if Black_Menu.enable then
  begin
    case Black_Menu.direct of
    1: Black_Menu.alpha := Black_Menu.alpha + 3;
    2: Black_Menu.alpha := Black_Menu.alpha - 3;
    end;
    if Black_Menu.alpha = 253 then
    begin
      Menu_Scene := Next_Scene;
      Scene := Next_Scene2;
      NewGame.alpha := 0;
      Options.alpha := 0;
      About.alpha := 0;
      TopScores.alpha := 0;
      Quit.alpha := 0;
      RBack.alpha := 0;
      pause.alpha := 0;
      tomenu.alpha := 0;
      restart.alpha := 0;
      nextlevel.alpha := 0;
      BigSounds.alpha := 0;
      BigOther.alpha := 0;
      Black_Menu.direct := 2;
      Nick_LabelType := 2;
      //Nick := Key_EndReadText;
    end;
    if Black_Menu.alpha <= 0 then Black_Menu.enable := false;
  end;
  case pause_BlackDir of
    1:
    begin
      if pause_blackalpha < 170 then pause_blackalpha := pause_blackalpha + 3;
      if pause_alpha < 255 then pause_alpha := pause_alpha + 5;
    end;
    2:
    begin
      if pause_blackalpha > 0 then pause_blackalpha := pause_blackalpha - 3;
      if pause_alpha > 0 then pause_alpha := pause_alpha - 5;
      if (pause_alpha <= 0) or (pause_blackalpha <= 0) then
      begin
        resume.alpha := 0;
        restart.alpha := 0;
        tomenu.alpha := 0;
        nextlevel.alpha := 0;
        Scene := sGame;
      end;
    end;
  end;
  case Pause_Scene of
  sPausse:
  begin
    // resume button
    if Col_PointInAABB( Mouse_X, Mouse_Y, @resume.rect ) and (resume.alpha < 254)
    then resume.alpha := resume.alpha + 3;
    if (Col_PointInAABB( Mouse_X, Mouse_Y, @resume.rect ) = false) and (resume.alpha >= 3)
    then resume.alpha := resume.alpha - 3;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @resume.Rect ) then
    begin
      if resume.play and (MuteSounds_check.check = false) then Snd_Play(button_snd, 1, volume(sounds_volume2), 50, false);
      resume.play := false
    end else
    begin
      resume.play := true;
    end;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @resume.rect ) and Mouse_Click( M_LBUTTON ) then
    begin
      if MuteSounds_Check.check = false then Snd_Play(click_snd, 1, volume(sounds_volume2), 50, false);
      Pause_BlackDir := 2;
    end;
    // tomenu button
    if Col_PointInAABB( Mouse_X, Mouse_Y, @tomenu.rect ) and (tomenu.alpha < 254)
    then tomenu.alpha := tomenu.alpha + 3;
    if (Col_PointInAABB( Mouse_X, Mouse_Y, @tomenu.rect ) = false) and (tomenu.alpha >= 3)
    then tomenu.alpha := tomenu.alpha - 3;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @tomenu.Rect ) then
    begin
      if tomenu.play and (MuteSounds_check.check = false) then Snd_Play(button_snd, 1, volume(sounds_volume2), 50, false);
      tomenu.play := false
    end else
    begin
      tomenu.play := true;
    end;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @tomenu.rect ) and Mouse_Click( M_LBUTTON ) then
    begin
      add_score;
      savenick := true;
      if MuteSounds_Check.check = false then Snd_Play(click_snd, 1, volume(sounds_volume2), 50, false);
      Black_Menu.enable := true;
      Black_Menu.alpha := 1;
      Black_Menu.direct := 1;
      Next_Scene := sMain;
      Next_Scene2 := sMenu;
    end;
    // restart button
    if Col_PointInAABB( Mouse_X, Mouse_Y, @restart.rect ) and (restart.alpha < 254)
    then restart.alpha := restart.alpha + 3;
    if (Col_PointInAABB( Mouse_X, Mouse_Y, @restart.rect ) = false) and (restart.alpha >= 3)
    then restart.alpha := restart.alpha - 3;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @restart.Rect ) then
    begin
      if restart.play and (MuteSounds_check.check = false) then Snd_Play(button_snd, 1, volume(sounds_volume2), 50, false);
      restart.play := false
    end else
    begin
      restart.play := true;
    end;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @restart.rect ) and Mouse_Click( M_LBUTTON ) then
    begin
      add_score;
      if MuteSounds_Check.check = false then Snd_Play(click_snd, 1, volume(sounds_volume2), 50, false);
      Pause_BlackDir := 2;
      for i := 1 to 18 do
      begin
        greed[i,1].byte := 10;
        greed[i,16].byte := 8;
        greed[i,1].tex := Blocks[10];
        greed[i,16].tex := Blocks[8];
        greed[i,1].full := true;
        greed[i,16].full := true;
        greed[i,1].openright := true;
        greed[i,16].openleft := true;
      end;
      for i := 2 to 17 do
      begin
        greed[i,1].delbyte := i - 1;
        greed[i,16].delbyte := 15 + i;
      end;
      greed[18,1].tex := Blocks[5];
      greed[18,16].tex := Blocks[4];
      greed[1,1].tex := Blocks[6];
      greed[1,16].tex := Blocks[7];
      for i := 2 to 15 do
      begin
        greed[18,i].byte := 3;
        greed[18,i].tex := Blocks[3];
        greed[1,i].byte := 3;
        greed[1,i].tex := Blocks[3];
      end;
      for i := 2 to 17 do
      for j := 2 to 15 do
      begin
        greed[i,j].byte := 1;
        greed[i,j].delalpha := 255;
        greed[i,j].move := true;
        greed[i,j].full := false;
        greed[i,j].alpha := 0;
        greed[i,j].openleft := false;
        greed[i,j].openright := false;
        greed[i,j].opentop := false;
        greed[i,j].opendown := false;
        greed[i,j].delbyte := 0;
      end;
      //for i := 0 to 17 do greed[20,i].byte := 2;
      play.nextf := random(7) + 1;
      play.f := play.nextf;
      play.nextf := random(7) + 1;
      preview1 := preview[play.nextf];
      preview_alpha := 255;
      play.a := random(4);
      play.stop := 500;
      play.stopl := 1;
      play.stopr := 1;
      play.stopro := 1;
      play.enable := true;
      play.x := 7;
      play.y := 2;
      Points := 0;
      Add_Points := 0;
      Level := 1;
      Pipes_Left := 50;
      game_over2 := true;
      game_over := false;
    end;
  end;
  sLose:
  begin
    // tomenu button
    if Col_PointInAABB( Mouse_X, Mouse_Y, @yes.rect ) and (tomenu.alpha < 254)
    then tomenu.alpha := tomenu.alpha + 3;
    if (Col_PointInAABB( Mouse_X, Mouse_Y, @yes.rect ) = false) and (tomenu.alpha >= 3)
    then tomenu.alpha := tomenu.alpha - 3;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @yes.Rect ) then
    begin
      if tomenu.play and (MuteSounds_check.check = false) then Snd_Play(button_snd, 1, volume(sounds_volume2), 50, false);
      tomenu.play := false
    end else
    begin
      tomenu.play := true;
    end;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @yes.rect ) and Mouse_Click( M_LBUTTON ) then
    begin
      add_score;
      savenick := true;
      if MuteSounds_Check.check = false then Snd_Play(click_snd, 1, volume(sounds_volume2), 50, false);
      Black_Menu.enable := true;
      Black_Menu.alpha := 1;
      Black_Menu.direct := 1;
      Next_Scene := sMain;
      Next_Scene2 := sMenu;
    end;
    // topscores button
    if Col_PointInAABB( Mouse_X, Mouse_Y, @no.rect ) and (topscores.alpha < 254)
    then topscores.alpha := topscores.alpha + 3;
    if (Col_PointInAABB( Mouse_X, Mouse_Y, @no.rect ) = false) and (topscores.alpha >= 3)
    then topscores.alpha := topscores.alpha - 3;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @no.Rect ) then
    begin
      if topscores.play and (MuteSounds_check.check = false) then Snd_Play(button_snd, 1, volume(sounds_volume2), 50, false);
      topscores.play := false
    end else
    begin
      topscores.play := true;
    end;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @no.rect ) and Mouse_Click( M_LBUTTON ) then
    begin
      add_score;
      savenick := true;
      if MuteSounds_Check.check = false then Snd_Play(click_snd, 1, volume(sounds_volume2), 50, false);
      Black_Menu.enable := true;
      Black_Menu.alpha := 1;
      Black_Menu.direct := 1;
      Next_Scene := sTop;
      Next_Scene2 := sMenu;
    end;
  end;
  sLevel:
  begin
    // NextLevel button
    if Col_PointInAABB( Mouse_X, Mouse_Y, @NextLevel.rect ) and (NextLevel.alpha < 254)
    then NextLevel.alpha := NextLevel.alpha + 3;
    if (Col_PointInAABB( Mouse_X, Mouse_Y, @NextLevel.rect ) = false) and (NextLevel.alpha >= 3)
    then NextLevel.alpha := NextLevel.alpha - 3;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @NextLevel.Rect ) then
    begin
      if NextLevel.play and (MuteSounds_check.check = false) then Snd_Play(button_snd, 1, volume(sounds_volume2), 50, false);
      NextLevel.play := false
    end else
    begin
      NextLevel.play := true;
    end;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @NextLevel.rect ) and Mouse_Click( M_LBUTTON ) then
    begin
      if MuteSounds_Check.check = false then Snd_Play(click_snd, 1, volume(sounds_volume2), 50, false);
      Pause_BlackDir := 2;
      for i := 2 to 17 do
      for j := 2 to 15 do
      begin
        greed[i,j].byte := 1;
        greed[i,j].delalpha := 255;
        greed[i,j].move := true;
        greed[i,j].full := false;
        greed[i,j].alpha := 0;
        greed[i,j].openleft := false;
        greed[i,j].openright := false;
        greed[i,j].opentop := false;
        greed[i,j].opendown := false;
        greed[i,j].delbyte := 0;
      end;
      play.nextf := random(7) + 1;
      play.f := play.nextf;
      play.nextf := random(7) + 1;
      preview1 := preview[play.nextf];
      preview_alpha := 255;
      play.a := random(4);
      play.stop := 500;
      play.stopl := 1;
      play.stopr := 1;
      play.stopro := 1;
      play.enable := true;
      play.x := 7;
      play.y := 2;
      Add_Points := 0;
      Level := Level + 1;
      Pipes_Left := 50 * Level;
      game_over2 := true;
      game_over := false;
    end;
  end;
  end;
  if Key_Press( K_ESCAPE ) then Message_test := Message_test + 1;
  if Message_test > 100 then Message_test := 0;
end;

procedure pausegame_paint;
var
i, j: integer;
begin
  SSprite_Draw( Back, -10, -10, 815, 615, 255, 0, FX_DEFAULT );
  if ShowGears then Balls_Paint;
  SSprite_Draw( Panel, 270, 25, 500, 550, 255, 0, FX_DEFAULT );
  SSprite_Draw( labelpanels[0], 30, 20, 200, 100, 255, 0, FX_DEFAULT );
  Text_Draw( Font[1], 90, 30, PChar('Level'));
  Text_Draw( Font[0], 130 - (Text_GetWidth(Font[0], PChar(U_IntToStr(Level)), 1, 1) / 2) + 2, 70, PChar(U_IntToStr(Level)));
  SSprite_Draw( labelpanels[1], 30, 135, 200, 100, 255, 0, FX_DEFAULT );
  Text_Draw( Font[1], 83, 145, PChar('Points'));
  Text_Draw( Font[0], 130 - (Text_GetWidth(Font[0], PChar(U_IntToStr(Points)), 1, 1) / 2) + 2, 185, PChar(U_IntToStr(Points)));
  SSprite_Draw( labelpanels[2], 30, 250, 200, 100, 255, 0, FX_DEFAULT );
  Text_Draw( Font[1], 55, 260, PChar('Pipes left'));
  Text_Draw( Font[0], 130 - (Text_GetWidth(Font[0], PChar(U_IntToStr(Pipes_Left)), 1, 1) / 2) + 2, 300, PChar(U_IntToStr(Pipes_Left)));
  SSprite_Draw( labelpanels[3], 30, 372, 200, 200, 255, 0, FX_DEFAULT );
  SSprite_Draw( preview1, 70, 402, 120, 120, preview_alpha, 0, FX_DEFAULT );
  if preview_alpha2 > 0 then SSprite_Draw( preview2, 70, 402, 120, 120, preview_alpha2, 0, FX_DEFAULT );
  Text_Draw( Font[1], 97, 382, PChar('Next'));
  SSprite_Draw( pause.tex[0], pause.rect.X, pause.rect.Y, pause.rect.Width, pause.rect.Height, 255, 0, FX_DEFAULT );
  if pause.alpha > 0 then SSprite_Draw( pause.tex[1], pause.rect.X, pause.rect.Y, pause.rect.Width, pause.rect.Height, pause.alpha, 0, FX_DEFAULT );
  for i := 0 to 19 do for j := 1 to 16 do if greed[i,j].byte > 1 then SSprite_Draw(greed[i,j].tex, greed[i,j].rect.X, greed[i,j].rect.y, 30, 30, 255, 0 );
  for i := 2 to 17 do for j := 2 to 15 do if greed[i,j].alpha > 0 then SSprite_Draw(greed2[i,j].tex, greed2[i,j].rect.X, greed2[i,j].rect.y, 30, 30, greed[i,j].alpha, 0 );
  for i := 2 to 17 do for j := 2 to 15 do if greed[i,j].full and (greed[i,j].text_alpha > 5) then Text_Draw( Font[1], greed[i,j].rect.X, round(greed[i,j].text_y), PChar('+'+U_IntToStr(1)), 0.7, 1, round(greed[i,j].text_alpha));
  if show_greed then for i := 2 to 17 do for j := 2 to 15 do Pr2D_TRect( @greed[i,j].rect, $FFFFFF, 10, PR_DEFAULT );
  //////////////////////////////////////////////////
  SSprite_Draw( Black_pause, -10, -10, 815, 615, Round(pause_blackalpha), 0, FX_DEFAULT );
  case Pause_Scene of
  sPausse:
  begin
    SSprite_Draw( big_gear, 200, 100, 400, 400, Round(pause_alpha), gear_angle, FX_DEFAULT );
    //Text_Draw( Font[1], 352, 200, PChar('PAUSE'), 1, 1, Round(pause_alpha));
    // resume button
    SSprite_Draw( resume.tex[0], resume.rect.X, resume.rect.Y, resume.rect.Width, resume.rect.Height, 255 - (255 - pause_alpha), 0, FX_DEFAULT );
    if resume.alpha > 0 then
    begin
      case pause_BlackDir of
        1:SSprite_Draw( resume.tex[1], resume.rect.X, resume.rect.Y, resume.rect.Width, resume.rect.Height, resume.alpha, 0, FX_DEFAULT );
        2:SSprite_Draw( resume.tex[1], resume.rect.X, resume.rect.Y, resume.rect.Width, resume.rect.Height, pause_alpha, 0, FX_DEFAULT );
      end;
    end;
    // restart button
    SSprite_Draw( restart.tex[0], restart.rect.X, restart.rect.Y, restart.rect.Width, restart.rect.Height, 255 - (255 - pause_alpha), 0, FX_DEFAULT );
    if restart.alpha > 0 then
    begin
      case pause_BlackDir of
        1:SSprite_Draw( restart.tex[1], restart.rect.X, restart.rect.Y, restart.rect.Width, restart.rect.Height, restart.alpha, 0, FX_DEFAULT );
        2:SSprite_Draw( restart.tex[1], restart.rect.X, restart.rect.Y, restart.rect.Width, restart.rect.Height, pause_alpha, 0, FX_DEFAULT );
      end;
    end;
    // tomenu button
    SSprite_Draw( tomenu.tex[0], tomenu.rect.X, tomenu.rect.Y, tomenu.rect.Width, tomenu.rect.Height, 255 - (255 - pause_alpha), 0, FX_DEFAULT );
    if tomenu.alpha > 0 then
    begin
      case pause_BlackDir of
        1:SSprite_Draw( tomenu.tex[1], tomenu.rect.X, tomenu.rect.Y, tomenu.rect.Width, tomenu.rect.Height, tomenu.alpha, 0, FX_DEFAULT );
        2:SSprite_Draw( tomenu.tex[1], tomenu.rect.X, tomenu.rect.Y, tomenu.rect.Width, tomenu.rect.Height, pause_alpha, 0, FX_DEFAULT );
      end;
    end;
  end;
  sLose:
  begin
    SSprite_Draw( message_panel, 215, 200, 370, 200, Round(pause_alpha), 0, FX_DEFAULT );
    Text_Draw( Font[0], 310, 220, PChar('You lose =('), 1, 0, Round(pause_alpha));
    Text_Draw( Font[1], round(((800 - Text_GetWidth( Font[1], PChar('Your score: '+U_IntToStr(Points)), 1, 1 )) / 2) + 3),
    260, PChar('Your score: '+U_IntToStr(Points)), 1, 0, Round(pause_alpha));
    // tomenu button
    SSprite_Draw( tomenu.tex[0], 260, 330, 120, 50, 255 - (255 - pause_alpha), 0, FX_DEFAULT );
    if tomenu.alpha > 0 then
    begin
      case pause_BlackDir of
        1:SSprite_Draw( tomenu.tex[1], 260, 330, 120, 50, tomenu.alpha, 0, FX_DEFAULT );
        2:SSprite_Draw( tomenu.tex[1], 260, 330, 120, 50, pause_alpha, 0, FX_DEFAULT );
      end;
    end;
    // top scores button
    SSprite_Draw( TopScores.tex[0], 420, 330, 120, 50, 255 - (255 - pause_alpha), 0, FX_DEFAULT );
    if TopScores.alpha > 0 then
    begin
      case pause_BlackDir of
        1:SSprite_Draw( TopScores.tex[1], 420, 330, 120, 50, TopScores.alpha, 0, FX_DEFAULT );
        2:SSprite_Draw( TopScores.tex[1], 420, 330, 120, 50, pause_alpha, 0, FX_DEFAULT );
      end;
    end;
  end;
  sLevel:
  begin
    SSprite_Draw( message_panel, 215, 200, 370, 200, Round(pause_alpha), 0, FX_DEFAULT );
    Text_Draw( Font[1], 303, 220, PChar('Excellent! =)'), 1, 0, Round(pause_alpha));
    case pause_BlackDir of
      1:Text_Draw( Font[1], round(((800 - Text_GetWidth( Font[1], PChar('Level '+U_IntToStr(Level)+' complete!'), 1, 1 )) / 2) + 7),
        260, PChar('Level '+U_IntToStr(Level)+' complete!'), 1, 0, Round(pause_alpha));
      2:Text_Draw( Font[1], round(((800 - Text_GetWidth( Font[1], PChar('Level '+U_IntToStr(Level-1)+' complete!'), 1, 1 )) / 2) + 7),
        260, PChar('Level '+U_IntToStr(Level-1)+' complete!'), 1, 0, Round(pause_alpha));
    end;
    //Text_Draw( Font[1], round(((800 - Text_GetWidth( Font[1], PChar('Level '+U_IntToStr(Level)+' complete!'), 1, 1 )) / 2) + 7),
    //260, PChar('Level '+U_IntToStr(Level)+' complete!'), 1, 0, Round(pause_alpha));
    // NextLevel button
    SSprite_Draw( NextLevel.tex[0], 340, 330, 120, 50, 255 - (255 - pause_alpha), 0, FX_DEFAULT );
    if NextLevel.alpha > 0 then
    begin
      case pause_BlackDir of
        1:SSprite_Draw( NextLevel.tex[1], 340, 330, 120, 50, NextLevel.alpha, 0, FX_DEFAULT );
        2:SSprite_Draw( NextLevel.tex[1], 340, 330, 120, 50, pause_alpha, 0, FX_DEFAULT );
      end;
    end;
  end;
  end;
  //////////////////////////////////////////////////
  SSprite_Draw( Cursor, Mouse_X, Mouse_Y, 50, 50, 255, 0, FX_DEFAULT );
  SSprite_Draw( Cursor_Color, Mouse_X, Mouse_Y, 50, 50, Round(Cursor_ColorAlpha), 0, FX_DEFAULT );
  if Brightness < 50 then SSprite_Draw( BrightTex[0], -10, -10, 815, 615, 150 - (Brightness * 3), 0, FX_DEFAULT );
  if Brightness > 50 then SSprite_Draw( BrightTex[1], -10, -10, 815, 615, Round((Brightness - 50) * 1.5), 0, FX_DEFAULT );
  if Black_Menu.enable then SSprite_Draw( Black_Menu.tex, -10, -10, 815, 615, Black_Menu.alpha, 0, FX_DEFAULT );
  if Black_Screen then SSprite_Draw( Black, -10, -10, 815, 615, Round(Black_Alpha), 0, FX_DEFAULT );
end;

procedure scroll_text;
var
i: integer;
begin
  strings[0].y := strings[0].y - 1 * 0.5;
  for i := 1 to 17 do strings[i].y := strings[i-1].y + 50;
  for i := 0 to 17 do
  begin
    if strings[i].y = 440 then strings[i].alpha_dir := 1;
    if strings[i].y = 180 then strings[i].alpha_dir := 2;
    case strings[i].alpha_dir of
      1: if strings[i].alpha < 255 then strings[i].alpha := strings[i].alpha + 5;
      2: if strings[i].alpha > 0 then strings[i].alpha := strings[i].alpha - 5;
    end;
    //if strings[i].alpha > 255 then strings[i].alpha := 255;
    //if strings[i].alpha < 0 then strings[i].alpha := 0;
  end;
  if (strings[0].y < -750) then
  begin
    strings[0].y := 500;
    for i := 0 to 17 do strings[i].alpha := 0;
  end;
end;

procedure scroll_text2;
var
i: integer;
begin
  strings2[0].y := strings2[0].y - 1 * 0.5;
  for i := 1 to 15 do strings2[i].y := strings2[i-1].y + 50;
  for i := 0 to 15 do
  begin
    if strings2[i].y = 440 then strings2[i].alpha_dir := 1;
    if strings2[i].y = 180 then strings2[i].alpha_dir := 2;
    case strings2[i].alpha_dir of
      1: if strings2[i].alpha < 255 then strings2[i].alpha := strings2[i].alpha + 5;
      2: if strings2[i].alpha > 0 then strings2[i].alpha := strings2[i].alpha - 5;
    end;
  end;
  if (strings2[0].y < -650) then
  begin
    strings2[0].y := 500;
    for i := 0 to 15 do strings2[i].alpha := 0;
  end;
end;

procedure Open( N: Integer );
var i, j: Integer;
begin
 f := TFileStream.Create('data\figures\figure'+U_IntToStr(N)+'.txt', fmOpenRead);
 for i := 0 to 3 do
 for j := 0 to 3 do
 begin
   f.Read(Figures[N,0].bytes[i,j],1);
   f.Read(Figures[N,1].bytes[i,j],1);
   f.Read(Figures[N,2].bytes[i,j],1);
   f.Read(Figures[N,3].bytes[i,j],1);
 end;
 f.Free;
end;

function BoolToStr( Bool: boolean ): String;
begin
  if Bool then result := 'True' else Result := 'False';
end;

function StrToBool( Str: String ): Boolean;
begin
  if Str = 'True' then result := true else Result := false;
end;

procedure Logo_Process;
begin
  logo_count := logo_count - 1;
  if logo_count <= 0 then
  begin
    logo_alpha := logo_alpha - 2;
  end;
  if logo_alpha <= 0 then
  begin
    Key_BeginReadText( PChar(String(Nick)) );
    Key_MaxReadText( 10 );
    Nick_LabelType := 2;
    Nick := Key_EndReadText;
    Scene := sMenu;
    Menu_Scene := sMain;
    Black_Alpha := 255;
    Black_Screen := true;
  end;
  if Key_Press( K_SPACE ) then logo_count := 0;
  if Key_Press( K_ENTER ) then logo_count := 0;
  if Key_Press( K_ESCAPE ) then logo_count := 0;
  if Mouse_Press( M_LBUTTON ) then logo_count := 0;
end;

procedure Logo_Paint;
begin
  SSprite_Draw( logo, 144, 40, 512, 512, round(logo_alpha), 0, FX_DEFAULT );
end;

// События
procedure Game_Process;
var
i, j, r, k, l, m, p, n: integer;
begin
  if Mouse_click ( M_RBUTTON ) then
  begin
    balls_count := balls_count + 1;
    if balls_count > 5 then balls_count := 0;
    Balls2[balls_count].x := mouse_x;
    Balls2[balls_count].y := Mouse_y;
    Balls2[balls_count].W := 1;
    Balls2[balls_count].H := 1;
    Balls2[balls_count].Alpha := 255;
    Balls2[balls_count].Speed := Random(3) + 1;
  end;
  // game over
  for i := 2 to 15 do
  begin
    if game_over2 then
    begin
      if (greed[2,i].move = false) and (greed[2,i].byte > 1) then
      begin
        play.enable := false;
        game_over := true;
      end;
    end;
  end;

  if game_over then
  begin
    //Black_Menu.enable := true;
    //Black_Menu.alpha := 1;
    //Black_Menu.direct := 1;
    //Next_Scene := sMain;
    //Next_Scene2 := sMenu;
    pause_Alpha := 0;
    pause_BlackAlpha := 0;
    pause_BlackDir := 1;
    Scene := sPause;
    Pause_Scene := sLose;
    game_over := false;
    game_over2 := false;
  end;
  // game
  if play.enable then
  begin
    if Key_Press ( K_SPACE ) then
    begin
      play.stop := 0;
      play.cand := false;
      play.cand2 := true;
    end;
    play.stop := play.stop - (level * 0.5) - play.down;
    if play.stop <= 0 then
    begin
      // По дефольту разрешаем передвижение вниз
      play.cand := true;
      if play.cand2 then
      begin
        play.cand := false;
        play.cand2 := false;
      end;
      // Очищаем прежнее положение
      for i := 0 to 3 do
      for j := 0 to 3 do
      begin
        if (figures[play.f, play.a].bytes[i,j] > 1) and (greed[play.y+i,play.x+j].byte > 1) then greed[play.y+i,play.x+j].byte := 1;
      end;
      // Проверяем можем ли мы сдвинуть фигуру вниз
      for i := 0 to 3 do
      for j := 0 to 3 do
      begin
        if (figures[play.f, play.a].bytes[i,j] > 1) and (greed[play.y+i+1,play.x+j].byte > 1) then play.cand := false;
      end;
      // Если моежм, то сдвигаем фигуру вниз
      if play.cand then
      begin
        play.y := play.y + 1;
        if MuteSounds_Check.check = false then Snd_Play(move_snd, 1, volume(sounds_volume2), 50, false);
      end;
      if play.cand = false then
      begin
        play.cans := true;
        if (MuteSounds_Check.check = false) and (game_over2 = true) then Snd_Play(down_snd, 1, volume(sounds_volume2), 50, false);
        for i := 0 to 3 do
        for j := 0 to 3 do
        begin
          if (figures[play.f, play.a].bytes[i,j] > 1) then greed[play.y+i,play.x+j].move := false;
          if (figures[play.f, play.a].bytes[i,j] > 1) then greed[play.y+i,play.x+j].byte := figures[play.f, play.a].bytes[i,j];
          case figures[play.f, play.a].bytes[i,j] of
            2:
            begin
              greed[play.y+i,play.x+j].opentop := true;
              greed[play.y+i,play.x+j].opendown := true;
              greed[play.y+i,play.x+j].byte := 2;
            end;
            3:
            begin
              greed[play.y+i,play.x+j].openleft := true;
              greed[play.y+i,play.x+j].openright := true;
              greed[play.y+i,play.x+j].byte := 3;
            end;
            4:
            begin
              greed[play.y+i,play.x+j].openleft := true;
              greed[play.y+i,play.x+j].opentop := true;
              greed[play.y+i,play.x+j].byte := 4;
            end;
            5:
            begin
              greed[play.y+i,play.x+j].opentop := true;
              greed[play.y+i,play.x+j].openright := true;
              greed[play.y+i,play.x+j].byte := 5;
            end;
            6:
            begin
              greed[play.y+i,play.x+j].opendown := true;
              greed[play.y+i,play.x+j].openright := true;
              greed[play.y+i,play.x+j].byte := 6;
            end;
            7:
            begin
              greed[play.y+i,play.x+j].opendown := true;
              greed[play.y+i,play.x+j].openleft := true;
              greed[play.y+i,play.x+j].byte :=7;
            end;
            8:
            begin
              greed[play.y+i,play.x+j].opendown := true;
              greed[play.y+i,play.x+j].opentop := true;
              greed[play.y+i,play.x+j].openleft := true;
              greed[play.y+i,play.x+j].byte :=8;
            end;
            9:
            begin
              greed[play.y+i,play.x+j].openleft := true;
              greed[play.y+i,play.x+j].openright := true;
              greed[play.y+i,play.x+j].opentop := true;
              greed[play.y+i,play.x+j].byte := 9;
            end;
            10:
            begin
              greed[play.y+i,play.x+j].opendown := true;
              greed[play.y+i,play.x+j].opentop := true;
              greed[play.y+i,play.x+j].openright := true;
              greed[play.y+i,play.x+j].byte := 10;
            end;
            11:
            begin
              greed[play.y+i,play.x+j].openleft := true;
              greed[play.y+i,play.x+j].openright := true;
              greed[play.y+i,play.x+j].opendown := true;
              greed[play.y+i,play.x+j].byte := 11;
            end;
            12:
            begin
              greed[play.y+i,play.x+j].openleft := true;
              greed[play.y+i,play.x+j].openright := true;
              greed[play.y+i,play.x+j].opendown := true;
              greed[play.y+i,play.x+j].opentop := true;
              greed[play.y+i,play.x+j].byte := 12;
            end;
          end;
          if (figures[play.nextf, 1].bytes[i,j] > 1) and (greed[2+i,7+j].byte > 1) then
          begin
            play.cans := false;
            if game_over2 then game_over := true;
            play.enable := false;
          end;
        end;
        if play.cans then
        begin
          play.f := play.nextf;
          play.nextf := random(7) + 1;
          preview2 := preview[play.nextf];
          preview_alpha2 := 1;
          play.a := 1;
          play.stop := 500;
          play.x := 7;
          play.y := 2;
        end;
      end;
      play.stop := 500;
    end;

    if (preview_alpha2 > 0) and (preview_alpha2 < 255) then
    begin
      preview_alpha2 := preview_alpha2 + 5;
      preview_alpha := preview_alpha - 5;
    end;

    if preview_alpha2 >= 255 then
    begin
      preview1 := preview2;
      preview_alpha := 255;
      preview_alpha2 := 0;
    end;

    if Key_Down( K_LEFT ) then
    begin
      play.stopl := play.stopl - 1;
      if play.stopl = 0 then
      begin
      play.canl := true;
      for i := 0 to 3 do
      for j := 0 to 3 do
      begin
        if (figures[play.f, play.a].bytes[i,j] > 1) and (greed[play.y+i,play.x+j].byte > 1) then greed[play.y+i,play.x+j].byte := 1;
      end;
      for i := 0 to 3 do
      for j := 0 to 3 do
      begin
        if (figures[play.f, play.a].bytes[i,j] > 1) and (greed[play.y+i,play.x+j-1].byte > 1) then play.canl := false;
      end;
      if play.canl then
      begin
        play.x := play.x - 1;
        if MuteSounds_Check.check = false then Snd_Play(move_snd, 1, volume(sounds_volume2), 50, false);
      end;
      play.stopl := 15;
      end
    end;

    if Key_Press( K_LEFT ) then play.stopl := 1;

    if Key_Down( K_RIGHT ) then
    begin
      play.stopr := play.stopr - 1;
      if play.stopr = 0 then
      begin
      play.canr := true;
      for i := 0 to 3 do
      for j := 0 to 3 do
      begin
        if (figures[play.f, play.a].bytes[i,j] > 1) and (greed[play.y+i,play.x+j].byte > 1) then greed[play.y+i,play.x+j].byte := 1;
      end;
      for i := 0 to 3 do
      for j := 0 to 3 do
      begin
        if (figures[play.f, play.a].bytes[i,j] > 1) and (greed[play.y+i,play.x+j+1].byte > 1) then play.canr := false;
      end;
      if play.canr then
      begin
        play.x := play.x + 1;
        if MuteSounds_Check.check = false then Snd_Play(move_snd, 1, volume(sounds_volume2), 50, false);
      end;
      play.stopr := 15;
      end;
    end;

    if Key_Press ( K_RIGHT ) then play.stopr := 1;

    if Key_Down ( K_UP ) then
    begin
      play.stopro := play.stopro - 1;
      if play.stopro = 0 then
      begin
        play.canro := true;
        for i := 0 to 3 do
        for j := 0 to 3 do
        begin
          if (figures[play.f, play.a].bytes[i,j] > 1) and (greed[play.y+i,play.x+j].byte > 1) then greed[play.y+i,play.x+j].byte := 1;
        end;
        if play.a < 3 then
        begin
          for i := 0 to 3 do
          for j := 0 to 3 do
          begin
            if (figures[play.f, play.a+1].bytes[i,j] > 1) and (greed[play.y+i,play.x+j].byte > 1) then play.canro := false;
          end;
        end;
        if play.a = 3 then
        begin
          for i := 0 to 3 do
          for j := 0 to 3 do
          begin
            if (figures[play.f, play.a-3].bytes[i,j] > 1) and (greed[play.y+i,play.x+j].byte > 1) then play.canro := false;
          end;
        end;
        if play.canro then
        begin
          play.a := play.a + 1;
          if MuteSounds_Check.check = false then Snd_Play(rotate_snd, 1, volume(sounds_volume2), 50, false);
        end;
        if play.a > 3 then play.a := 0;
        play.stopro := 30;
      end;
    end;

    if Key_Press ( K_UP ) then play.stopro := 1;

    if Key_Down ( K_DOWN ) then play.down := 30;
    if Key_Press ( K_DOWN ) then play.down := 0;

    // Заполнение
    for i := 2 to 17 do
    for j := 2 to 15 do
    begin
      // слева
      if (greed[i,j-1].full = true) and (greed[i,j-1].move = false)
      and (greed[i,j].full = false) and (greed[i,j].move = false) then
      begin
        if (greed[i,j-1].openright = true) and (greed[i,j].openleft = true) and (greed[i,j].alpha < 255) then
        begin
          greed2[i,j].tex := blocks[greed[i,j].byte];
          greed[i,j].alpha := greed[i,j].alpha + 250;
          greed[i,j].delbyte := greed[i,j-1].delbyte;
        end;
      end;
      // справа
      if (greed[i,j+1].full = true) and (greed[i,j+1].move = false)
      and (greed[i,j].full = false) and (greed[i,j].move = false) then
      begin
        if (greed[i,j+1].openleft = true) and (greed[i,j].openright = true) and (greed[i,j].alpha < 255) then
        begin
          greed2[i,j].tex := blocks[greed[i,j].byte];
          greed[i,j].alpha := greed[i,j].alpha + 250;
          greed[i,j].delbyte := greed[i,j+1].delbyte;
        end;
      end;
      // сверху
      if (greed[i-1,j].full = true) and (greed[i-1,j].move = false)
      and (greed[i,j].full = false) and (greed[i,j].move = false) then
      begin
        if (greed[i-1,j].opendown = true) and (greed[i,j].opentop = true) and (greed[i,j].alpha < 255) then
        begin
          greed2[i,j].tex := blocks[greed[i,j].byte];
          greed[i,j].alpha := greed[i,j].alpha + 250;
          greed[i,j].delbyte := greed[i-1,j].delbyte;
        end;
      end;
      // снизу
      if (greed[i+1,j].full = true) and (greed[i+1,j].move = false)
      and (greed[i,j].full = false) and (greed[i,j].move = false) then
      begin
        if (greed[i+1,j].opentop = true) and (greed[i,j].opendown = true) and (greed[i,j].alpha < 255) then
        begin
          greed2[i,j].tex := blocks[greed[i,j].byte];
          greed[i,j].alpha := greed[i,j].alpha + 250;
          greed[i,j].delbyte := greed[i+1,j].delbyte;
        end;
      end;
      // отмечаем что труба заполнена
      if greed[i,j].alpha >= 255 then
      begin
        greed[i,j].alpha := 255;
        greed[i,j].full := true;
      end;
      // +1 =)
      if greed[i,j].alpha = 250 then
      begin
        Snd_Play(pipe_snd, 1, volume(sounds_volume2), 50, false);
        greed[i,j].text_alpha := 255;
        greed[i,j].text_y := greed[i,j].rect.y;
        Add_Points := Add_Points + 1;
        Minus_Pipes := Minus_Pipes + 1;
      end;
      if greed[i,j].text_alpha > 5 then
      begin
        greed[i,j].text_alpha := greed[i,j].text_alpha - 5;
        greed[i,j].text_y := greed[i,j].text_y - 0.3;
      end;
    end;


      for i := 2 to 17 do
      for j := 2 to 15 do
      begin
        // слева
        if (greed[i,j-1].full = true) and (greed[i,j-1].move = false)
        and (greed[i,j].full = true) and (greed[i,j].move = false) then
        begin
          if (greed[i,j-1].openright = true) and (greed[i,j].openleft = true) then
          begin
            if greed[i,j-1].delbyte <> greed[i,j].delbyte then greed[i,j].delbyte := greed[i,j-1].delbyte;
          end;
        end;
        // сверху
        if (greed[i-1,j].full = true) and (greed[i-1,j].move = false)
        and (greed[i,j].full = true) and (greed[i,j].move = false) then
        begin
          if (greed[i-1,j].opendown = true) and (greed[i,j].opentop = true) then
          begin
            if greed[i-1,j].delbyte <> greed[i,j].delbyte then greed[i,j].delbyte := greed[i-1,j].delbyte;
          end;
        end;
        // справа
        if (greed[i,j+1].full = true) and (greed[i,j+1].move = false)
        and (greed[i,j].full = true) and (greed[i,j].move = false) then
        begin
          if (greed[i,j+1].openleft = true) and (greed[i,j].openright = true) then
          begin
            if greed[i,j+1].delbyte <> greed[i,j].delbyte then greed[i,j].delbyte := greed[i,j+1].delbyte;
          end;
        end;
        // снизу
        if (greed[i+1,j].full = true) and (greed[i+1,j].move = false)
        and (greed[i,j].full = true) and (greed[i,j].move = false) then
        begin
          if (greed[i+1,j].opentop = true) and (greed[i,j].opendown = true) then
          begin
            if greed[i+1,j].delbyte <> greed[i,j].delbyte then greed[i,j].delbyte := greed[i+1,j].delbyte;
          end;
        end;
      end;


    // Удаление труб
    for r := 1 to 32 do
    begin
      m := 0;
      for k := 2 to 17 do
      for l := 2 to 15 do
      begin
        delgreed[k,l].byte := 1;
      end;
      for k := 2 to 17 do
      for l := 2 to 15 do
      begin
        if (greed[k,l].delbyte = r) and (greed[k,l].alpha = 255) then delgreed[k,l].byte := 55;
      end;
      for k := 2 to 17 do
      for l := 2 to 15 do
      begin
        if (delgreed[k,l].byte = 55) then m := m + 1;
      end;
      if m > 8 then
      begin
        for k := 2 to 17 do
        for l := 2 to 15 do
        begin
          if (delgreed[k,l].byte = 55) then
          begin
            greed[k,l].delete := true;
          end;
        end;
      end;
    end;

    for k := 2 to 17 do
    for l := 2 to 15 do
    begin
      if greed[k,l].delete then n := n + 1;
    end;
    if n > 8 then
    begin
      for k := 2 to 17 do
      for l := 2 to 15 do
      begin
        if greed[k,l].delete then greed[k,l].delete2 := true;
      end;
    end;
    n := 0;

    for i := 2 to 17 do
    for j := 2 to 15 do
    begin
      // слева
      if (greed[i,j-1].full = true) and (greed[i,j-1].move = false)
      and (greed[i,j].full = true) and (greed[i,j].move = false) then
      begin
        if (greed[i,j-1].openright = true) and (greed[i,j].openleft = true) then
        begin
          if greed[i,j-1].delete and (greed[i,j].delete = false) then greed[i,j].delete := true;
        end;
      end;
      // сверху
      if (greed[i-1,j].full = true) and (greed[i-1,j].move = false)
      and (greed[i,j].full = true) and (greed[i,j].move = false) then
      begin
        if (greed[i-1,j].opendown = true) and (greed[i,j].opentop = true) then
        begin
          if greed[i-1,j].delete and (greed[i,j].delete = false) then greed[i,j].delete := true;
        end;
      end;
      // справа
      if (greed[i,j+1].full = true) and (greed[i,j+1].move = false)
      and (greed[i,j].full = true) and (greed[i,j].move = false) then
      begin
        if (greed[i,j+1].openleft = true) and (greed[i,j].openright = true) then
        begin
          if greed[i,j+1].delete and (greed[i,j].delete = false) then greed[i,j].delete := true;
        end;
      end;
      // снизу
      if (greed[i+1,j].full = true) and (greed[i+1,j].move = false)
      and (greed[i,j].full = true) and (greed[i,j].move = false) then
      begin
        if (greed[i+1,j].opentop = true) and (greed[i,j].opendown = true) then
        begin
          if greed[i+1,j].delete and (greed[i,j].delete = false) then greed[i,j].delete := true;
        end;
      end;
    end;

    for k := 2 to 17 do
    for l := 2 to 15 do
    begin
      if (greed[k,l].delete2 = true) and (greed[k,l].delalpha > 0) then
      begin
        greed[k,l].delalpha := greed[k,l].delalpha - 3;
      end;
      if (greed[k,l].delalpha = 0) then
      begin
        Add_Points := Add_Points + 5;
        greed[k,l].byte := 1;
        greed[k,l].move := true;
        greed[k,l].full := false;
        greed[k,l].alpha := 0;
        greed[k,l].openleft := false;
        greed[k,l].openright := false;
        greed[k,l].opentop := false;
        greed[k,l].opendown := false;
        greed[k,l].delbyte := 0;
        greed[k,l].delete := false;
        greed[k,l].delete2 := false;
        greed[k,l].delalpha := 255;
      end;
    end;

    for i := 0 to 3 do
    for j := 0 to 3 do
    begin
      if figures[play.f, play.a].bytes[i,j] > 1 then
      begin
        greed[play.y+i, play.x+j].byte := figures[play.f, play.a].bytes[i,j];
        greed[play.y+i, play.x+j].tex := blocks2[greed[play.y+i, play.x+j].byte];
      end;
    end;

    // pause button
    if Col_PointInAABB( Mouse_X, Mouse_Y, @pause.rect ) and (pause.alpha < 254)
    then pause.alpha := pause.alpha + 3;
    if (Col_PointInAABB( Mouse_X, Mouse_Y, @pause.rect ) = false) and (pause.alpha >= 3)
    then pause.alpha := pause.alpha - 3;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @pause.Rect ) then
    begin
      if pause.play and (MuteSounds_check.check = false) then Snd_Play(button_snd, 1, volume(sounds_volume2), 50, false);
      pause.play := false
    end else
    begin
      pause.play := true;
    end;
    if Col_PointInAABB( Mouse_X, Mouse_Y, @pause.rect ) and Mouse_Click( M_LBUTTON ) then
    begin
      if MuteSounds_Check.check = false then Snd_Play(click_snd, 1, volume(sounds_volume2), 50, false);
      pause_Alpha := 0;
      pause_BlackAlpha := 0;
      pause_BlackDir := 1;
      Scene := sPause;
      Pause_Scene := sPausse;
    end;

    if Key_Press( K_ESCAPE ) then
    begin
      pause_Alpha := 0;
      pause_BlackAlpha := 0;
      pause_BlackDir := 1;
      Scene := sPause;
      Pause_Scene := sPausse;
    end;
  end;

  // Добавление очков
  if Add_Points > 0 then
  begin
    Points := Points + 1;
    Add_Points := Add_Points - 1;
  end;

  if Minus_Pipes > 0 then
  begin
    Pipes_Left := Pipes_Left - 1;
    Minus_Pipes := Minus_Pipes - 1;
  end;

  if (Pipes_Left <= 0) then
  begin
    pause_Alpha := 0;
    pause_BlackAlpha := 0;
    pause_BlackDir := 1;
    Scene := sPause;
    Pause_Scene := sLevel;
  end;

  if Key_Press( K_G ) then
  begin
    if show_greed then show_greed := false else show_greed := true;
  end;

  if Key_Press( K_SnapShot ) then
  begin
    U_SnapShot( 'data\screens', 'screen' );
  end;

  if ShowGears then Balls_Process;
  if Black_Menu.enable then
  begin
    case Black_Menu.direct of
    1: Black_Menu.alpha := Black_Menu.alpha + 3;
    2: Black_Menu.alpha := Black_Menu.alpha - 3;
    end;
    if Black_Menu.alpha = 253 then
    begin
      Menu_Scene := Next_Scene;
      Scene := Next_Scene2;
      NewGame.alpha := 0;
      Options.alpha := 0;
      About.alpha := 0;
      TopScores.alpha := 0;
      Quit.alpha := 0;
      RBack.alpha := 0;
      BigSounds.alpha := 0;
      BigOther.alpha := 0;
      Black_Menu.direct := 2;
    end;
    if Black_Menu.alpha <= 0 then Black_Menu.enable := false;
  end;
  FPS := OE_GETFPS;
  if Cursor_ColorAlpha <= 001 then Cursor_ColorCoff := 1;
  if Cursor_ColorAlpha >= 150 then Cursor_ColorCoff := 2;
  if Cursor_ColorCoff = 1 then Cursor_ColorAlpha := Cursor_ColorAlpha + 2;
  if Cursor_ColorCoff = 2 then Cursor_ColorAlpha := Cursor_ColorAlpha - 2;
  if Black_Screen and (Black_Alpha > 0) then Black_Alpha := Black_Alpha - 2;
  if Black_Alpha < 0 then Black_Screen := false;
end;

// Отрисовка
procedure Game_Paint;
var i, j: integer;
begin
  SSprite_Draw( Back, -10, -10, 815, 615, 255, 0, FX_DEFAULT );
  if ShowGears then Balls_Paint;
  SSprite_Draw( Panel, 270, 25, 500, 550, 255, 0, FX_DEFAULT );
  SSprite_Draw( labelpanels[0], 30, 20, 200, 100, 255, 0, FX_DEFAULT );
  Text_Draw( Font[1], 90, 30, PChar('Level'));
  Text_Draw( Font[0], 130 - (Text_GetWidth(Font[0], PChar(U_IntToStr(Level)), 1, 1) / 2) + 2, 70, PChar(U_IntToStr(Level)));
  SSprite_Draw( labelpanels[1], 30, 135, 200, 100, 255, 0, FX_DEFAULT );
  Text_Draw( Font[1], 83, 145, PChar('Points'));
  Text_Draw( Font[0], 130 - (Text_GetWidth(Font[0], PChar(U_IntToStr(Points)), 1, 1) / 2) + 2, 185, PChar(U_IntToStr(Points)));
  SSprite_Draw( labelpanels[2], 30, 250, 200, 100, 255, 0, FX_DEFAULT );
  Text_Draw( Font[1], 55, 260, PChar('Pipes left'));
  Text_Draw( Font[0], 130 - (Text_GetWidth(Font[0], PChar(U_IntToStr(Pipes_Left)), 1, 1) / 2) + 2, 300, PChar(U_IntToStr(Pipes_Left)));
  SSprite_Draw( labelpanels[3], 30, 372, 200, 200, 255, 0, FX_DEFAULT );
  SSprite_Draw( preview1, 70, 402, 120, 120, preview_alpha, 0, FX_DEFAULT );
  if preview_alpha2 > 0 then SSprite_Draw( preview2, 70, 402, 120, 120, preview_alpha2, 0, FX_DEFAULT );
  //if ShowFPS then Text_Draw( Font[0], 10, 10, PChar('FPS: '+U_IntToStr(FPS)));
  Text_Draw( Font[1], 97, 382, PChar('Next'));
  // pause button
  SSprite_Draw( pause.tex[0], pause.rect.X, pause.rect.Y, pause.rect.Width, pause.rect.Height, 255, 0, FX_DEFAULT );
  if pause.alpha > 0 then SSprite_Draw( pause.tex[1], pause.rect.X, pause.rect.Y, pause.rect.Width, pause.rect.Height, pause.alpha, 0, FX_DEFAULT );
  //Text_Draw( Font[0], 10, 40, PChar('Play.Stop: '+U_IntToStr(Play.stop)));
  //Text_Draw( Font[0], 10, 40, PChar('X: '+U_IntToStr(Music_Bar.move_x)));
  //Text_Draw( Font[0], 10, 40, PChar(String(Save_Nick)));
  //for i := 0 to 3 do for j := 0 to 3 do if testt[i,j].byte = 2 then SSprite_Draw(testt[i,j].tex, testt[i,j].rect.X, testt[i,j].rect.y, 30, 30, 255, 0 );
  //for i := 0 to 3 do for j := 0 to 3 do Pr2D_TRect( @testt[i,j].rect, $FFFFFF, 255, PR_DEFAULT );

  for i := 0 to 19 do for j := 1 to 16 do if (greed[i,j].byte > 1) and (greed[i,j].delete = false) then SSprite_Draw(greed[i,j].tex, greed[i,j].rect.X, greed[i,j].rect.y, 30, 30, 255, 0 );
  for i := 0 to 19 do for j := 1 to 16 do if (greed[i,j].byte > 1) and (greed[i,j].delete = true) then SSprite_Draw(greed[i,j].tex, greed[i,j].rect.X, greed[i,j].rect.y, 30, 30, greed[i,j].delalpha, 0 );
  for i := 2 to 17 do for j := 2 to 15 do if (greed[i,j].alpha > 0) and (greed[i,j].delete = false) then SSprite_Draw(greed2[i,j].tex, greed2[i,j].rect.X, greed2[i,j].rect.y, 30, 30, greed[i,j].alpha, 0 );
  for i := 2 to 17 do for j := 2 to 15 do if (greed[i,j].alpha > 0) and (greed[i,j].delete = true) then SSprite_Draw(greed2[i,j].tex, greed2[i,j].rect.X, greed2[i,j].rect.y, 30, 30, greed[i,j].delalpha, 0 );

  for i := 2 to 17 do for j := 2 to 15 do if greed[i,j].full and (greed[i,j].text_alpha > 5) then Text_Draw( Font[1], greed[i,j].rect.X, round(greed[i,j].text_y), PChar('+'+U_IntToStr(1)), 0.7, 1, round(greed[i,j].text_alpha));


  //for i := 0 to 19 do for j := 0 to 17 do if (greed[i,j].delete2) then Pr2D_TRect( @greed[i,j].rect, $FFFFFF, 255, PR_DEFAULT );


  if show_greed then for i := 2 to 17 do for j := 2 to 15 do Pr2D_TRect( @greed[i,j].rect, $FFFFFF, 10, PR_DEFAULT );
  //for i := 2 to 15 do if (greed[2,i].byte > 1) and (greed[2,i].move = false) then Pr2D_TRect( @greed[2,i].rect, $FFFFFF, 255, PR_DEFAULT );

  //for i := 0 to 3 do for j := 0 to 3 do Pr2D_TRect( @greed[play.y+i,play.x+j].rect, $FFFFFF, 255, PR_DEFAULT );

  SSprite_Draw( Cursor, Mouse_X, Mouse_Y, 50, 50, 255, 0, FX_DEFAULT );
  SSprite_Draw( Cursor_Color, Mouse_X, Mouse_Y, 50, 50, Round(Cursor_ColorAlpha), 0, FX_DEFAULT );
  if Brightness < 50 then SSprite_Draw( BrightTex[0], -10, -10, 815, 615, 150 - (Brightness * 3), 0, FX_DEFAULT );
  if Brightness > 50 then SSprite_Draw( BrightTex[1], -10, -10, 815, 615, Round((Brightness - 50) * 1.5), 0, FX_DEFAULT );
  if Black_Menu.enable then SSprite_Draw( Black_Menu.tex, -10, -10, 815, 615, Black_Menu.alpha, 0, FX_DEFAULT );
  if Black_Screen then SSprite_Draw( Black, -10, -10, 815, 615, Round(Black_Alpha), 0, FX_DEFAULT );
end;

end.

