百变方块游戏

  • 格式:docx
  • 大小:202.90 KB
  • 文档页数:23

C#开发-----百变方块游戏例子代码:/zh-cn/files/b6ed0bc0-8e9e-11e1-8178-0015c55db73d/n游戏在 6× 6 格子的棋盘中进行,可排出55种不同的组合图案。

主要开发人的抽象思维能力、空间想象能力、动手能力、几何构建能力。

游戏运行时功能如下。

n(1)实现用鼠标拖动拼块,拼块任意位置摆放。

n(2)绕拼块的中心点旋转(旋转由鼠标右键操作实现)。

n(3)拼块水平翻转(由鼠标双击操作实现)n百变方块游戏效果如图22-1所示。

用户拖动棋盘周围的8种拼块到棋盘中,直到棋盘所有空白方块格子被填满则此关游戏胜利。

单击“新方块图案”按钮则进入下一关游戏。

如果玩家无法完成则可以单击“参考答案”按钮查看参考拼图方案。

地图信息存储n地图信息采用文本文件map.txt存储保存。

根据目标图案按列存放,每关占一行。

0代表固定不变的绿色填充方格,1代表蓝色填充的方格(即需要用户的8种拼块填充的方格)。

1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,0,1,0,1,1,0,0,0,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,0,1,1,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1, n游戏在 6× 6 格子的棋盘中进行,每关开始时从文本文件map.txt读取相应关所对应行的字符串,分割后将数据按列将目标图案存储到二维数组OrgMap[6,6],而用户移动拼块后的图案按列存储到二维数组Map[6,6]中。

通过对比两个数组知道是否成功完成此关。

1.拼块类(CChip.cs)字段m_nType存储拼块的类型代号,总计有7个拼块。

分别用1—8代表图22-3的七个拼块。

m_nPointCount存储拼块的顶点个数,m_pointList存储拼块的顶点坐标。

myPath是形成拼块的路径。

[csharp] class CChip{Point []m_pointList; //顶点坐标int m_nPointCount; //顶点个数int m_nType; //类型代号private GraphicsPath myPath;…….}拼块的参数设置方法SetChip拼块类提供对拼块的参数设置方法SetChip,该方法完成拼块类型代号的设置,拼块图案顶点坐标初始化,最终形成拼块的路径。

[csharp] public void SetChip(int type, Point []ppointlist, int count) {m_nType = type;m_nPointCount = count;m_pointList = new Point[m_nPointCount];for(int i=0; i<count; i++)m_pointList[i] = ppointlist[i];myPath = new GraphicsPath();myPath.AddLines(m_pointList);myPath.CloseFigure();//CloseFigure方法闭合当前图形并开始新图形。

}public void SetChip(int type, Point []ppointlist, int count){m_nType = type;m_nPointCount = count;m_pointList = new Point[m_nPointCount];for(int i=0; i<count; i++)m_pointList[i] = ppointlist[i];myPath = new GraphicsPath();myPath.AddLines(m_pointList);myPath.CloseFigure();//CloseFigure方法闭合当前图形并开始新图形。

}拼块的平移[csharp] Move(int x_offset, int y_offset)应用Matrix实现GraphicsPath路径的平移。

public void Move(int x_offset, int y_offset){Matrix matrix = new Matrix();matrix.Translate(x_offset, y_offset); //追加平移myPath.Transform(matrix);//应用变形myPath.CloseFigure();}Move2(int x_offset, int y_offset)不用Matrix实现GraphicsPath路径的平移,而是直接对路径的每个顶点一一平移。

public void Move2(int x_offset, int y_offset){myPath.Reset(); //清空路径for (int i = 0; i < m_nPointCount; i++) // 平移各顶点{m_pointList[i].X += x_offset;m_pointList[i].Y += y_offset;}myPath.AddLines(m_pointList);myPath.CloseFigure();}Move(int x_offset, int y_offset)应用Matrix实现GraphicsPath路径的平移。

public void Move(int x_offset, int y_offset){Matrix matrix = new Matrix();matrix.Translate(x_offset, y_offset); //追加平移myPath.Transform(matrix);//应用变形myPath.CloseFigure();}Move2(int x_offset, int y_offset)不用Matrix实现GraphicsPath路径的平移,而是直接对路径的每个顶点一一平移。

public void Move2(int x_offset, int y_offset){myPath.Reset(); //清空路径for (int i = 0; i < m_nPointCount; i++) // 平移各顶点{m_pointList[i].X += x_offset;m_pointList[i].Y += y_offset;}myPath.AddLines(m_pointList);myPath.CloseFigure();}拼块的旋转[csharp] Rotation()应用Matrix实现GraphicsPath路径的旋转,每次旋转45度。

public void Rotation(){Matrix matrix = new Matrix();RectangleF rect = new RectangleF();rect = myPath.GetBounds();// 计算旋转中心坐标(x,y)double x = rect.Left + rect.Width / 2;double y = rect.Top + rect.Height / 2;//matrix.Rotate(45.0f); //旋转顺时针45度matrix.RotateAt(45.0f, new Point((int)x, (int)y)); //旋转顺时针45度}Rotation()应用Matrix实现GraphicsPath路径的旋转,每次旋转45度。

public void Rotation(){Matrix matrix = new Matrix();RectangleF rect = new RectangleF();rect = myPath.GetBounds();// 计算旋转中心坐标(x,y)double x = rect.Left + rect.Width / 2;double y = rect.Top + rect.Height / 2;//matrix.Rotate(45.0f); //旋转顺时针45度matrix.RotateAt(45.0f, new Point((int)x, (int)y)); //旋转顺时针45度}拼块的旋转(2)[csharp] Rotation2()不用Matrix实现GraphicsPath路径的旋转,而是获取myPath 的矩形区域,计算旋转中心,从而计算出每个顶点的新坐标。

public void Rotation2(){RectangleF rect=new RectangleF() ;rect=myPath.GetBounds();myPath.Reset(); //清空路径double x = rect.Left + rect.Width/2; // 计算旋转中心double y = rect.Top + rect.Height/ 2;double dx, dy;for (int i = 0; i < m_nPointCount; i++) // 旋转各顶点{dx = m_pointList[i].X - x;dy = m_pointList[i].Y - y;m_pointList[i].X = (int)(x + dx * 0.7071 - dy * 0.7071);m_pointList[i].Y = (int)(y + dx * 0.7071 + dy * 0.7071);}myPath.AddLines(m_pointList);myPath.CloseFigure();}Rotation2()不用Matrix实现GraphicsPath路径的旋转,而是获取myPath的矩形区域,计算旋转中心,从而计算出每个顶点的新坐标。

public void Rotation2(){RectangleF rect=new RectangleF() ;rect=myPath.GetBounds();myPath.Reset(); //清空路径double x = rect.Left + rect.Width/2; // 计算旋转中心double y = rect.Top + rect.Height/ 2;double dx, dy;for (int i = 0; i < m_nPointCount; i++) // 旋转各顶点{dx = m_pointList[i].X - x;dy = m_pointList[i].Y - y;m_pointList[i].X = (int)(x + dx * 0.7071 - dy * 0.7071);m_pointList[i].Y = (int)(y + dx * 0.7071 + dy * 0.7071);}myPath.AddLines(m_pointList);myPath.CloseFigure();}拼块水平反转[csharp] ReverseTurn()获取myPath的矩形区域,计算旋转中心,从而计算出水平翻转后每个顶点的新坐标。