VCL.Blocks Sample

From RAD Studio Code Examples
Jump to: navigation, search

Language:

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

Contents

Location

You can find the Blocks sample project at:

Description

This sample consists of a game with blocks. Each block is a shape composed of four squares that is falling down on a board. The purpose of the game is to create complete lines by rotating the blocks and moving them to the sides. The game interface also provides other features: you can see the help, pause the game, start a new game, increase/decrease the speed and block size.

This application creates a form with a TPaintBox, a TTimer, and a TPopupMenu for a context menu. The blocks are drawn on the canvas of a TPaintBox and the animation is created using a TTimer.

How to Use the Sample

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

Files

The project has one source file, MainForm, which contains the TBlock class and the form class.

Classes

  • TBlock represents a block.
  • TBlockForm contains three visual components: a TPaintBox, a TTimer, and a TPopupMenu. TBlockForm handles the drawing and redrawing of the graphic objects (blocks, Pause screen, Game Over screen), the interaction with the player, and the game settings (increase and decrease of the block size and speed).

Implementation

  • Each block is stored as an AnsiChar matrix that corresponds to the square that encloses the piece. Each element in the matrix is * if it corresponds to a square in the block, and a space otherwise.
  • The board on which the blocks are stacked is stored as an integer matrix. Each element in the matrix is 0 if the corresponding square on the board is free, or has assigned a color number otherwise.
  • The TTimer.OnTimer event of TTimer calls the TControl.Invalidate method of TPaintBox, which generates an OnPaint event. The handler for OnPaint calls the functions that update the position of the current block and remove the completed lines, if necessary.

Uses

See Also

Personal tools