首页>代码>java swing编写坦克大战源码>/坦克大战/src/wyf/cgq/ClientAgentThread.java
package wyf.cgq;
import java.awt.*;import java.awt.event.*;
import javax.swing.*;import javax.swing.event.*;
import java.util.*;import java.io.*;
import java.net.*;
public class ClientAgentThread extends Thread{
	TankClient father;//声明客户端主窗体的引用
	Socket sc;//声明Socket引用
	boolean flag=true;//控制该线程的标志位
	DataInputStream din;//声明数据输入流 
	DataOutputStream dout;//声明数据输出流
	public ClientAgentThread(TankClient father,Socket sc){//构造器
		this.father=father;
		this.sc=sc;
		try{//创建输入输出流
			din=new DataInputStream(sc.getInputStream());
			dout=new DataOutputStream(sc.getOutputStream());
			String name=father.jtfNickName.getText().trim();//获得昵称
			dout.writeUTF("<#NICK_NAME#>"+name);//将昵称传送给服务器
		}
		catch(Exception e){e.printStackTrace();}
	}
	public void run(){//重写的run方法
		while(flag){
			try{
				String msg=din.readUTF().trim();//接收客户端信息
				if(msg.startsWith("<#SERVER_DOWN#>")){//收到服务器离开的信息
					father.jpz.setStart(false);//设置jpz状态为,停止游戏
					father.jpz.initialTank();//调用初始化方法还原游戏模型
					this.flag=false;//设置该线程标志位,停止该线程
					this.sc.close();//关闭Socket
					father.cat=null;//将主窗体的代理线程设为空
					JOptionPane.showMessageDialog(father,"主机离开了!!!","提示",
					          JOptionPane.INFORMATION_MESSAGE);//弹出提示对话框
					father.setState(true);//设置客户端主窗体的状态
				}
				else if(msg.startsWith("<#START#>"))
				{
					father.jpz.setStart(true);//设置jpz状态位,开始游戏
				}
				else if(msg.startsWith("<#HOST#>")){//收到更新主机的消息
					String info=msg.substring(8);//获得有用信息
					String[] detail=info.split("/");//分解信息
					int direction=new Integer(detail[0]);//将信息转化为所需要的 
					int blood=new Integer(detail[1]);
					int x=new Integer(detail[2]);
					int y=new Integer(detail[3]);
					father.jpz.updateHost(direction,blood,x,y);//调用updateHost方法更新主机状态
				}
				else if(msg.startsWith("<#BULLET#>")){//收到增加子弹的信息
					String info=msg.substring(10);
					String[] detail=info.split("/");//获得有用信息,并分解
					int id=new Integer(detail[0]);//将信息转化为所需要的
					int x=new Integer(detail[1]);
					int y=new Integer(detail[2]);
					father.jpz.addBullet(id,x,y);//调用addBullet方法向子弹列表中添加子弹
				}
				else if(msg.startsWith("<#UPDATEBULLET#>")){//收到更细子弹的信息
					String info=msg.substring(16);
					String[] detail=info.split("/");//获得有用信息并分解
					int id=new Integer(detail[0]);//将信息转化为需要的
					int x=new Integer(detail[1]);
					int y=new Integer(detail[2]);
					father.jpz.updateBullet(id,x,y);//调用updateBullet方法更新子弹
				}
				else if(msg.startsWith("<#BADTANK#>")){//获得更新电脑坦克的信息
					String info=msg.substring(11);
					String[] detail=info.split("/");//获得有用信息并分解
					int number=new Integer(detail[0]);
					int direction=new Integer(detail[1]);//转化为需要的
					int x=new Integer(detail[2]);
					int y=new Integer(detail[3]);
					father.jpz.updateTank(number,direction,x,y);//更新电脑坦克
				}
				else if(msg.startsWith("<#REMOVEBULLET#>")){//收到删除子弹的信息
					int id=new Integer(msg.substring(16));//获得该子弹的id号
					father.jpz.removeBullet(id);//调用removeBullet方法从子弹列表删除该子弹
				}
				else if(msg.startsWith("<#ADDSCORE#>")){//获得加分的信息
					int id=new Integer(msg.substring(12));//获得加分坦克的id号
					father.jpz.addScore(id);//给该坦克加分
				}
				else if(msg.startsWith("<#SUBBLOOD#>")){//收到减血的信息
					String info=msg.substring(12);//获得有用信息
					String [] detail=info.split("/");//将信息分解
					int id=new Integer(detail[0]);//获得减血的坦克的id号 
					int subnum=new Integer(detail[1]);//获得减血的量
					father.jpz.subBlood(id,subnum);//调用subBlood方法进行减血
				}
			}
			catch(Exception e){e.printStackTrace();}
		}
	}
	public void setFlag(boolean flag){this.flag=flag;}
}
最近下载更多
12345685  LV2 1月7日
陈小灏  LV14 2023年7月1日
哇撒是大帅耶  LV3 2022年12月17日
干翻元宇宙  LV1 2022年11月8日
WEIdong123  LV2 2022年10月26日
胡胡霜  LV2 2022年5月29日
xiaoqing12350  LV4 2022年5月21日
你想  LV1 2021年6月25日
射手湖心  LV1 2021年5月26日
5408517  LV1 2021年5月23日
最近浏览更多
dengge123  LV13 1月5日
12345685  LV2 1月2日
法拉利  LV1 2023年12月12日
woldxy  LV12 2023年9月28日
wangjialiang1  LV17 2023年8月23日
wgsidnb  LV1 2023年6月11日
gzryue  LV6 2023年4月23日
3043864991  LV2 2023年1月5日
哇撒是大帅耶  LV3 2022年12月17日
80730176  LV7 2022年12月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友