////////////////////////////////////////////////////
// Игра "ТАНЧИКИ" версия 1.0                      //
// Создатель: Осипов Евгений Анатольевич (cyborg) //
// Город Бузулук Оренбургская область             //
// Дата начала:     22.06.2002 год.               //
// Дата окончания:  XX.08.2002 год.               //
// Home page: http://www.buzuluk.ru/~cyborg       //
////////////////////////////////////////////////////
{$H+,J+,X+}
program MyFirstGame;

uses
  SysUtils,
  SDL,
//  SDL_Image,
  SDLSprites,
  SDLUtils,
  SDL_Mixer,
  SDL_ttf,
  SDLStreams,
{  jclFileUtils,}
{  JclSysInfo,}
  Classes, IniFiles;
Var
  //editor
  strGround,strWater,strWall,strSteel,
  strClear,strSave,strLoad,strPlay : String; //Multilangual
  //options
  strParams,strNumPlayers,strNumPlayers1,strNumPlayers2,
  strDefficult,strDefficult1,strDefficult2,strDefficult3,strDefficult4,
  strMusicInGame,strMusicInGameOff,strMusicInGameOn,
  strMusicVolume,strPlayer1Name,strPlayer2Name : String;
  //Лучшие
  strHonor : String;
  strLevel : String;
Type

  PlayerRecord = record
    Name : String[20];
    Count : Cardinal;
  End;

Const
  Version_no = 'v. 1.0';
  ProgramCaption : PChar = 'Tanks '+Version_no;
  ConfigFile='Tanks.cfg';
  TICK_INTERVAL = 1000 div 30;

  // Directories
  Gfx   = 'DATA\';
  SoundDir = Gfx;
  Maps = 'MAPS\';
  MusicDir=Gfx;

  MaxMusic=2;
  MusicInGame : array[0..MaxMusic] of String =
  ('music1.xm','music2.xm','music3.xm');

  Chars = 'abcdefghijklmnopqrstuvwxyz0123456789O';

  MaxSounds=12;

  idNone=0;
  idTankPlayer=99;
  idTankComputer=100;
  idProjectile=101;
  idBoom = 102;
  idPrise = 103;
  idVzriv = 104;

  Player1StatusY=100;
  Player2StatusY=220;

Const
  GameTitle=0;
  GameGame=1;
  GameEnd=2;
  EndTitle=3;//титры
  GameMapEditor=4;
  GameTotal=5;
  GameOptions=6;//Параметры
  GameEndTotal=7;//Таблица рекордов

  Ground = 0; //Земля
  Water  = 1; //Вода
  Wall   = 2; //Стена
  Steel  = 3; //Сталь
Type
  TMap = array[0..29,0..29] of byte;
  TLevel = (easy,medium,hard,veryhard);
Var
  TwoPlayer : Boolean = False; //Два игрока иле один
  PlayMusic : Boolean = True;//Играть музыку
  Level : TLevel = Easy; //Уровень сложности
  Map,EditorMap : TMap;  //Карты
  //таблица передвижений танков
  TanksMoveMap : array[0..29,0..29] of byte;
  Bpp : Integer = 32; //разрядность цвета
  //ещё жвой или нет :-)
  PlayersLife : Integer = 7; //Общие жизни
  ComputerLife : Integer;//Жизни компьютера, кол-во доступных танчиков
  ComputerMove : Integer;//Кол-во ездющих танчиков, ели ComputerLife и ComputerMove понулям то выиграл
  Player1Active : boolean = false;
  Player2Active : boolean = false;
  Player1Points,
  Player2Points,
  ComputerPoints : Cardinal; //Очки игроков
  GameLevel : Cardinal = 0; //Текущий уровень игры

  ActiveMusic : Integer = 0;
  MusicVolume : Integer = 64;

  EndGame:Boolean=false;

    Player1Life,
    Player1Speed,
    Player1ProjectilePower : Integer;
    Player1MaxTimeVistrel,
    Player1ProjectileSpeed : Byte;
    Player1Water : boolean; //Ездит по воде

    Player2Life,
    Player2Speed,
    Player2ProjectilePower : Integer;
    Player2MaxTimeVistrel,
    Player2ProjectileSpeed : Byte;
    Player2Water : boolean; //Ездит по воде

    Player1Name : String[20] = 'Player 1';//Имена игроков
    Player2Name : String[20] = 'Player 2';
    PlayersNames : array[1..10] of PlayerRecord;//Таблца рекордов
VAR
  Screen, Background,
  Sprites32, TankSprites, RightPanel,
  RightPanelMapEditor,TempImage, Projectile,
  vzriv, VzrivTank,Status,HQExplosion,
  Text,Titles,Cursor,SmallTank :PSDL_Surface;
  TempColor : TSDL_Color;
  Font,font1,Font2,Font3,Font4 : PTTF_Font;
  TextColor : TSDL_Color;
  SrcRect, DestRect : TSDL_Rect;
  SpriteEngine: TSpriteEngine;
  QuitGame: boolean = false;
  Event: TSDL_Event;
  Next_Time: cardinal = 0;
  sounds: array[0..MaxSounds-1] of PMix_Chunk;
  Music : PMix_Music;
  StageOfGame : Integer = GameTitle;
  ActiveMenu : Integer;
  Puk : Boolean = false;
  keys : PKeyStateArr;
  //Место очередного появления копьютера, один из трёх
  NewComputerLocation : byte = 0;
  PriseTimer : Integer;//таймер появления приза
  MenuNames : array[1..6] of String;//Названия пунктов меню
  F_Stream : TFileStream;


Type

  TFileSelect = Class
    Saving : Boolean;
    SelectFileName : String;
    NumFiles : Integer; //Кол-во найденых файлов
    TopName, ActiveName : Integer; //номер верхнего имени и активного имени
    NumWrite : Integer; //Кол-во выводимых имён
    SFileNames : TStringList; //Список найденых файлов
    constructor Create(FileName : String;Save : Boolean);
    Procedure Free;
    Procedure Move;
    Procedure Draw;
  end;

  TTitles = class( TSprite )

    constructor Create(_Rect : TSDL_Rect);

    procedure Move; override;

    procedure Draw; override;
  end;


  TVzriv = class( TSprite )

    Count : Integer;

    constructor Create(_X,_Y : Integer);
    procedure Draw; override;
  end;


  TVzrivTank = class( TSprite )

    Count : Integer;

    constructor Create(_X,_Y : Integer);
    procedure Draw; override;
  end;


  THQExplosion = class( TSprite )

    Count : Integer;

    constructor Create;
    procedure Draw; override;
  end;




  TMoveTo = (mStop,mUp,mDown,MLeft,MRight);


  TProjectile = class( TSprite ) //Снаряд

    PPoints : ^Cardinal;

    MoveTo : TMoveTo;
    Power,
    Speed,Life : Integer;
    Protivnik : Integer;
    constructor Create(_X,_Y : Integer;_MoveTo : TMoveTo;_Power,_Speed : byte;_Protivnik : Integer;Var Points : Cardinal);
    procedure CollisionTest;
    procedure Draw; override;
    procedure Move; override;
  end;

  XPlayer =(plone,pltwo,plcomputer);

  TTank = class( TSprite )

    MoveTank,See : TMoveTo;

    OffsX, OffsY : Integer;
    Life : Integer;//Жизнь от 1 до 3
    Speed : Integer;//Скорость танка
    Time : Integer;
    Player : XPlayer; //Чей танк человека или компьютера
    ProjectilePower : Integer;//Мощьность пульки
    MaxTimeVistrel : Byte; //Время следующего выстрела 1сек=30
    ProjectileSpeed : Byte;//Скорость пульки
    Water : boolean; //Ездит по воде
    constructor Create(_Player : XPlayer);
    procedure Draw; override;
    Function CollisionTest : boolean;
    procedure Move; override;
  end;

  TPrise = class( TSprite )

    Prise : Integer;

    Time : Integer;

    constructor Create;

    procedure CollisionTest;

    procedure Draw; override;
  end;


  MapPointer = record

    ObjectNum : Integer; //Предмет, объект

    AnimPhaze : Integer; //Анимация

    Hook : set of (HookBomb,HookSpring);//Сюрприз :-)

    Angle : Integer; //На сколько градусов предмет повёрнут

  end;

Var
  Player1, Player2 : TTank;
  FS : TFileSelect; //Диалог выбора файла
  SelectFile : String;


Procedure WritePlayer1Status;forward;
Procedure WritePlayer2Status;forward;
Procedure WritePlayersLife;forward;
Procedure WriteComputerLives;forward;
function TimeLeft: UInt32;forward;
Procedure Options;forward;
Procedure TotalCount;forward;

constructor  TFileSelect.Create(FileName : String;Save : Boolean);
Var
  sr: TSearchRec;
//  I : Integer;
Begin
 Inherited create;
 TopName:=0; ActiveName:=0; NumWrite:=12;
 Saving:=Save;
 SelectFile:='';
 SFileNames:=TStringList.Create;
 SFileNames.BeginUpdate;
 NumFiles:=0;
 SelectFileName:='';
if FindFirst(Maps+FileName, faArchive , sr) = 0 then
    begin
      repeat
        inc(NumFiles);
        SFileNames.Add(PathExtractFileNameNoExt(sr.Name));
      until FindNext(sr) <> 0;
      FindClose(sr);
    end;
  if NumFiles<NumWrite then NumWrite:=NumFiles;
  SFileNames.Sort;
  SFileNames.EndUpdate;
if SFileNames.Count>0 then  SelectFileName:=SFileNames.Strings[ActiveName];
End;
Procedure TFileSelect.Free;
//Var I : Integer;
Begin
 SFileNames.Free;
 Inherited;
End;
Procedure TFileSelect.Move;
Var //I : Integer;
    quit : Boolean;
    Ch : Char;
Begin
  quit:=false;
  Repeat
  while SDL_PollEvent(@Event) > 0 do
  case Event.key.type_ of
    SDL_QUITEV : Begin Quit:=true;end;
    SDL_KeyDown :
     Case Event.key.keysym.sym of
      SDLK_ESCAPE : Begin Quit:=true;end;
      SDLK_Up : Begin Dec(ActiveName);if ActiveName<0 then ActiveName:=0;
                If ActiveName<TopName then Dec(TopName);
                if SFileNames.Count>0 then SelectFileName:=SFileNames.Strings[ActiveName];
                draw;End;
      SDLK_Down : Begin Inc(ActiveName);if ActiveName>NumFiles-1 then ActiveName:=NumFiles-1;
                   If ActiveName>=TopName+NumWrite then inc(TopName);
                  if SFileNames.Count>0 then SelectFileName:=SFileNames.Strings[ActiveName];
                  draw;End;
      SDLK_0..SDLK_9,
      SDLK_A..SDLK_Z : Begin
                  Case Event.key.keysym.sym of
                    SDLK_0 : Ch:='0';
                    SDLK_1 : Ch:='1';
                    SDLK_2 : Ch:='2';
                    SDLK_3 : Ch:='3';
                    SDLK_4 : Ch:='4';
                    SDLK_5 : Ch:='5';
                    SDLK_6 : Ch:='6';
                    SDLK_7 : Ch:='7';
                    SDLK_8 : Ch:='8';
                    SDLK_9 : Ch:='9';
                    SDLK_A : Ch:='A';
                    SDLK_B : Ch:='B';
                    SDLK_C : Ch:='C';
                    SDLK_D : Ch:='D';
                    SDLK_E : Ch:='E';
                    SDLK_F : Ch:='F';
                    SDLK_G : Ch:='G';
                    SDLK_H : Ch:='H';
                    SDLK_I : Ch:='I';
                    SDLK_J : Ch:='J';
                    SDLK_K : Ch:='K';
                    SDLK_L : Ch:='L';
                    SDLK_M : Ch:='M';
                    SDLK_N : Ch:='N';
                    SDLK_O : Ch:='O';
                    SDLK_P : Ch:='P';
                    SDLK_Q : Ch:='Q';
                    SDLK_R : Ch:='R';
                    SDLK_S : Ch:='S';
                    SDLK_T : Ch:='T';
                    SDLK_U : Ch:='U';
                    SDLK_V : Ch:='V';
                    SDLK_W : Ch:='W';
                    SDLK_X : Ch:='X';
                    SDLK_Y : Ch:='Y';
                    SDLK_Z : Ch:='Z';
                    else Ch:='+';
                   End;
                       if (Ch<>'+')and saving then SelectFileName:=SelectFileName+Ch;
                       Draw;
                       End;
      SDLK_BackSpace : if saving then Begin
                       Delete(SelectFileName,Length(SelectFileName),1);
                       Draw;
                       End;
      SDLK_Delete : if saving then Begin
                       SelectFileName:='';
                       Draw;
                       End;
      SDLK_RETURN : begin SelectFile:=SelectFileName; quit:=True;end;
     end;
  end;
  SDL_Flip(Screen);
  SDL_Delay(TimeLeft);
  until quit;
  SDL_UpperBlit(BackGround, nil, Screen, nil);
end;

Procedure TFileSelect.Draw;
Var I : Integer;
    TempColor,TempColorA : TSDL_Color;
    srcRect,DestRect : TSDL_Rect;
    SRF : PSDL_Surface;
Begin
  inherited;
  TempColor.r:=255;
  TempColor.g:=255;
  TempColor.b:=255;
  TempColor.unused:=0;
  TempColorA.r:=155;
  TempColorA.g:=255;
  TempColorA.b:=255;
  TempColorA.unused:=0;
  SDL_UpperBlit(BackGround, nil, Screen, nil);

  DestRect.x:=160;
  DestRect.y:=90;
  SrcRect.x:=10;
  SrcRect.y:=40;
  SrcRect.w:=280;
  SrcRect.h:=250;
  SRF:=SDL_CreateRGBSurface( SDL_HWSURFACE or SDL_SRCALPHA,300,300,Bpp, 50, 150, 50, 0 );
  SDL_FillRect(SRF, @srcRect, 10);
  SDL_SetAlpha(SRF,SDL_HWSURFACE or SDL_SRCALPHA,128);
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.x:=160;
  DestRect.y:=70;
  if Saving then
  SRF:=TTF_RenderText_Solid(Font,PChar('SAVE'),TempColorA )
  else SRF:=TTF_RenderText_Solid(Font,PChar('LOAD'),TempColorA );
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);

  SrcRect.x:=0;
  SrcRect.y:=0;
  SrcRect.w:=260;
  SrcRect.h:=20;
  DestRect.x:=180;
  DestRect.y:=100;
  SRF:=TTF_RenderText_Solid(Font,PChar(SelectFileName),TempColorA );
  SDL_UpperBlit(SRF, @SrcRect, Screen, @DestRect);

  SDL_FreeSurface(SRF);
  For I:=1 to NumWrite do
   begin
    DestRect.y:=115+(I*20);
//    if SFileNames.Count>=TopName+I then
    if TopName+I-1=ActiveName then
    SRF:=TTF_RenderText_Solid(Font,PChar(SFileNames.Strings[TopName+I-1]),TempColorA ) else
    SRF:=TTF_RenderText_Solid(Font,PChar(SFileNames.Strings[TopName+I-1]),TempColor );
    SDL_UpperBlit(SRF, @SrcRect, Screen, @DestRect);
    SDL_FreeSurface(SRF);
   end;
   SDL_Flip(Screen);
end;

//Обнулить карту передфижений
Procedure ZeroTanksMoveMap;
var X,Y : Integer;
Begin
  for y:=0 to 29 do
  for x:=0 to 29 do
  TanksMoveMap[x,y]:=0;
  TanksMoveMap[14,28]:=1;
  TanksMoveMap[15,28]:=1;
  TanksMoveMap[14,29]:=1;
  TanksMoveMap[15,29]:=1;
End;
//Нарисовать участок карты
Procedure WriteMapPoint(X,Y : Integer);
Begin
  if Odd(Y) then
  SrcRect.y:=16 else SrcRect.y:=0;
  SrcRect.x:=Map[X,Y] * 32;
  SrcRect.h:=16;
  SrcRect.w:=16;
  if Odd(X) then inc(SrcRect.x,16);
  DestRect.x := X*16;
  DestRect.y := Y*16;
  DestRect.h := 16;
  DestRect.w := 16;
  SDL_UpperBlit(Sprites32, @SrcRect, BackGround, @DestRect);
  SDL_UpperBlit(Background, @DestRect, Screen, @DestRect);
//  SDL_BlitSurface(Sprites32, @SrcRect, Screen, @DestRect);
End;
constructor TTitles.Create(_Rect : TSDL_Rect);
Begin
  AnimPhase :=0;
  X:=(420 -_Rect.w) div 2;
  Y:=480;
  w:=_Rect.w;
  h:=_Rect.h;
  Z:=10;
  isDead:=false;
  SrcRect:=_Rect;
End;
procedure TTitles.Move;
Begin
 if (Y<-h) or (srcRect.w=0) or (StageOfGame<>EndTitle)then kill else Dec(Y);
End;
procedure TTitles.Draw;
var
  DestRect : TSDL_Rect;
Begin
  DestRect.x := x;
  DestRect.y := y;
  DestRect.w := w;
  DestRect.h := h;
  SDL_UpperBlit(Titles, @SrcRect, Screen, @DestRect );
  PrevRect := DestRect;
End;

Constructor TVzriv.Create(_X,_Y : Integer);
Begin
  AnimPhase := 0;
  Count:=0;
  X:=_X;
  Y:=_Y;
  w:=32;
  h:=32;
  Z:=27;
  isDead:=false;
  SrcRect.y:=0;
  SrcRect.w:=w;
  SrcRect.h:=h;
  ID:=idVzriv;
  Mix_PlayChannel(2,sounds[6],0);
end;
Procedure TVzriv.Draw;
var
  DestRect : TSDL_Rect;
begin
  SrcRect.x :=AnimPhase * w;
  DestRect.x := x;
  DestRect.y := y;
  DestRect.h := h;
  DestRect.w := w;
  if (AnimPhase<10) and (StageOfGame=GameGame) then SDL_UpperBlit( Vzriv, @SrcRect, Screen, @DestRect ) else kill;
  PrevRect := DestRect;
  inc(AnimPhase);
end;

Constructor TVzrivTank.Create(_X,_Y : Integer);
Begin
  AnimPhase := 0;
  Count:=0;
  X:=_X-16;
  Y:=_Y-16;
  w:=64;
  h:=64;
  Z:=28;
  isDead:=false;
  SrcRect.y:=0;
  SrcRect.w:=w;
  SrcRect.h:=h;
  ID:=idVzriv;
  Mix_PlayChannel(3,sounds[4],0);
end;
Procedure TVzrivTank.Draw;
var
  DestRect : TSDL_Rect;
begin
  SrcRect.x :=AnimPhase * w;
  DestRect.x := x;
  DestRect.y := y;
  DestRect.h := h;
  DestRect.w := w;
  if (AnimPhase<15) and (StageOfGame=GameGame) then SDL_UpperBlit( VzrivTank, @SrcRect, Screen, @DestRect ) else kill;
  PrevRect := DestRect;
  inc(AnimPhase);
end;

Constructor THQExplosion.Create;
Begin
  AnimPhase := 0;
  Count:=0;
  X:=208;
  Y:=432;
  w:=64;
  h:=64;
  Z:=29;
  isDead:=false;
  SrcRect.y:=0;
  SrcRect.w:=w;
  SrcRect.h:=h;
  ID:=idVzriv;
  Mix_PlayChannel(5,sounds[10],0);
  WriteMapPoint(14,28);
  WriteMapPoint(15,28);
  WriteMapPoint(14,29);
  WriteMapPoint(15,29);
end;
Procedure THQExplosion.Draw;
var
  DestRect : TSDL_Rect;
begin
  SrcRect.x :=AnimPhase * w;
  DestRect.x := x;
  DestRect.y := y;
  DestRect.h := h;
  DestRect.w := w;
  if (AnimPhase<20) and (StageOfGame=GameGame) then SDL_UpperBlit( HQExplosion, @SrcRect, Screen, @DestRect ) else kill;
  PrevRect := DestRect;
  inc(AnimPhase);
end;


constructor TProjectile.Create(_X,_Y : Integer;_MoveTo : TMoveTo;_Power,_Speed : byte;_Protivnik : Integer;Var Points : Cardinal);
begin
  PPoints:=@Points;
  Speed:=_Speed;
  Power:=_Power;
  Protivnik:=_Protivnik;
  Life:=1;
  MoveTo:=_MoveTo;
  AnimPhase := 0;
  X:=_X;
  Y:=_Y;
  w:=4;
  h:=4;
  Z:=26;
  isDead:=false;
  SrcRect.x:=0;
  SrcRect.y:=0;
  SrcRect.w:=w;
  SrcRect.h:=h;
  ID:=idProjectile;
  Mix_PlayChannel(1,sounds[Power-1],0);
end;

//Нарисовать кусочек карты при столкновении пули
Procedure PulBoom(X,Y : Integer;Projectile : TProjectile);
Begin
  Dec(Projectile.Life);
  Map[X,Y]:=Ground;
  WriteMapPoint(X,Y);
  SDL_UpdateRect(Screen,X*16,Y*16,16,16);
End;

procedure TProjectile.CollisionTest;
var
  Rect1, Rect2 : TSDL_Rect;
  i,XX,YY: integer;
begin
  case MoveTo of
  mUp,mleft: begin
       XX:=x div 16;YY:=y div 16;
       end;
  mDown:begin
       XX:=x div 16;YY:=(y+h) div 16;
       end;
  mRight:begin
       XX:=(x+w) div 16;YY:=y div 16;
        end else
        begin
         XX:=x div 16;YY:=y div 16;
        end;
  end;

  if Protivnik=idTankPlayer then
  if (X>=220) and (X<=256) and (Y>=448) and (Y<=480) then
  begin
//    Капец игре
        Dec(Life);
        isDead:=true;
        SpriteEngine.AddSprite(THQExplosion.Create);
        EndGame := true;
  end;

  if XX<0 then XX:=0;
  if XX>29 then XX:=29;
  if YY<0 then YY:=0;
  if YY>29 then YY:=29;
    case MoveTo of
    mUp,mDown :
      begin
        case Map[XX,YY] of
          Wall : PulBoom(XX,YY,self);
          Steel : if power>1 then PulBoom(XX,YY,self) else Dec(Life);
        end; //case Map[XX,YY]
        case Map[XX+1,YY] of
          Wall : PulBoom(XX+1,YY,self);
          Steel :
              if power>1 then PulBoom(XX+1,YY,self) else Dec(Life);
        end;//case Map[XX+1,YY]
      end;
    mLeft :
      begin
        case Map[XX,YY] of
          Wall : PulBoom(XX,YY,self);
          Steel : if power>1 then PulBoom(XX,YY,self) else Dec(Life);
        end; //case Map
        case Map[XX,YY+1] of
          Wall : PulBoom(XX,YY+1,self);
          Steel : if power>1 then PulBoom(XX,YY+1,self) else Dec(Life);
        end;//case Map
      end;
    mRight :
      begin
        case Map[XX,YY] of
          Wall : PulBoom(XX,YY,self);
          Steel : if power>1 then PulBoom(XX,YY,self) else Dec(Life);
        end; //case Map
        case Map[XX,YY+1] of
          Wall : PulBoom(XX,YY+1,self);
          Steel : if power>1 then PulBoom(XX,YY+1,self) else Dec(Life);
        end;//case Map
      end;
    end;
  GetCollisionRect(@Rect1);
  if not self.isDead then for i := 0 to ParentList.Count - 1 do
    if not isDead and (ParentList[i] <> self) then
    Begin
    if TSprite(ParentList[i]).ID = Protivnik then
    begin
     ParentList[i].GetCollisionRect(@Rect2);
      if isCollideRects(@Rect1, @Rect2) then
      begin
        Dec(Life);
        isDead:=true;
        dec(TTank(ParentList[i]).Life,power);
        Inc(PPoints^,10);
        if TTank(ParentList[i]).Life<=0 then
        Case Level of
        Easy : Inc(PPoints^,80);
        Medium : Inc(PPoints^,100);
        Hard : Inc(PPoints^,120);
        VeryHard : Inc(PPoints^,150);
        end;//case
        case TTank(ParentList[i]).Player of
        plComputer : begin
                     if PPoints=@Player1Points then WritePlayer1Status else
                     if PPoints=@Player2Points then WritePlayer2Status;
                     end;
        plOne : WritePlayer1Status;
        plTwo : WritePlayer2Status;
        end;//case
      end;
    end;
    if TSprite(ParentList[i]).ID = idProjectile then
    begin
     ParentList[i].GetCollisionRect(@Rect2);
      if isCollideRects(@Rect1, @Rect2) then
      begin
        Inc(PPoints^,5);
        isDead:=true;
        Dec(Life);
        dec(TProjectile(ParentList[i]).Life,power);
        if PPoints=@Player1Points then WritePlayer1Status else
        if PPoints=@Player2Points then WritePlayer2Status;
      end;
    end;
    End;
end;

procedure TProjectile.Move;
begin
  Case MoveTo of
    mUp   : dec(Y,speed);
    mDown : inc(Y,Speed);
    mLeft : dec(X,Speed);
    mRight: inc(X,Speed);
  end;
  CollisionTest;
end;

procedure TProjectile.Draw;
var
  DestRect : TSDL_Rect;
begin

  DestRect.x := x;
  DestRect.y := y;
  DestRect.h := h;
  DestRect.w := w;
  if (Life<=0) or (x<=0) or (x>=480-w) or (y<=0) or (y>=480-h) or (StageOfGame<>GameGame) then
   Begin
    kill;
    SpriteEngine.AddSprite(Tvzriv.Create(x-14,y-14));
   End else SDL_UpperBlit( Projectile, @SrcRect, Screen, @DestRect );
  PrevRect := DestRect;
end;

constructor TPrise.Create;
begin
  ID:=idPrise;
  Prise:=Random(8);
  AnimPhase := 1;
  Time:=0;
  X:=Random(15)*32;
  Y:=Random(15)*32;
  if (X=7*32) and (Y=14*32) then Dec(Y,32);
  w:=32;
  h:=32;
  Z:=35;
  isDead:=false;
  SrcRect.x :=Prise * w;
  SrcRect.y:=32;
  SrcRect.w:=w;
  SrcRect.h:=h;
  PrevRect.x:=x;
  PrevRect.y:=y;
  PrevRect.h:=h;
  PrevRect.w:=w;
end;

procedure TPrise.CollisionTest;
var
  Rect1, Rect2 : TSDL_Rect;
  i,ii : integer;
begin
  GetCollisionRect(@Rect1);
  for i := 0 to ParentList.Count - 1 do
    if (ParentList[i] <> self) then
    Begin
    if TSprite(ParentList[i]).ID =idTankPlayer then
    begin
     ParentList[i].GetCollisionRect(@Rect2);
      if isCollideRects(@Rect1, @Rect2) then
      if not isDead then begin
        Mix_PlayChannel(5,sounds[11],0);
        Case Prise of
         0 : Begin Inc(PlayersLife); kill; WritePlayersLife;end;
         1 : Begin for ii:=0 to ParentList.Count-1 do if TSprite(ParentList[ii]).ID=idTankComputer then TTank(ParentList[ii]).Life:=0; kill end;
         2 : if not TTank(ParentList[i]).Water then begin TTank(ParentList[i]).Water:=True; Kill; end;
         3 : if TTank(ParentList[i]).MaxTimeVistrel>30 then begin dec(TTank(ParentList[i]).MaxTimeVistrel,15); kill; end;
         4 : if TTank(ParentList[i]).ProjectileSpeed<4 then begin inc(TTank(ParentList[i]).ProjectileSpeed); kill; end;
         5 : if TTank(ParentList[i]).ProjectilePower<3 then Begin inc(TTank(ParentList[i]).ProjectilePower); kill; end;
         6 : if TTank(ParentList[i]).Life<3 then Begin inc(TTank(ParentList[i]).Life);kill; end;
         7 : if (TTank(ParentList[i]).Speed<2) and (Frac(TTank(ParentList[i]).y/2)=0) and (Frac(TTank(ParentList[i]).x/2)=0) then Begin inc(TTank(ParentList[i]).Speed);kill; end;
        end;
        WritePlayer1Status;
        WritePlayer2Status;
//        dec(TTank(ParentList[i]).Life,1);
      end;
    end;
    if TSprite(ParentList[i]).ID=idTankComputer then
    begin
     ParentList[i].GetCollisionRect(@Rect2);
      if isCollideRects(@Rect1, @Rect2) then
      if not isDead then begin
        Mix_PlayChannel(5,sounds[11],0);
        Case Prise of
         0 : Begin Inc(ComputerLife); kill; WriteComputerLives; end;
         1 : Begin for ii:=0 to ParentList.Count-1 do if TSprite(ParentList[ii]).ID=idTankPlayer then TTank(ParentList[ii]).Life:=0; kill end;
         2 : if not TTank(ParentList[i]).Water then begin TTank(ParentList[i]).Water:=True; Kill; end;
         3 : if TTank(ParentList[i]).MaxTimeVistrel>30 then begin dec(TTank(ParentList[i]).MaxTimeVistrel,15); kill; end;
         4 : if TTank(ParentList[i]).ProjectileSpeed<4 then begin inc(TTank(ParentList[i]).ProjectileSpeed); kill; end;
         5 : if TTank(ParentList[i]).ProjectilePower<3 then Begin inc(TTank(ParentList[i]).ProjectilePower); kill; end;
         6 : if TTank(ParentList[i]).Life<3 then Begin inc(TTank(ParentList[i]).Life);kill; end;
         7 : if (TTank(ParentList[i]).Speed<2) and (Frac(TTank(ParentList[i]).y/2)=0) and (Frac(TTank(ParentList[i]).x/2)=0) then Begin inc(TTank(ParentList[i]).Speed);kill; end;
        end;
      end;
    end;
    End;
end;

procedure TPrise.Draw;
var
  DestRect : TSDL_Rect;
begin
  inc(Time);
  if Time>450 then AnimPhase:=0;
  if AnimPhase<>0 then inc(AnimPhase) else kill;
  if AnimPhase>60 then AnimPhase:=1;
  Case Prise of
   0 : ;
  end;
  DestRect.x := x;
  DestRect.y := y;
  DestRect.h := h;
  DestRect.w := w;
  if (AnimPhase>0)and(AnimPhase<55) then SDL_UpperBlit(Sprites32,@SrcRect,Screen,@DestRect);
  if StageOfGame<>GameGame then kill;
  if not isDead then CollisionTest;
end;
constructor TTank.Create(_Player : XPlayer);
Var XX,YY : Integer;
begin
  Player:=_Player;
  MoveTank:=mStop;
  See:=mUp;
  AnimPhase := 0;
  ID:=idTankPlayer;
  Case Player of
  plOne : begin X:=6*32-16;Y:=14*32+14; player1Active:=true;MoveTank:=mUp; dec(PlayersLife);
  ProjectilePower:=Player1ProjectilePower;
  Speed:=Player1Speed;
  ProjectileSpeed := Player1ProjectileSpeed;
  Life:=Player1Life;
  Water:=Player1Water;
  MaxTimeVistrel:=Player1MaxTimeVistrel;
  WritePlayersLife;
  end;
  plTwo : begin X:=8*32+16;Y:=14*32+14; player2Active:=true;MoveTank:=mUp; dec(PlayersLife);
  ProjectilePower:=Player2ProjectilePower;
  Speed:=Player2Speed;
  ProjectileSpeed := Player2ProjectileSpeed;
  Life:=Player2Life;
  Water:=Player2Water;
  MaxTimeVistrel:=Player2MaxTimeVistrel;
  WritePlayersLife;
  end;
  plComputer :
    Begin
      Case Level of
        Easy : Begin
                  MaxTimeVistrel:=60;
                  Speed:=1;
                  Life:=1;
               End;
        Medium : Begin
                  MaxTimeVistrel:=60;
                  Speed:=1;
                  Life:=2;
                 End;
        Hard : Begin
                  MaxTimeVistrel:=45;
                  Speed:=1;
                  Life:=2;
               End;
        VeryHard : Begin
                  MaxTimeVistrel:=45;
                  Speed:=2;
                  Life:=2;
                   End;
        end;
      ProjectilePower:=1;
      ProjectileSpeed := 3;
      Water:=False;
      MoveTank:=mDown;
      AnimPhase := 2;
      inc(ComputerMove);
      dec(ComputerLife);
      ID:=idTankComputer;
      Y:=-14;
      See:=mDown;
      case NewComputerLocation of
      0 : X:=0;
      1 : X:=7*32;
      2 : X:=14*32;
      end;//case
      inc(NewComputerLocation);
      if NewComputerLocation>2 then NewComputerLocation:=0;
      WriteComputerLives;
    end;
  end;//case
  XX:=X div 16;YY:=Y div 16;
  Map[XX,YY]:=Ground;
  Map[XX+1,YY]:=Ground;
  Map[XX,YY+1]:=Ground;
  Map[XX+1,YY+1]:=Ground;
  WriteMapPoint(XX,YY);
  WriteMapPoint(XX+1,YY);
  WriteMapPoint(XX,YY+1);
  WriteMapPoint(XX+1,YY+1);

  if ID=idTankPlayer then Mix_PlayChannel(4,sounds[7],0) else if Random(4)=0 then Mix_PlayChannel(6,sounds[5],0);
  w:=32;
  h:=32;
  Z:=25;
  isDead:=false;
  SrcRect.y:=0;
  SrcRect.w:=w;
  SrcRect.h:=h;
  Time:=MaxTimeVistrel;
  TanksMoveMap[x div 16,y div 16]:=1;
  TanksMoveMap[x div 16 +1,y div 16]:=1;
  TanksMoveMap[x div 16,y div 16 +1]:=1;
  TanksMoveMap[x div 16+1,y div 16 +1]:=1;
end;
function TTank.CollisionTest : boolean;
Var XX,YY : Integer;
begin
  result:=false;
  XX:=x div 16; YY:=y div 16;
  case see of
    mUp    :
      begin
        if (TanksMoveMap[XX,YY-1]=1)
        or (TanksMoveMap[XX+1,YY-1]=1)
        then result:=true;
      end;
    mDown  :
      begin
        if (TanksMoveMap[XX,YY+2]=1)
        or (TanksMoveMap[XX+1,YY+2]=1)
        then result:=true;
      end;
    mLeft  :
      begin
        if (TanksMoveMap[XX-1,YY]=1)
        or (TanksMoveMap[XX-1,YY+1]=1)
        then result:=true;
      end;
    mRight :
      begin
        if (TanksMoveMap[XX+2,YY]=1)
        or (TanksMoveMap[XX+2,YY+1]=1)
        then result:=true;
      end;
  End;

end;

procedure TTank.Move;
Var XX,YY,_XX,_YY : Integer;
begin
  XX:=x div 16; YY:=y div 16;
  inc(Time);
  If   StageOfGame<>GameGame then
  begin
  case player of
   plOne : begin
    Player1Life:=Player1.Life;
    Player1Speed:=Player1.Speed;
    Player1ProjectilePower:=Player1.ProjectilePower;
    Player1MaxTimeVistrel:=Player1.MaxTimeVistrel;
    Player1ProjectileSpeed:=Player1.ProjectileSpeed;
    Player1Water:=Player1.Water;
   end;
   plTwo : begin
    Player2Life:=Player2.Life;
    Player2Speed:=Player2.Speed;
    Player2ProjectilePower:=Player2.ProjectilePower;
    Player2MaxTimeVistrel:=Player2.MaxTimeVistrel;
    Player2ProjectileSpeed:=Player2.ProjectileSpeed;
    Player2Water:=Player2.Water;
   end;
  end;
  kill;
  end;
  if (Frac(X/16)=0) and (Frac(Y/16)=0)  then MoveTank:=mStop;

  Case Player of
  plOne : Begin
  if (keys[SDLK_Space]=SDL_PRESSED) then
    Begin
      case See of
      mUp    : Begin _XX:=X + w div 2 -2; _YY:=Y{-4} End;
      mDown  : Begin _XX:=X + w div 2 -2; _YY:=Y+h-4; End;
      mLeft  : Begin _XX:=X{-4}; _YY:=Y+h div 2 - 2;End;
      mRight : Begin _XX:=X+w-4; _YY:=Y+h div 2 - 2;End;
      end;
      if Time>MaxTimeVistrel then
      Begin
        SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankComputer,Player1Points));
        Time:=0;
      End;
    end;

  if MoveTank<>mStop then exit;
  if (keys[SDLK_Up]=SDL_PRESSED) then
    Begin
      AnimPhase:=0; See:=mUp;
      if (YY>0) and
      ((map[XX,YY-1]=0) or ((map[XX,YY-1]=1) and water))
       and ((map[XX+1 ,YY-1]=0) or ((map[XX+1,YY-1]=1) and water))then
       if not collisiontest then
       Begin
         TanksMoveMap[XX,YY+1]:=0;
         TanksMoveMap[XX+1,YY+1]:=0;
         TanksMoveMap[XX,YY-1]:=1;
         TanksMoveMap[XX+1,YY-1]:=1;
         MoveTank:=mUp;
       End;
    End else
  if (keys[SDLK_Down]=SDL_PRESSED) then
    Begin
      AnimPhase:=2;See:=mDown;
      if (YY<28) and
      ((map[XX,YY+2]=0) or ((map[XX,YY+2]=1) and water))
      and ((map[XX+1 ,YY+2]=0) or (map[XX+1 ,YY+2]=1) and water) then
      if not collisiontest then
      Begin
         TanksMoveMap[XX,YY]:=0;
         TanksMoveMap[XX+1,YY]:=0;
         TanksMoveMap[XX,YY+1]:=1;
         TanksMoveMap[XX+1,YY+1]:=1;
         MoveTank:=mDown;
      End;
    End else
  if (keys[SDLK_Left]=SDL_PRESSED) then
    Begin
      AnimPhase:=3; See:=mLeft;
      if (XX>0) and
      ((map[XX-1 ,YY]=0) or ((map[XX-1 ,YY]=1) and water))
      and ((map[XX-1,YY+1]=0) or ((map[XX-1,YY+1]=1) and water)) then
      if not collisiontest then
      begin
        TanksMoveMap[XX+1,YY]:=0;
        TanksMoveMap[XX+1,YY+1]:=0;
        TanksMoveMap[XX-1,YY]:=1;
        TanksMoveMap[XX-1,YY+1]:=1;
        MoveTank:=mLeft;
      end;
    End else
  if (keys[SDLK_Right]=SDL_PRESSED) then
    Begin
      AnimPhase:=1; See:=mRight;
      if (XX<28) and
      ((map[XX+2 ,YY]=0) or ((map[XX+2 ,YY]=1) and water))
      and ((map[XX+2,YY+1]=0) or ((map[XX+2,YY+1]=1) and water)) then
      if not collisiontest then
      begin
        TanksMoveMap[XX,YY]:=0;
        TanksMoveMap[XX,YY+1]:=0;
        TanksMoveMap[XX+2,YY]:=1;
        TanksMoveMap[XX+2,YY+1]:=1;
        MoveTank:=mRight;
      end;
    End;
  end;
  plTwo : Begin
  if (keys[SDLK_BACKQUOTE]=SDL_PRESSED) then
    Begin
      case See of
      mUp    : Begin _XX:=X + w div 2 -2; _YY:=Y-4 End;
      mDown  : Begin _XX:=X + w div 2 -2; _YY:=Y+h; End;
      mLeft  : Begin _XX:=X-4; _YY:=Y+h div 2 - 2;End;
      mRight : Begin _XX:=X+w; _YY:=Y+h div 2 - 2;End;
      end;
      if Time>MaxTimeVistrel then
      Begin
        SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankComputer,Player2Points));
        Time:=0;
      End;
    end;

  if MoveTank<>mStop then exit;
  if (keys[SDLK_r]=SDL_PRESSED) then
    Begin
      AnimPhase:=0; See:=mUp;
      if (YY>0) and
      ((map[XX,YY-1]=0) or ((map[XX,YY-1]=1) and water))
       and ((map[XX+1 ,YY-1]=0) or ((map[XX+1,YY-1]=1) and water))then
       if not collisiontest then
       Begin
         TanksMoveMap[XX,YY+1]:=0;
         TanksMoveMap[XX+1,YY+1]:=0;
         TanksMoveMap[XX,YY-1]:=1;
         TanksMoveMap[XX+1,YY-1]:=1;
         MoveTank:=mUp;
       End;
    End else
  if (keys[SDLK_f]=SDL_PRESSED) then
    Begin
      AnimPhase:=2;See:=mDown;
      if (YY<28) and
      ((map[XX,YY+2]=0) or ((map[XX,YY+2]=1) and water))
      and ((map[XX+1 ,YY+2]=0) or (map[XX+1 ,YY+2]=1) and water) then
      if not collisiontest then
      Begin
        TanksMoveMap[XX,YY]:=0;
        TanksMoveMap[XX+1,YY]:=0;
        TanksMoveMap[XX,YY+1]:=1;
        TanksMoveMap[XX+1,YY+1]:=1;
        MoveTank:=mDown;
      End;
    End else
  if (keys[SDLK_d]=SDL_PRESSED) then
    Begin
      AnimPhase:=3; See:=mLeft;
      if (XX>0) and
      ((map[XX-1 ,YY]=0) or ((map[XX-1 ,YY]=1) and water))
      and ((map[XX-1,YY+1]=0) or ((map[XX-1,YY+1]=1) and water)) then
      if not collisiontest then
      begin
        TanksMoveMap[XX+1,YY]:=0;
        TanksMoveMap[XX+1,YY+1]:=0;
        TanksMoveMap[XX-1,YY]:=1;
        TanksMoveMap[XX-1,YY+1]:=1;
        MoveTank:=mLeft;
      end;
    End else
  if (keys[SDLK_g]=SDL_PRESSED) then
    Begin
      AnimPhase:=1; See:=mRight;
      if (XX<28) and
      ((map[XX+2 ,YY]=0) or ((map[XX+2 ,YY]=1) and water))
      and ((map[XX+2,YY+1]=0) or ((map[XX+2,YY+1]=1) and water)) then
      if not collisiontest then
      begin
        TanksMoveMap[XX,YY]:=0;
        TanksMoveMap[XX,YY+1]:=0;
        TanksMoveMap[XX+2,YY]:=1;
        TanksMoveMap[XX+2,YY+1]:=1;
        MoveTank:=mRight;
      end;
    End;
  end;
    plComputer : Begin
    if Time>MaxTimeVistrel then
    Begin
      case See of
      mUp    : Begin _XX:=X + w div 2 -2; _YY:=Y{-4};
        if player1Active then
        begin
          if (Time>MaxTimeVistrel) and (random(10)=1) and (Player1.x>X-32) and (Player1.x<X+32) then
            begin
              SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
              Time:=0;
            end;
        end;
        if player2Active then
        begin
          if (Time>MaxTimeVistrel) and (random(10)=1) and (Player2.x>X-32) and (Player1.x<X+32) then
            begin
              SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
              Time:=0;
            end;
        end;
      End;
      mDown  : Begin _XX:=X + w div 2 -2; _YY:=Y+h-4;
        if player1Active then
        begin
          if (Time>MaxTimeVistrel) and (random(10)=1) and (Player1.x>X-32) and (Player1.x<X+32) then
            begin
              SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
              Time:=0;
            end;
        end;
        if player2Active then
        begin
          if (Time>MaxTimeVistrel) and (random(10)=1) and (Player2.x>X-32) and (Player1.x<X+32) then
            begin
              SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
              Time:=0;
            end;
        end;
      End;
      mLeft  : Begin _XX:=X{-4}; _YY:=Y+h div 2 - 2;
        if player1Active then
        begin
          if (Time>MaxTimeVistrel) and (random(10)=1) and (((Player1.y>Y-32) and (Player1.y<Y+32))or(Y>=432)) then
            begin
              SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
              Time:=0;
            end;
        end;
        if player2Active then
        begin
          if (Time>MaxTimeVistrel) and (random(10)=1) and (((Player2.y>Y-32) and (Player2.y<Y+32))or(Y>=432)) then
            begin
              SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
              Time:=0;
            end;
        end;
      End;
      mRight : Begin _XX:=X+w-4; _YY:=Y+h div 2 - 2;
        if player1Active then
        begin
          if (Time>MaxTimeVistrel) and (random(10)=1) and (((Player1.y>Y-32) and (Player1.y<Y+32))or(Y>=432)) then
            begin
              SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
              Time:=0;
            end;
        end;
        if player2Active then
        begin
          if (Time>MaxTimeVistrel) and (random(10)=1) and (((Player2.y>Y-32) and (Player2.y<Y+32))or(Y>=432)) then
            begin
              SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
              Time:=0;
            end;
        end;
      End;
      end;
      if (Time>MaxTimeVistrel) and (random(80)=30) then
      Begin
        SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
        Time:=0;
      End;
    end;
  if MoveTank<>mStop then exit;
  MoveTank:=See;
  case Random(300) of
   1..4   : MoveTank:=mUp;
   28..38 : MoveTank:=mDown;
   50..56 : MoveTank:=mLeft;
   40..46 : MoveTank:=mRight;
  end;
  case MoveTank of
   mUp    : Begin _XX:=X + w div 2 -2; _YY:=Y{-4};end;
   mDown  : Begin _XX:=X + w div 2 -2; _YY:=Y+h-4;end;
   mLeft  : Begin _XX:=X{-4}; _YY:=Y+h div 2 - 2;end;
   mRight : Begin _XX:=X+w-4; _YY:=Y+h div 2 - 2;end;
  end;
  Case MoveTank of
  mUp:
    Begin
      AnimPhase:=0; See:=mUp;
      if (YY>0) and
      ((map[XX,YY-1]=0) or ((map[XX,YY-1]=1) and water))
       and ((map[XX+1 ,YY-1]=0) or ((map[XX+1,YY-1]=1) and water))then
       begin
       if not collisiontest then
       Begin
         TanksMoveMap[XX,YY+1]:=0;
         TanksMoveMap[XX+1,YY+1]:=0;
         TanksMoveMap[XX,YY]:=1;
         TanksMoveMap[XX+1,YY]:=1;
         TanksMoveMap[XX,YY-1]:=1;
         TanksMoveMap[XX+1,YY-1]:=1;
       End else MoveTank:=mStop;
      End else begin MoveTank:=mStop;
      if (Time>MaxTimeVistrel) then
      Begin
        SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
        Time:=0;
      End; end;
    end;
  mDown :
    Begin
      AnimPhase:=2;See:=mDown;
      if (YY<28) and
      ((map[XX,YY+2]=0) or ((map[XX,YY+2]=1) and water))
      and ((map[XX+1 ,YY+2]=0) or (map[XX+1 ,YY+2]=1) and water) then
      begin
      if not collisiontest then
      Begin
         TanksMoveMap[XX,YY]:=0;
         TanksMoveMap[XX+1,YY]:=0;
         TanksMoveMap[XX,YY+1]:=1;
         TanksMoveMap[XX+1,YY+1]:=1;
         TanksMoveMap[XX,YY+2]:=1;
         TanksMoveMap[XX+1,YY+2]:=1;
       End else MoveTank:=mStop;
      End else begin MoveTank:=mStop;
      if (Time>MaxTimeVistrel) then
      Begin
        SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
        Time:=0;
      End; end;
    end;
  mLeft :
    Begin
      AnimPhase:=3; See:=mLeft;
      if (XX>0) and
      ((map[XX-1 ,YY]=0) or ((map[XX-1 ,YY]=1) and water))
      and ((map[XX-1,YY+1]=0) or ((map[XX-1,YY+1]=1) and water)) then
      begin
      if not collisiontest then
      begin
         TanksMoveMap[XX+1,YY]:=0;
         TanksMoveMap[XX+1,YY+1]:=0;
         TanksMoveMap[XX,YY]:=1;
         TanksMoveMap[XX,YY+1]:=1;
         TanksMoveMap[XX-1,YY]:=1;
         TanksMoveMap[XX-1,YY+1]:=1;
       End else MoveTank:=mStop;
      End else begin MoveTank:=mStop;
      if (Time>MaxTimeVistrel) then
      Begin
        SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
        Time:=0;
      End; end;
    end;
  mRight :
    Begin
      AnimPhase:=1; See:=mRight;
      if (XX<28) and
      ((map[XX+2 ,YY]=0) or ((map[XX+2 ,YY]=1) and water))
      and ((map[XX+2,YY+1]=0) or ((map[XX+2,YY+1]=1) and water)) then
      begin
      if not collisiontest then
      begin
         TanksMoveMap[XX,YY]:=0;
         TanksMoveMap[XX,YY+1]:=0;
         TanksMoveMap[XX+1,YY]:=1;
         TanksMoveMap[XX+1,YY+1]:=1;
         TanksMoveMap[XX+2,YY]:=1;
         TanksMoveMap[XX+2,YY+1]:=1;
       End else MoveTank:=mStop;
      End else begin MoveTank:=mStop;
      if (Time>MaxTimeVistrel) then
      Begin
        SpriteEngine.AddSprite(TProjectile.Create(_XX,_YY, See,ProjectilePower,ProjectileSpeed,idTankPlayer,ComputerPoints));
        Time:=0;
      End; end;
    end;
  end;
  end;
  End;
end;

procedure TTank.Draw;
var
  DestRect : TSDL_Rect;
  XX,YY : Integer;
begin
  Case MoveTank of
    mUp :   dec(Y,Speed);
    mDown : inc(Y,Speed);
    mLeft : Dec(X,Speed);
    mRight : inc(X,Speed);
  end;
  Case Player of
  plOne : SrcRect.y:=32;
  plTwo : SrcRect.y:=64;
  plComputer : SrcRect.y:=0;
  end;
  SrcRect.x := AnimPhase * w;
  DestRect.x := x;
  DestRect.y := y;
  DestRect.h:=30;
  DestRect.w:=30;
  XX:=x div 16; YY:=y div 16;
  if life>0 then SDL_UpperBlit( TankSprites, @SrcRect, Screen, @DestRect )
  else begin
         TanksMoveMap[XX,YY]:=0;
         TanksMoveMap[XX,YY+1]:=0;
         TanksMoveMap[XX+1,YY]:=0;
         TanksMoveMap[XX+1,YY+1]:=0;
  Case MoveTank of
    mUp :   if (YY)>0 then Begin
             TanksMoveMap[XX,YY-1]:=0;
             TanksMoveMap[XX+1,YY-1]:=0;
            end;
    mDown : if (YY)<28 then Begin
             TanksMoveMap[XX,YY+2]:=0;
             TanksMoveMap[XX+1,YY+2]:=0;
            end;
    mLeft : if (XX)>0 then Begin
             TanksMoveMap[XX-1,YY]:=0;
             TanksMoveMap[XX-1,YY+1]:=0;
            end;
    mRight :if (x div 16)<28 then Begin
             TanksMoveMap[XX+2,YY]:=0;
             TanksMoveMap[XX+2,YY+1]:=0;
            end;
  end;
         case player of
         plOne : begin player1Active:=false;
    Player1Life:=1;
    Player1Speed:=1;
    Player1ProjectilePower:=1;
    Player1MaxTimeVistrel:=60;
    Player1ProjectileSpeed:=3;
    Player1Water:=False;
    WritePlayer1Status;
         end;
         plTwo : begin player2Active:=false;
    Player2Life:=1;
    Player2Speed:=1;
    Player2ProjectilePower:=1;
    Player2MaxTimeVistrel:=60;
    Player2ProjectileSpeed:=3;
    Player2Water:=False;
    WritePlayer2Status;
         end;
         plComputer : dec(ComputerMove);
         end;
         if ID=idTankPlayer then Mix_PlayChannel(5,sounds[Random(2)+8],0);
         SpriteEngine.AddSprite(TvzrivTank.Create(x,y));
         kill;
       end;

  PrevRect := DestRect;
end;

function TimeLeft: UInt32;
var
  now: UInt32;
begin
  now := SDL_GetTicks;
  if next_time <= now then
  begin
    next_time := now + TICK_INTERVAL;
    result := 0;
    exit;
  end;
  result := next_time - now;
end;

procedure Pause;
var
  Quit : boolean;
  Event : TSDL_Event;
begin
  Mix_VolumeMusic(32);
  Quit := false;
  repeat
    while SDL_PollEvent( @Event ) > 0 do
      if Event.key.type_ = SDL_KeyDown then
        if Event.key.keysym.sym = SDLK_PAUSE then
          Quit := true;
        SDL_UpdateRect(Screen,0,0,0,0);
        SDL_Delay(333);
  until Quit = true;
  Mix_VolumeMusic(MusicVolume);
end;

Procedure Music_Hook;
Begin
 if StageOfGame=GameGame then
 if PlayMusic then
 begin
 Mix_ResumeMusic;
 Mix_FreeMusic(Music);
 if ActiveMusic>MaxMusic then ActiveMusic:=0;
 music:=Mix_LoadMUS(PChar(MusicDir+MusicInGame[ActiveMusic]));
 inc(ActiveMusic);
 end;
 Mix_PlayMusic(music, 0);
// Mix_VolumeMusic(MusicVolume);
End;

Procedure WritePlayersLife;
Var SrcRect,DestRect : TSDL_Rect;
    I : Integer;
Begin
     DestRect.w:=140;DestRect.h:=24;
     DestRect.x:=500;DestRect.y:=40;
     SDL_UpperBlit(BackGround,@DestRect,Screen,@DestRect);

     SrcRect.x:=0;SrcRect.y:=32;SrcRect.w:=24;SrcRect.h:=24;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     DestRect.x:=500;DestRect.y:=40;
   if (PlayersLife>0) and (PlayersLife<=5)then
     begin
       for I:=1 to PlayersLife do
       Begin
         SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
         inc(DestRect.x,24);
       end;
     end else if PlayersLife>5 then
     begin
       for I:=1 to 5 do
       Begin
         SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
         inc(DestRect.x,24);
       end;
     SrcRect.x:=24;SrcRect.y:=32;SrcRect.w:=14;SrcRect.h:=14;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     DestRect.x:=DestRect.x+3;DestRect.y:=DestRect.y+5;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     end;
End;
Procedure WritePlayer1Status;//Выводим в правой части все данные
Var SrcRect,DestRect : TSDL_Rect;
    TempColorA : TSDL_Color;
    SRF : PSDL_Surface;
Begin
  if Player1Active then
   begin
     DestRect.x:=500;DestRect.y:=Player1StatusY;
     DestRect.w:=140;DestRect.h:=64;
     SDL_UpperBlit(BackGround,@DestRect,Screen,@DestRect);
     Case Player1.Life of
      1 : SrcRect.x:=0;
      2 : SrcRect.x:=32;
      3 : SrcRect.x:=64;
      else SrcRect.x:=0;
     end;
     SrcRect.y:=0;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=500;DestRect.y:=Player1StatusY;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     Case Player1.MaxTimeVistrel of
      60 : SrcRect.x:=40;
      45 : SrcRect.x:=51;
      30 : SrcRect.x:=62;
      else SrcRect.x:=40;
     end;
     SrcRect.y:=32;SrcRect.w:=11;SrcRect.h:=19;
     DestRect.x:=532;DestRect.y:=Player1StatusY+7;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     Case Player1.ProjectilePower of
      1 : SrcRect.x:=96;
      2 : SrcRect.x:=128;
      3 : SrcRect.x:=160;
      else SrcRect.x:=96;
     end;
     SrcRect.y:=0;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=544;DestRect.y:=Player1StatusY;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     if Player1.ProjectileSpeed=4 then
     Begin
     SrcRect.x:=96;SrcRect.y:=32;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=576;DestRect.y:=Player1StatusY;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     End;

     SrcRect.x:=128;SrcRect.y:=32;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=608;DestRect.y:=Player1StatusY;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     if Player1.Water then
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);

     SrcRect.x:=160;SrcRect.y:=32;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=608;DestRect.y:=Player1StatusY+32;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     if Player1.Speed=2 then SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);

  TempColorA.r:=255;
  TempColorA.g:=155;
  TempColorA.b:=255;
  TempColorA.unused:=0;
  DestRect.x:=500;
  DestRect.y:=Player1StatusY+45;
  SRF:=TTF_RenderText_Solid(Font,PChar(IntToStr(Player1Points)),TempColorA );
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);

   end;
End;
Procedure WritePlayer2Status;//Выводим в правой части все данные
Var SrcRect,DestRect : TSDL_Rect;
    TempColorA : TSDL_Color;
    SRF : PSDL_Surface;
Begin
  if Player2Active then
   begin
     DestRect.x:=500;DestRect.y:=Player2StatusY;
     DestRect.w:=140;DestRect.h:=64;
     SDL_UpperBlit(BackGround,@DestRect,Screen,@DestRect);
     Case Player2.Life of
      1 : SrcRect.x:=0;
      2 : SrcRect.x:=32;
      3 : SrcRect.x:=64;
      else SrcRect.x:=0;
     end;
     SrcRect.y:=0;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=500;DestRect.y:=Player2StatusY;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     Case Player2.MaxTimeVistrel of
      60 : SrcRect.x:=40;
      45 : SrcRect.x:=51;
      30 : SrcRect.x:=62;
      else SrcRect.x:=40;
     end;
     SrcRect.y:=32;SrcRect.w:=11;SrcRect.h:=19;
     DestRect.x:=532;DestRect.y:=Player2StatusY+7;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     Case Player2.ProjectilePower of
      1 : SrcRect.x:=96;
      2 : SrcRect.x:=128;
      3 : SrcRect.x:=160;
      else SrcRect.x:=96;
     end;
     SrcRect.y:=0;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=544;DestRect.y:=Player2StatusY;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     if Player2.ProjectileSpeed=4 then
     Begin
     SrcRect.x:=96;SrcRect.y:=32;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=576;DestRect.y:=Player2StatusY;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);
     End;

     SrcRect.x:=128;SrcRect.y:=32;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=608;DestRect.y:=Player2StatusY;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     if Player2.Water then
     SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);

     SrcRect.x:=160;SrcRect.y:=32;SrcRect.w:=32;SrcRect.h:=32;
     DestRect.x:=608;DestRect.y:=Player2StatusY+32;
     DestRect.w:=SrcRect.w;DestRect.h:=SrcRect.h;
     if Player2.Speed=2 then SDL_UpperBlit(Status,@SrcRect,Screen,@DestRect);

  TempColorA.r:=255;
  TempColorA.g:=155;
  TempColorA.b:=255;
  TempColorA.unused:=0;
  DestRect.x:=500;
  DestRect.y:=Player2StatusY+45;
  SRF:=TTF_RenderText_Solid(Font,PChar(IntToStr(Player2Points)),TempColorA );
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);
   end;
End;
Procedure WriteComputerLives;
Var X,Y,I : Integer;
    DestRect : TSDL_Rect;
Begin
  DestRect.x:=510;DestRect.y:=350;
  DestRect.w:=150;DestRect.h:=130;
  SDL_UpperBlit(Background,@DestRect,Screen,@DestRect);
  DestRect.w:=16;DestRect.h:=16;
  For I:=0 to ComputerLife-1 do
  begin
   Y:=350+((I div 5)*20);
   X:=510+(I-((I div 5)*5))*20;
  DestRect.x:=X;DestRect.y:=Y;
  SDL_UpperBlit(SmallTank,nil,Screen,@DestRect);
  end;
End;

Procedure Game;
Var
  Quit: boolean;
//  I : Integer;
  EndGameCount : Integer; //Счётчик окончания игры, как дойдёт до 60 выход(что бы сразу игра не вылетала)
  NextGame:Boolean;
  Procedure WriteMap;
    Var X,Y : Integer;
  Begin
    SrcRect.x:=0;
    SrcRect.y:=0;
    SrcRect.w := 32;
    SrcRect.h := 32;
    DestRect.w := 32;
    DestRect.h := 32;
    For Y:=0 to 14 do
      for X:=0 to 14 do
        Begin
          DestRect.x := X*32;
          DestRect.y := Y*32;
          SDL_UpperBlit(Sprites32, @SrcRect, BackGround, @DestRect);
        End;
    SrcRect.w := 16;
    SrcRect.h := 16;
    DestRect.w := 16;
    DestRect.h := 16;
    For Y:=0 to 29 do
      for X:=0 to 29 do
        Begin
         WriteMapPoint(X,Y);
        End;
  Map[14,28]:=Ground;
  Map[15,28]:=Ground;
  Map[14,29]:=Ground;
  Map[15,29]:=Ground;
  End;

  Procedure WriteBackground;
   Var
    SrcRect, DestRect : TSDL_Rect;
    SRF : PSDL_Surface;
    S : String;
    TempColorA : TSDL_Color;

  Begin
  WriteMap;
    DestRect.x := 480;
    DestRect.y := 0;
    DestRect.w := 160;
    DestRect.h := 480;
    SDL_UpperBlit(RightPanel, nil, BackGround, @DestRect);

  TempColorA.r:=200;TempColorA.g:=200;TempColorA.b:=255;TempColorA.unused:=0;
  DestRect.x:=490;
  DestRect.y:=75;
  S:=Player1Name;
  SRF:=TTF_RenderText_Solid(Font3,PChar(S),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.y:=193;
  S:=Player2Name;
  SRF:=TTF_RenderText_Solid(Font3,PChar(S),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  SrcRect.x:=5*32;
  SrcRect.y:=0;
  SrcRect.h:=32;
  SrcRect.w:=32;
  DestRect.x := 7*32;
  DestRect.y := 14*32;
  DestRect.h := 32;
  DestRect.w := 32;
  SDL_UpperBlit(Sprites32, @SrcRect, BackGround, @DestRect);
    SpriteEngine.BackgroundSurface := Background;
    SDL_UpperBlit(Background, nil, Screen, nil);
    SDL_UpdateRect(Screen,0,0,0,0);
  End;
  Procedure CreateComputerTank;
  Var ZZZ : Boolean;
  Begin
    ZZZ:=False;
    case NewComputerLocation of
      0 : if (TanksMoveMap[0,0]=0) and (TanksMoveMap[1,0]=0) and (TanksMoveMap[0,1]=0) and (TanksMoveMap[1,1]=0) then ZZZ:=True else inc(NewComputerLocation);
      1 : if (TanksMoveMap[14,0]=0) and (TanksMoveMap[15,0]=0) and (TanksMoveMap[14,1]=0) and (TanksMoveMap[15,1]=0) then ZZZ:=True else inc(NewComputerLocation);
      2 : if (TanksMoveMap[28,0]=0) and (TanksMoveMap[29,0]=0) and (TanksMoveMap[28,1]=0) and (TanksMoveMap[29,1]=0) then ZZZ:=True else inc(NewComputerLocation);
    end;//case
  if NewComputerLocation>2 then NewComputerLocation:=0;
  if (ComputerLife>0) and (ComputerMove<5) and ZZZ then SpriteEngine.AddSprite(TTank.Create(plComputer))
  else if (ComputerLife<=0) and (ComputerMove<=0) then
  begin
   inc(EndGameCount);
//   Quit:=true;
   NextGame:=True;
   //Стадия успешно завершена, переходим на следующий уровень, показать заставку подсчёта очков
  end;
  End;
Var Count1,Count2 : Integer;
Begin
  PriseTimer:=0;
  Count1:=30;
  Count2:=30;
  NextGame:=False;
  if TwoPlayer then inc(PlayersLife,2) else inc(PlayersLife,1);
  ComputerLife:=20;
  ComputerMove:=0;
  EndGameCount:=0;
  EndGame:=False;
//  Map:=EditorMap;
  ZeroTanksMoveMap;//обнуляем карту передвижений
  Quit:=False;
 if ActiveMusic>MaxMusic then ActiveMusic:=0;

  WriteBackground;
if Playmusic then
  Begin
  music:=Mix_LoadMUS(PChar(MUSICDir+MusicInGame[ActiveMusic]));
  Mix_PlayMusic(music, 0);
  Mix_VolumeMusic(MusicVolume);
  inc(ActiveMusic);
  End;
  player1Active:=false;
  player2Active:=false;

  SDL_Delay(1000);

  Repeat
  if not player1Active then if PlayersLife>0 then
    Begin Inc(Count1); if (Count1>30) and((TanksMoveMap[11,28]=0) and (TanksMoveMap[12,28]=0) and (TanksMoveMap[11,29]=0) and (TanksMoveMap[12,29]=0))
     then begin Player1:=TTank.Create(plOne); SpriteEngine.AddSprite(Player1);WritePlayer1Status;Count1:=0;end;end;
  if twoplayer and (not player2Active) then if PlayersLife>0 then
   Begin Inc(Count2); if (Count2>30) and ((TanksMoveMap[17,28]=0) and (TanksMoveMap[18,28]=0) and (TanksMoveMap[17,29]=0) and (TanksMoveMap[18,29]=0))
    then begin Player2:=TTank.Create(plTwo); SpriteEngine.AddSprite(Player2);WritePlayer2Status;Count2:=0;end;end;
  while SDL_PollEvent(@Event) > 0 do
  case Event.key.type_ of
    SDL_QUITEV : Begin Quit:=true; StageOfGame:=GameTitle; end;
    SDL_KeyDown :
     Case Event.key.keysym.sym of
      SDLK_ESCAPE : Begin Quit:=true; StageOfGame:=GameTitle; end;
      SDLK_BACKSPACE : SDL_UpdateRect(Screen,0,0,0,0);
      SDLK_PAUSE : Pause;
      SDLK_UP,SDLK_DOWN,SDLK_LEFT,SDLK_RIGHT : if Player1Active then Mix_PlayChannel(0,sounds[3],0);
      SDLK_R,SDLK_F,SDLK_D,SDLK_G : if Player2Active then Mix_PlayChannel(0,sounds[3],0);
     end;
  end;//case
  inc(PriseTimer);
  if PriseTimer>900 then begin  SpriteEngine.AddSprite(TPrise.Create); PriseTimer:=0;end;
//  WritePlayer1Status;
//  WritePlayer2Status;
//  WritePlayersLife;
  SpriteEngine.Move;
  SpriteEngine.Draw;
//  SDL_UpdateRect(Screen,0,0,0,0);
  SDL_Flip(Screen);
//  SDL_UpdateRects(Screen, SpriteEngine.NumberOfRects, @SpriteEngine.UpdateRects[0]);
  if ((not player1Active) and (not player2Active) and (PlayersLife<=0)) or endgame then inc(EndGameCount);
  if EndGameCount> 60 then begin Quit:=true; StageOfGame:=GameEndTotal;end;
  SDL_Delay(TimeLeft);
  CreateComputerTank;
  Until Quit;
  SpriteEngine.Move;
  SpriteEngine.Draw;

if Playmusic then
  Begin
     Mix_FadeOutMusic(1000);
     SDL_Delay(1000);
     Mix_FreeMusic(Music);
  End;
  if nextgame then begin StageOfGame:=GameTotal;end;
  if StageOfGame=GameEndTotal then
    begin
      TotalCount;
//      StageOfGame:=EndTitle;
    end;
End; //Procedure Game;

Procedure MapEditor;
  Procedure ZeroMap;
    Var X,Y : Integer;
  Begin
    For Y:=0 to 29 do
      for X:=0 to 29 do
        Begin
         Map[X,Y]:=Ground;
         WriteMapPoint(X,Y);
        End;
  End;
  Procedure WriteMap;
    Var X,Y : Integer;
  Begin
    SrcRect.x:=0;
    SrcRect.y:=0;
    SrcRect.w := 32;
    SrcRect.h := 32;
    DestRect.w := 32;
    DestRect.h := 32;
    For Y:=0 to 14 do
      for X:=0 to 14 do
        Begin
          DestRect.x := X*32;
          DestRect.y := Y*32;
          SDL_UpperBlit(Sprites32, @SrcRect, BackGround, @DestRect);
        End;
    SrcRect.w := 16;
    SrcRect.h := 16;
    DestRect.w := 16;
    DestRect.h := 16;
    For Y:=0 to 29 do
      for X:=0 to 29 do
        Begin
         WriteMapPoint(X,Y);
        End;
  Map[14,28]:=Ground;
  Map[15,28]:=Ground;
  Map[14,29]:=Ground;
  Map[15,29]:=Ground;
  End;

  Procedure WriteBackground;
   Var
    SrcRect, DestRect : TSDL_Rect;
    TempColorA : TSDL_Color;
    SRF : PSDL_Surface;
  Begin
  WriteMap;
    DestRect.x := 480;
    DestRect.y := 0;
    DestRect.w := 160;
    DestRect.h := 480;
    SDL_UpperBlit(RightPanelMapEditor, nil, BackGround, @DestRect);

  SrcRect.x:=5*32;
  SrcRect.y:=0;
  SrcRect.h:=32;
  SrcRect.w:=32;
  DestRect.x := 7*32;
  DestRect.y := 14*32;
  DestRect.h := 32;
  DestRect.w := 32;

  SDL_UpperBlit(Sprites32, @SrcRect, BackGround, @DestRect);

//  SDL_UpperBlit(Sprites32, @SrcRect, BackGround, @DestRect);

  TempColorA.r:=255;TempColorA.g:=255;TempColorA.b:=255;TempColorA.unused:=0;
  SRF:=TTF_RenderText_Solid(Font,PChar(Version_no),TempColorA );
  DestRect.x:=560-(SRF.w div 2);
  DestRect.y:=45;
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  TempColorA.r:=255;TempColorA.g:=255;TempColorA.b:=0;TempColorA.unused:=0;
  DestRect.x:=485;
  DestRect.y:=85;
  SRF:=TTF_RenderText_Solid(Font4,PChar(strGround),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);
  DestRect.y:=128;
  SRF:=TTF_RenderText_Solid(Font4,PChar(strWater),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);
  DestRect.y:=173;
  SRF:=TTF_RenderText_Solid(Font4,PChar(strWall),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);
  DestRect.y:=218;
  SRF:=TTF_RenderText_Solid(Font4,PChar(strSteel),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.x:=485;
  TempColorA.r:=255;TempColorA.g:=255;TempColorA.b:=255;TempColorA.unused:=0;
  SRF:=TTF_RenderText_Solid(Font4,PChar(strClear),TempColorA );
  DestRect.y:=283;
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  SRF:=TTF_RenderText_Solid(Font4,PChar(strSave),TempColorA );
  DestRect.y:=342;
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  SRF:=TTF_RenderText_Solid(Font4,PChar(strLoad),TempColorA );
  DestRect.y:=398;
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  SRF:=TTF_RenderText_Solid(Font4,PChar(strPlay),TempColorA );
  DestRect.y:=455;
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

    SDL_UpperBlit(Background, nil, Screen, nil);
    SDL_UpdateRect(Screen,0,0,0,0);
  End;
Var
  Quit: boolean;
//  I : Integer;
  X,Y,XX,YY,What : Integer;//координаты курсора
  Mousedown : Boolean;
  _DestRect : TSDL_Rect;
//  FileList: TStrings;
  Fl : File;
Begin
  Map:=EditorMap;
  X:=0;Y:=0;XX:=0;YY:=0;What:=Ground;
  Mousedown := False;
  ZeroTanksMoveMap;//обнуляем карту передвижений
  Quit:=False;

  WriteBackground;

  music:=Mix_LoadMUS(PChar(MusicDir+'MapEditor.xm'));
  Mix_PlayMusic(music, 0);
  Mix_VolumeMusic(MusicVolume);

  Repeat
  while SDL_PollEvent(@Event) > 0 do
  case Event.key.type_ of
    SDL_QUITEV : Begin Quit:=true; StageOfGame:=GameTitle; end;
    SDL_KeyDown :
     Case Event.key.keysym.sym of
      SDLK_ESCAPE : Begin Quit:=true; StageOfGame:=GameTitle; end;
      SDLK_1 : What:=Ground;
      SDLK_2 : What:=Water;
      SDLK_3 : What:=Wall;
      SDLK_4 : What:=Steel;
      SDLK_F1 : begin ZeroMap; WriteBackground;end;
      SDLK_F2 : begin
                  //Сохранить
                  FS:=TFileSelect.Create('*.tm1',True);
                  try
                    fs.draw;
                    fs.Move;
                  Finally
                    fs.free;
                  end;
                  if SelectFile<>'' then
                  begin
                    AssignFile(Fl,Maps+SelectFile+'.tm1');
                    Rewrite(fl,1);
                    BlockWrite(Fl,Map,SizeOf(Map));
                    Close(fl);
                  end;
                end;
      SDLK_F3 : begin
                  //Загрузить
                  FS:=TFileSelect.Create('*.tm1',false);
                  try
                    fs.draw;
                    fs.Move;
                  finally
                    fs.free;
                  end;
                  if SelectFile<>'' then
                  begin
                    AssignFile(Fl,Maps+SelectFile+'.tm1');
                    Reset(fl,1);
                    BlockRead(Fl,Map,SizeOf(Map));
                    Close(fl);
                    WriteBackground;
                  end;
                end;
      SDLK_F4 : begin GameLevel:=0; StageOfGame:=GameGame; quit:=true;end;
     end;
     SDL_MOUSEBUTTONDOWN:
       begin
         Mousedown := true;
       end;
       SDL_MOUSEBUTTONUP:
       begin
         Mousedown := False;
       end;
  end;//case
    if MouseDown then
    begin
      if (x>=0)and(x<480)and (y>=0)and(y<480) then
      begin
        XX:=X div 16;YY:=Y div 16;
        if (not(((XX=14) and (YY=28)) or ((XX=15) and (YY=28)) or ((XX=14) and (YY=29)) or ((XX=15) and (YY=29))))
        and (not(((XX=0) and (YY=0)) or ((XX=1) and (YY=0)) or ((XX=0) and (YY=1)) or ((XX=1) and (YY=1))))
        and (not(((XX=14) and (YY=0)) or ((XX=15) and (YY=0)) or ((XX=14) and (YY=1)) or ((XX=15) and (YY=1))))
        and (not(((XX=28) and (YY=0)) or ((XX=29) and (YY=0)) or ((XX=28) and (YY=1)) or ((XX=29) and (YY=1))))
        and (not(((XX=11) and (YY=28)) or ((XX=12) and (YY=28)) or ((XX=11) and (YY=29)) or ((XX=12) and (YY=29))))
        and (not(((XX=17) and (YY=28)) or ((XX=18) and (YY=28)) or ((XX=17) and (YY=29)) or ((XX=18) and (YY=29))))
        and (map[XX,YY]<>What) then
        begin
        Map[XX,YY]:=What;
        WriteMapPoint(XX,YY);
//        SDL_UpperBlit(Background, @DestRect, Screen, @DestRect);
        SDL_UpdateRect(Screen, XX * 16, YY * 16, 16, 16);
        end;
      end;
//      Mousedown := False;
    end;

  SDL_UpperBlit(Background, @_DestRect, Screen, @_DestRect);
  SDL_GetMouseState(x, y);
  _DestRect.x := X-8;
  _DestRect.y := Y-8;
  _DestRect.h := 16;
  _DestRect.w := 16;
  SDL_UpperBlit(Cursor, nil, Screen, @_DestRect);
  SDL_Flip(Screen);
//  SDL_UpdateRects(Screen, SpriteEngine.NumberOfRects, @SpriteEngine.UpdateRects[0]);
  SDL_Delay(TimeLeft);
  Until Quit;
//  EditorMap:=Map;
//  SpriteEngine.Move;
//  SpriteEngine.Draw;
  Mix_ResumeMusic;
  Mix_FreeMusic(Music);

End; //Procedure MapEditor;


Procedure HeadTitle;
Var  Quit: boolean;
// P : Pointer;
 TempColorA : TSDL_Color;
 DestRect : TSDL_Rect;
 Menu : array[1..6] of PSDL_Surface;
Procedure WriteMenu;
Var I : Integer;
Begin
  For i:=1 to 6 do
  Begin
   if i=ActiveMenu then
     begin
       TempColorA.r:=240;
       TempColorA.g:=250;
       TempColorA.b:=100;
     end else
     begin
       TempColorA.r:=80;
       TempColorA.g:=80;
       TempColorA.b:=255;
     end;
   TempColorA.unused:=0;
   Menu[i]:=TTF_RenderText_Solid(Font1,PChar(MenuNames[i]),TempColorA );
   DestRect.x:=320 - (Menu[i].w div 2);
   DestRect.y:=90+(I*50);
   DestRect.w:=Menu[i].w;DestRect.h:=Menu[i].h;
   SDL_UpperBlit(Menu[I], nil, Screen, @DestRect);
   SDL_FreeSurface(Menu[i]);
  end;
End;
Begin
    PlayersLife:=2;
    Player1Life:=1;
    Player1Speed:=1;
    Player1ProjectilePower:=1;
    Player1MaxTimeVistrel:=60;
    Player1ProjectileSpeed:=3;
    Player1Water:=False;

    Player2Life:=1;
    Player2Speed:=1;
    Player2ProjectilePower:=1;
    Player2MaxTimeVistrel:=60;
    Player2ProjectileSpeed:=3;
    Player2Water:=False;

  Player1Points:=0;
  Player2Points:=0;
  ComputerPoints:=0;//Очки игроков
  GameLevel:=0;
  Quit:=False; ActiveMenu:=1;
  music:=Mix_LoadMUS(PChar(MusicDir+'begin.xm'));

  F_Stream:=TFileStream.Create(GFX+'tanks.dat',fmOpenRead);
  F_Stream.Position:=1398072;
  TempImage:=LoadSDLBMPFromStream(F_Stream);
  SDL_UpperBlit(TempImage, nil, Background, nil);

  SDL_FreeSurface(TempImage);

  F_Stream.Free;

//  TempImage:=SDL_LoadBMP(GfxDir+'Title.bmp');
//  SDL_UpperBlit(TempImage, nil, Background, nil);
//  SDL_FreeSurface(TempImage);

  TempColor.r:=0;
  TempColor.g:=5;
  TempColor.b:=250;
  TempColor.unused:=0;
  Text:=TTF_RenderText_Blended(Font,Version_no,TempColor );
  DestRect.x:=580;
  DestRect.y:=105;

  SDL_UpperBlit(Text, nil, Background, @DestRect);
  SpriteEngine.BackgroundSurface := Background;
  SDL_UpperBlit(Background, nil, Screen, nil);

  SDL_FreeSurface(Text);

  Mix_PlayMusic(music, 0);
  Mix_VolumeMusic(MusicVolume);
  SDL_UpdateRect(Screen,0,0,0,0);

  WriteMenu;
  Repeat
  while SDL_PollEvent(@Event) > 0 do
  case Event.key.type_ of
    SDL_QUITEV : Begin Quit:=true;QuitGame:=true; end;
    SDL_KeyDown :
     Case Event.key.keysym.sym of
//      SDLK_ESCAPE : Begin Quit:=true;QuitGame:=true; end;
      SDLK_BACKSPACE : SDL_UpdateRect(Screen,0,0,0,0);
      SDLK_PAUSE : Pause;
      SDLK_UP : if ActiveMenu>1 then Begin Dec(ActiveMenu); Mix_PlayChannel(0,sounds[0],0); WriteMenu;end;
      SDLK_DOWN : if ActiveMenu<6 then Begin Inc(ActiveMenu); Mix_PlayChannel(0,sounds[0],0); WriteMenu;end;
      SDLK_RETURN : Case ActiveMenu of
                     1 : Begin StageOfGame:=GameTotal; Quit:=True end;
                     2 : Begin StageOfGame:=GameMapEditor; quit:=True; end;
                     3 : Begin StageOfGame:=GameOptions; Quit:=True;End;
                     4 : Begin StageOfGame:=GameEndTotal; Quit:=True;End;
                     5 : Begin StageOfGame:=EndTitle; Quit:=True; end;
                     6 : Begin Quit:=true;QuitGame:=true; end;
                   End;
//      SDLK_F1 : Begin Quit:=true;StageOfGame:=GameOptions; end;
     end;
  end;//case
//  SDL_UpdateRect(Screen,0,0,0,0);
  SDL_Flip(Screen);
  SDL_Delay(50);
  Until Quit;
//  Mix_FadeOutMusic(1000);
//  SDL_Delay(1000);
  Mix_ResumeMusic;
  Mix_FreeMusic(Music);
End;
Procedure Title;
Const NumTitles = 34;
Var  Quit: boolean;
  MTime, ActiveTitles : Integer;
  TitlesArray : Array[1..NumTitles] of TSDL_Rect;

Begin
 TitlesArray[1]:=SDLRect(0,0,355,32); //Над игрой работали
 TitlesArray[2]:=SDLRect(0,0,0,0);
 TitlesArray[3]:=SDLRect(0,0,0,0);
 TitlesArray[4]:=SDLRect(0,33,302,23); //Программрование
 TitlesArray[5]:=SDLRect(0,0,0,0);
 TitlesArray[6]:=SDLRect(0,152,253,21); //Осипов Евгений
// TitlesArray[7]:=SDLRect(361,0,150,189); // Фото
// TitlesArray[8]:=SDLRect(0,0,0,0);
 TitlesArray[7]:=SDLRect(0,0,0,0);
 TitlesArray[8]:=SDLRect(0,0,0,0);
 TitlesArray[9]:=SDLRect(0,0,0,0);
 TitlesArray[10]:=SDLRect(0,0,0,0);
 TitlesArray[11]:=SDLRect(0,61,142,23);//Графика
 TitlesArray[12]:=SDLRect(0,0,0,0);
 TitlesArray[13]:=SDLRect(0,152,253,21); //Осипов Евгений
 TitlesArray[14]:=SDLRect(0,0,0,0);
 TitlesArray[15]:=SDLRect(0,0,0,0);
 TitlesArray[16]:=SDLRect(0,0,0,0);
 TitlesArray[17]:=SDLRect(0,0,0,0);
 TitlesArray[18]:=SDLRect(164,61,121,23);//Музыка
 TitlesArray[19]:=SDLRect(0,0,0,0);
 TitlesArray[20]:=SDLRect(0,88,346,60);//Арвидас
 TitlesArray[21]:=SDLRect(0,0,0,0);
 TitlesArray[22]:=SDLRect(0,0,0,0);
 TitlesArray[23]:=SDLRect(0,0,0,0);
 TitlesArray[24]:=SDLRect(0,0,0,0);
 TitlesArray[25]:=SDLRect(0,208,420,21); //
 TitlesArray[26]:=SDLRect(0,183,272,18);
 TitlesArray[27]:=SDLRect(0,0,0,0);
 TitlesArray[28]:=SDLRect(0,0,0,0);
 TitlesArray[29]:=SDLRect(0,0,0,0);
 TitlesArray[30]:=SDLRect(0,0,0,0);
 TitlesArray[31]:=SDLRect(0,0,0,0);
 TitlesArray[32]:=SDLRect(0,0,0,0);
 TitlesArray[33]:=SDLRect(0,0,0,0);
 TitlesArray[34]:=SDLRect(0,0,0,0);

  Activetitles:=1; MTime:=0;

  Quit:=False;
  music:=Mix_LoadMUS(PChar(MusicDir+'end.xm'));

  F_Stream:=TFileStream.Create(GFX+'tanks.dat',fmOpenRead);
  F_Stream.Position:=3241380;
  TempImage:=LoadSDLBMPFromStream(F_Stream);
  SDL_UpperBlit(TempImage, nil, Background, nil);

  SDL_FreeSurface(TempImage);

  F_Stream.Free;

//  TempImage:=SDL_LoadBMP(GfxDir+'EndGame.bmp');
//  SDL_UpperBlit(TempImage, nil, Background, nil);
//  SDL_FreeSurface(TempImage);

  SpriteEngine.BackgroundSurface := Background;
  SDL_UpperBlit(Background, nil, Screen, nil);

  Mix_PlayMusic(music, 0);
  Mix_VolumeMusic(MusicVolume);
  SDL_UpdateRect(Screen,0,0,0,0);

  Repeat
  while SDL_PollEvent(@Event) > 0 do
  case Event.key.type_ of
    SDL_QUITEV : Begin Quit:=true;QuitGame:=true; end;
    SDL_KeyDown :
     Case Event.key.keysym.sym of
      SDLK_ESCAPE : Begin Quit:=true;StageOfGame:=GameTitle; end;
      SDLK_BACKSPACE : SDL_UpdateRect(Screen,0,0,0,0);
      SDLK_PAUSE : Pause;
     end;
  end;//case
  if Mtime=0 then
    begin
      SpriteEngine.AddSprite(TTitles.Create(TitlesArray[ActiveTitles]));
      inc(ActiveTitles);
      if Activetitles>NumTitles then activeTitles:=1;
    end;
  inc(Mtime);
  if Mtime>59 then Mtime:=0;
  SpriteEngine.Move;
  SpriteEngine.Draw;
//  SDL_UpdateRect(Screen,0,0,0,0);
  SDL_Flip(Screen);
//  SDL_UpdateRects(Screen, SpriteEngine.NumberOfRects, @SpriteEngine.UpdateRects[0]);
  SDL_Delay(50);
  Until Quit;
  Mix_FadeOutMusic(1000);
  SDL_Delay(1000);
  Mix_ResumeMusic;
  Mix_FreeMusic(Music);
End;
Procedure TotalCount; //Таблица рекордов
Var  Quit: boolean;
//     sr: TSearchRec;
    TempColorA : TSDL_Color;
    DestRect : TSDL_Rect;
    SRF : PSDL_Surface;
    Fl : File;
    I : Integer;
    S:String;
procedure bubble;//Пузырьковая сортировка списка рекордов
 var
  i: integer;
  Temp : PlayerRecord;
  switch:boolean;
begin
  repeat
  switch:=false;
  for i:=1 to 9 do
  if PlayersNames[i].Count<PlayersNames[i+1].Count then
    begin
      switch:=true;
      temp:=PlayersNames[i];
      PlayersNames[i]:=PlayersNames[i+1];
      PlayersNames[i+1]:=temp
    end
  until not(switch)
end;
BEGIN
  Quit:=False;
  if SysUtils.fileexists('Records.tnk') then
  try
  AssignFile(Fl,'Records.tnk');
  Reset(Fl,1);
  BlockRead(Fl,PlayersNames,SizeOf(PlayersNames));
  Finally
  CloseFile(Fl);
  end;
  bubble;
  if PlayersNames[10].Count<Player1Points then
    Begin
      PlayersNames[10].Name:=Player1Name;
      PlayersNames[10].Count:=Player1Points;
    End;
  bubble;
  if PlayersNames[10].Count<Player2Points then
    Begin
      PlayersNames[10].Name:=Player2Name;
      PlayersNames[10].Count:=Player2Points;
    End;
  bubble;
  try
  AssignFile(Fl,'Records.tnk');
  Rewrite(Fl,1);
  BlockWrite(Fl,PlayersNames,SizeOf(PlayersNames));
  Finally
  CloseFile(Fl);
  end;
  F_Stream:=TFileStream.Create(GFX+'tanks.dat',fmOpenRead);
  F_Stream.Position:=2319726;
  TempImage:=LoadSDLBMPFromStream(F_Stream);
  SDL_UpperBlit(TempImage, nil, Background, nil);

  SDL_FreeSurface(TempImage);

  F_Stream.Free;

//  TempImage:=SDL_LoadBMP(GfxDir+'LevelN.bmp');
//  SDL_UpperBlit(TempImage, nil, BackGround, nil);
//  SDL_FreeSurface(TempImage);

  DestRect.x:=0;
  DestRect.y:=0;
  SrcRect.x:=70;
  SrcRect.y:=50;
  SrcRect.w:=500;
  SrcRect.h:=400;
  SRF:=SDL_CreateRGBSurface( SDL_HWSURFACE or SDL_SRCALPHA,640,480,Bpp, 50, 150, 50, 0 );
  SDL_FillRect(SRF, @srcRect, 28);
  SDL_SetAlpha(SRF,SDL_HWSURFACE or SDL_SRCALPHA,164);
  SDL_UpperBlit(SRF, nil, Background, @DestRect);
  SDL_FreeSurface(SRF);

  TempColorA.r:=255;
  TempColorA.g:=155;
  TempColorA.b:=255;
  TempColorA.unused:=0;

  SRF:=TTF_RenderText_Solid(Font2,PChar(strHonor),TempColorA );
  DestRect.x:=320 - (Srf.w div 2);
  DestRect.y:=0;
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  TempColorA.r:=55;
  TempColorA.g:=55;
  TempColorA.b:=100;
  TempColorA.unused:=0;

  //PlayersNames
  For I:=1 to 10 do
    begin
      DestRect.y:=15+i*40;
      DestRect.x:=73;
      inc(TempColorA.b,5);
      inc(TempColorA.g,5);
      TempColorA.r:=55;
      SRF:=TTF_RenderText_Solid(Font,PChar(IntToStr(i)),TempColorA );
      SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
      SDL_FreeSurface(SRF);

     TempColorA.r:=185;
      inc(DestRect.x,25);
      S:=PlayersNames[i].Name;
      SRF:=TTF_RenderText_Solid(Font,PChar(S),TempColorA );
      SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
      SDL_FreeSurface(SRF);

      TempColorA.r:=255;
      SRF:=TTF_RenderText_Solid(Font,PChar(intToStr(PlayersNames[i].Count)),TempColorA );
      DestRect.x:=565-Srf.w;
      SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
      SDL_FreeSurface(SRF);
    end;

//  SpriteEngine.BackgroundSurface := Background;
  SDL_UpperBlit(Background, nil, Screen, nil);

  SDL_UpdateRect(Screen,0,0,0,0);
  music:=Mix_LoadMUS(PChar(MusicDir+'heart.xm'));
  Mix_PlayMusic(music, 0);
  Mix_VolumeMusic(MusicVolume);

  Repeat
  while SDL_PollEvent(@Event) > 0 do
  case Event.key.type_ of
    SDL_QUITEV : Begin Quit:=true;QuitGame:=true; end;
    SDL_KeyDown :
     Case Event.key.keysym.sym of
      SDLK_ESCAPE,SDLK_RETURN : Begin Quit:=true;StageOfGame:=GameTitle; end;
      SDLK_BACKSPACE : SDL_UpdateRect(Screen,0,0,0,0);
//      SDLK_PAUSE : Pause;
     end;
  end;//case

  SpriteEngine.Move;
  SpriteEngine.Draw;
//  SDL_UpdateRect(Screen,0,0,0,0);
  SDL_Flip(Screen);
//  SDL_UpdateRects(Screen, SpriteEngine.NumberOfRects, @SpriteEngine.UpdateRects[0]);
  SDL_Delay(TimeLeft);
  Until Quit;
  Mix_FadeOutMusic(1000);
  SDL_Delay(1000);
  Mix_ResumeMusic;
  Mix_FreeMusic(Music);
End;

Procedure NextLevel;
Var  Quit: boolean;
     sr: TSearchRec;
    TempColorA : TSDL_Color;
    DestRect : TSDL_Rect;
    SRF : PSDL_Surface;
    Fl : File;
Begin
Inc(GameLevel);
if FindFirst(maps+'LEVELS\'+IntToStr(GameLevel)+'.tm1', faArchive , sr) = 0 then
 Begin
  TempColorA.r:=155;
  TempColorA.g:=155;
  TempColorA.b:=255;
  TempColorA.unused:=0;

  Quit:=False;

  F_Stream:=TFileStream.Create(GFX+'tanks.dat',fmOpenRead);
  F_Stream.Position:=2319726;
  TempImage:=LoadSDLBMPFromStream(F_Stream);
  SDL_UpperBlit(TempImage, nil, Background, nil);

  SDL_FreeSurface(TempImage);

  F_Stream.Free;

//  TempImage:=SDL_LoadBMP(GfxDir+'LevelN.bmp');
//  SDL_UpperBlit(TempImage, nil, BackGround, nil);
//  SDL_FreeSurface(TempImage);

  DestRect.x:=280;
  DestRect.y:=70;
  SRF:=TTF_RenderText_Solid(Font2,PChar(strLevel+' '+IntToStr(GameLevel)),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

//  SpriteEngine.BackgroundSurface := Background;
  SDL_UpperBlit(Background, nil, Screen, nil);

  SDL_UpdateRect(Screen,0,0,0,0);
  AssignFile(Fl,Maps+'LEVELS\'+SR.Name);
  Reset(fl,1);
  BlockRead(Fl,Map,SizeOf(Map));
  Close(fl);

  Repeat
  while SDL_PollEvent(@Event) > 0 do
  case Event.key.type_ of
    SDL_QUITEV : Begin Quit:=true;QuitGame:=true; end;
    SDL_KeyDown :
     Case Event.key.keysym.sym of
      SDLK_ESCAPE,SDLK_SPACE,SDLK_RETURN : Begin Quit:=true;StageOfGame:=GameGame; end;
      SDLK_BACKSPACE : SDL_UpdateRect(Screen,0,0,0,0);
//      SDLK_PAUSE : Pause;
     end;
  end;//case

  SpriteEngine.Move;
  SpriteEngine.Draw;
//  SDL_UpdateRect(Screen,0,0,0,0);
//  SDL_Flip(Screen);
//  SDL_UpdateRects(Screen, SpriteEngine.NumberOfRects, @SpriteEngine.UpdateRects[0]);
  SDL_Delay(TimeLeft);
  Until Quit;
 end else
 Begin
   TotalCount;
   StageOfGame:=EndTitle;
 end;
End;

Procedure Options; //Параметры
Var  Quit: boolean;
    TempColorA : TSDL_Color;
    SrcRect,DestRect : TSDL_Rect;
    SRF : PSDL_Surface;
    MenuValue : Integer;
    F : File;
    Ch : Char;
Procedure WriteParameters;
Var S : String;
Begin
  TempColorA.r:=200;TempColorA.g:=200;TempColorA.b:=255;TempColorA.unused:=0;
  DestRect.x:=310;DestRect.y:=100;DestRect.w:=330;DestRect.h:=300;
  SDL_UpperBlit(Background, @DestRect, Screen, @DestRect);

  if TwoPlayer then S:=strNumPlayers2 else S:=strNumPlayers1;
  DestRect.x:=310;
  DestRect.y:=100;
  SRF:=TTF_RenderText_Solid(Font2,PChar(S),TempColorA );
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);
  Case Level of
    Easy     : S:=strDefficult1;
    Medium   : S:=strDefficult2;
    Hard     : S:=strDefficult3;
    VeryHard : S:=strDefficult4;
  End;
  DestRect.x:=310;
  DestRect.y:=150;
  SRF:=TTF_RenderText_Solid(Font2,PChar(S),TempColorA );
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);
  if PlayMusic then S:=strMusicInGameOn else S:=strMusicInGameOff;
  DestRect.x:=310;
  DestRect.y:=200;
  SRF:=TTF_RenderText_Solid(Font2,PChar(S),TempColorA );
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.x:=310;
  DestRect.y:=260;
  DestRect.w:=MusicVolume*2;
  DestRect.h:=30;
  SDL_FillRect(Screen, @DestRect, 255);

  DestRect.x:=310;
  DestRect.y:=315;
  S:=Player1Name;
  SRF:=TTF_RenderText_Solid(Font,PChar(S),TempColorA );
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.y:=365;
  S:=Player2Name;
  SRF:=TTF_RenderText_Solid(Font,PChar(S),TempColorA );
  SDL_UpperBlit(SRF, nil, Screen, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.x:=10;DestRect.y:=100;DestRect.w:=32;DestRect.h:=300;
  SDL_UpperBlit(Background, @DestRect, Screen, @DestRect);

  SrcRect.x:=160;SrcRect.y:=0;SrcRect.w:=32;SrcRect.h:=32;
  DestRect.x:=10;DestRect.y:=55+(MenuValue*50);DestRect.w:=32;DestRect.h:=32;
  SDL_UpperBlit(Status, @SrcRect, Screen, @DestRect);

End;
Begin
  MenuValue:=1;
  Quit:=False;

  F_Stream:=TFileStream.Create(GFX+'tanks.dat',fmOpenRead);
  F_Stream.Position:=2319726;
  TempImage:=LoadSDLBMPFromStream(F_Stream);
  SDL_UpperBlit(TempImage, nil, Background, nil);

  SDL_FreeSurface(TempImage);

  F_Stream.Free;

//  TempImage:=SDL_LoadBMP(GfxDir+'LevelN.bmp');
//  SDL_UpperBlit(TempImage, nil, BackGround, nil);
//  SDL_FreeSurface(TempImage);
  DestRect.x:=0;
  DestRect.y:=0;
  DestRect.w:=640;
  DestRect.h:=480;
  SRF:=SDL_CreateRGBSurface( SDL_HWSURFACE or SDL_SRCALPHA,640,480,Bpp, 50, 150, 50, 0 );
  SDL_FillRect(SRF, @DestRect, 0);
  SDL_SetAlpha(SRF,SDL_HWSURFACE or SDL_SRCALPHA,200);
  SDL_UpperBlit(SRF, nil, BackGround, nil);
  SDL_FreeSurface(SRF);

  TempColorA.r:=155;
  TempColorA.g:=155;
  TempColorA.b:=255;
  TempColorA.unused:=0;
  DestRect.y:=25;
  SRF:=TTF_RenderText_Solid(Font2,PChar(strParams),TempColorA );
  DestRect.x:=320-(SRF.w div 2);
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  TempColorA.r:=155;
  TempColorA.g:=55;
  TempColorA.b:=255;
  TempColorA.unused:=0;

  DestRect.x:=50;
  DestRect.y:=100;
  SRF:=TTF_RenderText_Solid(Font2,PChar(strNumPlayers),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.x:=50;
  DestRect.y:=150;
  SRF:=TTF_RenderText_Solid(Font2,PChar(strDefficult),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.x:=50;
  DestRect.y:=200;
  SRF:=TTF_RenderText_Solid(Font2,PChar(strMusicInGame),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);


  DestRect.x:=50;
  DestRect.y:=250;
  SRF:=TTF_RenderText_Solid(Font2,PChar(strMusicVolume),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);


  DestRect.x:=50;
  DestRect.y:=300;
  SRF:=TTF_RenderText_Solid(Font2,PChar(strPlayer1Name),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  DestRect.x:=50;
  DestRect.y:=350;
  SRF:=TTF_RenderText_Solid(Font2,PChar(strPlayer2Name),TempColorA );
  SDL_UpperBlit(SRF, nil, BackGround, @DestRect);
  SDL_FreeSurface(SRF);

  SDL_UpperBlit(Background, nil, Screen, nil);

  SDL_UpdateRect(Screen,0,0,0,0);

  music:=Mix_LoadMUS(PChar(MusicDir+'begin.xm'));
  Mix_PlayMusic(music, 0);
  Mix_VolumeMusic(MusicVolume);

  Repeat
  if MenuValue<1 then MenuValue:=1;
  if MenuValue>6 then MenuValue:=6;
  WriteParameters;
  while SDL_PollEvent(@Event) > 0 do
  case Event.key.type_ of
    SDL_QUITEV : Begin Quit:=true;QuitGame:=true; end;
    SDL_KeyDown :
     Case Event.key.keysym.sym of
      SDLK_ESCAPE : Begin Quit:=true;StageOfGame:=GameTitle; end;
      SDLK_Up : if Menuvalue>1 then Dec(MenuValue);
      SDLK_Down : if Menuvalue<6 then Inc(MenuValue);
      SDLK_0..SDLK_9,
      SDLK_A..SDLK_Z, SDLK_SPACE : Begin
                  Case Event.key.keysym.sym of
                    SDLK_0 : Ch:='0';
                    SDLK_1 : Ch:='1';
                    SDLK_2 : Ch:='2';
                    SDLK_3 : Ch:='3';
                    SDLK_4 : Ch:='4';
                    SDLK_5 : Ch:='5';
                    SDLK_6 : Ch:='6';
                    SDLK_7 : Ch:='7';
                    SDLK_8 : Ch:='8';
                    SDLK_9 : Ch:='9';
                    SDLK_A : Ch:='A';
                    SDLK_B : Ch:='B';
                    SDLK_C : Ch:='C';
                    SDLK_D : Ch:='D';
                    SDLK_E : Ch:='E';
                    SDLK_F : Ch:='F';
                    SDLK_G : Ch:='G';
                    SDLK_H : Ch:='H';
                    SDLK_I : Ch:='I';
                    SDLK_J : Ch:='J';
                    SDLK_K : Ch:='K';
                    SDLK_L : Ch:='L';
                    SDLK_M : Ch:='M';
                    SDLK_N : Ch:='N';
                    SDLK_O : Ch:='O';
                    SDLK_P : Ch:='P';
                    SDLK_Q : Ch:='Q';
                    SDLK_R : Ch:='R';
                    SDLK_S : Ch:='S';
                    SDLK_T : Ch:='T';
                    SDLK_U : Ch:='U';
                    SDLK_V : Ch:='V';
                    SDLK_W : Ch:='W';
                    SDLK_X : Ch:='X';
                    SDLK_Y : Ch:='Y';
                    SDLK_Z : Ch:='Z';
                    SDLK_SPACE : Ch:=' ';
                    else Ch:='+';
                   End;
                       if (Ch<>'+') then
                         begin
                           Case MenuValue of
                            5 : Player1Name:=Player1Name+Ch;
                            6 : Player2Name:=Player2Name+Ch;
                           end;//case
                         end;
                       End;
      SDLK_BackSpace : Case MenuValue of
                        5 : Delete(Player1Name,Length(Player1Name),1);
                        6 : Delete(Player2Name,Length(Player2Name),1);
                       End;
      SDLK_Delete :   Case MenuValue of
                        5 : Player1Name:='';
                        6 : Player2Name:='';
                       End;
      SDLK_Left : Case Menuvalue of
        1 : TwoPlayer:=false;
        2 : if Level>Easy then Dec(Level);
        3 : PlayMusic:=False;
        4 : if MusicVolume>=16 then begin Dec(MusicVolume,16);Mix_VolumeMusic(MusicVolume);end;
        5 : Begin
            End;
        6 : Begin
            End;
      end;//case

      SDLK_Right: Case Menuvalue of
        1 : TwoPlayer:=true;
        2 : if Level<VeryHard then Inc(Level);
        3 : PlayMusic:=True;
        4 : if MusicVolume<=112 then begin Inc(MusicVolume,16);Mix_VolumeMusic(MusicVolume);end;
      end;//case
     end;
  end;//case

  SDL_Flip(Screen);
  SDL_Delay(TimeLeft);
  Until Quit;
  Mix_ResumeMusic;
  Mix_FreeMusic(Music);
  AssignFile(F,ConfigFile);
  Try
  Rewrite(F,1);
//  BlockWrite(F,TwoPlayer,SizeOf(TwoPlayer));
  BlockWrite(F,Level,SizeOf(Level));
  BlockWrite(F,PlayMusic,SizeOf(PlayMusic));
  BlockWrite(F,MusicVolume,SizeOf(MusicVolume));
  BlockWrite(F,Player1Name,SizeOf(Player1Name));
  BlockWrite(F,Player2Name,SizeOf(Player2Name));
  Finally
  CloseFile(F);
  end;
End;

Var I : Integer;
    F : File;
    SoundFrequency,
    SoundChannel,
    SoundBufferSize : Integer;
    IniFile : TIniFile;
    T_File : System.TextFile;
begin
  IniFile:=TIniFile.Create(GetCurrentFolder+'\tanks.ini');
    Bpp:=IniFile.ReadInteger('HARDWARE','BitPerPixel(15_16_24_32)',16);
    SoundFrequency:=IniFile.ReadInteger('HARDWARE','SoundFrequencyHz(11025_22050_44100)',44100);
    SoundChannel:=IniFile.ReadInteger('HARDWARE','SoundChannel(1-Mono_2-Stereo)',2);
    SoundBufferSize:=IniFile.ReadInteger('HARDWARE','SoundBufferSize',2048);
    MenuNames[1]:=IniFile.ReadString('LANGUAGE','Menu1Game','ИГРА');
    MenuNames[2]:=IniFile.ReadString('LANGUAGE','Menu2Map','КАРТА');
    MenuNames[3]:=IniFile.ReadString('LANGUAGE','Menu3Options','ОПЦИИ');
    MenuNames[4]:=IniFile.ReadString('LANGUAGE','Menu4Best','ЛУЧШИЕ');
    MenuNames[5]:=IniFile.ReadString('LANGUAGE','Menu5Credits','ТИТРЫ');
    MenuNames[6]:=IniFile.ReadString('LANGUAGE','Menu6Exit','ВЫХОД');
    strGround:=IniFile.ReadString('LANGUAGE','GroundName','Земля');
    strWater:=IniFile.ReadString('LANGUAGE','WaterName','Вода');
    strWall:=IniFile.ReadString('LANGUAGE','WallName','Стена');
    strSteel:=IniFile.ReadString('LANGUAGE','SteelName','Сталь');
    strClear:=IniFile.ReadString('LANGUAGE','TextToClear','Очистить');
    strSave:=IniFile.ReadString('LANGUAGE','TextToSave','Сохранить');
    strLoad:=IniFile.ReadString('LANGUAGE','TextToLoad','Загрузить');
    strPlay:=IniFile.ReadString('LANGUAGE','TextToPlay','Играть');
    strParams:=IniFile.ReadString('LANGUAGE','TextToParameters','Настройка игры');
    strNumPlayers:=IniFile.ReadString('LANGUAGE','TextToPlayers','Игроков');
    strNumPlayers1:=IniFile.ReadString('LANGUAGE','TextToPlayers1','Один в поле воин');
    strNumPlayers2:=IniFile.ReadString('LANGUAGE','TextToPlayers2','Два танкиста без собаки');
    strDefficult:=IniFile.ReadString('LANGUAGE','TextToDifficulty','Сложность');
    strDefficult1:=IniFile.ReadString('LANGUAGE','TextToDifficulty1level','Слабаки');
    strDefficult2:=IniFile.ReadString('LANGUAGE','TextToDifficulty2Level','В бой!');
    strDefficult3:=IniFile.ReadString('LANGUAGE','TextToDifficulty3Level','Берегись!');
    strDefficult4:=IniFile.ReadString('LANGUAGE','TextToDifficulty4Level','Где моя большая пулька?');
    strMusicInGame:=IniFile.ReadString('LANGUAGE','TextToMusicInGame','Музыка в игре');
    strMusicInGameOff:=IniFile.ReadString('LANGUAGE','TextToMusicInGameOff','Не нужно мне музыки!');
    strMusicInGameOn:=IniFile.ReadString('LANGUAGE','TextToMusicInGameOn','Помирать так с музыкой!');
    strMusicVolume:=IniFile.ReadString('LANGUAGE','TextToMusicVolume','Громкость музыки');
    strPlayer1Name:=IniFile.ReadString('LANGUAGE','TextToName1Player','Имя первого игрока');
    strPlayer2Name:=IniFile.ReadString('LANGUAGE','TextToName2Player','Имя второго игрока');
    strHonor:=IniFile.ReadString('LANGUAGE','TextToHallOfGlory','Зал славы');
    strLevel:=IniFile.ReadString('LANGUAGE','TextToLevel','Уровень');
  IniFile.Free;

  Case Bpp of
    15,16,24,32 : ;
    else Bpp:=16;
  end;
  Case SoundFrequency of
   11025,22050,44100 : ;
   else SoundFrequency:=44100;
  end;
  Case SoundChannel of
   1,2 : ;
   else SoundChannel:=2;
  end;

  ActiveMusic:=0;
  Randomize;
  if SDL_Init(SDL_INIT_VIDEO) <> 0 then halt;
  SDL_WM_SetCaption(ProgramCaption, nil);
  Screen := SDL_SetVideoMode( 640, 480, Bpp, SDL_FULLSCREEN);
  TTF_Init;
  Font:=TTF_OpenFont(Gfx+'ASSIMILA.TTF',20) ;
  Font1:=TTF_OpenFont(Gfx+'Rublik.TTF',30) ;
  Font2:=TTF_OpenFont(Gfx+'BEDR-CYR.TTF',40) ;
  Font3:=TTF_OpenFont(Gfx+'ASSIMILA.TTF',12) ;
  Font4:=TTF_OpenFont(Gfx+'Rublik.TTF',16) ;
  Mix_OpenAudio(SoundFrequency, AUDIO_S16SYS,SoundChannel, SoundBufferSize);
  Mix_HookMusicFinished(@Music_Hook);

  Background:=SDL_CreateRGBSurface( SDL_HWSURFACE or SDL_SRCALPHA,640,480,Bpp, 0, 0, 0, 0 );

  F_Stream:=TFileStream.Create(GFX+'tanks.dat',fmOpenRead);

  Sprites32:=LoadSDLBMPFromStream(F_Stream);
  SDL_SetColorKey(Sprites32, SDL_HWSURFACE or SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, SDL_MapRGB(Sprites32.format,0,0,0));

  TankSprites:=LoadSDLBMPFromStream(F_Stream);
  SDL_SetColorKey(TankSprites, SDL_HWSURFACE or SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, SDL_MapRGB(TankSprites.format,0,0,0));

  RightPanel:=LoadSDLBMPFromStream(F_Stream);

  RightPanelMapEditor:=LoadSDLBMPFromStream(F_Stream);

  Projectile:=LoadSDLBMPFromStream(F_Stream);

  vzriv:=LoadSDLBMPFromStream(F_Stream);
  SDL_SetColorKey(vzriv, SDL_HWSURFACE or SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, SDL_MapRGB(vzriv.format,0,0,0));

  VzrivTank:=LoadSDLBMPFromStream(F_Stream);
  SDL_SetColorKey(vzrivTank, SDL_HWSURFACE or SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, SDL_MapRGB(vzrivTank.format,0,0,0));

  Status:=LoadSDLBMPFromStream(F_Stream);
  SDL_SetColorKey(Status, SDL_HWSURFACE or SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, SDL_MapRGB(Status.format,0,0,0));

  HQExplosion:=LoadSDLBMPFromStream(F_Stream);
  SDL_SetColorKey(HQExplosion, SDL_HWSURFACE or SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, SDL_MapRGB(HQExplosion.format,0,0,0));

  Titles:=LoadSDLBMPFromStream(F_Stream);
  SDL_SetColorKey(Titles, SDL_HWSURFACE or SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, SDL_MapRGB(Titles.format,0,0,0));

  Cursor:=LoadSDLBMPFromStream(F_Stream);
  SDL_SetColorKey(Cursor, SDL_HWSURFACE or SDL_SRCCOLORKEY or SDL_RLEACCEL or SDL_HWACCEL, SDL_MapRGB(Cursor.format,0,0,0));

  SmallTank:=LoadSDLBMPFromStream(F_Stream);

  F_Stream.Free;

//Всю графику в один файл
{  AssignFile(T_File,'GfxLogo.txt');
  Rewrite(T_File);
  F_Stream:=TFileStream.Create(GFX+'tanks.dat',fmOpenWrite);

  WriteLn(T_File,'Sprites32 '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(Sprites32, F_Stream);

  WriteLn(T_File,'TankSprites '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(TankSprites, F_Stream);

  WriteLn(T_File,'RightPanel '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(RightPanel, F_Stream);

  WriteLn(T_File,'RightPanelMapEditor '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(RightPanelMapEditor, F_Stream);

  WriteLn(T_File,'Projectile '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(Projectile, F_Stream);

  WriteLn(T_File,'vzriv '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(vzriv, F_Stream);

  WriteLn(T_File,'VzrivTank '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(VzrivTank, F_Stream);

  WriteLn(T_File,'Status '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(Status, F_Stream);

  WriteLn(T_File,'HQExplosion '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(HQExplosion, F_Stream);

  WriteLn(T_File,'Titles '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(Titles, F_Stream);


  WriteLn(T_File,'Cursor '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(Cursor, F_Stream);


  WriteLn(T_File,'SmallTank '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(SmallTank, F_Stream);


  TempImage:=SDL_LoadBMP(GfxDir+'Title.bmp');
  WriteLn(T_File,'Title.bmp '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(TempImage, F_Stream);

  SDL_FreeSurface(TempImage);

  TempImage:=SDL_LoadBMP(GfxDir+'LevelN.bmp');
  WriteLn(T_File,'LevelN.bmp '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(TempImage, F_Stream);

  SDL_FreeSurface(TempImage);

  TempImage:=SDL_LoadBMP(GfxDir+'EndGame.bmp');
  WriteLn(T_File,'EndGame.bmp '+IntToStr(F_Stream.Position));
  SaveSDLBMPToStream(TempImage, F_Stream);

  SDL_FreeSurface(TempImage);

  F_Stream.Free;
  Close(T_File);
}
  SDL_ShowCursor( SDL_DISABLE ); //Мышь вырубить
  SpriteEngine := TSpriteEngine.Create(Screen);
  TextColor.r:=50;
  TextColor.g:=50;
  TextColor.b:=200;

//  SDL_Init(SDL_INIT_AUDIO);
  sounds[0]:=Mix_LoadWAV(PChar(SoundDir+'Sound1.wav'));
  sounds[1]:=Mix_LoadWAV(PChar(SoundDir+'Sound2.wav'));
  sounds[2]:=Mix_LoadWAV(PChar(SoundDir+'Sound3.wav'));
  sounds[3]:=Mix_LoadWAV(PChar(SoundDir+'Sound4.wav'));
  sounds[4]:=Mix_LoadWAV(PChar(SoundDir+'Sound5.wav'));
  sounds[5]:=Mix_LoadWAV(PChar(SoundDir+'Sound6.wav'));
  sounds[6]:=Mix_LoadWAV(PChar(SoundDir+'Sound7.wav'));
  sounds[7]:=Mix_LoadWAV(PChar(SoundDir+'Sound8.wav'));
  sounds[8]:=Mix_LoadWAV(PChar(SoundDir+'Sound9.wav'));
  sounds[9]:=Mix_LoadWAV(PChar(SoundDir+'Sound10.wav'));
  sounds[10]:=Mix_LoadWAV(PChar(SoundDir+'Sound11.wav'));
  sounds[11]:=Mix_LoadWAV(PChar(SoundDir+'Sound12.wav'));
  keys := PKeyStateArr(SDL_GetKeyState(nil));
  if SysUtils.FileExists(ConfigFile) then
  begin
  AssignFile(F,ConfigFile);
  Try
  Reset(F,1);
//  BlockRead(F,TwoPlayer,SizeOf(TwoPlayer));
  BlockRead(F,Level,SizeOf(Level));
  BlockRead(F,PlayMusic,SizeOf(PlayMusic));
  BlockRead(F,MusicVolume,SizeOf(MusicVolume));
  BlockRead(F,Player1Name,SizeOf(Player1Name));
  BlockRead(F,Player2Name,SizeOf(Player2Name));
  Finally
  CloseFile(F);
  end;
  end;

Repeat
  Case StageOfGame of
    GameTitle : HeadTitle;
    GameGame  : Game;
    EndTitle : Title;
    GameMapEditor : MapEditor;
    GameTotal : NextLevel;
    GameOptions : Options;
    GameEndTotal : TotalCount;
  End;
until QuitGame;
  SDL_ShowCursor( SDL_ENABLE ); //Мышь вырубить
  TTF_CloseFont(Font);
  TTF_CloseFont(Font1);
  TTF_CloseFont(Font2);
  TTF_CloseFont(Font3);
  TTF_CloseFont(Font4);
//if Music<>Nil then Mix_FreeMusic(Music);
  Mix_CloseAudio;
for i:=0 to MaxSounds-1 do  Mix_FreeChunk(sounds[i]);
  SpriteEngine.Free;
  SDL_FreeSurface(Status);
  SDL_FreeSurface(vzriv);
  SDL_FreeSurface(HQExplosion);
  SDL_FreeSurface(VzrivTank);
  SDL_FreeSurface(SmallTank);
  SDL_FreeSurface(Titles);
  SDL_FreeSurface(Cursor);
  SDL_FreeSurface(Projectile);
  SDL_FreeSurface(RightPanel);
  SDL_FreeSurface(RightPanelMapEditor);
  SDL_FreeSurface(Sprites32);
  SDL_FreeSurface(TankSprites);
  SDL_FreeSurface(Background);
  TTF_Quit;
  SDL_Quit;
  IniFile:=TIniFile.Create(GetCurrentFolder+'\tanks.ini');
    IniFile.WriteInteger('HARDWARE','BitPerPixel(15_16_24_32)',Bpp);
    IniFile.WriteInteger('HARDWARE','SoundFrequencyHz(11025_22050_44100)',SoundFrequency);
    IniFile.WriteInteger('HARDWARE','SoundChannel(1-Mono_2-Stereo)',SoundChannel);
    IniFile.WriteInteger('HARDWARE','SoundBufferSize',SoundBufferSize);
    IniFile.WriteString('LANGUAGE','Menu1Game',MenuNames[1]);
    IniFile.WriteString('LANGUAGE','Menu2Map',MenuNames[2]);
    IniFile.WriteString('LANGUAGE','Menu3Options',MenuNames[3]);
    IniFile.WriteString('LANGUAGE','Menu4Best',MenuNames[4]);
    IniFile.WriteString('LANGUAGE','Menu5Credits',MenuNames[5]);
    IniFile.WriteString('LANGUAGE','Menu6Exit',MenuNames[6]);
    IniFile.WriteString('LANGUAGE','GroundName',strGround);
    IniFile.WriteString('LANGUAGE','WaterName',strWater);
    IniFile.WriteString('LANGUAGE','WallName',strWall);
    IniFile.WriteString('LANGUAGE','SteelName',strSteel);
    IniFile.WriteString('LANGUAGE','TextToClear',strClear);
    IniFile.WriteString('LANGUAGE','TextToSave',strSave);
    IniFile.WriteString('LANGUAGE','TextToLoad',strLoad);
    IniFile.WriteString('LANGUAGE','TextToPlay',strPlay);
    IniFile.WriteString('LANGUAGE','TextToParameters',strParams);
    IniFile.WriteString('LANGUAGE','TextToPlayers',strNumPlayers);
    IniFile.WriteString('LANGUAGE','TextToPlayers1',strNumPlayers1);
    IniFile.WriteString('LANGUAGE','TextToPlayers2',strNumPlayers2);
    IniFile.WriteString('LANGUAGE','TextToDifficulty',strDefficult);
    IniFile.WriteString('LANGUAGE','TextToDifficulty1level',strDefficult1);
    IniFile.WriteString('LANGUAGE','TextToDifficulty2Level',strDefficult2);
    IniFile.WriteString('LANGUAGE','TextToDifficulty3Level',strDefficult3);
    IniFile.WriteString('LANGUAGE','TextToDifficulty4Level',strDefficult4);
    IniFile.WriteString('LANGUAGE','TextToMusicInGame',strMusicInGame);
    IniFile.WriteString('LANGUAGE','TextToMusicInGameOff',strMusicInGameOff);
    IniFile.WriteString('LANGUAGE','TextToMusicInGameOn',strMusicInGameOn);
    IniFile.WriteString('LANGUAGE','TextToMusicVolume',strMusicVolume);
    IniFile.WriteString('LANGUAGE','TextToName1Player',strPlayer1Name);
    IniFile.WriteString('LANGUAGE','TextToName2Player',strPlayer2Name);
    IniFile.WriteString('LANGUAGE','TextToHallOfGlory',strHonor);
    IniFile.WriteString('LANGUAGE','TextToLevel',strLevel);
  IniFile.Free;
end.
