📄 fallingblocks.html
字号:
<!--------------------------------------------------------------------------->
<!-- INTRODUCTION
The Code Project article submission template (HTML version)
Using this template will help us post your article sooner. We are using MS
IIS and ASP pages on the server, allowing us to simplify the templates used
to create the articles.
To fill in this template, just follow the 3 easy steps below:
1. Fill in the article description details
2. Add links to your images and downloads
3. Include the main article text
That's all there is to it! All formatting will be done by the ASP script
engine.
-->
<!--------------------------------------------------------------------------->
<!-- IGNORE THIS SECTION -->
<html>
<head>
<title>The Code Project</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<Style>
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
</style>
</head>
<body bgcolor="#FFFFFF" color=#000000>
<!--------------------------------------------------------------------------->
<!------------------------------- STEP 1 --------------------------->
<!-- Fill in the details (CodeProject will reformat this section for you) -->
<pre>
Basename: FallingBlocks
Title: Falling Blocks
Author: Xavier John
Email: xavier_john@yahoo.com
Environment: VC++ 6.0, Windows 2000, Win Me, Direct X 7 or 8
Keywords: Games, Game, DirectX, Tetris,
Level: Intermediate
Description: Falling Blocks is a game like Tetris.
Section DirectX
SubSection Games
</pre>
<!------------------------------- STEP 2 --------------------------->
<!-- Include download and sample image information. All included files -->
<!-- should be in /my_cool_code/ -->
<li class=download><a href="FallingBlocks/FallingBlocks.zip">Download the game - 50 Kb </a></li>
<li class=download><a href="FallingBlocks/FallingBlocks_src.zip">Download source - 115 Kb</a></li>
<h2>Falling Blocks.</h2>
<p> Falling Blocks is a game similar to Tetris.</p>
<p>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.<br>
The objective is to get continuous blocks in a row. A row filled with blocks are removed and points are given.</p>
<p><img src="FallingBlocks.gif" alt="Sample Image" ></p>
<p>
Software required to build the project<br>
1) Visual C++ 6.0<br>
2) Direct X 8.0 SDK (Direct X 7.0 SDK should also work but I have not tried it)
</p>
<p>
Software required to run the game <br>
1) Direct X 7.0 <br>
2) Windows 2000 or Win 9x
</p>
<!------------------------------- STEP 3 --------------------------->
<!-- Add the article text. Please use simple formatting (<h2>, <p> etc) -->
<p>This code should help you create small games using Direct X. I have sprinkled commends all over the code so it should help you with understanding the code.</p>
<h3>The Code</h3>
<p>The project consists of the following Classes, <br>
1) <code>CBlockList</code> <br>
2) <code>CDisplay</code><br>
3) <code>CFlooredBlocks</code><br>
4) <code>CShape</code><br>
5) <code>CSurface</code>
<br><br>
The classes <code>CDisplay</code> and <code>CSurface</code> 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.
<br><Br>
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.
<br><br>
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.
<br><br>
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 you own shapes in this game. All you have to do is add the shape to the array and update the array information.
<br><br>
<code>m_pStockShapes</code> array holds the shape definitation.
<pre>
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
}
</pre>
<code>SBlock</code> structure holds the block coordinates and the color.
</p>
<pre>
struct SBlock {
Short nX,nY,nColor;
};
</pre>
<br>
<code>CBlockList</code> will be the parent class for the <code>CShape</code> class and the <code>CFlooredBlocks</code> class. It contains the methods to maintain the linked list.
<pre>
class CBlockList {
public:
bool IsOccupied(short nX, short ,nY) ; // Return true if the given location is already occupied
Bool Insert(Sblock Block); //Inserts the block based on the value in linked list.
Bool Add (const Sblock Block) ; // Adds the block to the end of the list.
void Display(short nX=0; short nY); // Displays the block on the screen offsetting it by nX and nY.
bool Delete(Sblock Block); //Deletes the block from the list.
void Destroy(); // Empties the linked list.
};
</pre>
<p>
The <code>CFlooredBlocks</code> maintains the list of blocks that have been placed on the floor. All the shapes that fall down are added to this list.
</p>
<pre>
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);
};
</pre>
<p>
<code>CShape</code> 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.
</p>
<pre>
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.
};
</pre>
<p>
Look at the code and everything should be self-explanatory. Have fun.<br>
Make games and share your knowledge :)
</p>
<!------------------------------- That's it! --------------------------->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -