实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Media; using System.Runtime.InteropServices; using System.Threading; using System.Resources; using System.Reflection; //======================================================================= //本程序由本人独立完成,允许修改,但请保留本处. //联系方式:blessyou312@163.com //blog:blog.csdn.net/blessyou312 hi.baidu.com/blessyou312 //======================================================================= namespace LLK { public partial class Form1 : Form { #region//API [DllImport("Winmm.dll")] public static extern long PlaySound(string name,long module,long flag); #endregion public Form1() { InitializeComponent(); IniteBmp(MAXPICS); } Music music = new Music(); SoundPlayer selectplayer = new SoundPlayer("Sounds\\select.wav"); SoundPlayer eraseplayer = new SoundPlayer("Sounds\\erase.wav"); SoundPlayer refreshplayer = new SoundPlayer("Sounds\\refresh.wav"); SoundPlayer bombplayer = new SoundPlayer("Sounds\\bomb.wav"); int[,] gmap = new int[MAPWIDTH,MAPHEIGHT];//实际的图片矩阵为19*11 Graphics g_g = null;//全局画布 const int PBMAX = 100;//processbar的最大值 static int pbvalue=PBMAX;//proecssbar现在的值 static int reducestep = 1;//第次减少的步数 static long score = 0;//游戏得分 //图片的宽和高 private const int PICWIDTH = 31; private const int PICHEIGHT = 34; private const int MAPWIDTH = 19; private const int MAPHEIGHT = 11; private const int MAXPICS = 39; //本局中图片的数量 private int picnum = 18; private int multipic = 4;//一张图片重复出来的次数,一定为偶数 //加载到内存中的图片 Image[] img = new Image[39]; Image[] bombimg = new Image[6]; Kernal AI=null; //游戏开始 bool bStart = false; //加载图 private void IniteBmp(int maxnum) { g_g = this.CreateGraphics(); for (int i = 0; i < MAPWIDTH; i ) for (int j = 0; j < MAPHEIGHT; j ) gmap[i, j] = 0; IniteRandomMap(ref gmap, maxnum); AI = new Kernal(ref gmap); for (int i = 0; i < maxnum; i ) { ResourceManager rm = new ResourceManager("LLK.data", Assembly.GetExecutingAssembly()); img[i]= (Image)rm.GetObject(i.ToString() ".bmp"); //img[i] = (Image)Bitmap.FromFile("Images\\" (i 1).ToString() ".bmp"); } for (int i = 0; i < 6; i ) { //bombimg[i] = (Image)Bitmap.FromFile("Images\\B" (i 1).ToString() ".bmp"); } } private bool CheckWin(ref int[,] map) { bool Win = true; for (int i = 0; i < MAPWIDTH; i ) for (int j = 0; j < MAPHEIGHT; j ) if (map[i, j] != 0) Win = false; return Win; } private void IniteRandomMap(ref int[,] map,int num) { Random r=new Random(); while (num > 0) { for (int i = 0; i < multipic; i ) { int xrandom = r.Next(19) ; int yrandom = r.Next(11) ; if (map[xrandom, yrandom] == 0) { map[xrandom, yrandom] = num; } else i--; } num--; } } private void FreshMap(ref int[,] map) { Random r = new Random(); for(int i=0;i<MAPWIDTH;i ) for (int j = 0; j < MAPHEIGHT; j ) { if (gmap[i, j] != 0) { int x = r.Next(19); int y = r.Next(11); int temp = gmap[x, y]; gmap[x, y] = gmap[i, j]; gmap[i, j] = temp; } } TransportMap(ref gmap); } private void TransportMap(ref int[,] map) { for(int i=0;i<MAPWIDTH;i ) for (int j = 0; j < MAPHEIGHT; j ) { AI.GiveMapValue(i, j, map[i, j]); } } //在指定位置画指定图 private void Draw(Graphics g, Image scrImg, int PicX,int PicY) { g.DrawImage(scrImg, new Point(PicX, PicY)); } private void Form1_Paint(object sender, PaintEventArgs e) { g_g.DrawLine(new Pen(new SolidBrush(Color.DeepSkyBlue), 5), 0, 11 * 34 5, 19 * 34, 11 * 34 5); if (bStart) { for (int i = 0; i < MAPWIDTH; i ) for (int j = 0; j < MAPHEIGHT; j ) { if (gmap[i, j] != 0) { Draw(g_g, img[gmap[i, j] - 1], i * PICWIDTH, j * PICHEIGHT); } } } } int two = 0;//用来计算点击块的次数,最大为2 Point fpoint;//记录第一次点击的位置 private void Form1_MouseDown(object sender, MouseEventArgs e) { int curx = e.X / 31; int cury = e.Y/ 34; if (curx > 18 || cury > 10) return; if (gmap[curx, cury] != 0) { selectplayer.Play(); Pen pen = new Pen(new SolidBrush(this.BackColor)); g_g.DrawRectangle(pen, new Rectangle(curx * 31, cury * 34, 31, 34)); two ; switch (two) { case 1: fpoint = new Point(curx, cury); break; case 2: if (AI.IsLink(fpoint.X, fpoint.Y, curx, cury)) { two = 0; eraseplayer.Play(); //获得拐点数目和位置,做画线处理 ProcessCorner(fpoint,new Point(curx,cury)); if (CheckWin(ref gmap)) { starttimer = false; MessageBox.Show("恭喜过关!"); bStart = false; picnum ;//种类加1 if (picnum>38) { picnum = 39; multipic = 2; if (picnum * multipic > 209) { MessageBox.Show("你太强悍了,请联系尚占锋,请为你提高难度!哈哈!"); return; } } button1_Click_1(sender, e); } } else { two = 1; fpoint = new Point(curx, cury); } break; default: break; } } } //此方法用做处理画线和消去,写的比较烂 private void ProcessCorner(Point p1,Point p2) { Point[] corner = new Point[3]; corner = AI.GetPoints(); Pen pen = new Pen(new SolidBrush(Color.Red), 5);//线画笔 Pen bkpen = new Pen(new SolidBrush(this.BackColor), 5);//擦去画笔 pbvalue = 4; // MoveUp(p1.X, p1.Y); //MoveUp(p2.X, p2.Y); switch (corner[2].X) { case 1: score = 20;//一个拐点加20; g_g.DrawLine(pen, new Point(p1.X*31 15,p1.Y*34 17), new Point(corner[0].X*31 15,corner[0].Y*34 17)); g_g.DrawLine(pen, new Point(p2.X * 31 15, p2.Y * 34 17), new Point(corner[0].X * 31 15, corner[0].Y * 34 17)); Thread.Sleep(100); EraseBlock(g_g, p1, p2); g_g.DrawLine(bkpen, new Point(p1.X * 31 15, p1.Y * 34 17), new Point(corner[0].X * 31 15, corner[0].Y * 34 17)); g_g.DrawLine(bkpen, new Point(p2.X * 31 15, p2.Y * 34 17), new Point(corner[0].X * 31 15, corner[0].Y * 34 17)); break; case 2: score = 40;//两个拐点加40 Point[] ps ={ new Point(p1.X*31 15,p1.Y*34 17),new Point( corner[1].X*31 15,corner[1].Y*34 17),new Point(corner[0].X*31 15,corner[0].Y*34 17),new Point(p2.X*31 15,p2.Y*34 17)}; g_g.DrawLines(pen, ps); Thread.Sleep(100); EraseBlock(g_g, p1, p2); g_g.DrawLines(bkpen,ps); //foreach (Point mp in ps) //{ //MessageBox.Show("(" mp.X.ToString() "," mp.Y.ToString() ")"); //} break; case 0: score = 10;//直连加10 g_g.DrawLine(pen, new Point(p1.X * 31 15, p1.Y * 34 17), new Point(p2.X * 31 15, p2.Y * 34 17)); Thread.Sleep(100); EraseBlock(g_g, p1, p2); g_g.DrawLine(bkpen, new Point(p1.X * 31 15, p1.Y * 34 17), new Point(p2.X * 31 15, p2.Y * 34 17)); break; default: break; } //RefreshMap(ref gmap); label5.Text = score.ToString(); } private void EraseBlock(Graphics g, Point p1, Point p2) { g.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(p1.X * 31, p1.Y * 34, 31, 34)); g.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(p2.X * 31, p2.Y * 34, 31, 34)); gmap[p1.X, p1.Y] = 0; gmap[p2.X, p2.Y] = 0; AI.GiveMapValue(p1.X, p1.Y, 0); AI.GiveMapValue(p2.X, p2.Y, 0); } bool starttimer = false; private void button1_Click_1(object sender, EventArgs e) { //处理Processbar if (!starttimer) { progressBar1.Value = PBMAX; pbtimer.Interval = 500; pbtimer.Start(); starttimer = true; } //处理分数 score = 0; picnum = Convert.ToInt16(textBox1.Text); multipic = Convert.ToInt16(textBox2.Text); if (picnum * multipic > 209) { MessageBox.Show("游戏区域内最多只有209个空,您选的数据太多!请重新选 !"); textBox1.Text = "18"; textBox2.Text = "4"; return; } IniteBmp(picnum); if (bStart) { MessageBox.Show("游戏已在运行!"); return; } else { bStart = true; this.Invalidate(); music.Play("Sounds\\bg-03.mid"); } } //将块向上移 private void MoveUp(int x, int y) { if (y < 1) return; for (int i = y; i < MAPHEIGHT-1;i ) { gmap[x, i] = gmap[x, i 1]; } } private void RefreshMap(ref int[,] map) { if (bStart) { for (int i = 0; i < MAPWIDTH; i ) for (int j = 0; j < MAPHEIGHT; j ) { if (gmap[i, j] != 0) { Draw(g_g, img[gmap[i, j] - 1], i * PICWIDTH, j * PICHEIGHT); } } } } private void button2_Click(object sender, EventArgs e) { refreshplayer.Play(); //Thread.Sleep(100); //bombplayer.Play(); FreshMap(ref gmap); this.Invalidate(); } /// <summary> /// 以下为爆炸处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { } private void pbtimer_Tick(object sender, EventArgs e) { pbvalue = pbvalue - reducestep; if (pbvalue > 100) pbvalue = 100; if (pbvalue == 0&&starttimer) { starttimer = false; pbtimer.Stop(); MessageBox.Show("Failed!"); return; } else progressBar1.Value = pbvalue; } } }
标签: 连连看
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论