Program    Line_Demo__________________________________________Bouncing_Lines;

(*$I A:HRG/TOOL.800*)


Type       LineDescr = Array (.1..4.) of Integer;

Var        handle : Integer;
           ch     : Char;


  Procedure  Animation; (* Moving line segments *)

  Const      maxL  = 4000;     min     =  1;
             _rnd  =    8;     _switch = 99;
             _sub  =    4;

  Var        Ball, Vel, max        : LineDescr;
             maxX, maxY            : Integer;
             Lines                 : Array (.0..maxL.) of LineDescr;
             index, t, i, new, old : Integer;
             maxLines              : Integer;
             doClear               : Boolean;

  Begin
    Repeat ClrScr;
           Box(132, 100, 406, 180, 1);
           Box(136, 102, 402, 178, 2);
           Box(140, 104, 398, 176, 3);
           GotoXY(20,16); Write('***   WANDERNDE LINIEN   ***');
           GotoXY(20,17); Write('----------------------------');
           GotoXY(20,20); Write('Anzahl der Linien (75) : '); Readln(maxLines);
           If maxLines < min
            then maxLines := 10;
           If maxLines > maxL
            then maxLines := maxL;

           ClrScr; Box(0, 0, 639, 191, 1);

           maxX := 638; maxY := 190;

           max(.1.) := maxX;  max(.2.) := maxY;
           max(.3.) := maxX;  max(.4.) := maxY;

           Vel(.1.) := Random(_rnd) - _sub;  Vel(.2.) := Random(_rnd) - _sub;
           Vel(.3.) := Random(_rnd) - _sub;  Vel(.4.) := Random(_rnd) - _sub;

           Ball(.1.) := 600; Ball(.2.) :=  75;
           Ball(.3.) := 600; Ball(.4.) := 125;

           new := 0; old := 0; doClear := FALSE;

           Repeat For i := 1 to 4 do
                   Begin t := Ball(.i.) + Vel(.i.);
                         If t > max(.i.)
                          then Begin t        := max(.i.);
                                     Vel(.i.) := - Vel(.i.)
                               End;
                         If t < min
                          then Begin t        := min;
                                     Vel(.i.) := - Vel(.i.)
                               End;
                         Ball(.i.) := t
                   End;
                  If Abs(Ball(.1.) - Ball(.3.)) > 200
                   then Begin Vel(.1.) := - Vel(.1.);
                              Vel(.3.) := - Vel(.3.)
                        End;
                  If doClear                             (* undraw old line *)
                   then Begin Line(Lines(.old.)(.1.), Lines(.old.)(.2.),
                                     Lines(.old.)(.3.), Lines(.old.)(.4.), 4);
                              If old = maxLines
                               then old := 0
                               else old := Succ(old);
                              If Random(_switch) = 5
                               then For i := 1 to 4 do
                                     Vel(.i.) := Random(_rnd) - _sub
                        End
                   else doClear := doClear or (new = maxLines);
                  Lines(.new.) := Ball;                    (* draw new line *)
                  If new = maxLines
                   then new := 0
                   else new := Succ(new);
                  Line(Ball(.1.), Ball(.2.), Ball(.3.), Ball(.4.), 4);
                  If KeyPressed
                   then Read(KBD, ch)
                   else ch := ^@;
                  If ch = ^S
                   then Begin While not (ch in (.^C, ^I, ^Q, ^P, ^R.)) do
                               Read(KBD, ch);
                              Case ch of ^I : FillBox(0, 0, 639, 191, 4);
                                         ^P : ScrLst
                               End
                        End
           Until (ch = ^C) or (ch = ^R)
    Until ch = ^C;
  End;


Begin
  HRG_Tool_Init; Pal(0, 3); Pal(3, 15); Animation; ClrScr
End.
