VCL.Meteor Sample

From RAD Studio Code Examples
Jump to: navigation, search

Language:

This sample demonstrates how to handle graphic objects in a Delphi/C++ application.

Contents

Location

You can find the DMeteor sample project at:

Description

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.

All graphic objects are drawn on the canvas of a TPaintBox component. The animation effect is created using a TTimer component.

How to Use the Sample

  1. Navigate to Start > Programs > Embarcadero RAD Studio > Samples and open DMeteor.dproj.
  2. Press F9 or choose Run > Run.

Files

File Contains

MainDMeteorForm

The class for the main form

SpriteUnit

The classes for all the graphic objects that can appear on screen

Classes

  • 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.

Implementation

  • 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.

Uses

See Also

Personal tools