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.

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:
// Return true if the given location is already occupiedbool IsOccupied(short nX, short ,nY) ;
//Inserts the block based on the value in linked list.bool Insert(Sblock Block);
// Adds the block to the end of the list.bool Add (const Sblock Block) ;
// Displays the block on the screen offsetting it by nX and nY.void Display(short nX=0; short nY);
//Deletes the block from the list.bool Delete(Sblock Block);
// Empties the linked list.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; // Holds the playing areapublic:
void Display();
short CheckAndRemoveContinuousBlocks();
// Returns the number of lines removed. // This can be used to calculate the score.
IncrementYabove(short nY);
// Helper function for CheckAndRemoveContinuousBlocks // used to drop the blocks above the removed line.bool IsOccupied(nX,nY);
// Returns true if the coordinates are occupied.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();
// Creates a shape from the build in shapes at random. // This function creates the blocks and gives it the color;bool MoveTo(x1,y1); //Moves the shape to the given coordinates.bool MoveRight(); // Moves the shape right. Returns true if successful.bool MoveLeft();
bool MoveDown(); // Moves the shape down. bool Rotate(); // Rotates based on the shape.void Display(); // Displays the shapevoid ConvertToSpaceCoord();
// Converts the internal SBlock to contain the actual // coordinates so that it can be added to the floored list.bool SetMaxNoOfShapesAllowed(short nMac);
// Sets the maximum number of shapes that // will be used from the array. This way you // can added new shapes to the array and active// it by changing this value.
};

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.

It looks like he defines how many different orientations the piece has, then for each orientation he defines where a block should be located. for the example he displayed, the line piece, and there are basically only two orientations for the line, vertical and horizontal. He defines both of them there. Then when the pieces rotate, he simply cycles through the orientations that he has recorded for each piece.

Build a man a fire, and he will be warm for a dayLight a man on fire, and he will be warm for the rest of his life!

You need to get, ddutil.h, ddutil.cpp, dxutil.h, dxutil.cpp. I got them from the Source Code HERE in the Adventures In Abattoirville article.

After i got these files, it said (in compile error) it was searching for them in the following directory... Desktop\common\src\ So i simply placed the files there and it compiled fine You can probly specify where the files should be somehow :\ Ohh, you also have to copy ddutil.h to wher you extracted the FallingBlocks_src.zip Source Code.

Edit Here: Well I just quickly looked into changing wher to find them files, its easy. The files should be listed under the Common Folder, in the FileView of Visual C++ 6. So just change ther properties... Simple :\

Snip of code near the end of the loop in CheckAndRemoveContinuousBlocks:

pPrev = pCurr;
pCurr=pCurr->pNext;

If someone were lucky enough to have removed all the rows, then pCurr->pNext will throw an exception because pCurr will be null. A simple if statement to check pCurr before that assignment corrects the problem:

Probably not. I think Dx 3 is good enough for me but I had no interest in going our and finding which is the oldest SDK version I can work with. Besides everything is free so why not use what is there now???
Would you go to a store and ask for 1996 camry because that is all the functionality you need?
And what do I care if my project does not work on NT 4. I make this for my learning and decided to share it. The code ain’t too great either. I could rewrite it much better now if I had the time.

After having read all the bad comments about your article from people who would have wanted you to send them files from Microsoft I felt I needed to write something positive cause it is one of the first comprehensive article I find about Direct X and games (a small one...).

Thanks for your knowledge

Mr Pink

P.S. I would need kernel32.dll and gdi32.dll cause you use it in your game and there are not in the zip file with the sources........(Just a joke!!!)

I downloaded this project to look at how to put a game together in DX with Win32. Unfortunately, you've not written this in a way that will work on other peoples machines. Hard coded include paths, objects I've never seen before ( what on earth is a CDisplay ? Is it a DIRECTDRAWSURFACE ??? ). This could be very useful if you wanted to take the time to make it work properly outside of your system configuration.

Christian

The content of this post is not necessarily the opinion of my yadda yadda yadda.

The classes CDisplay and CSurface are created by Microsoft and are shipped along with the Direct X SDK. I developed this game using Direct X 8 SDK. It should work fine with Direct X 7 but I have not tried it. To run the game Direct X 7 is all that is required. You will have to adjust the project setting to reflect your Direct X SDK paths

Yep, those files do exist the author is correct. Just look in your SDK path and look for the headers. I was confused at first when I first got SDK 8, the Microsoft crew used CDisplay for a cool demo but I was baffled until I found the hidden and undocumented class files. Nice of Microsoft to lead us in confusion eh?