uses
  graphabc, abcobjects, abcsprites, timers;

var
  percent, percent2, sob: integer;
var texet:textabc;
var mass:array of integer;
var
  biggestbool := true;
var mus:system.Media.SoundPlayer;
var secs:timer;

var
  bullets3: array of circleabc;

var
  tyu := true;

var
  cappic: spriteabc;

var
  tim: timer;

var
  y: timer;

var
  bullets1: array of circleabc;

var
  bullets2: array of circleabc;

var
  y2: timer;
  speed: integer;
//var dfield: RectangleABC;
var
  ship: spriteabc;

var
  bmtim: timer;

var
  expcount: integer; 
  bossmovetim: timer;

var
  speedpic: spriteabc;

var
  grbool := true;

var
  field: rectangleabc;

var
  exp: system.Media.SoundPlayer;

var
  enemy1, enemy2: spriteabc;

var
  zap: System.Media.soundplayer;

var
  countsp: integer;

var
  bpic: spriteabc;

var
  boss: system.Media.soundplayer;

procedure gameover();
begin
  sleep(1000);
  var fon:=new PictureABC(0,0,'textures\interface\90.png');
  ship.Destroy;
  //ship2.Destroy;
  
  //field.destroy;
  //boss.destroy;
  
  
  
  
end;

procedure sec();
begin
texet.Text:=inttostr(strtoint(texet.Text)+1);
end;




procedure explosion();
begin
  if expcount = 3 then
  begin
  if biggestbool then
    exp.Play;
    var x, y: integer;
    x := ship.Position.X;
    y := ship.Position.y;
    ship.Destroy;
    ship := new SpriteABC(x, y, 'textures\ship\ship3.png');
    grbool := false;
    gameover();
  end;
  if expcount = 2 then
  begin
    //ship.Add('textures\ship2.jpg');
    // ship.Add('textures\ship3.jpg');
    var x, y: integer;
    if biggestbool then
    exp.Play;
    x := ship.Position.X;
    y := ship.Position.y;
    ship.Destroy;
    ship := new SpriteABC(x, y, 'textures\ship\ship2.png');
    expcount := 3;
    //ship.Frame:=2; 
  end;
  
  
  
  
  
  
end;



procedure shooting1();
begin
  bullets1[0].MoveOn(0, 10);
  bullets1[1].MoveOn(0, 10);
  bullets1[2].MoveOn(0, 10);
  
  if bullets1[0].center.Y >= 700 then
  begin
    bullets1[0].destroy;
    bullets1[1].destroy;
    bullets1[2].destroy;
    percent := 0
  end;
  
  for var e := 0 to 2 do
  begin
    if bullets1[e].Intersect(ship) then
    begin
      y.Stop;
      bullets1[0].destroy;
      bullets1[1].destroy;
      bullets1[2].destroy;
      percent := 0;
      explosion();
    end;  
  end;
  
  
end;

procedure shooting2();
begin
  bullets2[0].MoveOn(0, 10);
  bullets2[1].MoveOn(0, 10);
  bullets2[2].MoveOn(0, 10);
  if bullets2[0].center.Y >= 700 then
  begin
    bullets2[0].destroy;
    bullets2[1].destroy;
    bullets2[2].destroy;
    percent2 := 0
  end;
  
  for var e := 0 to 2 do
  begin
    if bullets2[e].Intersect(ship) then
    begin
      y2.stop;
      bullets2[0].destroy;
      bullets2[1].destroy;
      bullets2[2].destroy;
      percent2 := 0;
      explosion();
    end;  
  end;
  
  
end;



procedure enemymove();
begin
  enemy1.MoveOn(2, 0);
  enemy2.MoveOn(-2, 0);
  if enemy1.Position.X >= 565 then
  begin
    percent := random(0, 5);
    enemy1.MoveTo(random(-300, -100), 20);
    
  end;
  
  if enemy2.Position.X <= -100 then
  begin
    percent2 := random(0, 5);
    enemy2.MoveTo(random(565, 765), 80);
  end;
  
  
  
  if (enemy1.Center.X >= 50) and (percent <> -100) then
  begin
    sob := random(101);
    //sob := 6;
   
    
    if percent > sob then
    begin
     if biggestbool then
      zap.Play;
      zap := new System.Media.SoundPlayer('sounds\zapusk.wav');
      bullets1 := new CircleABC[3];
      bullets1[0] := new CircleABC(enemy1.center.x, enemy1.Center.Y, 5, clred);
      bullets1[1] := new CircleABC(enemy1.center.x, enemy1.Center.Y + 25, 5, clred);
      bullets1[2] := new CircleABC(enemy1.center.x, enemy1.Center.Y + 50, 5, clred);
      y := new Timer(50, shooting1);
      y.Start;
      // var r:circleabc;
      // r:=new CircleABC(100,100,80,clred);
      // r.Number:=percent;
      percent := -100;
    end
    else
      percent := percent + 1;
  end;
  
  if (enemy2.Center.X <= 530) and (percent2 <> -100) then
  begin
    sob := random(100);
    if percent2 > sob then
    begin
      zap := new System.Media.SoundPlayer('sounds\zapusk.wav');
      if biggestbool then
      zap.Play;
      bullets2 := new CircleABC[3];
      bullets2[0] := new CircleABC(enemy2.center.x, enemy2.Center.Y, 5, clgreen);
      bullets2[1] := new CircleABC(enemy2.center.x, enemy2.Center.Y + 25, 5, clgreen);
      bullets2[2] := new CircleABC(enemy2.center.x, enemy2.Center.Y + 50, 5, clgreen);
      // var r:circleabc;
      // r:=new CircleABC(100,100,80,clred);
      // r.Number:=percent;
      y2 := new Timer(50, shooting2);
      y2.Start;
      percent2 := -100;
    end
    else
      percent2 := percent2 + 1;
  end;
  
  
  
end;






procedure enemy();
begin
  percent := random(0, 5);
  percent2 := random(0, 5);
  tim := new timer(30, enemymove);
  tim.start;
  
  
  //enemy2:=new 
  
  
end;








procedure sh2();
begin
  bullets3[0].MoveOn(0, 3);
  bullets3[1].MoveOn(0, 3);
  bullets3[2].MoveOn(0, 3);
  
  if bullets3[0].center.Y >= 700 then
  begin
    bullets3[0].destroy;
    bullets3[1].destroy;
    bullets3[2].destroy;
    //percent := 0;
    tyu := true;
  end;
  
  for var e := 0 to 2 do
  begin
    if bullets3[e].Intersect(ship) then
    begin
    //bossmovetim.stop;
    bmtim.Stop;
    bpic.Destroy;
    enemy();
      bossmovetim.Stop;
      bullets3[0].destroy;
      bullets3[1].destroy;
      bullets3[2].destroy;
      tyu := true;
      //percent := 0;
      explosion();
    end;  
  end;
  
end;


procedure bossmove();
begin
  bpic.moveto(random(15, 565 - 200), 15);
  //bullets3[0].destroy;
  //bullets3[1].destroy;
  //bullets3[2].destroy;
  if tyu then
  begin
    tyu := false;
    bullets3[0] := new CircleABC(bpic.center.x, bpic.Center.Y, 5, clorange);
    bullets3[1] := new CircleABC(bpic.center.x, bpic.Center.Y + 25, 5, clorange);
    bullets3[2] := new CircleABC(bpic.center.x, bpic.Center.Y + 50, 5, clorange);
    bossmovetim := new Timer(20, sh2);
    bossmovetim.Start();
  end;
end;



procedure bossappear();
begin
  //window.Close();
  enemy1.moveto(-100, 15);
  enemy2.moveto(-100, 15);
  zap.stop;
  tim.Stop;
  boss := new System.Media.soundplayer('sounds\boss(1).wav');
  if biggestbool then
  boss.Play;
  
  //sob:=
  
  cappic := new SpriteABC(15, 100, 'textures\cap\cap1.png');
  sleep(400);
  cappic.Destroy;
  cappic := new SpriteABC(15, 100, 'textures\cap\cap2.png');
  sleep(400);
  cappic.Destroy;
  cappic := new SpriteABC(15, 100, 'textures\cap\cap3.png');
  sleep(400);
  cappic.Destroy;
  boss.Stop;
  bpic := new SpriteABC(100, 15, 'textures\cap\228.png');
  bullets3 := new CircleABC[3];
  bmtim := new Timer(800, bossmove);
  bmtim.Start;
  
end;











var ert:=true;

procedure events(key: integer);
begin
  if grbool then
  begin
    if (key = vk_e) and (countsp < 4) then
    begin
      speedpic.destroy;
      inc(countsp);
      speedpic := new SpriteABC(570, 460, 'textures\speed\' + inttostr(countsp) + '.png');
      speed := speed + 2;
       if (sob > 30) and (sob < 70) and(ert )then
    begin
      ert:=false;
      bossappear();
    end;
    end;
    
    if (key = vk_q) and (countsp > 1) then
    begin
      speedpic.destroy;
      dec(countsp);
      speed := speed - 2;
      speedpic := new SpriteABC(570, 460, 'textures\speed\' + inttostr(countsp) + '.png');
    end;
    
    if (key = vk_a) and (ship.Position.X > 15) then
    begin
      ship.moveon(-speed, 0);
    end;
    if (key = vk_d) and (ship.Position.X + 200 < 565) then
    begin
      ship.moveon(speed, 0);
    end;
    if (key = vk_w) and (ship.Position.y  > 520) then
    begin
      ship.moveon(0, -speed);
    end;
    if (key = vk_s) and (ship.Position.y + 100  < 680) then
    begin
      ship.moveon(0, speed);
    end;
    
    if key = vk_delete then
      explosion();
  end;
end;







procedure drawning();
begin
  var fon: pictureabc;
  
  
  
  field := new RectangleABC(15, 15, 550, 670, cllightyellow);
  //line(15,550,465,550,clblack);
  
  countsp := 1;
  enemy1 := new SpriteABC(-100, 20, 'textures\en\enemy1.png');
  enemy2 := new SpriteABC(565, 80, 'textures\en\enemy2.png');
  
  ship := new SpriteABC(200, 550, 'textures\ship\ship1.png');
  fon := new Pictureabc(0, 0, 'textures\fn.png');
  ship.Active := false;
  
  speedpic := new SpriteABC(570, 460, 'textures\speed\1.png');
  var re:pictureabc;
  re:=new PictureABC(570,30,'textures\interface\ad.png');
  texet:=new Textabc(640,228,30,'0',clwhite);
  secs:=new Timer(1000,sec);
  secs.Start;
  //680
  //p.
  line(15, 550, 465, 550, clblack);
  line(15, 550, 465, 550, clblack);
  speed := 1;
  expcount := 2;
  onkeydown := events;
  enemy();
  
end;




var
  bjkl := true;

var
  poi := false;

var
  poi2 := false;

var
  reg := true;

procedure k(x, y, onmb: integer);
label lab;
begin
  if bjkl then
  begin
    var fon: pictureabc;
    if(x > 333) and (x < 707) and (y > 231) and (y < 298) then
    begin
      drawning();
      bjkl := false;
      poi:=false;
      mus.Stop;
      poi2:=false;
    end;
    //game
    if(x > 426) and (x < 711) and (y > 305) and (y < 361) then
    //options
    begin
    if reg then
    begin
      fon := new PictureABC(0, 0, 'textures\interface\2.png');
      end
      else
      fon := new PictureABC(0, 0, 'textures\interface\3.png');
      poi := true;
      bjkl := false;
      poi2:=true;
    end;
    if(x > 459) and (x < 710) and (y > 385) and (y < 450) then
    begin
      fon := new PictureABC(0, 0, 'textures\interface\4.png');
      bjkl := false;
      poi2:=true;
    end;
    //help
    if(x > 510) and (x < 714) and (y > 463) and (y < 519) then
    //plot
    begin
      fon := new PictureABC(0, 0, 'textures\interface\5.png');
      bjkl := false;
      poi2:=true;
    end;
    if(x > 500) and (x < 711) and (y > 537) and (y < 593) then
      window.Close;
    
    
  end;
  if poi  then
  begin
    var fon: pictureabc;
    if(x > 560) and (x < 708) and (y > 168) and (y < 207) then
    begin
      if reg then
      begin
        fon := new PictureABC(0, 0, 'textures\interface\3.png');
        biggestbool := false;
        mus.Stop;
        reg := false;
         goto lab;
      end;
      if reg = false then
      begin
        fon := new PictureABC(0, 0, 'textures\interface\2.png');
        biggestbool := true;
        mus.play;
        reg := true;
        goto lab;
      end;
      lab:
    end;
    
    
  end;
  
  if(x>586) and(x<719) and(y>619) and(y<666)and(poi2) then
  begin
  var fon: pictureabc;
  poi2:=false;
   fon := new PictureABC(0, 0, 'textures\interface\1.png');
   bjkl:=true;
   poi:=false;
  end;
  
  
  
end;

procedure startingthegame();
begin
  var fon: pictureabc;
  fon := new PictureABC(0, 0, 'textures\interface\1.png');
  onmousedown := k;// drawning();
  
end;






begin
   
  mass:=new integer[7];
  
  exp := new System.Media.SoundPlayer('sounds\explosion.wav');
  mus:=new System.Media.SoundPlayer('sounds\01.wav');
  mus.Play;


  window.height := 700;//680
  window.Width := 750;//710
  window.Title := 'ship_fight2001';
  window.Top := 0;
  window.left := 0;
  window.IsFixedSize := true;
  startingthegame();
  
  
  
  
  
  
  
end.