unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 =
class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormDblClick(Sender: TObject);
procedure TimeTimer(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private-Deklarationen }
SpeziesR, SpeziesB:
Array of TShape;
procedure Move;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
Max_Spezies = 10;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
Randomize;
Timer1.Enabled := FALSE;
Self.DoubleBuffered := TRUE;
SetLength(SpeziesR, Max_Spezies);
SetLength(SpeziesB, Max_Spezies);
for i := 0
to Max_Spezies - 1
do
begin
SpeziesR[i]:= TShape.Create(Self);
SpeziesR[i].Parent := Self;
SpeziesR[i].Shape := stCircle;
SpeziesR[i].Width := 30;
SpeziesR[i].Height := 30;
SpeziesR[i].Brush.Color := clRed;
SpeziesR[i].Left := Random(470);
SpeziesR[i].Top := Random(470);
SpeziesR[i].Visible := TRUE;
SpeziesB[i]:= TShape.Create(Self);
SpeziesB[i].Parent := Self;
SpeziesB[i].Shape := stCircle;
SpeziesB[i].Width := 30;
SpeziesB[i].Height := 30;
SpeziesB[i].Brush.Color := clBlue;
SpeziesB[i].Left:= Random(470);
SpeziesB[i].Top := Random(470);
SpeziesB[i].Visible := TRUE;
end;
Timer1.Enabled := TRUE;
Self.Caption := format('
Timer: ms = %d, Enabled = %d', [Timer1.Interval, INTEGER(Timer1.Enabled)]);
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for i := 0
to Max_Spezies - 1
do
begin
if Assigned(SpeziesR[i])
then SpeziesR[i].Free;
if Assigned(SpeziesB[i])
then SpeziesB[i].Free;
end;
end;
procedure TForm1.FormDblClick(Sender: TObject);
begin
Timer1.Enabled :=
not Timer1.Enabled;
Self.Caption := format('
Timer: ms = %d, Enabled = %d', [Timer1.Interval, INTEGER(Timer1.Enabled)]);
end;
procedure TForm1.Move;
var
i, Direction: Integer;
begin
for i := 0
to Max_Spezies - 1
do
begin
Direction := Random(5);
// Random(4) gibt ZufallsZahlen von 0..3
// "Random returns a random number within the range
// 0 <= X < Range. If Range is not specified, the
// result is a real-type random number within the range
// 0 <= X < 1."
case Direction
of
1 : SpeziesR[i].Left := SpeziesR[i].Left -1;
2 : SpeziesR[i].Top:= SpeziesR[i].Top -1;
3 : SpeziesR[i].Left := SpeziesR[i].Left +1;
4 : SpeziesR[i].Top := SpeziesR[i].Top +1;
end;
Direction := Random(5);
case Direction
of
1 : SpeziesB[i].Left := SpeziesB[i].Left -1;
2 : SpeziesB[i].Top:= SpeziesB[i].Top -1;
3 : SpeziesB[i].Left := SpeziesB[i].Left +1;
4 : SpeziesB[i].Top := SpeziesB[i].Top +1;
end;
end;
end;
procedure TForm1.TimeTimer(Sender: TObject);
begin
Move;
end;
end.