Click here to Skip to main content
Click here to Skip to main content

Falling Blocks

By , 17 Apr 2008
 

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.

Sample Image

Software required to build the project:

  1. Visual C++ 6.0
  2. DirectX 8.0 SDK (DirectX 7.0 SDK should also work but I have not tried it)

Software required to run the game:

  1. DirectX 7.0
  2. 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:

  1. CBlockList
  2. CDisplay
  3. CFlooredBlocks
  4. CShape
  5. 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,   // No Of shapes in the array
     2 /*No of orientation shapes */, 4 /*No Of blocks for this shape*/,
     2,1, 2,2, 3,2, 3,3,     //  O
     1,2, 2,2, 2,1, 3,1,     //  OO 
                             //   O 
     0,   // Each shape ends with a 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:
    // Return true if the given location is already occupied
    bool 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 area
public:
   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 shape
     
     void 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Xavier John
Software Developer
United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalfalling animationmemberMember 389234315-Nov-10 1:13 
Generalerrormemberabdennassir31-Mar-08 11:45 
GeneralRe: errormemberXavier John17-Apr-08 13:42 
Generalfailed to executedmemberqistina1-Dec-04 23:48 
GeneralRe: failed to executedmemberXavier John24-Feb-06 9:11 
Generalgood codememberrichard sancenot2-Oct-04 0:19 
GeneralDisplay a modal dialog from a DirectX gamememberWebZoner27-Aug-03 8:40 
GeneralRe: Display a modal dialog from a DirectX gamememberJohn M. Drescher27-Aug-03 8:53 
General'CDisplay'memberDeSiPiMp18-Mar-03 18:50 
GeneralRe: 'CDisplay'memberhoangquy7720-May-04 22:10 
GeneralSyntax Error / RedefinitionsussAnonymous14-Jan-03 7:54 
GeneralGreat game, Great code!sussAnonymous6-Nov-02 22:50 
Questionhow to add new blockssusswei10-Sep-02 21:00 
AnswerRe: how to add new blocksmemberPaul Watt30-Oct-02 6:24 
GeneralCompiler Error :(memberSelevercin21-Jul-02 3:42 
Hi,
 
I know I'm doing something very stupid. I keep getting this compile error:
 
fatal error C1083: Cannot open source file: 'C:\My Documents\Nic\Visual C++\Visual C++ Programms\DirectX games\common\src\ddutil.cpp': No such file or directory
 
I think I need to change the directory information, but how and where?
 
Thanks,
 
~ Selevercin
 
If you have a problem with my spelling, just remember that's not my fault. I [as well as everyone
else who learned to spell after 1976] blame it on
Robert A. Kolpek for U.S. Patent 4,136,395.

GeneralRe: Compiler Error :(sussAnonymous22-Jul-02 4:18 
GeneralRe: Compiler Error :(memberYoshua9-Feb-04 4:37 
GeneralRe: Compiler Error, Fixed :)memberYoshua9-Feb-04 5:31 
Questionhow to compile your codememberThanks15-May-02 21:28 
AnswerRe: how to compile your codememberXavier John17-Jun-02 14:01 
Generallittle bugmemberAnonymous12-Apr-02 10:47 
GeneralRe: little bugmemberLukeV30-Aug-02 4:29 
GeneralRe: little bugmemberXavier John22-Apr-08 7:56 
GeneralImage ToolmemberDon Burton9-Apr-02 16:15 
GeneralRe: Image ToolmemberJeremy Falcon9-Apr-02 17:27 
GeneralDX versionmemberMike Nordell9-Apr-02 8:27 
GeneralRe: DX versionmemberXavier John9-Apr-02 9:05 
GeneralCoolmemberHockeyDude3-Apr-02 3:53 
GeneralThanks for the article !!memberBraulio Díez21-Jan-02 20:49 
Generalprobs in NT4.0memberAli Issa7-Jan-02 5:40 
GeneralRe: probs in NT4.0memberXavier John7-Jan-02 7:01 
GeneralGreat!memberamanjit.gill20-Oct-01 14:00 
GeneralRe: Great!memberXavier John7-Jan-02 7:02 
GeneralThanksmemberAnonymous5-Sep-01 13:37 
GeneralRe: ThanksmemberTantalus16-Sep-01 3:27 
GeneralRe: ThanksmemberXavier John8-Oct-01 8:57 
GeneralTotally brokenmemberChristian Graus9-Feb-01 11:12 
GeneralRe: Totally brokenmemberAnonymous4-Jun-01 4:40 
GeneralFailed to rebuild this projectmemberwogo3-Jan-01 16:47 
GeneralRe: Failed to rebuild this projectmemberXavier John3-Jan-01 17:22 
GeneralRe: Failed to rebuild this projectmemberDon Lazov15-Feb-01 1:08 
GeneralRe: Failed to rebuild this projectmemberAnonymous13-Aug-01 3:02 
GeneralRe: Failed to rebuild this projectmemberAnonymous9-Mar-02 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 17 Apr 2008
Article Copyright 2001 by Xavier John
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid