Falling Blocks
The controls for the game are simple. Use the Left arrow and the Right arrow to move the block left or right. Up arrow or R to rotate the block. Down arrow to move the block down faster and the center key (5) to drop the block.
The objective is to get continuous blocks in a row. A row filled with blocks are removed and points are given.
Software required to build the project:
- Visual C++ 6.0
- DirectX 8.0 SDK (DirectX 7.0 SDK should also work but I have not tried it)
Software required to run the game:
- DirectX 7.0
- Windows 2000 or Win9x
This code should help you create small games using DirectX. I have sprinkled comments all over the code so it should help you with understanding the code.
The Code
The project consists of the following classes:
CBlockList
CDisplay
CFlooredBlocks
CShape
CSurface
The classes CDisplay
and CSurface
are created by Microsoft and are shipped along with the DirectX SDK. I developed this game using DirectX 8 SDK. It should work fine with DirectX 7 but I have not tried it. To run the game, DirectX 7 is all that is required. You will have to adjust the project setting to reflect your DirectX SDK paths.
The game creates two shapes when the game starts. One is the shape currently falling and the other is the next shape. When a shape hits the bottom, it is added to the Floored Block list and a new Next shape is created.
For each line of blocks removed, 10* NumberOfLinesRemoved
* NumberOfLinesRemoved
* GameSpeed
points are given.
Game is over when a shape hits the bottom and some of the blocks are above the grey line. You can start a new game from the menu.
Under the level menu, you can select the game speed. If you set the game to crazy mode, you will get weird shapes. It is easy to add your own shapes in this game. All you have to do is add the shape to the array and update the array information.
m_pStockShapes
array holds the shape definitions.
const short CShape::m_pStockShapes[] = {
11,
2 , 4 ,
2,1, 2,2, 3,2, 3,3,
1,2, 2,2, 2,1, 3,1,
0,
}
SBlock
structure holds the block coordinates and the color.
struct SBlock {
Short nX,nY,nColor;
};
CBlockList
will be the parent class for the CShape
class and the CFlooredBlocks
class. It contains the methods to maintain the linked list.
class CBlockList {
public:
bool IsOccupied(short nX, short ,nY) ;
bool Insert(Sblock Block);
bool Add (const Sblock Block) ;
void Display(short nX=0; short nY);
bool Delete(Sblock Block);
void Destroy();
};
The CFlooredBlocks
maintains the list of blocks that have been placed on the floor. All the shapes that fall down are added to this list.
class CFlooredBlocks: public CBlockList {
RECT m_rcBoundary;
public:
void Display();
short CheckAndRemoveContinuousBlocks();
IncrementYabove(short nY);
bool IsOccupied(nX,nY);
bool Insert(Sblock Block);
};
CShape
class creates the shapes from the given array. It helps with moving the shape and checks if it had gone outside the boundary or hit any other blocks.
class CShape:public CBlockList {
CFlooredBlocks* m_pFlooredBlocks;
public:
bool CreateRandShape();
bool MoveTo(x1,y1);
bool MoveRight();
bool MoveLeft();
bool MoveDown();
bool Rotate();
void Display();
void ConvertToSpaceCoord();
bool SetMaxNoOfShapesAllowed(short nMac);
};
Look at the code and everything should be self-explanatory. Have fun. Make games and share your knowledge :)
History
5 Apr 2002 - new VC7 project, with files to make it easier for those having difficulties setting directories for SDK files.