⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 软件名称:挖雷 游戏版 本:1.01编程语言:Visual Studio .NET C# (Beta 2)调试环境:WINDOWS 2000 Professional运行环境:需要 Microsoft
💻 CS
📖 第 1 页 / 共 2 页
字号:
			g.FillRectangle(new SolidBrush(Color.Gainsboro), mineCountControl1.Right, 12, faceControl1.Left - mineCountControl1.Right, 27);
			g.FillRectangle(new SolidBrush(Color.Gainsboro), faceControl1.Right, 12, timerControl1.Left - faceControl1.Right , 27);

			ColorBox(10, 50, this.ClientSize.Width-10, this.ClientSize.Height-10, 3, true, false, Color.Gainsboro);

			ReDraw( this.MToAT(e.ClipRectangle.Left, e.ClipRectangle.Top), this.MToAT(e.ClipRectangle.Right, e.ClipRectangle.Bottom) );
		}

		protected override void OnMouseDown(MouseEventArgs e)
		{
			Point p;
			try
			{
				p = MToA(e.X, e.Y);
			}
			catch
			{
				return;
			}
			if(Turn[p.Y, p.X] == -1 || GameStartMark == false)//方格被翻开或游戏结束
			{
				return;
			}
			timerControl1.Start();
			switch(e.Button)
			{
				case MouseButtons.Left:
					faceControl1.faceState = FaceControl.FaceState.Startle;
					if(Turn[p.Y, p.X] != 1 )//未插红旗
					{
						DrawMine( p.X, p.Y, -4);
						//记录鼠标单击时按下的方格
						MP.X = p.X;	MP.Y = p.Y;
					}
					break;
				case MouseButtons.Right:
					if(Turn[p.Y, p.X] == 0)//未插红旗则插入红旗
					{
						Turn[p.Y, p.X] = 1;
						mineCountControl1.mineCount --;
					}
					else
					{
						Turn[p.Y, p.X] = 0;
						mineCountControl1.mineCount ++;
					}
					ReDraw(p, p);
					break;
			}
		}

		protected override void OnMouseUp(MouseEventArgs e)
		{
			Point p;
			try
			{
				p = MToA(e.X, e.Y);
			}
			catch
			{
				if(InBox(new System.Drawing.Rectangle(0, 0, BoardWidth - 1, BoardHeight - 1), MP) == true) ReDraw(MP, MP);
				faceControl1.faceState = FaceControl.FaceState.Up;
				return;
			}
			if(GameStartMark == false)//如果游戏结束
			{
				return;
			}
			switch(e.Button)
			{
				case MouseButtons.Left:
					if(MP.X == p.X && MP.Y == p.Y && Turn[p.Y, p.X] != 1)//未插红旗
					{
						if(Turn[p.Y, p.X] != -1 )//未被翻开
						{
							if(Mine[p.Y, p.X] == 0)//如果这个位置周围没有地雷
							{
								ClearBoardMine(p.X, p.Y);//自动清除无雷区
							}
							else
							{
								Turn[p.Y, p.X] = -1;
								if(Mine[p.Y, p.X] == -1)//碰到地雷
								{
									timerControl1.Stop();
									Mine[p.Y, p.X] = -2;
									//游戏结束
									for(int i=0; i< BoardHeight; i++)
									{
										for(int j=0; j< BoardWidth; j++)
										{
											Turn[i, j] = -1;
										}
									}
									ReDraw(new Point(0, 0), new Point(BoardWidth-1, BoardHeight-1) );
									faceControl1.faceState = FaceControl.FaceState.Die;
									GameStartMark = false;
									return;
								}
								ReDraw( p, p );
							}
							faceControl1.faceState = FaceControl.FaceState.Up;
							//统计游戏结果
							int tn=0;
							for(int i=0; i< BoardHeight; i++)
							{
								for(int j=0;j< BoardWidth; j++)
								{
									if(Turn[i,j] == -1) tn++;
								}
							}
							if(tn == (BoardWidth * BoardHeight - MineCount) )//胜利
							{
								timerControl1.Stop();
								System.Windows.Forms.MessageBox.Show("恭喜您凯旋归来!","游戏结束");
								ReStart();
							}
						}
					}
					else
					{
						if(InBox(new System.Drawing.Rectangle(0, 0, BoardWidth - 1, BoardHeight - 1), MP) == true) ReDraw(MP, MP);
						faceControl1.faceState = FaceControl.FaceState.Up;
					}
					break;
			}
		}

		//重画
		private void ReDraw(Point p1, Point p2)
		{
			if(InBox(new Rectangle(0,0,BoardWidth-1,BoardHeight-1), p1) == false ||
				InBox(new Rectangle(0,0,BoardWidth-1,BoardHeight-1), p2) == false) return;
			System.Drawing.Graphics g = this.CreateGraphics();

			for(int i = p1.Y; i <= p2.Y; i++)
			{
				for(int j = p1.X; j <= p2.X; j++)
				{
					switch( Turn[i, j] )
					{
						case 0://未动过
							DrawMine(j, i, -3);
							break;
						case 1://插入红旗
							DrawMine(j, i, -5);
							break;
						default:
							DrawMine(j, i, Mine[i, j]);
							break;
					}
				}
			}
		}

		//重画
		private void ReDraw()
		{
			System.Drawing.Graphics g = this.CreateGraphics();
			System.Drawing.Rectangle rec = new Rectangle(0, 0, this.ClientRectangle.Right, this.ClientRectangle.Bottom);
			OnPaint( new PaintEventArgs(g, rec) );
		}

		//重新开始
		private void ReStart()
		{
			Reset(BoardWidth,BoardHeight,MineCount);
		}

		//以指定的行、列和地雷数量初始化,重新开始
		private void Reset(int NWidth, int NHeight, uint NCount)
		{
			System.Random ran = new Random(System.DateTime.Now.Second + (int)System.DateTime.Now.Ticks);
			BoardWidth = NWidth;
			BoardHeight = NHeight;
			Count = NCount;
			Mine = new int[ NHeight, NWidth ];
			Turn = new int[ NHeight, NWidth ];
			//初始化地雷
			for(int i=0; i< NHeight; i++)
			{
				for(int j=0; j< NWidth; j++)
				{
					Mine[i, j] = 0;
					Turn[i,j] = 0;
				}
			}
			//生成 N 颗地雷
			for(int i = 0; i < NCount; i++)
			{
				int nx, ny;
				do
				{
					nx = ran.Next(0, NWidth);
					ny = ran.Next(0, NHeight);
				}while(Mine[ ny, nx ] == -1);
				Mine[ ny, nx ] = -1;
				//描述雷区
				for(int j = nx-1; j <= nx +1; j++)
				{
					for(int k=ny -1 ; k <= ny+1; k++)
					{
						if( (j >=0 && j < NWidth) && (k >=0 && k < NHeight) )
						{
							if(Mine[k, j] != -1) Mine[k, j] ++;
						}
					}
				}
			}
			
			this.ClientSize = new System.Drawing.Size(26 + BoardWidth * 20, 66 + BoardHeight * 20);//设置窗体大小
			GameStartMark = true;
			mineCountControl1.Location = new Point(20, 13);
			faceControl1.Location = new Point(this.ClientSize.Width / 2 - 10, 15);
			timerControl1.Location = new Point(this.ClientSize.Width - 70, 13);
			timerControl1.Clear();
			ReDraw();
			faceControl1.faceState = FaceControl.FaceState.Up;
		}

		//在指定位置画物件
		private void DrawMine(int x, int y, int Value)
		{
			System.Drawing.Graphics g = this.CreateGraphics();
			System.Drawing.Pen p = new Pen(Color.Black, 1);//确定画笔的颜色和粗细
			System.Drawing.Color textColor = new Color();

			x = 13 + x * 20;
			y = 53 + y * 20;

			switch(Value)
			{
				case -5://画红旗
					ColorBox(x, y, x+ 19, y+ 19, 2, false, true, Color.Gainsboro);
					
					g.DrawLine(p, x+8, y + 14, x + 12, y + 14);
					g.FillPolygon(new SolidBrush(Color.Red),new Point[]{new Point(x+12, y + 11), new Point(x + 12, y + 2),new Point(x + 5, y + 7)});
					p.Width = 2;
					g.DrawLine(p, x+5, y + 16, x + 15, y + 16);
					g.DrawLine(p, x+11, y + 11, x + 11, y + 14);
					return;
				case -4://画按下的区域
					ColorBox(x, y, x+ 19, y+ 19, 2, true, true, Color.Gainsboro);
					return;
				case -3://画未按下的区域
					ColorBox(x, y, x+ 19, y+ 19, 2, false, true, Color.Gainsboro);
					return;
				case -2://画爆炸的地雷
				case -1://画未爆炸的地雷
					g.DrawRectangle( new Pen(System.Drawing.Color.Gray, 2) , x + 1, y + 1, 19, 19);
					g.FillRectangle( new SolidBrush( (Value == -2 )? Color.Red: Color.Gainsboro), x+2, y+2, 17, 17);
					g.FillEllipse(new SolidBrush(Color.Black), x+4, y+4, 12,12);

					g.DrawLine(p, x+4, y+4, x + 16, y + 16);
					g.DrawLine(p, x+10, y+3, x + 10, y + 17);
					g.DrawLine(p, x+15, y + 4, x + 4, y + 15);
					g.DrawLine(p, x+3, y + 10, x + 18, y + 10);
					g.FillEllipse(new SolidBrush(Color.White), x + 7, y + 7 , 3, 3);
					return;
				default:
					//确定字体颜色
				switch(Value)
				{
					case 0:
						textColor = Color.Gainsboro;
						break;
					case 1:
						textColor = Color.Blue;
						break;
					case 2:
						textColor = Color.Green;
						break;
					case 3:
						textColor = Color.Red;
						break;
					case 4:
						textColor = Color.DarkBlue;
						break;
					case 5:
						textColor = Color.DarkGreen;
						break;
					case 6:
						textColor = Color.DarkRed;
						break;
					case 7:
						textColor = Color.Fuchsia;
						break;
					case 8:
						textColor = Color.Black;
						break;
				}
					//显示周围的雷数
					g.DrawRectangle( new Pen(System.Drawing.Color.Gray, 2) , x + 1, y + 1, 19, 19);
					g.FillRectangle( new SolidBrush(Color.Gainsboro), x+2, y+2, 17, 17);
					g.DrawString( Value.ToString(),this.Font, new System.Drawing.SolidBrush( textColor ), x+2, y-2);
					return;
			}
		}

		private void menuItem3_Click(object sender, System.EventArgs e)
		{
			ReStart();
		}

		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			Reset(9, 9, 10);
		}

		private void menuItem6_Click(object sender, System.EventArgs e)
		{
			Reset(16, 16, 40);
		}

		private void menuItem7_Click(object sender, System.EventArgs e)
		{
			Reset(30, 16, 99);
		}

		private void menuItem13_Click(object sender, System.EventArgs e)
		{
			new Help().ShowDialog();
		}

		private void menuItem14_Click(object sender, System.EventArgs e)
		{
			new About().ShowDialog();
		}

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

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -