Pentominos-著名益智游戏的C#实现(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/pentominos-a-csharp-implementation-of-the-famous-p-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2448 个词 阅读量 0Pentominos-著名益智游戏的C#实现(译文)
原文地址:https://www.codeproject.com/Articles/1971/Pentominos-A-Csharp-Implementation-of-the-Famous-P
原文作者:Mauricio Ritter
译文由本站 robot-v1.0 翻译
前言
This is a simple implementation of Pentominos, a puzzle game. The source code is written in C# and uses a lot of concepts about drawing and matrix handling. Hope you enjoy it !
这是一个益智游戏Pentominos的简单实现.源代码是用C#编写的,并使用了许多有关绘图和矩阵处理的概念.希望你喜欢它 !
游戏(The Game)
这是著名的Pentominos Puzzle游戏的C#实现.游戏的目标是将12个棋子组合成矩形.在最简单的模式(6x10矩形)中,有2339种可能的解决方案.您必须在考虑… 2339个可能的解决方案…简单!如果您有这种想法(以前从未玩过这款游戏)…我挑战您以不到5分钟的时间完成拼图!玩得开心!(This is a C# implementation of the famous Pentominos Puzzle game. The game objective is to combine the 12 pieces into the rectangle. In the easiest mode (6x10 rectangle) there are 2339 possible solutions. You must be thinking… 2339 possible solutions… easy! If you have that thought (and never had played this game before)… I CHALLENGE YOU TO FINISH THE PUZZLE IN LESS THAN 5 MINUTES! Have fun!)
该程序(The Program)
该游戏是一个简单的Windows窗体应用程序.在主表单结构中,我们声明了一个数组(The game is a simple Windows Forms application. In the main form structure, we have declared an array of) cPiece
类实例,它们代表窗口周围的实例和两个(class instances that represent the instances lying around the window and two) int
代表主矩形尺寸的变量.(variables that represent the dimensions of the main rectangle.)
public int iCols;
public int iRows;
cPiece[] piece = new cPiece[12];
在游戏开始时,我们致电了(At the beginning of the game, we have a call to the) InitForm()
该功能负责创建12个部件并重置计时器.时间由(function that is responsible for the creation of the 12 pieces and resetting the time counter. The time controlled by a) DateTime
存储游戏开始时间的变量.(variable that stores the start time of the game.)
for(int i=0;i<12;i++)
{
piece[i] = new cPiece();
piece[i].CreatePiece(i+1);
piece[i].SetPos(20 + ( (i%6) *80), i>=6 ? 280 : 200);
}
dtStart = DateTime.Now;
cPiece类(The cPiece Class)
的(The) cPiece
类具有处理屏幕周围各个部分所需的所有功能.件结构存储在(class has all the functionality needed to work with the pieces around the screen. The piece structure is stored in a) int
5x5矩阵.当我们打电话给(5x5 matrix. When we call the) CreatePiece
该类的方法,我们用0和1填充矩阵,以描述块的填充位置和未填充的位置.(method of the class, we populate the matrix with 0 and 1 to describe where the piece is filled and where it is not filled.)
public void CreatePiece(int iPieceType)
{
switch(iPieceType)
{
case 1:
iMatrix[0,0] = 0;
iMatrix[1,0] = 1;
iMatrix[2,0] = 0;
iMatrix[3,0] = 0;
iMatrix[4,0] = 0;
...
case 12:
iMatrix[0,0] = 1;
iMatrix[1,0] = 0;
iMatrix[2,0] = 0;
iMatrix[3,0] = 0;
iMatrix[4,0] = 0;
iMatrix[0,1] = 1;
iMatrix[1,1] = 0;
iMatrix[2,1] = 0;
iMatrix[3,1] = 0;
iMatrix[4,1] = 0;
iMatrix[0,2] = 1;
iMatrix[1,2] = 0;
iMatrix[2,2] = 0;
iMatrix[3,2] = 0;
iMatrix[4,2] = 0;
iMatrix[0,3] = 1;
iMatrix[1,3] = 0;
iMatrix[2,3] = 0;
iMatrix[3,3] = 0;
iMatrix[4,3] = 0;
iMatrix[0,4] = 1;
iMatrix[1,4] = 0;
iMatrix[2,4] = 0;
iMatrix[3,4] = 0;
iMatrix[4,4] = 0;
piecebrush = new SolidBrush(Color.LightGreen);
break;
}
}
如果您在最后一段代码中注意到,我们有一个(If you notice in the last piece of code, we have a) piecebrush
持有一个变量(variable that holds a) SolidBrush
实例.该变量在作品绘画过程中使用.当我们创建作品时,我们为每个作品分配不同的颜色.作品的绘制过程是通过(instance. This variable is used in the piece painting process. When we create the pieces, we assign a different color to each one. The drawing process of the piece is done with the) Draw()
方法,该方法接收(method, that receives an instance of the) Graphics
对象并在其当前位置绘画作品.(object and paints the piece in its current position.)
public void Draw( Graphics oGraph)
{
Pen blackpen = new Pen(Color.Black, 2);
Point[] tmpPoints = (Point[]) thePoints.Clone();
for(int i=0;i < iNumPoints;i++)
{
tmpPoints[i].X += iPosX;
tmpPoints[i].Y += iPosY;
}
//gradbrush.InterpolationColors = oGradColor;
oGraph.DrawPolygon(blackpen, tmpPoints);
oGraph.FillPolygon(piecebrush, tmpPoints, FillMode.Winding);
}
的(The) Draw()
方法使用(method uses a) Point
表示形成块多边形的点的数组.这点存储在(array that represents the points the form the piece polygon. This points are stored in the) thePoints
变量,它是在(variable that is an array created dynamically in the) CreatePoints
方法.的(method. The) CreatePoints
方法获得矩阵的起点,并开始围绕零件的跟踪过程,以找到多边形顶点.可用的其他方法(method gets a starting point on the matrix and starts a tracing process around the piece, to find the polygon vertices. The other methods available in the) cPiece
类是(class are) SetPos
,用于设置作品在屏幕中的位置,(, that sets the position of the piece in the screen,) RotatePiece()
旋转并移动描述零件结构的矩阵,并且(that rotates and shifts the matrix that describes the piece structure, and) FlipPiece
翻转计件矩阵.还有一个额外的方法叫做(that flips the piece matrix. There is an extra method called) Hitted
用于检查鼠标当前位置是否在一块内部.(that is used to check if the mouse current position is inside a piece.)
主要形式绘画(The Main Form Painting)
在(At the) OnPaint
事件中,我们绘制拼图盒和所有处于其当前位置的棋子,(event, we draw the puzzle box and all the pieces in their current positions, calling the) Draw
每件的功能.(function of each piece.)
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics grPaint = e.Graphics;
SolidBrush brushWhite = new SolidBrush(Color.White);
Pen blackpen = new Pen(Color.Black,2);
// Clear the screen
grPaint.FillRectangle(brushWhite, e.ClipRectangle);
// Draw the grid
grPaint.DrawString("Left-Click on a piece do drag or drop it",
new Font("Arial",12),
new SolidBrush(Color.Black),20,10);
grPaint.DrawString("Right-Click to rotate and flip " +
"it (some pieces doesn´t flip)",
new Font("Arial",12),
new SolidBrush(Color.Black),20,30);
for(int i=0;i < iCols;i++)
{
for(int j=0;j < iRows;j++)
{
grPaint.DrawRectangle(blackpen, 100 + (i*20), 60 + (j*20),20,20);
}
}
for(int i=0;i < 12;i++)
{
piece[i].Draw(grPaint);
}
}
的(The) MouseDown
和(and) MouseMove
表单的事件实现是游戏逻辑所在.我们有一个名为(event implementations of the form is where the game logic is. We have a variable called) draggedpiece
保留用户正在拖动的作品.当用户单击作品时,我们会将作品的实例分配给拖动的作品变量.在里面(that holds the piece that the user is dragging. When the user clicks in a piece, we assign the instance of the piece to the dragged piece variable. In the) MouseMove
事件,我们使用(event, we use the) draggedpiece
变量以设置作品的新位置并重新绘制.(variable to set the piece’s new position and redraw it.)
我知道本文不是主题特定的,但它显示了有关C#和Windows窗体的绘制过程的许多内容.希望你喜欢它 !(I know this article is not TOPIC SPECIFIC, but it shows a lot of things about the drawing process of C# and Windows Forms. Hope you enjoy it !)
更新(Updates)
- 2002年5月26日(05/26/2002):我已经纠正了一些资源泄漏问题,以便在Win 9x上运行游戏的用户没有任何问题.主要区别在于,游标变量位于表单范围内,并且仅被加载一次.特别感谢PJ Arens的错误报告和测试,以及James T. Johnson对问题的出色解释.(: I’ve corrected some resource leak problems so that users running the game on Win 9x don’t have any problems. The main difference is that the cursor variables are at form scope and are loaded just once. Special thanks to PJ Arens for the bug report and testing, and James T. Johnson for his great explanation about the problem.)
执照(License)
本文没有附带任何明确的许可,但可能在文章文本或下载文件本身中包含使用条款.如有疑问,请通过下面的讨论区与作者联系.(This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.) 可以找到作者可能使用的许可证列表(A list of licenses authors might use can be found) 这里(here) .(.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# Win2K WinXP Windows .NET .NET1.0 Visual-Studio Dev 新闻 翻译