unit Unit1; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, Math;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Shape1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Start(Sender: TObject);
    procedure ZufallsPosition;
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form1: TForm1; 
  Ball: array [1..5] of TShape;
  Rechts: array [1..5] of Boolean;
  Oben: array [1..5] of Boolean;
  Zaehler: integer;
  Treffer: Integer;
  Geschwindigkeit: Integer;
  StartZeit: Double;


implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Start(Sender: TObject);
begin
  Label4.Caption:= FloatToStr(RoundTo((GetTickCount - StartZeit) / 1000, -1)) + ' s';
  for Zaehler := 1 to 5 do
  begin
    if Oben[Zaehler] = True then
    begin
       Ball[Zaehler].Top := Ball[Zaehler].Top - Geschwindigkeit;
    end
    else
    begin
       Ball[Zaehler].Top := Ball[Zaehler].Top + Geschwindigkeit;
    end;

    if Rechts[Zaehler] = True then
    begin
       Ball[Zaehler].Left := Ball[Zaehler].Left + Geschwindigkeit;
    end
    else
    begin
       Ball[Zaehler].Left := Ball[Zaehler].Left - Geschwindigkeit;
    end;

    // Ball hat obere Begrenzung nicht erreicht
    if Ball[Zaehler].Top <= 40 then
    begin
      Oben[Zaehler] := False;
      Ball[Zaehler].Left := Random(Form1.Width - Ball[Zaehler].Width);
    end;

    // Ball erreicht untere Grenze der Form (Form1.Height)
    // obere Kante (top) + Höhe des Balls (Height)
    if Ball[Zaehler].Top + Ball[Zaehler].Height >= Form1.Height then
    begin
      Oben[Zaehler] := True;
      Ball[Zaehler].Left := Random(Form1.Width - Ball[Zaehler].Width);
    end;

    // linke Kante des Balls (Left) ist <= 0 -> linke Grenze der Form
    if Ball[Zaehler].Left <= 0 then
    begin
      Rechts[Zaehler] := True;
      Ball[Zaehler].Top := Random(Form1.Height - Ball[Zaehler].Height);
    end;

    // linke Kante des Balls (Left) + Breite des Balls (Width)
    // >= Breite der Form (Form1.Width
    if Ball[Zaehler].Left + Ball[Zaehler].Width >= Form1.Width then
    begin
      Rechts[Zaehler] := False;
      Ball[Zaehler].Top := Random(Form1.Height - Ball[Zaehler].Height);
    end;
  end;
end;

procedure TForm1.ZufallsPosition;
begin
  Randomize;
  for Zaehler := 1 to 5 do
  begin
    // verhindern, dass ein Ball oberhalb der Linie auftaucht
    while Ball[Zaehler].Top < Shape6.Top do
    begin
       Ball[Zaehler].Top := Random(Form1.Height - Ball[Zaehler].Width);
    end;
    Ball[Zaehler].Left := Random(Form1.Width - Ball[Zaehler].Width);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Treffer:= 0;
  Ball[1] := Shape1;
  Ball[2] := Shape2;
  Ball[3] := Shape3;
  Ball[4] := Shape4;
  Ball[5] := Shape5;
  Geschwindigkeit:= 1;
  Label1.caption:= 'Treffer: ' + IntToStr(Treffer);
  Label2.Caption:= 'Geschwindigkeit: ' + IntToStr(Geschwindigkeit);
  StartZeit := GetTickCount;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  inc(Geschwindigkeit);
  Label2.Caption:= 'Geschwindigkeit: ' + IntToStr(Geschwindigkeit);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if Geschwindigkeit > 1 then dec(Geschwindigkeit);
  Label2.Caption:= 'Geschwindigkeit: ' + IntToStr(Geschwindigkeit);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Timer1.Enabled:= false;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Timer1.Enabled:= true;
  StartZeit := GetTickCount;
  ZufallsPosition;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  if Timer1.Interval > 1 then Timer1.Interval:= Timer1.Interval - 1;
  Label5.Caption:= 'Zeitintervall:  ' + IntToStr(Timer1.Interval);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  Timer1.Interval:= Timer1.Interval + 1 ;
  Label5.Caption:= 'Zeitintervall:  ' + IntToStr(Timer1.Interval);
end;

procedure TForm1.FormClick(Sender: TObject);
begin
   ZufallsPosition;
end;

procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  inc(Treffer);
  Label1.caption:= 'Treffer: ' + IntToStr(Treffer);
  ZufallsPosition;
end;


end.

