当前位置:编程学习 > C#/ASP.NET >>

C#开发-----百变方块游戏

例子代码:

http://www.rayfile.com/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();                  //清空路径
        &

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,