unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Ball: TShape;
Timer1: TTimer;
Label1: TLabel;
Timer2: TTimer;
Label2: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
RichtungY: Integer;
RichtungX: Integer;
MyShape: TShape;
procedure MovingBallX(Objekt: TShape; Speed: Integer);
//procedure MovingBall(Speed: Integer);
procedure MovingBallY(Objekt: TShape; Speed: Integer);
{ Private declarations }
public
{ Public declarations }
end;
type
TMyShapes = class(TShape)
Kreis: TMyShapes;
public
RichtingX: Integer;
RichtungY: Integer;
constructor Create(Owner: TComponent);
procedure Erstellen;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TMyShapes.Create(Owner: TComponent);
begin
inherited Create(Owner);
end;
procedure TMyShapes.Erstellen;
begin
Kreis := Kreis.Create(Form1);
with Kreis do
begin
Parent := Form1;
Height := 65;
Left := 100;
Top := 20;
Width := Kreis.Height;
Brush.Color := clYellow;
Pen.Width := 3;
Shape := stCircle;
end;
end;
procedure TForm1.MovingBallY(Objekt: TShape; Speed: Integer);
begin
if (RichtungY <> 1) and (RichtungY <> -1) then
RichtungY := 1;
if (Objekt.Top + 65) >= Form1.ClientHeight then
begin
Objekt.Top := Objekt.Top - Speed;
RichtungY := -1;
Exit;
end;
if Objekt.Top <= 0 then
begin
Objekt.Top := Objekt.Top + Speed;
RichtungY := 1;
Exit;
end;
if (Objekt.Top > 0) and ((Objekt.Top + 65) < Form1.ClientHeight) then
if RichtungY = 1 then
begin
Objekt.Top := Objekt.Top + Speed;
Exit;
end
else
begin
Objekt.Top := Objekt.Top - Speed;
Exit;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label1.Caption := IntToStr(Ball.Left);
Label2.Caption := IntToStr(Ball.Top);
MovingBallX(Ball, 5);
MovingBallX(MyShape, 3);
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
MovingBallY(Ball, 5);
MovingBallY(MyShape, 4);
end;
procedure TForm1.MovingBallX(Objekt: TShape; Speed: Integer);
begin
if (RichtungX <> 1) and (RichtungX <> -1) then
RichtungX := 1;
if (Objekt.Left + 65) >= Form1.ClientWidth then
begin
Objekt.Left := Objekt.Left - Speed;
RichtungX := -1;
Exit;
end;
if Objekt.Left <= 0 then
begin
Objekt.Left := Objekt.Left + Speed;
RichtungX := 1;
Exit;
end;
if (Objekt.Left > 0) and ((Objekt.Left + 65) < Form1.ClientWidth) then
if RichtungX = 1 then
begin
Objekt.Left := Objekt.Left + Speed;
Exit;
end
else
begin
Objekt.Left := Objekt.Left - Speed;
Exit;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyShape := TShape.Create(Form1);
MyShape.Parent := Form1;
MyShape.Height := 65;
MyShape.Left := 100;
MyShape.Top := 20;
MyShape.Width := MyShape.Height;
MyShape.Brush.Color := clMoneyGreen;
MyShape.Pen.Width := 3;
MyShape.Shape := stCircle;
end;
end.