连连看游戏源代码C#.doc_第1页
连连看游戏源代码C#.doc_第2页
连连看游戏源代码C#.doc_第3页
连连看游戏源代码C#.doc_第4页
连连看游戏源代码C#.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

连连看using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Collections; /ArrayList命名空间namespace 连连看 public partial class Form1 : Form private Bitmap Source; /所有动物图案的图片 private int W = 50; /动物方块图案的宽度 private int GameSize=10; /布局大小即行列数 private bool Select_first = false; /是否已经选中第一块 private int x1, y1; /被选中第一块的地图坐标 private int x2, y2; /被选中第二块的地图坐标 Point z1, z2; /折点棋盘坐标 private int m_nCol = 10; private int m_nRow = 10; private int m_map = new int10*10; private int BLANK_STATE = -1; public enum LinkType LineType,OneCornerType,TwoCornerType; LinkType LType; /连通方式 public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) Source = (Bitmap)Image.FromFile(.resanimal.bmp); this.pictureBox1.Height = W * (m_nRow + 2); this.pictureBox1.Width = W * (m_nCol+2); this.pictureBox1.Top = 0; this.pictureBox1.Left = 0; /当前窗体标题栏高度 int d = (this.Height - this.ClientRectangle.Height); this.Height = this.pictureBox1.Height + this.pictureBox1.Top+ d; this.Width = this.pictureBox1.Width + this.pictureBox1.Left ; /for (int i = 0; i 10 * 10; i+) / / m_mapi = i % 6; / StartNewGame(); Init_Graphic(); private void StartNewGame() /初始化地图,将地图中所有方块区域位置置为空方块状态 for(int iNum=0;iNum(m_nCol*m_nRow);iNum+) m_mapiNum = 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;j4;j+) tmpMap.Add(i); /每次从上面的临时地图tmpMap中取走(获取后并在临时地图删除) /一个动物放到地图的空方块上 for (int i = 0; i m_nRow * m_nCol; i+) /随机挑选一个位置 int nIndex = r.Next() % tmpMap.Count ; /获取该选定物件放到地图的空方块 m_mapi=(int)tmpMapnIndex; /在临时地图tmpMap除去该动物 tmpMap.RemoveAt(nIndex); private void Init_Graphic() Graphics g = get_Graphic(); /生成Graphics对象 for (int i = 0; i 10 * 10; i+) g.DrawImage(create_image(m_mapi), 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) System.Drawing.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 *39, 39, 39); /截取原图中b矩形区域的图形 g.DrawImage(Source, a, b, GraphicsUnit.Pixel); return bit; / / 检测是否已经赢得了游戏 / bool IsWin() /检测所有是否尚有非未被消除的方块 / (非BLANK_STATE状态) for(int i=0;iy2) /数据交换 int n=y1; y1=y2; y2=n; /直通 for(int i=y1+1;ix2) int x=x1; x1=x2; x2=x; /直通 for(int i=x1+1;i x2) /目标点(x1,y1),(x2,y2)两点交换 int n=x1; x1=x2; x2=n; n=y1; y1=y2; y2=n; if (y2 x2) int n = x1; x1 = x2; x2 = n; n = y1; y1 = y2; y2 = n; /右 int x, y; 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_mapy1 * 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_mapy * 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_mapy * m_nCol + x1 != BLANK_STATE) break; if (OneCornerLink(x1, y, x2, y2) z2.X = x1; z2.Y = y; return true; return false; bool XThrough(int x, int y, bool bAdd)/水平方向判断到边界的连通性 if (bAdd) /True,水平向右判断是否连通(是否为空) for (int i = x; i m_nCol; i+) if (m_mapy * m_nCol + i != BLANK_STATE) return false; else /false, 水平向左判断是否连通(是否为空) for (int i = 0; i = x; i+) if (m_mapy * m_nCol + i != BLANK_STATE) return false; return true; bool YThrough(int x, int y, bool bAdd) /垂直方向判断到边界的连通性) if(bAdd) /True, 垂直方向向下判断是否连通(是否为空) for(int i=y;im_nRow;i+) if(m_mapi*m_nCol+x!=BLANK_STATE) return false; else /false, 垂直方向向上判断是否连通(是否为空) for(int i=0;i=y;i+) if(m_mapi*m_nCol+x!=BLANK_STATE) return false; return true; / / 判断选中的两个方块是否可以消除 / 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_mapi = BLANK_STATE) continue; /第二个方块从前一个方块的后面开始 for (int j = i + 1; j DateTime.Now) / / 画选中方块之间连接线 / private void DrawLinkLine(int x1, int y1, int x2, int y2,LinkType LType) Graphics g = this.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); / / 清除选中方块之间连接线 / private void UnDrawLinkLine(int x1, int y1, int x2, int y2, LinkType LType) Graphics g = this.pictureBox1.CreateGraphics(); /生成Graphics对象 Pen p = new Pen(this.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 = n

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论