unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) Button1: TButton
Button2: TButton
Shape1: TShape
Shape2: TShape
Shape3: TShape
procedure Button1Click(Sender: TObject)
procedure Button2Click(Sender: TObject)
procedure FormCreate(Sender: TObject)
procedure FormClose(Sender: TObject
var Action: TCloseAction)
private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses Unit_Move;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin TBounceThread.Create(False, Shape2, 2, 3);end;procedure TForm1.Button2Click(Sender: TObject);begin TBounceThread.Create(False, Shape3, 2, 3);end;procedure TForm1.FormCreate(Sender: TObject);begin InitializeCriticalSection(CriticalSection);end;procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);begin DeleteCriticalSection(CriticalSection);end;end.unit Unit_Move;interfaceuses WinProcs, Classes, Graphics, ExtCtrls;type TBounceThread = class(TThread) private FShape: TShape
FXSpeed: Integer
FYSpeed: Integer
procedure MoveShape
protected procedure Execute
override
public constructor Create(Suspended: Boolean
Shape: TShape
XSpeed, YSpeed: Integer)
property Shape: TShape read FShape
destructor Destroy
override
end;var CriticalSection: TRTLCriticalSection;implementationprocedure TBounceThread.MoveShape;begin with FShape do begin Left := Left + FXSpeed
Top := Top + FYSpeed
if (Left < 0) or (Left + Width > Parent.Width) then FXSpeed := FXSpeed * -1
if (Top < 0) or (Top + Height > Parent.Height) then FYSpeed := FYSpeed * -1
end;end;procedure TBounceThread.Execute;begin while not Terminated do begin EnterCriticalSection(CriticalSection)
MoveShape
LeaveCriticalSection(CriticalSection)
// Synchronize(MoveShape)
end;end;constructor TBounceThread.Create(Suspended: Boolean
Shape: TShape
XSpeed, YSpeed: Integer);begin inherited Create(Suspended)
FShape := Shape
FXSpeed := XSpeed
FYSpeed := YSpeed
FreeOnTerminate := True;end;destructor TBounceThread.Destroy;begin inherited;end;end.