unit uDemoMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TfrmDemo =
class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private-Deklarationen }
FPaddle: TPanel;
FBall: TImage;
FTimer: TTimer;
procedure DoOnTimer(Sender: TObject);
function Collision(FirstRect, SecondRect: TRect): Boolean;
public
{ Public-Deklarationen }
end;
var
frmDemo: TfrmDemo;
implementation
{$R *.dfm}
const
STEP = 2;
{ TfrmDemo }
function TfrmDemo.Collision(FirstRect, SecondRect: TRect): Boolean;
var
OutRect: TRect;
begin
Result := IntersectRect(OutRect, FirstRect, SecondRect);
end;
procedure TfrmDemo.DoOnTimer(Sender: TObject);
var
PaddleRect, BallRect: TRect;
begin
FBall.Top := FBall.Top + STEP;
PaddleRect := Rect(FPaddle.Left, FPaddle.Top, FPaddle.Left + FPaddle.Width,
FPaddle.Top + FPaddle.Height);
BallRect := Rect(FBall.Left, FBall.Top, FBall.Left + FBall.Width,
FBall.Top + FBall.Height);
if Collision(PaddleRect, BallRect)
then
begin
FTimer.Enabled := false;
ShowMessage('
Kollision');
end;
end;
procedure TfrmDemo.FormCreate(Sender: TObject);
const
EDGELENGTH = 20;
var
FBitmap: TBitmap;
begin
//Paddel erzeugen
FPaddle := TPanel.Create(self);
FPaddle.Parent := self;
FPaddle.Top := ClientHeight - FPaddle.Height;
FPaddle.Left := (ClientWidth - FPaddle.Width)
div 2;
//Ball erzeugen
FBall := TImage.Create(self);
FBall.Parent := self;
FBall.Width := EDGELENGTH;
FBall.Height := EDGELENGTH;
FBall.Top := 0;
FBall.Left := (ClientWidth - FBall.Width)
div 2;
//Ball malen und darstellen
FBitmap := TBitmap.Create;
try
FBitmap.Width := EDGELENGTH;
FBitmap.Height := EDGELENGTH;
FBitmap.Canvas.Brush.Color := clRed;
FBitmap.Canvas.Ellipse(0, 0, FBitmap.Width, FBitmap.Height);
FBall.Picture.Assign(FBitmap);
finally
FBitmap.Free;
end;
//Timer erzeugen
FTimer := TTimer.Create(self);
FTimer.Interval := 20;
FTimer.OnTimer := DoOnTimer;
end;
end.