Topple The Tower Box2d Game Source Code For Delphi XE6 Firemonkey On Windows

Delphi XE5 XE6 Firemonkey Topple The Tower Game Source CodeI have polished up a new Box2D Firemonkey game demo prototype with full source code for Delphi XE5 and XE6 Firemonkey (and AppMethod). This demo is called Topple The Tower that I put together to demonstrate two different pieces of functionality that are useful for building games with the Box2D Firemonkey Physics Engine. The two pieces of functionality are attaching Delphi Firemonkey objects to Box2d bodies and detecting the strength of collisions between Box2d bodies.

The Tb2Body has a property called UserData which is a Pointer. You can assign your own objects or data to the UserData property (like a TRectangle) and then access them later. Here is a simple example of how to do that in code:

What this allows you to do is mirror the position of a Box2d body as a Firemonkey object like a TRectangle. I used a bit of a hack and use the OnPaint event of the TPaintBox in the project to move all of the objects like the TRectangles to keep them in sync with the Box2d bodies. You should probably use a game loop instead however. The drawing of the TPaintBox is actually turned off (change Settings.drawShapes to True if you want to see the Box2d bodies drawn).

For detecting collisions and the strength of collisions between Box2d objects I followed this tutorial and some source code from the other Box2d Firemonkey demos to create a TMyb2ContactListener class that has two event methods. The first one is BeginContact(var contact: Tb2Contact) and the second one is PostSolve(var contact: Tb2Contact; const impulse: Tb2ContactImpulse). BeginContact works if you just need to know when collisions happened and I originally started with this function. However, if you need to know the strength of collisions you use PostSolve(). First I check to see if the two bodies that collided were bodies that I want to take action on and then I use the TagFloat property to record how many times the bodies have collided. In my game loop (in this case the OnPaint event) is where I count up the damage each body has and remove it. Here is the PostSolve() event code (you will notice how I use the UserData property here to interface with my Firemonkey objects):

Finally, I used TFrames to create the start screen, instruction screen, and game over screen. They are set to design time visible false. This keeps them separate which makes them much easier to use. For the sound I use the Windows API but it has a TMediaPlayer for other platforms (or if you want to use each platform’s native play sound API you could). For the music I used a TMediaPlayer. The popping sound and the music are from

I set this project up to use Win64 as that has the best speed at handling the Box2d Firemonkey calculations but Win32 will also work fine. I built this demo for Windows and OSX desktop but it would run on Android and IOS with some changes to the layout of the project. On a tablet it would probably run fine without any layout changes.

Download the full source code for the Box2d Firemonkey Topple The Tower Prototype Demo.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Topple The Tower Box2d Game Source Code For Delphi XE6 Firemonkey On Windows by Delphi XE5 XE6 XE7 XE8 10 Seattle Berlin Firemonkey, Delphi Android, Delphi IOS is licensed under a Creative Commons Attribution 4.0 International License.

Leave a Reply

Show Buttons
Hide Buttons
Read previous post:
Delphi XE6 Firemonkey Calculator App Demo Source Code
Basic Calculator App Source Code For Delphi XE6 Firemonkey On Android And IOS

Lin Shou Shan (superlevin), a Delphi developer from Taiwan, has a blog post tutorial up where he builds a calculator...