很多人都喜欢Windows附带的扫雷游戏,从上学的学生到上班一族,都在玩扫雷游戏,现在在网上找到了C#版得扫雷游戏,放到下面供需要者参考。
usingSystem;usingSystem.Drawing;usingSystem.Windows.Forms;namespaceWindowsFormsApplication1{publicpartialclassFrm1:Form{#region变量publicintqishu;publicstaticintls=20,hs=11;publicintlgs=30;//列数,行数。publicintbl=0;//是否重新布雷publicinttime;publicint[,]bulei=newint[ls,hs];//bulei数组的值0代表无雷,1-8代表雷的个数,9代表雷publicintceshi=0;publicint[,]click=newint[ls,hs];//10表示要清除既是单击的11表示右击一次,12表示右击两次16表示以清除过了publicintsum=0;publicinttimerEnable=0;//游戏所处的状态0是游戏中#endregionpublicFrm1(){InitializeComponent();}//////标识雷的个数和可插的旗数///publicvoidQishu(){if(qishu<=lgs){gamesource.Text="00"+(lgs-qishu)。ToString();}else{gamesource.Text="00"+(lgs-qishu)。ToString();}}//////画方格//////<实例对象>///<要画图的位置>///<要画图的位置>///<画图使用的颜色>publicvoidDline(Graphicsdline,intmx1,intmy1,Penmypen){dline.DrawLine(mypen,mx1,my1,mx1+20,my1);dline.DrawLine(mypen,mx1,my1,mx1,my1+20);dline.DrawLine(newPen(Color.Black),mx1+20,my1,mx1+20,my1+20);dline.DrawLine(newPen(Color.Black),mx1,my1+20,mx1+20,my1+20);}//////看是否可以向周围移动//////<移动的基本X坐标>///<移动的基本Y坐标>publicvoidPanjie(intx,inty){Pl(x,y);if(y-1>=0){Pl(x,y-1);if(x-1>=0){Pl(x-1,y-1);}if(x+1{Pl(x+1,y-1);}}if(y+1<hs){Pl(x,y+1);if(x-1>=0){Pl(x-1,y+1);}if(x+1<ls){Pl(x+1,y+1);}}if(x-1>=0){Pl(x-1,y);}if(x+1<ls){Pl(x+1,y);}}//////判断是否要清清除//////<要判断的基本X坐标>///<要判断的基本Y坐标>publicvoidPl(intx,inty){if(bulei[x,y]==0&&click[x,y]!=10&&click[x,y]!=11&&click[x,y]!=16){click[x,y]=10;Panjie(x,y);}if(bulei[x,y]>0&&bulei[x,y]<9&&click[x,y]!=10&&click[x,y]!=11&&click[x,y]!=16){click[x,y]=10;}}//////初始化数组,布雷,判断周围的雷数///publicvoidBlpl(){sum=ls*hs;qishu=0;intmx,my;//初始化数组9表示雷数intmx1,my1;intsum1=0;for(mx=0;mx<ls;mx++){for(my=0;my{bulei[mx,my]=0;click[mx,my]=0;}}while(sum1//决定雷的个数和雷德位置{sum1=0;Randomr=newRandom((int)DateTime.Now.Ticks);mx=r.Next(ls-1);my=r.Next(hs-1);bulei[mx,my]=9;for(mx1=0;mx1<ls;mx1++){for(my1=0;my1<hs;my1++){sum1+=bulei[mx1,my1];}}}//判断周围雷的个数for(mx=0;mx<ls;mx++){for(my=0;my<hs;my++){if(bulei[mx,my]!=9){if(mx-1>=0){if(bulei[mx-1,my]==9){bulei[mx,my]++;}if(my-1>=0&&bulei[mx-1,my-1]==9){bulei[mx,my]++;}if(my+1<hs&&bulei[mx-1,my+1]==9){bulei[mx,my]++;}}if(mx+1{if(bulei[mx+1,my]==9){bulei[mx,my]++;}if(my-1>=0&&bulei[mx+1,my-1]==9){bulei[mx,my]++;}if(my+1<hs&&bulei[mx+1,my+1]==9){bulei[mx,my]++;}}if(my-1>=0&&bulei[mx,my-1]==9){bulei[mx,my]++;}if(my+1{bulei[mx,my]++;}}}}}//////右击的重绘//////>publicvoidRdraw(Graphicsdline){stringtext;intmx,my;intmx1,my1;for(mx=0;mx<ls;mx++){for(my=0;my<hs;my++){mx1=mx*2+mx*20+4;my1=my*2+my*20+4;if(click[mx,my]==11){//第一次右击画旗dline.DrawLine(newPen(Color.White),mx1,my1,mx1+20,my1+20);dline.DrawLine(newPen(Color.White),mx1,my1,mx1,my1+20);dline.DrawLine(newPen(Color.Black),mx1+20,my1,mx1+20,my1+20);dline.DrawLine(newPen(Color.Black),mx1,my1+20,mx1+20,my1+20);dline.FillRectangle(newSolidBrush(Color.FromArgb(224,224,224)),mx1+1,my1+1,18,18);text="P";dline.DrawString(text,newFont("宋体",15),newSolidBrush(Color.Red),mx1+2,my1+2);}if(click[mx,my]==12){//第二次右击画问号dline.DrawLine(newPen(Color.White),mx1,my1,mx1+20,my1+20);dline.DrawLine(newPen(Color.White),mx1,my1,mx1,my1+20);dline.DrawLine(newPen(Color.Black),mx1+20,my1,mx1+20,my1+20);dline.DrawLine(newPen(Color.Black),mx1,my1+20,mx1+20,my1+20);dline.FillRectangle(newSolidBrush(Color.FromArgb(224,224,224)),mx1+1,my1+1,18,18);text="?";dline.DrawString(text,newFont("宋体",15),newSolidBrush(Color.Black),mx1+2,my1+2);}if(click[mx,my]==13){//第三次右击恢复原样click[mx,my]=2;dline.DrawLine(newPen(Color.White),mx1,my1,mx1+20,my1+20);dline.DrawLine(newPen(Color.White),mx1,my1,mx1,my1+20);dline.DrawLine(newPen(Color.Black),mx1+20,my1,mx1+20,my1+20);dline.DrawLine(newPen(Color.Black),mx1,my1+20,mx1+20,my1+20);dline.FillRectangle(newSolidBrush(Color.FromArgb(224,224,224)),mx1+1,my1+1,18,18);}}}}//////初始化屏幕(包还所有的)///publicvoidStartscr(){Graphicsdline=labdraw.CreateGraphics();time=0;Blpl();timer1.Enabled=false;Colorc=Color.FromArgb(224,224,224);labdraw.CreateGraphics()。Clear(c);Drawscreen(dline);timerEnable=0;labtime.Text=("000");}//////画界面//////>publicvoidDrawscreen(Graphicsdline){sum=0;//得到没清楚的格子数intmx,my;intmx1=0,my1=0;//得到要画图的坐标stringtext;for(mx=0;mx<ls;mx++){for(my=0;my<hs;my++){//画图地位置mx,my分别是屏幕x,y轴mx1=mx*2+mx*20+4;my1=my*2+my*20+4;if(click[mx,my]!=10&&click[mx,my]!=12&&click[mx,my]!=16&&click[mx,my]!=15){//下面用于画立体形矩形;sum++;Penmypen=newPen(Color.Gray);Dline(dline,mx1,my1,mypen);dline.FillRectangle(newSolidBrush(Color.FromArgb(224,224,224)),mx1+1,my1+1,18,18);}if((click[mx,my]==10||click[mx,my]==16)){click[mx,my]=16;//清除的格子//提示周围的雷数if(bulei[mx,my]<=8&&bulei[mx,my]>0){text=bulei[mx,my].ToString();dline.DrawString(text,newFont("宋体",15),newSolidBrush(Color.Black),mx1+2,my1+2);}//显示雷if((bulei[mx,my]==9||bulei[mx,my]==100)){text="*";dline.DrawString(text,newFont("宋体",15),newSolidBrush(Color.Black),mx1+2,my1+2);if(bulei[mx,my]==100){//被单击的雷上画上叉号dline.DrawLine(newPen(Color.Red),mx1,my1,mx1+20,my1+20);dline.DrawLine(newPen(Color.Red),mx1,my1+20,mx1+20,my1);}}}}//用于画旗和问号Rdraw(dline);}}//////当为雷时的算法//////>///>publicvoidPanlei(intx,inty){if(bulei[x,y]!=0){if(bulei[x,y]==9){timer1.Enabled=false;//使计时器停止timerEnable=1;//使游戏出游结束状态bulei[x,y]=100;inti,j;for(i=0;i<ls;i++){for(j=0;j<hs;j++){if(bulei[i,j]==9||bulei[i,j]==100){click[i,j]=10;//显示所有的雷}}}}if(bulei[x,y]>0&&bulei[x,y]<9){click[x,y]=10;}}else{Panjie(x,y);}}//////回到游戏开始的界面/////////privatevoidbtnreplay_Click(objectsender,EventArgse){//初始化一切包括布雷,界面,click数组Startscr();}//////计算游戏所用的时间/////////privatevoidtimer1_Tick(objectsender,EventArgse){time++;if(time<=99){if(time<10){labtime.Text=("00"+time.ToString());}else{labtime.Text=("0"+time.ToString());}}else{if(time<99){labtime.Text=(time.ToString());}else{timer1.Enabled=false;MessageBox.Show("RIMEOVER");time=0;timerEnable=1;}}}//////在启动的游戏时初始化界面/////////privatevoidlabdraw_Paint(objectsender,PaintEventArgse){Graphicsdline=e.Graphics;if(bl==0){Blpl();Drawscreen(dline);}else{Drawscreen(dline);}}////////单击时触发的事件/////////privatevoidlabdraw_MouseClick(objectsender,MouseEventArgse){bl=10;inti=0,j=0;if(timerEnable==0)//判断游戏是否处于结束状态{timer1.Enabled=true;Graphicsdline=labdraw.CreateGraphics();//将LAB1建立成图形对象intmx=0,my=0;intmx2=0,my2=0;intmx3=0,my3=0;//判断鼠标所点位置mx2=e.X;my2=e.Y;for(i=0;i<ls;i++){for(j=0;j<hs;j++){mx=4+i*2+i*20;my=4+j*2+j*20;//找到单击位置代表的数组if(mx<mx2&&mx+20+4>mx2&&my<my2&&my2<my+20+4){mx3=i;my3=j;break;}}if(j<9){break;}}//判断是左击还是右击if(e.Button==MouseButtons.Right&&click[mx3,my3]!=10&&mx3>=0&&my3>=0&&click[mx3,my3]!=16){//第2次右击if(click[mx3,my3]==11&&click[mx3,my3]!=10){click[mx3,my3]=12;--qishu;}else{//第1次右击if(click[mx3,my3]!=12&&click[mx3,my3]!=10){if(qishu{click[mx3,my3]=11;qishu++;}}if(click[mx3,my3]==12&&click[mx3,my3]!=10){click[mx3,my3]=13;}}//右击的绘图事件Rdraw(dline);Qishu();}else{//左击if(click[mx3,my3]!=11&&click[mx3,my3]!=16){click[mx3,my3]=15;Colorc=Color.FromArgb(224,224,224);//清屏用背景颜色labdraw.CreateGraphics()。Clear(c);Panlei(mx3,my3);Drawscreen(dline);}if(bulei[mx3,my3]==100){MessageBox.Show("GAMEOVER");timer1.Enabled=false;timerEnable=1;}}if(sum==lgs){MessageBox.Show("GAMEWIN");timer1.Enabled=false;timerEnable=1;}}}privatevoidFrm1_Load(objectsender,EventArgse){Qishu();//输出雷的个数//this.Height=hs*22+20;//this.Width=ls*22+8;}}}