This sample demonstrates how to handle graphic objects in a Delphi/C++ application.
You can find the DMeteor sample project at:
- Start > Programs > Embarcadero RAD Studio > Samples and then navigate to either:
- Subversion Repository for Delphi: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE4/Delphi/VCL/Meteor/
- Subversion Repository for C++: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE4/CPP/VCL/Meteor/
The application consists of a game in which the player, having control over a space ship, needs to hit as many meteors as possible while avoiding getting crashed. The player ship can rotate and move around in space. The meteors float around and they divide in smaller meteors if they are hit. The player gets more points for hitting smaller meteors.
How to Use the Sample
- Navigate to Start > Programs > Embarcadero RAD Studio > Samples and open DMeteor.dproj.
- Press F9 or choose Run > Run.
The class for the main form
The classes for all the graphic objects that can appear on screen
- TSprite is the base class for all objects that appear on screen. It provides virtual methods to Draw itself on the canvas and Update its coordinates based on the momentum.
- TSpriteList represents a collection of sprites. It contains methods for collision detection.
- TMeteor, derived from TSprite, is the class handling the meteor objects. Beside implementing the Draw and Update methods, TMeteor also provides a Hit method to split the meteor in many smaller meteors.
- TShot, derived from TSprite, represents the bullet launched by the ship. The bullet has only a finite time to live, after which it disappears from screen.
- TDebris, derived from TSprite, represents a spinning line that floats around for a time before disappearing. The player ship is turned into debris when destroyed.
- TShip, derived from TSprite, represents the player ship. A TShip can Rotate, Explode if hit by a meteor, and increase its thrust (AddThrust).
- TSprMessage, derived from TSprite, is used to display digits to show the score.
- TDMeteorForm, the class corresponding to the form, handles the initialization, painting, and repainting of all graphic objects, the initialization of the game settings, and the interaction with the player.
- The meteors are specified as a list of polar coordinates (an angle and a radius).
- For the animation effect, a TTimer component is used. After the specified interval of time, the Invalidate method of the paint box is called, which completely repaints the canvas by generating an OnPaint event. The handler for the OnPaint event calls the procedure for updating the objects’ positions.
- Exit is used to return from a method, instead of assigning the return value to Result.