This sample demonstrates how to use graphic objects in a Delphi/C++ application.
You can find the Blocks 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/Blocks/
- Subversion Repository for C++: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE4/CPP/VCL/Blocks/
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.
How to Use the Sample
- Navigate to Start > Programs > Embarcadero RAD Studio > Samples and open DBlocks.dproj.
- Press F9 or choose Run > Run.
The project has one source file, MainForm, which contains the TBlock class and the form class.
- 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).
- 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.