程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#開發-----百變方塊游戲

C#開發-----百變方塊游戲

編輯: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();                  //清空路徑
            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的矩形區域,計算旋轉中心,從而計算出水平翻轉後每個頂點的新坐標。 
        public void ReverseTurn()               // 水平反轉  
        { 
            RectangleF rect = new RectangleF(); 
            rect = myPath.GetBounds(); 
            myPath.Reset(); 
            double x = rect.Left + rect.Width / 2; // 計算旋轉中心  
            double y = rect.Top + rect.Height / 2; 
            for (int i = 0; i < m_nPointCount; i++) // 水平反轉各點  
            { 
                m_pointList[i].X = (int)(2 * x - m_pointList[i].X); 
                m_pointList[i].Y = m_pointList[i].Y; 
            } 
            myPath.AddLines(m_pointList); 
            myPath.CloseFigure(); 
        } 
ReverseTurn()獲取myPath的矩形區域,計算旋轉中心,從而計算出水平翻轉後每個頂點的新坐標。
        public void ReverseTurn()               // 水平反轉
        {
            RectangleF rect = new RectangleF();
            rect = myPath.GetBounds();
            myPath.Reset();
            double x = rect.Left + rect.Width / 2; // 計算旋轉中心
            double y = rect.Top + rect.Height / 2;
            for (int i = 0; i < m_nPointCount; i++) // 水平反轉各點
            {
                m_pointList[i].X = (int)(2 * x - m_pointList[i].X);
                m_pointList[i].Y = m_pointList[i].Y;
            }
            myPath.AddLines(m_pointList);
            myPath.CloseFigure();
        }


DrawChip( )在Graphics對象上畫出拼塊。每個拼塊設置不同的填充顏色。

[csharp] public void DrawChip(Graphics g)//畫拼塊  
        {   
            Pen myPen = new Pen(Color.Black, 1); 
            g.DrawPath(myPen, myPath); 
            int alpha = 140;   //透明度  
            Color c= Color.FromArgb(alpha, 255, 255, 255); 
            switch (m_nType) 
            { 
                case 1: 
                    c = Color.FromArgb(alpha, 127, 127, 127); 
                    break; 
                case 2: 
                    c = Color.FromArgb(alpha, 255, 0, 0); 
                    break; 
                case 3: 
                    c = Color.FromArgb(alpha, 200, 255, 0); 
                    break; 
                ....... 
                case 8: 
                    c = Color.FromArgb(alpha, 228, 128, 128); 
                    break; 
            }             
            SolidBrush brushNew = new SolidBrush(c); 
            g.FillPath(brushNew, myPath);  
        } 
public void DrawChip(Graphics g)//畫拼塊
        { 
            Pen myPen = new Pen(Color.Black, 1);
            g.DrawPath(myPen, myPath);
            int alpha = 140;   //透明度
            Color c= Color.FromArgb(alpha, 255, 255, 255);
            switch (m_nType)
            {
                case 1:
                    c = Color.FromArgb(alpha, 127, 127, 127);
                    break;
                case 2:
                    c = Color.FromArgb(alpha, 255, 0, 0);
                    break;
                case 3:
                    c = Color.FromArgb(alpha, 200, 255, 0);
                    break;
                .......
                case 8:
                    c = Color.FromArgb(alpha, 228, 128, 128);
                    break;
            }           
            SolidBrush brushNew = new SolidBrush(c);
            g.FillPath(brushNew, myPath);
        }


對用戶移動的拼塊進行位置校正

\


 

Verity(int CHIP_WIDTH)對用戶移動的拼塊進行位置校正,方法是判斷拼塊的第一個頂點坐標m_pointList[0]接近棋盤中那個方格,則計算出偏移量後,對拼塊的所有頂點進行修正,保證拼塊移動到適當的方格位置處。 
        public void Verity(int CHIP_WIDTH) 
        { 
            myPath.Reset();                         //清空路徑  
            int x_offset=0, y_offset=0; 
            int d; 
            d=m_pointList[0].X % CHIP_WIDTH; 
            if (d != 0) 
                x_offset -= (d < CHIP_WIDTH / 2 ? d:d- CHIP_WIDTH); 
            d = m_pointList[0].Y % CHIP_WIDTH; 
            if (d != 0) 
                y_offset -= (d < CHIP_WIDTH / 2 ? d:d- CHIP_WIDTH); 
            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(); 
        } 
Verity(int CHIP_WIDTH)對用戶移動的拼塊進行位置校正,方法是判斷拼塊的第一個頂點坐標m_pointList[0]接近棋盤中那個方格,則計算出偏移量後,對拼塊的所有頂點進行修正,保證拼塊移動到適當的方格位置處。
        public void Verity(int CHIP_WIDTH)
        {
            myPath.Reset();                         //清空路徑 www.2cto.com
            int x_offset=0, y_offset=0;
            int d;
            d=m_pointList[0].X % CHIP_WIDTH;
            if (d != 0)
                x_offset -= (d < CHIP_WIDTH / 2 ? d:d- CHIP_WIDTH);
            d = m_pointList[0].Y % CHIP_WIDTH;
            if (d != 0)
                y_offset -= (d < CHIP_WIDTH / 2 ? d:d- CHIP_WIDTH);
            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();
        }


2.設計窗體類(Form1.cs)
 
窗體加載事件

 

[csharp] int [,]Map=new int[6,6]; 
    int[,] OrgMap = new int[6, 6];//初始化目標地圖OrgMap  
 
     窗體加載事件中,調用Reset()對8個拼塊的頂點坐標m_chipList 數組初始化。調用ReadOrgMap(1)方法讀出第一關目標圖案的地圖信息到OrgMap二維數組中。 
    private void Form1_Load(object sender, EventArgs e) 
    { 
        Reset();// 初始化拼圖塊  
        //以下兩句是為了設置控件樣式為雙緩沖,這可以有效減少閃爍的問題  
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); 
        this.UpdateStyles(); 
        ReadOrgMap(1); //讀出第一關目標圖案的地圖信息到OrgMap二維數組中  
        Draw_AllChip();//畫出所有拼塊  
    } 
int [,]Map=new int[6,6];
    int[,] OrgMap = new int[6, 6];//初始化目標地圖OrgMap

     窗體加載事件中,調用Reset()對8個拼塊的頂點坐標m_chipList 數組初始化。調用ReadOrgMap(1)方法讀出第一關目標圖案的地圖信息到OrgMap二維數組中。
    private void Form1_Load(object sender, EventArgs e)
    {
        Reset();// 初始化拼圖塊
        //以下兩句是為了設置控件樣式為雙緩沖,這可以有效減少閃爍的問題
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
        this.UpdateStyles();
        ReadOrgMap(1); //讀出第一關目標圖案的地圖信息到OrgMap二維數組中
        Draw_AllChip();//畫出所有拼塊
    }
 
 

初始化拼塊
Reset()初始化拼圖塊頂點坐標m_chipList 數組,並形成拼塊的圖形路徑。
\


 

[csharp]  private void Reset() 
    { 
        for (int i = 0; i < CHIP_COUNT; i++) 
            m_chipList[i] = new CChip(); 
        Point[] pointList = new Point[MAX_POINTS]; 
        int sx=250, sy=0; 
        pointList[0] = new Point(sx , sy ); 
        pointList[1] = new Point(sx +  2* CHIP_WIDTH, sy ); 
        pointList[2] = new Point(sx +  2* CHIP_WIDTH, sy + 2 * CHIP_WIDTH); 
        pointList[3] = new Point(sx , sy + 2 * CHIP_WIDTH);        
        m_chipList[0].SetChip(1, pointList, 4); //m_chipList[0]為一個2W*2W的正方形  
        sx =170 ; sy = 90; 
        pointList[0] = new Point(sx + CHIP_WIDTH * 2, sy); 
        pointList[1] = new Point(sx + CHIP_WIDTH * 6, sy); 
        pointList[2] = new Point(sx + CHIP_WIDTH * 6, sy + CHIP_WIDTH); 
        pointList[3] = new Point(sx + CHIP_WIDTH * 2, sy + CHIP_WIDTH);        
        m_chipList[1].SetChip(2, pointList, 4); //m_chipList[1]為一個4W*1W的長方形  
        …… 
} 
private void Reset()
    {
        for (int i = 0; i < CHIP_COUNT; i++)
            m_chipList[i] = new CChip();
        Point[] pointList = new Point[MAX_POINTS];
        int sx=250, sy=0;
        pointList[0] = new Point(sx , sy );
        pointList[1] = new Point(sx +  2* CHIP_WIDTH, sy );
        pointList[2] = new Point(sx +  2* CHIP_WIDTH, sy + 2 * CHIP_WIDTH);
        pointList[3] = new Point(sx , sy + 2 * CHIP_WIDTH);      
        m_chipList[0].SetChip(1, pointList, 4); //m_chipList[0]為一個2W*2W的正方形
        sx =170 ; sy = 90;
        pointList[0] = new Point(sx + CHIP_WIDTH * 2, sy);
        pointList[1] = new Point(sx + CHIP_WIDTH * 6, sy);
        pointList[2] = new Point(sx + CHIP_WIDTH * 6, sy + CHIP_WIDTH);
        pointList[3] = new Point(sx + CHIP_WIDTH * 2, sy + CHIP_WIDTH);      
        m_chipList[1].SetChip(2, pointList, 4); //m_chipList[1]為一個4W*1W的長方形
        ……

 
讀出第n關的地圖信息
ReadOrgMap()方法從map.txt文本文件按行讀出第n關目標圖案的地圖信息到OrgMap二維數組中。
[csharp] private void ReadOrgMap(int n) 
    { 
        string filename = "map.txt"; 
        FileStream fs = File.OpenRead(filename); 
        StreamReader sr = new StreamReader(fs, Encoding.Default); 
        string t = null; 
        while (n > 0) 
        { 
            t = sr.ReadLine(); 
            n--; 
        } 
        sr.Close(); 
        fs.Close(); 
        string [] a = new string[36 + 1]; 
        a = t.Split(','); 
        for (int i = 1; i < 7; i++) 
            for (int j = 1; j < 7; j++)                 
                OrgMap[i-1, j-1]=Convert.ToInt16(a[(i-1)*6+j-1]); 
    } 
private void ReadOrgMap(int n)
    {
        string filename = "map.txt";
        FileStream fs = File.OpenRead(filename);
        StreamReader sr = new StreamReader(fs, Encoding.Default);
        string t = null;
        while (n > 0)
        {
            t = sr.ReadLine();
            n--;
        }
        sr.Close();
        fs.Close();
        string [] a = new string[36 + 1];
        a = t.Split(',');
        for (int i = 1; i < 7; i++)
            for (int j = 1; j < 7; j++)               
                OrgMap[i-1, j-1]=Convert.ToInt16(a[(i-1)*6+j-1]);
    }
 

 

“新方塊圖案”按鈕單擊事件
計算出新圖案序號n,調用Reset()對8個拼塊m_chipList 數組初始化並調用Draw_AllChip()畫出所有8個拼塊。
[csharp] private void button1_Click(object sender, EventArgs e)//“新圖案”按鈕  
    { 
        n++;                         
        if (n > max) 
        { 
            MessageBox.Show("沒有新圖案了"); 
            n--; 
            return; 
        } 
        ReadOrgMap(n);//讀目標地圖OrgMap文件  
        Map = new int[6, 6]; 
        Reset();                    // 初始化拼圖塊  
        Draw_AllChip();             //畫出所有拼塊  
    } 
private void button1_Click(object sender, EventArgs e)//“新圖案”按鈕
    {
        n++;                       
        if (n > max)
        {
            MessageBox.Show("沒有新圖案了");
            n--;
            return;
        }
        ReadOrgMap(n);//讀目標地圖OrgMap文件
        Map = new int[6, 6];
        Reset();     // 初始化拼圖塊
        Draw_AllChip();    //畫出所有拼塊
    }


Draw_AllChip()畫出所有拼塊

Draw_AllChip()根據新圖案序號n縮半畫出640*480的目標圖案,通過循環調用拼塊類Cchip. DrawChip()方法畫出所有拼塊。
[csharp] private void Draw_AllChip()         ///畫出所有拼塊及目標圖案  
    { 
        Bitmap bmp = new Bitmap(this.Width, this.Height); 
        this.BackgroundImage = bmp; 
        //Graphics g = this.CreateGraphics();  
        Graphics g = Graphics.FromImage(bmp);             
        g.Clear(this.BackColor); 
        string r=n.ToString()+".jpg";  
        Bitmap s = (Bitmap)Image.FromFile(r); 
        //g.DrawImage(s,new  Point(400, 0));   
        g.DrawImage(s, new Rectangle(450, 10, 320, 240),  
            new Rectangle(0, 0, 640, 480),GraphicsUnit.Pixel); //在(450,10)處顯示320*240圖案  
        for (int i = 0; i < CHIP_COUNT; i++) 
            m_chipList[i].DrawChip(g); 
    } 
private void Draw_AllChip()   ///畫出所有拼塊及目標圖案
    {
        Bitmap bmp = new Bitmap(this.Width, this.Height);
        this.BackgroundImage = bmp;
        //Graphics g = this.CreateGraphics();
        Graphics g = Graphics.FromImage(bmp);           
        g.Clear(this.BackColor);
        string r=n.ToString()+".jpg";
        Bitmap s = (Bitmap)Image.FromFile(r);
        //g.DrawImage(s,new  Point(400, 0));
        g.DrawImage(s, new Rectangle(450, 10, 320, 240),
            new Rectangle(0, 0, 640, 480),GraphicsUnit.Pixel); //在(450,10)處顯示320*240圖案
        for (int i = 0; i < CHIP_COUNT; i++)
            m_chipList[i].DrawChip(g);
    }
 

 

窗體鼠標按下事件
在窗體鼠標按下事件中,首先獲取鼠標坐標Point(e.X, e.Y),判斷是否在某拼塊區域中,如果在記錄用戶選中的拼塊序號。並通過e.Button判斷用戶是否右鍵單擊,如果右鍵單擊則將用戶選中的拼塊順時針旋轉90度,重畫所有拼塊。
[csharp] private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
        Point p = new Point(e.X, e.Y); 
        for (int i = 0; i < CHIP_COUNT; i++) 
            if (m_chipList[i].PtInChip(p) == true) 
            { 
                m_nCurrIndex = i;           //記錄用戶選中的拼塊  
                Drag_PictBox = true;        //移動標志賦值為真  
                oldx = e.X; 
                oldy = e.Y; 
                break;   
            }             
        if (e.Button == MouseButtons.Right) //右鍵旋轉  
        {                 
            m_chipList[m_nCurrIndex].Rotation2(); //旋轉順時針45度  
            m_chipList[m_nCurrIndex].Rotation2(); //旋轉順時針45度  
            Draw_AllChip();//畫出所有拼塊  
        } 
     } 
private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        Point p = new Point(e.X, e.Y);
        for (int i = 0; i < CHIP_COUNT; i++)
            if (m_chipList[i].PtInChip(p) == true)
            {
                m_nCurrIndex = i;           //記錄用戶選中的拼塊
                Drag_PictBox = true;        //移動標志賦值為真
                oldx = e.X;
                oldy = e.Y;
                break; 
            }           
        if (e.Button == MouseButtons.Right) //右鍵旋轉
        {               
            m_chipList[m_nCurrIndex].Rotation2(); //旋轉順時針45度
            m_chipList[m_nCurrIndex].Rotation2(); //旋轉順時針45度
            Draw_AllChip();//畫出所有拼塊
        }
     }


窗體鼠標移動事件
在窗體鼠標移動中,首先獲取鼠標坐標Point(e.X, e.Y),顯示在窗體上,然後計算偏移量傳給拼塊類Cchip.Move2方法,改變用戶選中拼塊m_nCurrIndex的各點坐標,重畫所有拼塊。
[csharp] private void Form1_MouseMove(object sender, MouseEventArgs e) 
    { 
        Point p = new Point(e.X, e.Y); 
        label1.Text = p.ToString(); 
        if (Drag_PictBox == true) 
        { 
            Cursor.Current = Cursors.Hand; 
            m_chipList[m_nCurrIndex].Move2(e.X - oldx, e.Y - oldy); //移動  
            Draw_AllChip();                //畫出所有拼塊  
        }         
        oldx = e.X; 
        oldy = e.Y; 
    } 
private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        Point p = new Point(e.X, e.Y);
        label1.Text = p.ToString();
        if (Drag_PictBox == true)
        {
            Cursor.Current = Cursors.Hand;
            m_chipList[m_nCurrIndex].Move2(e.X - oldx, e.Y - oldy); //移動
            Draw_AllChip();                //畫出所有拼塊
        }       
        oldx = e.X;
        oldy = e.Y;
    }
 
窗體鼠標松開事件
在窗體鼠標松開事件中,對用戶移動的m_nCurrIndex拼塊進行位置校正,保證移動到適當的方格位置處。並判斷游戲是否成功。
[csharp] private void Form1_MouseUp(object sender, MouseEventArgs e) 
    { 
        Drag_PictBox = false; 
        //對用戶選中的m_nCurrIndex拼塊坐標進行修正,放置到正確位置  
        m_chipList[m_nCurrIndex].Verity(CHIP_WIDTH);             
        Draw_AllChip();                //畫出所有拼塊  
        if(Win()) MessageBox.Show("成功完成此關")  ;   //判斷是否成功  
    } 
private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        Drag_PictBox = false;
        //對用戶選中的m_nCurrIndex拼塊坐標進行修正,放置到正確位置
        m_chipList[m_nCurrIndex].Verity(CHIP_WIDTH);           
        Draw_AllChip();                //畫出所有拼塊
        if(Win()) MessageBox.Show("成功完成此關")  ;   //判斷是否成功
    }
 
窗體的鼠標雙擊事件
在窗體的鼠標雙擊事件中,對相應拼塊進行水平翻轉。
[csharp] private void Form1_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
        Point p = new Point(e.X, e.Y); 
        for (int i = 0; i < CHIP_COUNT; i++) 
            if (m_chipList[i].PtInChip(p) == true) 
            { 
                m_nCurrIndex = i;           //記錄用戶選中的拼塊  
                Drag_PictBox = true;          
                oldx = e.X; 
                oldy = e.Y; 
                break; 
            } 
        m_chipList[m_nCurrIndex].ReverseTurn(); //// 水平反轉  
        Draw_AllChip();//畫出所有拼塊  
    } 
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        Point p = new Point(e.X, e.Y);
        for (int i = 0; i < CHIP_COUNT; i++)
            if (m_chipList[i].PtInChip(p) == true)
            {
                m_nCurrIndex = i;           //記錄用戶選中的拼塊
                Drag_PictBox = true;        
                oldx = e.X;
                oldy = e.Y;
                break;
            }
        m_chipList[m_nCurrIndex].ReverseTurn(); //// 水平反轉
        Draw_AllChip();//畫出所有拼塊
    }
 
窗體重繪事件
[csharp] </pre><pre class="csharp" name="code">在窗體重繪事件中,繪制棋盤和本關已固定的綠色方塊。 
    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
        Graphics gp = e.Graphics; 
        SolidBrush myBrush = new SolidBrush(Color.Green); 
        for (int i = 0; i < 6; i++)  //繪制本關已固定的綠色方塊  
            for (int j = 0; j < 6; j++) { 
                if(OrgMap[i,j]==0) 
                    gp.FillRectangle(myBrush, i * CHIP_WIDTH,  
                                    j * CHIP_WIDTH, CHIP_WIDTH, CHIP_WIDTH); 
            } 
        Pen p = new Pen(Color.Brown, 1); 
        for (int i = 1; i < 7; i++)              //繪制棋盤  
            for (int j = 1; j < 7; j++) { 
                gp.DrawLine(p, 1, j * CHIP_WIDTH, CHIP_WIDTH*6, j * CHIP_WIDTH); 
                gp.DrawLine(p, i * CHIP_WIDTH, 1, i * CHIP_WIDTH, CHIP_WIDTH * 6);                      
            } 
    }  
</pre><pre class="csharp" name="code">在窗體重繪事件中,繪制棋盤和本關已固定的綠色方塊。
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics gp = e.Graphics;
        SolidBrush myBrush = new SolidBrush(Color.Green);
        for (int i = 0; i < 6; i++) //繪制本關已固定的綠色方塊
            for (int j = 0; j < 6; j++) {
                if(OrgMap[i,j]==0)
                    gp.FillRectangle(myBrush, i * CHIP_WIDTH,
                                    j * CHIP_WIDTH, CHIP_WIDTH, CHIP_WIDTH);
            }
        Pen p = new Pen(Color.Brown, 1);
        for (int i = 1; i < 7; i++)     //繪制棋盤
            for (int j = 1; j < 7; j++) {
                gp.DrawLine(p, 1, j * CHIP_WIDTH, CHIP_WIDTH*6, j * CHIP_WIDTH);
                gp.DrawLine(p, i * CHIP_WIDTH, 1, i * CHIP_WIDTH, CHIP_WIDTH * 6);                    
            }
    }

 

摘自 chenyujing1234的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved