基本信息
源码名称:C#连连看 游戏源码完整实例
源码大小:0.10M
文件格式:.rar
开发语言:C#
更新时间:2013-01-09
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
连连看 .net版本
public partial class Form1 : Form { private Bitmap Source; //所有动物图案的图片 private int W; //动物方块图案的宽度 private int GameSize=10; //布局大小即行列数 private bool Select_first = false; //是否已经选中第一块 private int x1, y1; //被选中第一块的地图坐标 private int x2, y2; //被选中第二块的地图坐标 private int m_nCol = 10; private int m_nRow = 10; private int[] m_map = new int[10*10]; private int BLANK_STATE = -1; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Source = (Bitmap)Image.FromFile("..\\..\\res\\animal.bmp"); W = this.Width / GameSize; //显示动物方块图案的宽度 this.Height = this.Width 30; for (int i = 0; i< 10 * 10; i ) { m_map[i] = i%6; } } //窗体第一次显示时发生 private void Form1_Shown(object sender, EventArgs e) { Init_Graphic(); } private void Init_Graphic() { //Graphics g = this.CreateGraphics(); //生成Graphics对象 //********************************** Graphics g = get_Graphic(); //生成Graphics对象 //************************************ for (int i = 0; i< 10 * 10; i ) { g.DrawImage(create_image(m_map[i]), W * (i % GameSize), W * (i / GameSize), W, W); } } private Graphics get_Graphic() { Bitmap bmp = new Bitmap(this.Width, this.Height); this.BackgroundImage = bmp; Graphics g = Graphics.FromImage(bmp); return g; } //public Graphics GetGraphicsObject(ref Form f) //{ // System.Drawing.Graphics g; // Bitmap bmp = new Bitmap(f.Width, f.Height); // f.BackgroundImage= 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 *39, 39, 39); //截取原图中b矩形区域的图形 g.DrawImage(Source, a, b, GraphicsUnit.Pixel); return bit; } private void Form1_MouseDown(object sender, MouseEventArgs e) { Cursor.Current = Cursors.Hand; Graphics g = this.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; y = e.Y / W; //如果该区域无方块 if (m_map[y * m_nCol x] == BLANK_STATE) return; if (Select_first == false) { x1 = x; y1 = y; Rectangle b1 = new Rectangle(x1 * W 1, y1 * W 1, W - 3, W - 3); g.DrawRectangle(myPen, b1); Select_first = true; } else { x2 = x; y2 = y; //判断第二次点击的方块是否已被第一次点击选取,如果是则返回。 if ((x1 == x2) && (y1 == y2)) return; // if (IsSame(x1, y1, x2, y2)&&IsLink(x1, y1, x2, y2)) { SolidBrush myBrush = new SolidBrush(this.BackColor); //定义背景色画刷 Rectangle b1 = new Rectangle(x1 * W, y1 * W, W, W); Rectangle b2 = new Rectangle(x2 * W, y2 * W, W , W); g.FillRectangle(myBrush, b2); g.FillRectangle(myBrush, b1); ////清空记录方块的值 m_map[y1 * m_nCol x1] = BLANK_STATE; m_map[y2 * m_nCol x2] = BLANK_STATE; Select_first = false; } else { //重画(x1,y1)处动物图案来达到取消选定的框线 int i = y1 * m_nCol x1; g.DrawImage(create_image(m_map[i]), W * (i % GameSize), W * (i / GameSize), W, W); x1 = e.X / W; y1 = e.Y / W; myPen = new Pen(Color.Red, 3); Rectangle b2 = new Rectangle(x2 * W 1, y2 * W 1, W - 3, W - 3); g.DrawRectangle(myPen, b2); Select_first = true; } } } //察看是否已经胜利 if (IsWin()) { MessageBox.Show("恭喜您胜利闯关,即将开始新局"); //StartNewGame(); } } /// /// 检测是否已经赢得了游戏 /// 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直接连通 // bool X1_Link_X2(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; } ////左通 //if(XThrough(x-1,y1,false)&&XThrough(x-1,y2,false)) // return true; ////右通 //if(XThrough(x 1,y1,true)&&XThrough(x 1,y2,true)) // return true; return false; } // //Y直接连通 // bool Y1_Link_Y2(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; } ////上通 //if(YThrough(x1,y-1,false)&&YThrough(x2,y-1,false)) // return true; ////下通 //if(YThrough(x1,y 1,true)&&YThrough(x2,y 1,true)) // return true; return false; } // // 是否同一直线通 // bool LineX(int x,int y1,int y2) { if(y1>y2) { int y=y1; y1=y2; y2=y; } for(int y=y1;y<=y2;y ) { if(m_map[y*m_nCol x]!=BLANK_STATE) return false; if(y==y2) return true; } return false; } // // 是否同一直线通 // bool LineY(int x1,int x2,int y) { if(x1>x2) { int x=x1; x1=x2; x2=x; } for(int x=x1;x<=x2;x ) { if(m_map[y*m_nCol x]!=BLANK_STATE) return false; if(x==x2) return true; } return false; } // // 1直角接口连通 // bool OneCornerLink(int x1, int y1,int x2, int y2) { if(x1>x2) { int n=x1; x1=x2; x2=n; n=y1; y1=y2; y2=n; } if(y2<y1) { if(LineY(x1 1,x2,y1)&&LineX(x2,y1,y2 1)) return true; if(LineY(x2-1,x1,y2)&&LineX(x1,y2,y1-1)) return true; } else { if(LineY(x1 1,x2,y1)&&LineX(x2,y1,y2-1)) return true; if(LineY(x2-1,x1,y2)&&LineX(x1,y2,y1 1)) return true; } return false; } // // 2直角接口连通 // 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; } //右通 if(XThrough(x1 1,y1,true)&&XThrough(x2 1,y2,true)) return true; //左通 if(XThrough(x1-1,y1,false)&&XThrough(x2-1,y2,false)) return true; //上通 if(YThrough(x1,y1-1,false)&&YThrough(x2,y2-1,false)) return true; //下通 if(YThrough(x1,y1 1,true)&&YThrough(x2,y2 1,true)) return true; //右 int x,y; for(x=x1 1;x<m_nCol;x ) { if (m_map[y1 * m_nCol x] != BLANK_STATE) break; if(OneCornerLink(x,y1,x2,y2)) return true; } //左 for(x=x1-1;x>-1;x--) { if(m_map[y1*m_nCol x]!=BLANK_STATE) break; if(OneCornerLink(x,y1,x2,y2)) return true; } //上 for(y=y1-1;y>-1;y--) { if(m_map[y*m_nCol x1]!=BLANK_STATE) break; if(OneCornerLink(x1,y,x2,y2)) return true; } //下 for(y=y1 1;y<m_nRow;y ) { if(m_map[y*m_nCol x1]!=BLANK_STATE) break; if(OneCornerLink(x1,y,x2,y2)) return true; } return false; } bool XThrough(int x, int y, bool bAdd) { if(bAdd) { for(int i=x;i<m_nCol;i ) if(m_map[y*m_nCol i]!=BLANK_STATE) return false; } else { for(int i=0;i<=x;i ) if(m_map[y*m_nCol i]!=BLANK_STATE) return false; } return true; } bool YThrough(int x, int y,bool bAdd) { if(bAdd) { for(int i=y;i<m_nRow;i ) if(m_map[i*m_nCol x]!=BLANK_STATE) return false; } else { for(int i=0;i<=y;i ) if(m_map[i*m_nCol x]!=BLANK_STATE) return false; } return true; } // // 判断选中的两个方块是否可以消除 // bool IsLink(int x1, int y1, int x2, int y2) { //X直连方式 if(x1==x2) { if(X1_Link_X2(x1,y1,y2)) return true; } //Y直连方式 else if(y1==y2) { if(Y1_Link_Y2(x1,x2,y1)) return true; } //一个转弯直角的联通方式 if(OneCornerLink(x1,y1,x2,y2)) { return true; } //两个转弯直角的联通方式 else if(TwoCornerLink(x1,y1,x2,y2)) { return true; } return false; } private void Form1_DoubleClick(object sender, EventArgs e) { 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 = this.CreateGraphics(); //生成Graphics对象 //********************************** //Graphics g = get_Graphic(); //生成Graphics对象 //************************************ Pen myPen = new Pen(Color.Red, 3); Rectangle b1 = new Rectangle(x1 * W 1, y1 * W 1, W - 3, W - 3); g.DrawRectangle(myPen, b1); Rectangle b2 = new Rectangle(x2 * W 1, y2 * W 1, W - 3, W - 3); g.DrawRectangle(myPen, b2); } } }