下落的方块






4.95/5 (10投票s)
使用 Visual C++ 和 DirectX 编写的游戏。
下落的方块
游戏的操作很简单。 使用向左箭头和向右箭头来向左或向右移动方块。 向上箭头或 R 旋转方块。 向下箭头可使方块更快地向下移动,中心键 (5) 可使方块掉落。
目标是使连续的方块排成一行。 填满方块的一行将被移除,并获得积分。
构建项目所需的软件
- Visual C++ 6.0
- DirectX 8.0 SDK(DirectX 7.0 SDK 也应该可以工作,但我没有尝试过)
运行游戏所需的软件
- DirectX 7.0
- Windows 2000 或 Win9x
这段代码应该可以帮助你使用 DirectX 创建小型游戏。 我在代码中到处都添加了注释,因此它应该可以帮助你理解代码。
代码
该项目由以下类组成
CBlockList
CDisplay
CFlooredBlocks
CShape
CSurface
类 CDisplay
和 CSurface
由 Microsoft 创建,并随 DirectX SDK 一起提供。 我使用 DirectX 8 SDK 开发了这个游戏。 它应该可以与 DirectX 7 正常工作,但我没有尝试过。 要运行游戏,只需要 DirectX 7。 你将必须调整项目设置以反映你的 DirectX SDK 路径。
游戏在开始时创建两个形状。 一个是当前正在下落的形状,另一个是下一个形状。 当一个形状到达底部时,它会被添加到 Floored Block 列表中,并创建一个新的 Next 形状。
对于移除的每一行方块,将获得 10 * NumberOfLinesRemoved
* NumberOfLinesRemoved
* GameSpeed
分。
当一个形状到达底部并且一些方块高于灰线时,游戏结束。 你可以从菜单开始一个新游戏。
在关卡菜单下,你可以选择游戏速度。 如果你将游戏设置为疯狂模式,你将获得奇怪的形状。 在此游戏中添加你自己的形状很容易。 你所要做的就是将形状添加到数组并更新数组信息。
m_pStockShapes
数组保存形状定义。
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
结构保存方块坐标和颜色。
struct SBlock {
Short nX,nY,nColor;
};
CBlockList
将是 CShape
类和 CFlooredBlocks
类的父类。 它包含维护链表的方法。
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(); };
CFlooredBlocks
维护已放置在地板上的方块列表。 所有掉落的形状都会添加到此列表中。
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 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. };
看看代码,一切都应该是不言自明的。 玩得开心。 制作游戏并分享你的知识 :)
历史
2002年4月5日 - 新的 VC7 项目,带有文件,使那些在为 SDK 文件设置目录时遇到困难的人更容易使用。