unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 =
class(TForm)
Timer1: TTimer;
Label1: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMeineShapes =
class(TShape)
public
Richtung: TPoint;
PositionSet:
Set of 1..2;
constructor Create(Owner: TComponent);
override;
procedure MovingBall(Speed: Integer; AndererKreis: TMeineShapes);
end;
var
Kreis: TMeineShapes;
Kreis1: TMeineShapes;
Form1: TForm1;
implementation
{$R *.dfm}
constructor TMeineShapes.Create(Owner: TComponent);
begin
inherited create(Owner);
Richtung.X := 0;
Richtung.Y := 0;
Height := 65;
Randomize;
Left := 50 + Random(200);
Top := 50 + Random (100);
Width := Height;
Brush.Color := clYellow;
Pen.Width := 3;
Shape := stCircle;
end;
procedure TMeineShapes.MovingBall(Speed: Integer; AndererKreis: TMeineShapes);
var
PlusMinus: Integer;
i, j, c, d : Integer;
begin
if Richtung.Y = 0
then
begin
Randomize;
PlusMinus := Random (1);
if PlusMinus = 1
then
Richtung.Y := 1
else
Richtung.Y := - 1;
end;
if Richtung.X = 0
then
begin
Randomize;
PlusMinus := Random (1);
if PlusMinus = 1
then
Richtung.X := 1
else
Richtung.X := - 1;
end;
for i := Left
to BoundsRect.Right
do
for j := AndererKreis.Left
to AndererKreis.BoundsRect.Right
do
if i = j
then
begin
for c := Top
to BoundsRect.Bottom
do
for d := AndererKreis.Top
to AndererKreis.BoundsRect.Bottom
do
if c = d
then
if Richtung.X = AndererKreis.Richtung.X
then
Richtung.X := Richtung.X * -1
else
begin
Richtung.X := Richtung.X * -1;
AndererKreis.Richtung.Y := AndererKreis.Richtung.Y * -1;
end;
end;
Top := Top + Richtung.Y * Speed;
Left := Left + Richtung.X * Speed;
if ((BoundsRect.Bottom) >= Parent.ClientHeight)
or ((Top) <= 0)
then
Richtung.Y := Richtung.Y * -1;
if ((BoundsRect.Right) >= Parent.ClientWidth)
or ((Left) <= 0)
then
Richtung.X := Richtung.X * -1;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Kreis.MovingBall(4, Kreis1);
Kreis1.MovingBall(7, Kreis);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Kreis := TMeineShapes.Create(Self);
Kreis.Parent := Self;
Kreis1 := TMeineShapes.Create(Self);
Kreis1.Parent := Self;
Kreis1.Brush.Color := clRed;
end;
end.