program rechteckschreiben;
uses crt, graph, maus;

type farbtyp = array[1..2250] of 0..15;

var Gd, Gm, x, y, i, j, d : Integer;
    farbe : farbtyp;
    felfeld : array[1..3] of pointtype;
    datei : file of farbtyp;
    inst, graf : boolean;
    but, xm, ym : word;
    xstr, ystr : string;

procedure tast;

  var ch : char;
  begin
    ch:=readkey;
    repeat
    until keypressed;
  end;

procedure rechteck(x1, y1, x2, y2: integer);

  var ind : word;
      ch : char;

  begin
    ind:=1;
    for i:=x1+1 to x2-1 do putpixel(i, y1, farbe[ind+i-x1-1]);
    ind:=ind+x2-x1-1;
    for i:=x1+1 to x2-1 do putpixel(i, y2, farbe[ind+i-x1-1]);
    ind:=ind+x2-x1-1;
    for i:=y1 to y2 do putpixel(x1, i, farbe[ind+i-y1]);
    ind:=ind+y2-y1+1;
    for i:=y1 to y2 do putpixel(x2, i, farbe[ind+i-y1]);
  end;

procedure linie(x1, y1, x2, y2: integer);

  var ind : word;

  begin
    ind:=1;
    for i:=y1 to y2 do putpixel(x1, i, farbe[ind+i-y1]);
    ind:=ind+y2-y1+1;
    for i:=y1 to y2 do putpixel(x2, i, farbe[ind+i-y1]);
  end;

procedure schreiben;
  begin
    x:=0;
    d:=0;
    assign(datei, 'd:\tp\tunnel.ppi');
    reset(datei);
    for j:=1 to 320 do
      begin
        if j mod 4 <> 1 then d:=d+1;
        read(datei, farbe);
        if j mod 4 <> 1
          then rechteck(320-j, 240-d, 320+j-1, 240+d)
          else linie(320-j, 240-d, 320+j-1, 240+d);
        setcolor(1);
        line(320-j-1, 240-d-1, 320-j-1, 240+d+1);
        line(320-j-1, 240-d-1, 320+j, 240-d-1);
        line(320+j, 240-d-1, 320+j, 240+d+1);
        if j<=100 then delay(80);
        if (j<200) and (j>100) then delay(50);
        if j>=200 then delay(20);
        if keypressed then readln;
      end;
    close(datei);
  end;

procedure bild;
  begin
    setcolor(3);                {Tunnelwand}
    setfillstyle(1, 3);
    bar(0, 0, 639, 479);
    setcolor(7);
    felfeld[1].x:=82;             {Straсe}
    felfeld[1].y:=479;
    felfeld[2].x:=320;
    felfeld[2].y:=240;
    felfeld[3].x:=560;
    felfeld[3].y:=479;
    setfillstyle(1, lightgray);
    fillpoly(3, felfeld);
    setcolor(white);
    setlinestyle(3, 10, 3);
    line(320, 479, 320, 240);
    setlinestyle(0, 0, 0);
    setcolor(1);
    line(0, 479, 639, 0);
    line(639, 479, 0, 0);
  end;

begin
  Gd := Detect; InitGraph(Gd, Gm, 'd:\tp\bgi');
  bild;
  schreiben;
  readln;
  {inst:=true;
  but:=3;
  graf:=true;
  init_maus(inst, but, graf);
  select_maus(4);
  show_maus;
  repeat
    maus_status(xm, ym, but);
    str(xm, xstr); str(ym, ystr);
    setcolor(0);
    outtextxy(320, 0, 'ллллллллллллл');
    setcolor(15);
    outtextxy(320, 0, xstr+' '+ystr);
    delay(10);
  until keypressed;}
  closegraph;
end.
