unit uKollision;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm4 =
class(TForm)
Shape1: TShape;
MoveObj: TShape;
Shape3: TShape;
Timer1: TTimer;
Shape2: TShape;
Shape4: TShape;
procedure Timer1Timer(Sender: TObject);
private
{ Private-Deklarationen }
FXDir, FYDir: Integer;
public
{ Public-Deklarationen }
end;
var
Form4: TForm4;
implementation
{$R *.dfm}
Procedure CheckMove(MoveObj:TControl;
var FXDir,FYDir:Integer);
var
Parent:TWinControl;
C:Tcontrol;
i:Integer;
rdummy,rx,ry:TRect;
begin
Parent := MoveObj.Parent;
rx := MoveObj.BoundsRect;
ry := rx;
OffsetRect(rx,FXDir,0);
OffsetRect(ry,0,FYDir);
for I := 0
to Parent.ControlCount - 1
do
begin
C := Parent.Controls[i];
if C <> MoveObj
then
begin
If IntersectRect(rdummy,rx,C.BoundsRect)
then
begin
FXDir := 0;
end;
If IntersectRect(rdummy,ry,C.BoundsRect)
then
begin
FYDir := 0;
end;
end;
end;
end;
procedure TForm4.Timer1Timer(Sender: TObject);
Const
C_Speed = 2;
var
L, R, U, D: Boolean;
begin
L := GetAsyncKeyState(VK_Left) < 0;
R := GetAsyncKeyState(VK_Right) < 0;
U := GetAsyncKeyState(VK_UP) < 0;
D := GetAsyncKeyState(VK_DOWN) < 0;
if (L
OR R)
and not(L
AND R)
then
begin
if L
then
FXDir := -C_Speed
else
FXDir := C_Speed
end
else
FXDir := 0;
if (U
OR D)
and not(U
AND D)
then
begin
if U
then
FYDir := -C_Speed
else
FYDir := C_Speed
end
else
FYDir := 0;
CheckMove(MoveObj,FXDir,FYDir);
MoveObj.Left := MoveObj.Left + FXDir;
MoveObj.Top := MoveObj.Top + FYDir;
end;
end.