在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 连连看小游戏源码

C# 连连看小游戏源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:1.23M
  • 下载次数:28
  • 浏览次数:379
  • 发布时间:2018-05-13
  • 实例类别:C#语言基础
  • 发 布 人:1813694588
  • 文件格式:.rar
  • 所需积分:1
 相关标签: 游戏 连连看 小游戏

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

//ArrayList命名空间

namespace 连连看
{
    public partial class Form1 : Form
    {
        #region LinkType enum

        public enum LinkType
        {
            LineType,
            OneCornerType,
            TwoCornerType
        };

        #endregion

        private readonly int[] m_map = new int[4*4];
        private int BLANK_STATE = -1;

        private int GameSize = 4; //布局大小即行列数
        private LinkType LType; //连通方式
        private bool Select_first; //是否已经选中第一块
        private Bitmap Source; //所有动物图案的图片
        private int W = 50; //动物方块图案的宽度
        private int m_nCol = 4;
        private int m_nRow = 4;
        private int x1; //被选中第一块的地图坐标
        private int x2; //被选中第二块的地图坐标
        private int y1; //被选中第一块的地图坐标
        private int y2; //被选中第二块的地图坐标
        private Point z1, z2; //折点棋盘坐标

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Source = (Bitmap) Image.FromFile("..\\..\\res\\animal.bmp");
            pictureBox1.Height = W*(m_nRow   2);
            pictureBox1.Width = W*(m_nCol   2);
            pictureBox1.Top = 0;
            pictureBox1.Left = 0;
            //当前窗体标题栏高度
            int d = (Height - ClientRectangle.Height);
            Height = pictureBox1.Height   pictureBox1.Top   d;
            Width = pictureBox1.Width   pictureBox1.Left;
            //for (int i = 0; i < 10 * 10; i  )
            //{
            //    m_map[i] = i % 6;
            //} 
            StartNewGame();
            Init_Graphic();
        }

        private void StartNewGame()
        {
            //初始化地图,将地图中所有方块区域位置置为空方块状态
            for (int iNum = 0; iNum < (m_nCol*m_nRow); iNum  )
            {
                m_map[iNum] = BLANK_STATE;
            }
            Random r = new Random();
            //生成随机地图
            //将所有的动物物种放进一个临时的地图tmpMap中
            ArrayList tmpMap = new ArrayList();
            for (int i = 0; i < (m_nCol*m_nRow)/4; i  )
                for (int j = 0; j < 4; j  )
                    tmpMap.Add(i);

            //每次从上面的临时地图tmpMap中取走(获取后并在临时地图删除)
            //一个动物放到地图的空方块上
            for (int i = 0; i < m_nRow*m_nCol; i  )
            {
                //随机挑选一个位置
                int nIndex = r.Next()%tmpMap.Count;
                //获取该选定物件放到地图的空方块
                m_map[i] = (int) tmpMap[nIndex];

                //在临时地图tmpMap除去该动物
                tmpMap.RemoveAt(nIndex);
            }
        }

        private void Init_Graphic()
        {
            Graphics g = get_Graphic(); //生成Graphics对象

            for (int i = 0; i < 4*4; i  )
            {
                g.DrawImage(create_image(m_map[i]), W*(i%GameSize)   W,
                            W*(i/GameSize)   W, W, W);
            }
        }

        private Graphics get_Graphic()
        {
            if (pictureBox1.Image == null)
            {
                Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                pictureBox1.Image = bmp;
            }
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            return g;
        }

        public Graphics GetGraphicsObject(ref PictureBox pic)
        {
            Graphics g;
            Bitmap bmp = new Bitmap(pic.Width, pic.Height);
            pic.Image = bmp;
            g = Graphics.FromImage(bmp);
            return g;
        }

        //create_image()方法实现按标号n从所有动物图案的图片中截图。
        private Bitmap create_image(int n) //按标号n截图 
        {
            Bitmap bit = new Bitmap(W, W);
            Graphics g = Graphics.FromImage(bit); //生成Graphics对象 
            Rectangle a = new Rectangle(0, 0, W, W);
            Rectangle b = new Rectangle(0, n*45, 45, 45);
            //截取原图中b矩形区域的图形
            g.DrawImage(Source, a, b, GraphicsUnit.Pixel);
            return bit;
        }

        ///检测是否已经赢得了游戏
        private bool IsWin()
        {
            //检测所有是否尚有非未被消除的方块
            // (非BLANK_STATE状态)
            for (int i = 0; i < m_nRow*m_nCol; i  )
            {
                if (m_map[i] != BLANK_STATE)
                {
                    return false;
                }
            }
            return true;
        }

        private bool IsSame(int x1, int y1, int x2, int y2)
        {
            if (m_map[y1*m_nCol   x1] == m_map[y2*m_nCol   x2])
                return true;
            else
                return false;
        }

        //
        //X直接连通即垂直方向连通
        //
        private bool X_Link(int x, int y1, int y2)
        {
            //保证y1的值小于y2
            if (y1 > y2)
            {
                //数据交换
                int n = y1;
                y1 = y2;
                y2 = n;
            }

            //直通 	
            for (int i = y1   1; i <= y2; i  )
            {
                if (i == y2)
                    return true;
                if (m_map[i*m_nCol   x] != BLANK_STATE)
                    break;
            }
            return false;
        }

        //
        //Y直接连通即水平方向连通
        //
        private bool Y_Link(int x1, int x2, int y)
        {
            if (x1 > x2)
            {
                int x = x1;
                x1 = x2;
                x2 = x;
            }
            //直通
            for (int i = x1   1; i <= x2; i  )
            {
                if (i == x2)
                    return true;
                if (m_map[y*m_nCol   i] != BLANK_STATE)
                    break;
            }
            return false;
        }

        //
        //  1直角接口连通
        //
        private bool OneCornerLink(int x1, int y1, int x2, int y2)
        {
            if (x1 > x2) //目标点(x1,y1),(x2,y2)两点交换
            {
                int n = x1;
                x1 = x2;
                x2 = n;
                n = y1;
                y1 = y2;
                y2 = n;
            }
            if (y2 < y1) //(x1,y1)为矩形左下顶点,(x2,y2)点为矩形右上顶点
            {
                //判断矩形右下角折点(x2,y1)是否空
                if (m_map[y1*m_nCol   x2] == BLANK_STATE)
                {
                    if (Y_Link(x1, x2, y1) && X_Link(x2, y1, y2))
                        //判断折点(x2,y1)与两个目标点是否直通
                    {
                        z1.X = x2;
                        z1.Y = y1; //保存折点坐标到z1
                        return true;
                    }
                }
                //判断矩形左上角折点(x1,y2)是否空
                if (m_map[y2*m_nCol   x1] == BLANK_STATE)
                {
                    if (Y_Link(x2, x1, y2) && X_Link(x1, y2, y1))
                        //判断折点 (x1,y2)与两个目标点是否直通
                    {
                        z1.X = x1;
                        z1.Y = y2; //保存折点坐标到z1
                        return true;
                    }
                }
                return false;
            }
            else //(x1,y1)为矩形左上顶点,(x2,y2)点为矩形右下顶点
            {
                //判断矩形左下角折点(x1,y2)是否空
                if (m_map[y2*m_nCol   x1] == BLANK_STATE)
                {
                    if (Y_Link(x1, x2, y2) && X_Link(x1, y1, y2))
                        //判断折点 (x1,y2)与两个目标点是否直通
                    {
                        z1.X = x1;
                        z1.Y = y2; //保存折点坐标到z1
                        return true;
                    }
                }
                //判断矩形右上角折点(x2,y1)是否空
                if (m_map[y1*m_nCol   x2] == BLANK_STATE)
                {
                    if (Y_Link(x1, x2, y1) && X_Link(x2, y1, y2))
                        //判断折点(x2,y1)与两个目标点是否直通  
                    {
                        z1.X = x2;
                        z1.Y = y1; //保存折点坐标到z1
                        return true;
                    }
                }
                return false;
            }
        }

        ///2折点连通
        private bool TwoCornerLink(int x1, int y1, int x2, int y2)
        {
            if (x1 > x2)
            {
                int n = x1;
                x1 = x2;
                x2 = n;
                n = y1;
                y1 = y2;
                y2 = n;
            }
            //右
            int x, y;
            for (x = x1   1; x <= m_nCol; x  )
            {
                if (x == m_nCol)
                    //两个折点在选中方块的右侧,且两个折点在图案区域之外
                    if (XThrough(x2   1, y2, true))
                        //Y_Link(x2   1, m_nCol-1, y2)&&m_map[y1 * (m_nCol-1)   x]== BLANK_STATE
                    {
                        z2.X = m_nCol;
                        z2.Y = y1;
                        z1.X = m_nCol;
                        z1.Y = y2;
                        return true;
                    }
                    else
                        break;
                if (m_map[y1*m_nCol   x] != BLANK_STATE)
                    break;
                if (OneCornerLink(x, y1, x2, y2))
                {
                    z2.X = x;
                    z2.Y = y1;
                    return true;
                }
            }
            //左
            for (x = x1 - 1; x >= -1; x--)
            {
                if (x == -1)
                    //两个折点在选中方块的左侧,且两个折点在图案区域之外
                    if (XThrough(x2 - 1, y2, false))
                    {
                        z2.X = -1;
                        z2.Y = y1;
                        z1.X = -1;
                        z1.Y = y2;
                        return true;
                    }
                    else
                        break;

                if (m_map[y1*m_nCol   x] != BLANK_STATE)
                    break;
                if (OneCornerLink(x, y1, x2, y2))
                {
                    z2.X = x;
                    z2.Y = y1;
                    return true;
                }
            }
            //上
            for (y = y1 - 1; y >= -1; y--)
            {
                if (y == -1)
                    //两个折点在选中方块的上侧,且两个折点在图案区域之外
                    if (YThrough(x2, y2 - 1, false))
                    {
                        z2.X = x1;
                        z2.Y = -1;
                        z1.X = x2;
                        z1.Y = -1;

                        return true;
                    }
                    else
                        break;
                if (m_map[y*m_nCol   x1] != BLANK_STATE)
                    break;
                if (OneCornerLink(x1, y, x2, y2))
                {
                    z2.X = x1;
                    z2.Y = y;
                    return true;
                }
            }
            //下
            for (y = y1   1; y <= m_nRow; y  )
            {
                if (y == m_nRow)
                    //两个折点在选中方块的下侧,且两个折点在图案区域之外
                    if (YThrough(x2, y2   1, true))
                    {
                        z2.X = x1;
                        z2.Y = m_nRow;
                        z1.X = x2;
                        z1.Y = m_nRow;
                        return true;
                    }
                    else
                        break;
                if (m_map[y*m_nCol   x1] != BLANK_STATE)
                    break;
                if (OneCornerLink(x1, y, x2, y2))
                {
                    z2.X = x1;
                    z2.Y = y;
                    return true;
                }
            }
            return false;
        }

        private bool XThrough(int x, int y, bool bAdd) //水平方向判断到边界的连通性  
        {
            if (bAdd) //True,水平向右判断是否连通(是否为空)
            {
                for (int i = x; i < m_nCol; i  )
                    if (m_map[y*m_nCol   i] != BLANK_STATE)
                        return false;
            }
            else //false, 水平向左判断是否连通(是否为空)
            {
                for (int i = 0; i <= x; i  )
                    if (m_map[y*m_nCol   i] != BLANK_STATE)
                        return false;
            }
            return true;
        }

        private bool YThrough(int x, int y, bool bAdd) //垂直方向判断到边界的连通性)
        {
            if (bAdd) //True, 垂直方向向下判断是否连通(是否为空)
            {
                for (int i = y; i < m_nRow; i  )
                    if (m_map[i*m_nCol   x] != BLANK_STATE)
                        return false;
            }
            else //false, 垂直方向向上判断是否连通(是否为空)
            {
                for (int i = 0; i <= y; i  )
                    if (m_map[i*m_nCol   x] != BLANK_STATE)
                        return false;
            }
            return true;
        }

        //
        //  判断选中的两个方块是否可以消除
        //
        private bool IsLink(int x1, int y1, int x2, int y2)
        {
            //X直连方式即垂直方向连通
            if (x1 == x2)
            {
                if (X_Link(x1, y1, y2))
                {
                    LType = LinkType.LineType;
                    return true;
                }
            }
                //Y直连方式即水平方向连通
            else if (y1 == y2)
            {
                if (Y_Link(x1, x2, y1))
                {
                    LType = LinkType.LineType;
                    return true;
                }
            }
            //一个转弯(折点)的联通方式
            if (OneCornerLink(x1, y1, x2, y2))
            {
                LType = LinkType.OneCornerType;
                return true;
            }
                //两个转弯(折点)的联通方式
            else if (TwoCornerLink(x1, y1, x2, y2))
            {
                LType = LinkType.TwoCornerType;
                return true;
            }
            return false;
        }

        private bool Find2Block()
        {
            bool bFound = false;
            //第一个方块从地图的0位置开始
            for (int i = 0; i < m_nRow*m_nCol; i  )
            {
                //找到则跳出循环
                if (bFound)
                    break;
                //无动物的空格跳过
                if (m_map[i] == BLANK_STATE)
                    continue;
                //第二个方块从前一个方块的后面开始
                for (int j = i   1; j < m_nRow*m_nCol; j  )
                {
                    //第二个方块不为空 且与第一个方块的动物相同
                    if (m_map[j] != BLANK_STATE && m_map[i] == m_map[j])
                    {
                        //算出对应的虚拟行列位置
                        x1 = i%m_nCol;
                        y1 = i/m_nCol;
                        x2 = j%m_nCol;
                        y2 = j/m_nCol;

                        //判断是否可以连通
                        if (IsLink(x1, y1, x2, y2))
                        {
                            bFound = true;
                            break;
                        }
                    }
                }
            }
            if (bFound)
            {
                //(x1,y1)与(x2,y2)连通
                Graphics g = pictureBox1.CreateGraphics(); //生成Graphics对象
                //**********************************
                //Graphics g = get_Graphic();              //生成Graphics对象
                //************************************
                Pen myPen = new Pen(Color.Blue, 3);
                Rectangle b1 = new Rectangle(x1*W   1   W, y1*W   1   W, W - 3, W - 3);
                g.DrawRectangle(myPen, b1);
                Rectangle b2 = new Rectangle(x2*W   1   W, y2*W   1   W, W - 3, W - 3);
                g.DrawRectangle(myPen, b2);
            }
            return bFound;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Init_Graphic();
        }

        private void Delay(int second) //让程序延时数秒
        {
            DateTime now = DateTime.Now;
            while (now.AddSeconds(second) > DateTime.Now)
            {
            }
        }

        /// <summary>
        ///   画选中方块之间连接线
        /// </summary>
        private void DrawLinkLine(int x1, int y1, int x2, int y2, LinkType LType)
        {
            Graphics g = pictureBox1.CreateGraphics(); //生成Graphics对象
            Pen p = new Pen(Color.Red, 3);
            Point p1 = new Point(x1*W   W/2   W, y1*W   W/2   W);
            Point p2 = new Point(x2*W   W/2   W, y2*W   W/2   W);
            if (LType == LinkType.LineType)
                g.DrawLine(p, p1, p2);
            if (LType == LinkType.OneCornerType)
            {
                Point pixel_z1 = new Point(z1.X*W   W/2   W, z1.Y*W   W/2   W);
                g.DrawLine(p, p1, pixel_z1);
                g.DrawLine(p, pixel_z1, p2);
            }
            if (LType == LinkType.TwoCornerType)
            {
                Point pixel_z1 = new Point(z1.X*W   W/2   W, z1.Y*W   W/2   W);
                Point pixel_z2 = new Point(z2.X*W   W/2   W, z2.Y*W   W/2   W);
                if (!(p1.X == pixel_z2.X || p1.Y == pixel_z2.Y))
                {
                    //p1与pixel_z2不在一直线上,则pixel_z1,pixel_z2交换
                    Point c;
                    c = pixel_z1;
                    pixel_z1 = pixel_z2;
                    pixel_z2 = c;
                }
                g.DrawLine(p, p1, pixel_z2);
                g.DrawLine(p, pixel_z2, pixel_z1);
                g.DrawLine(p, pixel_z1, p2);
            }
        }

        /// <summary>
        ///   清除选中方块之间连接线
        /// </summary>
        private void UnDrawLinkLine(int x1, int y1, int x2, int y2, LinkType LType)
        {
            Graphics g = pictureBox1.CreateGraphics(); //生成Graphics对象
            Pen p = new Pen(BackColor, 3);
            Point p1 = new Point(x1*W   W/2   W, y1*W   W/2   W);
            Point p2 = new Point(x2*W   W/2   W, y2*W   W/2   W);
            if (LType == LinkType.LineType)
                g.DrawLine(p, p1, p2);
            if (LType == LinkType.OneCornerType)
            {
                Point pixel_z1 = new Point(z1.X*W   W/2   W, z1.Y*W   W/2   W);
                g.DrawLine(p, p1, pixel_z1);
                g.DrawLine(p, pixel_z1, p2);
            }
            if (LType == LinkType.TwoCornerType)
            {
                Point pixel_z1 = new Point(z1.X*W   W/2   W, z1.Y*W   W/2   W);
                Point pixel_z2 = new Point(z2.X*W   W/2   W, z2.Y*W   W/2   W);
                if (!(p1.X == pixel_z2.X || p1.Y == pixel_z2.Y))
                {
                    //p1与pixel_z2不在一直线上,则pixel_z1,pixel_z2交换
                    Point c;
                    c = pixel_z1;
                    pixel_z1 = pixel_z2;
                    pixel_z2 = c;
                }
                g.DrawLine(p, p1, pixel_z2);
                g.DrawLine(p, pixel_z2, pixel_z1);
                g.DrawLine(p, pixel_z1, p2);
            }
        }

        private void DrawSelectedBlock(int x, int y, Pen myPen, Graphics g)
        {
            //画选中方块的示意边框线
            Rectangle b1 = new Rectangle(x*W   1   W, y*W   1   W, W - 3, W - 3);
            g.DrawRectangle(myPen, b1);
        }

        private void ClearSelectedBlock(int x, int y, Graphics g)
        {
            //清除选中方块
            SolidBrush myBrush = new SolidBrush(BackColor); //定义背景色画刷
            Rectangle b1 = new Rectangle(x*W   W, y*W   W, W, W);
            g.FillRectangle(myBrush, b1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Cursor.Current = Cursors.Hand;
            Graphics g = pictureBox1.CreateGraphics(); //生成Graphics对象
            //**********************************
            //Graphics g = get_Graphic();              //生成Graphics对象
            //************************************
            Pen myPen = new Pen(Color.Red, 3);
            int x, y;
            if (e.Button == MouseButtons.Left)
            {
                //计算点击的方块的位置坐标
                x = (e.X - W)/W;
                y = (e.Y - W)/W;
                //如果该区域无方块
                if (m_map[y*m_nCol   x] == BLANK_STATE) return;
                if (Select_first == false)
                {
                    x1 = x;
                    y1 = y;
                    //画选定(x1,y1)处的框线
                    DrawSelectedBlock(x1, y1, myPen, g);
                    Select_first = true;
                }
                else
                {
                    x2 = x;
                    y2 = y;
                    //判断第二次点击的方块是否已被第一次点击选取,如果是则返回。
                    if ((x1 == x2) && (y1 == y2)) return;
                    //画选定(x2,y2)处的框线
                    //myPen = new Pen(Color.Red, 3);
                    //Rectangle b = new Rectangle(x2 * W   1, y2 * W   1, W - 3, W - 3);
                    //g.DrawRectangle(myPen, b);
                    DrawSelectedBlock(x2, y2, myPen, g);
                    //判断是否连通
                    if (IsSame(x1, y1, x2, y2) && IsLink(x1, y1, x2, y2))
                    {
                        DrawLinkLine(x1, y1, x2, y2, LType); //画选中方块之间连接线
                        Thread.Sleep(500); //延时0.5秒
                        ClearSelectedBlock(x1, y1, g); //清除第一个选定方块
                        ClearSelectedBlock(x2, y2, g); //清除第一个选定方块
                        //清空记录方块的值
                        m_map[y1*m_nCol   x1] = BLANK_STATE;
                        m_map[y2*m_nCol   x2] = BLANK_STATE;
                        Select_first = false;
                        UnDrawLinkLine(x1, y1, x2, y2, LType); //清除选中方块之间连接线
                    }
                    else //重新选定第一个方块
                    {
                        //重画(x1,y1)处动物图案来达到取消原选定(x1,y1)处的框线
                        int i = y1*m_nCol   x1;
                        g.DrawImage(create_image(m_map[i]), W*(i%GameSize)   W,
                                    W*(i/GameSize)   W, W, W);
                        //设置重新选定第一个方块的坐标
                        x1 = x;
                        y1 = y;
                        //画新选定(x1,y1)处的框线
                        //myPen = new Pen(Color.Red, 3);
                        //Rectangle b2 = new Rectangle(x1 * W   1, y1 * W   1, W - 3, W - 3);
                        //g.DrawRectangle(myPen, b2);
                        Select_first = true;
                    }
                }
            }
            if (e.Button == MouseButtons.Right) //智能查找功能
            {
                if (!Find2Block()) MessageBox.Show("没有连通的方块了!!");
            }
            //察看是否已经胜利
            if (IsWin())
            {
                MessageBox.Show("恭喜您胜利闯关,即将开始新局", "提示",  MessageBoxButtons.OK, MessageBoxIcon.Question);
                Form2 f2 = new Form2();
                f2.Show();
                this.Hide();
                //StartNewGame();
            }
        }
    }
}

实例下载地址

C# 连连看小游戏源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警