end;
end;
PRocedureTBounceThread. Execute;
begin
WhilenotTerminateddo
begin
Synchronize(MoveShape);
end;
end;
constructorTBounceThread. Create(Suspended: Boolean; Shape: TShape; XSpeed, YSpeed: Integer);
begin
inheritedCreate(Suspended);
FShape: ΚShape;
FXSpeed: ΚXSpeed; {X-axis speed}
FYSpeed: ΚYSpeed; {Speed of Y-axis direction}
FreeOnTerminate:ΚTrue;
end;
end.
This is a multi-threaded bumper ball game. You can have multiple different balls, which belong to different threads and collide on the screen independently. Obviously, since the display of multiple balls running will operate VCL resources at the same time, for safety, we added Synchronize (MoveShape) in the execution part of the Execute process to call the MoveShape process. In fact, wherever VCL resources need to be operated, , such as forms and bitmaps, should be added to the Synchronize call.
When executing, we can create a new program, then add the above BncThrd unit in the USES section, and then add two Shape controls Shape1 and Shape2 to its form FORM1. Shape1 can be a rectangle and Shape2 is a circle. Add the following code to make the rectangle and circle move.
procedureTForm1. Button1Click(Sender: TObject);
begin
TBounceThread. Create(False,Shape1,1,2);
TBounceThread. Create(False,Shape2,2,3);
end;
Author's Blog: http://blog.csdn.net/zou5655/