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

📄 createwallsmsg.java

📁 基于尚学堂坦克大战的基础上开发,支持多人对战,在线聊天,坦克大战网络版.
💻 JAVA
字号:
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class CreateWallsMsg implements Msg {

	int msgType = Msg.CREATE_WALLS_MSG;

    private TankClient tc;
    
	public CreateWallsMsg(TankClient tc) {
		this.tc=tc;
	}

	public void send(DatagramSocket ds, String IP, int udpPort) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			System.out.println("发出一份创建墙的MSG");
			dos.writeInt(msgType);
			
			dos.writeInt(tc.walls.size());
			for(int i=0; i<tc.walls.size(); i++){
				Wall wall = tc.walls.get(i);
			    dos.writeInt(wall.x);
			    dos.writeInt(wall.y);
			    dos.writeInt(wall.style);
			    dos.writeInt(wall.life);
			}
	    	byte[] buf = baos.toByteArray();
			DatagramPacket dp = new DatagramPacket(buf, buf.length,
					new InetSocketAddress(IP, udpPort));
			ds.send(dp);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void parse(DataInputStream dis) {
		System.out.println("客户端接收到服务端发来的CreateWallsMsg的数据");
		try {
			
			/*if(tc.walls.size()>0) return;*/
		
			
			int size = dis.readInt();
			tc.walls.removeAll(tc.walls);
			for(int i=0; i<size; i++){
				int x = dis.readInt();
				int y = dis.readInt();
				int style = dis.readInt();
				int life = dis.readInt();
				Wall wall = new Wall(x,y,style,tc);
				wall.life=life;
				tc.walls.add(wall);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

⌨️ 快捷键说明

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