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

📄 main.cs

📁 visual c# 网络编程技术与实践实例包括几个源码
💻 CS
📖 第 1 页 / 共 2 页
字号:
            this.Controls.Add(this.Beginbtn);
            this.Controls.Add(this.IPtextBox);
            this.Controls.Add(this.netinforlable);
            this.Controls.Add(this.JoinGamebtn);
            this.Controls.Add(this.st);
            this.Controls.Add(this.ConfigIpbtn);
            this.Controls.Add(this.launchGamebtn);
            this.Controls.Add(this.pictureBox5);
            this.Controls.Add(this.pictureBox4);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.pictureBox3);
            this.Controls.Add(this.pictureBox2);
            this.Controls.Add(this.pictureBox1);
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "网络五子棋对战";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
            this.Load += new System.EventHandler(this.MainForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox5)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			usingbmp=oldbmp=new Bitmap(this.pictureBox1.Image);
		}

		private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(exists)
			try
			{
				five.pointx=e.X-21;
				five.pointy=e.Y-24;
				five.cheesemove(this.pictureBox1,this.turn);
			}
			catch
			{
			}
		}
		

		private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(this.turn)
			{
				if(five.CanDown())
				{ 
					try
					{
						string sendinfor=five.cellx.ToString()+"."+five.celly.ToString()+"."+(five.myturn);
						conn.Send(sendinfor);
						string t1,t2;
						this.pictureBox1.Image=five.DownChess(out t1,out t2);		
						this.label1.Text=t1;
						this.label2.Text=t2;
						turn=false;
					}
					catch(Exception ex)
					{
						MessageBox.Show(ex.Message);
					}
				}
				
			}
			try
			{
				if(iswin.winchess(five.cellx,five.celly)==5) 
				{
					MessageBox.Show("游戏结束,白方胜利");
					this.pictureBox1.Enabled=false;
					this.st.Text="游戏结束,白方胜利";
					this.timer1.Stop();
					this.conn.StopListen();
				}
				if(iswin.winchess(five.cellx,five.celly)==-5) 
				{
					MessageBox.Show("游戏结束,黑方胜利");
					this.pictureBox1.Enabled=false;
				
					this.timer1.Stop();
					this.conn.StopListen();
				}
			}
			catch
			{
				this.st.Text="游戏还没有开始,无法下棋";
			}
			
			
		}
		private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try{conn.StopListen();}
			catch{}
			Application.Exit();
		}

		private void Configbutton_Click(object sender, System.EventArgs e)
		{
			config.Show();
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			this.st.Text=this.conn.str;
			string nowinfor;

			info=this.conn.info;
			if(oldinfo!=info)//有新消息
			{
				this.netinforlable.Text=info;
				try
				{
					int dot=info.IndexOf(".");
					five.cellx=Convert.ToInt32(info.Substring(0,dot));
					nowinfor=info.Substring(dot+1);
					dot=nowinfor.IndexOf(".");
					five.celly=Convert.ToInt32(nowinfor.Substring(0,dot));
					nowinfor=nowinfor.Substring(dot+1);
					five.myturn=Convert.ToBoolean(nowinfor);
					string t1,t2;
					this.pictureBox1.Image=five.DownChess(out t1,out t2);	//对方下棋
					
					this.label1.Text=t1;
					this.label2.Text=t2;
					if(iswin.winchess(five.cellx,five.celly)==5) 
					{

						this.st.Text="游戏结束,白方胜利";
						this.timer1.Stop();
						this.conn.StopListen();
						this.pictureBox1.Enabled=false;						
					}
					if(iswin.winchess(five.cellx,five.celly)==-5) 
					{

						this.st.Text="游戏结束,黑方胜利";
						this.timer1.Stop();
						this.conn.StopListen();
						this.pictureBox1.Enabled=false;
					}
					this.turn=true;
				}
				catch
				{
					this.st.Text=info;
					try
					{
						if(info.IndexOf("加")>0) exists=true;
						string joinedip=info.Substring(0,info.IndexOf("加"));
						IPtextBox.Text=conn.IP=joinedip;
					}
					catch
					{

					}
				}
			}
			oldinfo=info;
		}
		private void NewGamebutton_Click(object sender, System.EventArgs e)
		{
			five=new chessgo();
			five.AddBmp(usingbmp,new Bitmap(this.pictureBox4.Image),new Bitmap(this.pictureBox5.Image));
			iswin=new Win(five.pos);
			this.pictureBox1.Enabled=true;
			this.pictureBox1.Image=oldbmp;
			this.pictureBox1.Refresh();
			conn.Start();
			this.timer1.Start();
			this.turn=true;
			this.exists=false;
			this.launchGamebtn.Enabled=false;
			this.JoinGamebtn.Enabled=false;
		}

		private void Joinbutton_Click(object sender, System.EventArgs e)
		{
			if(this.IPtextBox.Text=="") 
			{
				MessageBox.Show("对方IP还没有配置,请点击配置");
				return;
			}
			conn.IP=IPtextBox.Text;
			string sta="";
			conn.Send(ref sta,Connect.getIP()+"加入游戏!");
			if(sta=="")
			{
				NewGamebutton_Click(null,null);
				this.st.Text="加入成功!";
				this.exists=true;
				this.JoinGamebtn.Enabled=false;
				this.launchGamebtn.Enabled=false;
			}
			else
			{
				this.st.Text=sta;
			}
			this.turn=false;
		
		}

		private void Rebuildbutton_Click(object sender, System.EventArgs e)
		{
			MainForm mf=new MainForm();
			mf.Show();
			mf.IPtextBox.Text=this.IPtextBox.Text;
			mf.Joinbutton_Click(null,null);
			if(mf.st.Text.IndexOf("拒绝")>0)//加入不成功
			{
				mf.NewGamebutton_Click(null,null);
			}
			try
			{
				this.conn.StopListen();
				this.timer1.Stop();
			}
			catch
			{
			}
			this.Hide();
		}

		private void StopNetbutton_Click(object sender, System.EventArgs e)
		{
			this.conn.StopListen();
		}
		
	}
}

⌨️ 快捷键说明

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