uses crt,graph;
const N=500;
var
 key:char;
 GrDriver,GrMode:integer;
 i,x1,y1:integer;
 st:boolean;
 x,y:array[1..n] of integer;
procedure Zdania;
begin
  {Труба}
  setColor(8);
  setFillStyle(11,8);
  SetLineStyle(0,3,3);
  line(200,250,200,298);
  line(210,250,210,290);
  line(210,250,205,240);
  line(200,250,205,240);

  setColor(1);
  setLineStyle(0,3,3);
  setFillStyle(11,8);
  Bar3D(200, 300, 400, 480, 2, TopOff);
  line(185,315,300,200);
  line(420,315,300,200);

  SetColor(3);
  circle(300,250,10);
  PutPixel(200,250,4);
  setColor(1);
  setLineStyle(0,3,4);
  rectangle(0,-2,GetMaxX,GetMaxY);
end;

begin
 randomize;
 initGraph(GrDriver,GrMode,'');
  zdania;
  for i:= 1 to n do
  begin
    X[i]:=random(GetMaxX);
    Y[i]:=random(GetMaxY);
  end;
  repeat
    for i:= 1 to n do
    begin
     if y[i]<GetMaxY then
     begin

     st:=false;
     x1:=x[i];
     y1:=y[i];
      if getPixel(x[i],y[i]+1)=0 then begin inc(Y[i],1) end
      else
      begin
        if GetPixel(x[i]+1,y[i]+1)=0 then
        begin
          inc(x[i]);
          inc(y[i]);
        end
        else
        begin
          if GetPixel(x[i]-1,y[i]+1)=0 then
          begin
            dec(x[i]);
            inc(y[i]);
          end
          else
          begin
            x[i]:=random(GetMaxX);
            y[i]:=0;
            st:=true;
          end;
        end;
      end;
      if not st then PutPixel(x1,y1,0);
      putPixel(x[i],y[i],15);
     end
     else
     begin PutPixel(x[i],y[i],15); y[i]:=0; x[i]:=random(GetMaxX); end;
    end;
  until KeyPressed;


  readKey;


  repeat
    for i:= 1 to n do
    begin
     if y[i]<GetMaxY then
     begin
     st:=false;
     x1:=x[i];
     y1:=y[i];
      if getPixel(x[i],y[i]+1)=0 then begin inc(Y[i],1) end
      else
       begin
        if GetPixel(x[i],y[i]+1)=15 then
        begin
          inc(y[i],1);
          PutPixel(x[i],y[i],0);
          y[i]:=0;
          x[i]:=random(GetMaxX);
        end
        else
          if GetPixel(x[i]+1,y[i]+1)=15 then
          begin
            inc(x[i]); inc(y[i]);
            PutPixel(x[i],y[i],0);
            x[i]:=random(GetMaxX); y[i]:=0;
          end
          else
           if GetPixel(x[i]-1,y[i]+1)=15 then
           begin
             dec(x[i]); inc(y[i]);
             PutPixel(x[i],y[i],0);
             x[i]:=random(GetMaxX); y[i]:=0;
           end
        else
         begin
           putPixel(x[i],y[i],0);
           x[i]:=random(GetMaxX);
           y[i]:=0;
         end;
        end;
     end
     else
     begin
       x[i]:=random(GetMaxX);
       y[i]:=0;
     end;
    end;
  until KeyPressed;

 closeGraph;
end.