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

📄 flightmanager.java

📁 J2ME 蓝牙对战游戏 大海战简化版 索爱w810
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

import java.io.*;

import javax.microedition.io.*;
import javax.microedition.lcdui.*;



public class FlightManager implements Runnable,CommandListener
{
	public boolean gameIsBegin;
	public boolean isConnected;
	public boolean token;
	public boolean sending;
	public FlightManager(FWmainCanvas Canvas)
	{
		this.Canvas = Canvas;
		session = new Session(this.Canvas.midlet);
		isFirst = true;
		isConnected = false;
		gameIsBegin = false;
		count = 0;
		opcount = 0;
		token = false;
		for(int i=0;i<9;i++)
			for(int j=0;j<9;j++)
			{
				intMyMap[i][j]=0;
				intOpMap[i][j]=0;
			}
		dot.x = 2;
		dot.y = 3;
		this.Canvas.setCommandListener(this);
	}
	

	public void commandAction(Command c, Displayable d)
	{
		// TODO 自动生成方法存根
		if(c == this.Canvas.midlet.cmdExit)
			this.Canvas.midlet.destroyApp(false);
		
	}
	
	public void showInfo(String info)
	{
		int tmp = gameState;
		gameState = FlightManager.GAMESTATE_SHOWINFO;
		this.Canvas.gInof.setColor(0xffffff);
		this.Canvas.gInof.fillRect(0, 0, this.Canvas.width, 20);
		this.Canvas.gInof.setColor(0x000000);
		this.Canvas.gInof.setFont(this.Canvas.font);
		this.Canvas.gInof.drawString(info, 0, 0, Graphics.TOP|Graphics.LEFT);
		this.Canvas.repaint();
		this.Canvas.serviceRepaints();
		gameState = tmp;
		//System.out.println(info);
	}
	
	public void action()
	{
		//System.out.println("here is FM action");
		//x+=10;
		//y+=10;
//		战斗机布局

		if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_UP_FLAG) != 0)	//响应上键
		{
			//System.out.println("响应上键");
			this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_UP_FLAG;	//清楚响应标志
			if( dot.y == I_VECTOR_Y )dot.y = A_VECTOR_Y;
			else dot.y++;
			this.Canvas.repaint();
			this.Canvas.serviceRepaints();
			//y -= this.Canvas.deltaY;
			
		}
		else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_DOWN_FLAG) != 0)	//响应下键
		{
			//System.out.println("响应下键");
			this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_DOWN_FLAG;	//清楚响应标志
			if( dot.y == A_VECTOR_Y )dot.y = I_VECTOR_Y;
			else dot.y--;
			this.Canvas.repaint();
			this.Canvas.serviceRepaints();
			//y+=this.Canvas.deltaY;
			
		}
		else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_LEFT_FLAG ) != 0 )	//相应左键
		{
			this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_LEFT_FLAG;
			//填入动作相应
			if( dot.x == 0 )dot.x = 8;
			else dot.x--;
			this.Canvas.repaint();
			this.Canvas.serviceRepaints();
			//x-=this.Canvas.deltaX;
		}
		else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_RIGHT_FLAG ) != 0 )	//相应右键
		{
			this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_RIGHT_FLAG;
			//填入动作相应
			if( dot.x == 8 )dot.x = 0;
			else dot.x++;
			this.Canvas.repaint();
			this.Canvas.serviceRepaints();
			//x+=this.Canvas.deltaX;
		}
		else if ((this.Canvas.allAction & LPKeyMask.MASK_KEY_OK_FLAG) != 0)	//响应确定键
		{
			//System.out.println("响应OK键");
			this.Canvas.allAction &= ~LPKeyMask.MASK_KEY_OK_FLAG;	//清楚响应标志
			fireControl();
			this.Canvas.repaint();
			this.Canvas.serviceRepaints();
		}//else
	}
	
	public void fireControl()
	{
		int type = 0;
		if (token)
		{
			if(sending)
			{
				showInfo("处理中,请等待...");
				return;
			}
			sending = true;
			if (intOpMap[dot.x][dot.y] == 0 )
			{
				count++;
				type = sendDot(dot);
				
//				if (type != -1)
//				{
//					intOpMap[dot.x][dot.y] = type;
//					gameState = FlightManager.GAMESTATE_GAMELOOP;
//					this.Canvas.repaint();
//				}
//				else
//				{
//					showInfo("处理中,请等待...");
//				}
				
			}
			else
			{
				showInfo("请瞄准另一个区域");
			}
		}
		else
		{
			showInfo("轮到对方开炮");
			//recvDot(dot);
		}
	}
	
	public int sendDot(Dot dot)
	{
		byte [] sbuf = new byte[3];
		sbuf[0] = (byte) count;
		sbuf[1] = (byte) dot.x;
		sbuf[2] = (byte) dot.y;
		
		byte [] rbuf = new byte[2];
		rbuf[0] = 0;
		rbuf[1] = 0;
		/*
		//匿名内部类
		Thread fetchThread = new Thread()
		{
			public void run()
			{
				
			}
		};
		fetchThread.start();
		*/
		while(true)
		{
			try
			{
				outs.write(sbuf);
			}
			catch (IOException e)
			{
				// TODO 自动生成 catch 块
				e.printStackTrace();
				showInfo("发送失败");
				continue;
			}
			break;
		}
		showInfo("读取结果...");
		while(true)
		{
			try
			{
				ins.read(rbuf);
			}
			catch (IOException e)
			{
				// TODO 自动生成 catch 块
				//e.printStackTrace();
				showInfo("接受返回信息失败");
				continue;
			}
			break;
		}
		System.out.println("the result is: x-"+sbuf[1]+" y-"+sbuf[2]+" is :"+rbuf[1]);

		while(true)
		{
			try
			{
				outs.writeBoolean(true);
			}
			catch (IOException e)
			{
				// TODO 自动生成 catch 块
				e.printStackTrace();
				continue;
			}
			break;
		}
		token = false;
		sending = false;
		//intOpMap[sbuf[1]][sbuf[2]] = rbuf[1];
		switch (rbuf[1])
		{
		case MAP_FLIGHT_HEAD:
			intOpMap[sbuf[1]][sbuf[2]] = MAP_SHOT_HEAD;
			showInfo("击落一架飞机");
			break;
			
		case MAP_EMPTY:
			intOpMap[sbuf[1]][sbuf[2]] = MAP_SHOT_EMPTY;
			showInfo("打空~");
			break;
			
		case MAP_FLIGHT_BODY:
			intOpMap[sbuf[1]][sbuf[2]] = MAP_SHOT_BODY;
			showInfo("击中一架飞机");
			break;
			
		default:
			showInfo("未知");
			break;
		}
		gameState = FlightManager.GAMESTATE_GAMELOOP;
		this.Canvas.repaint();
		this.Canvas.serviceRepaints();
		Thread recvThread = new Thread()
		{
			public void run()
			{
				recvDot();
			}
		};
		recvThread.start();
		try
		{
			Thread.sleep(100);
		}
		catch (InterruptedException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		if( rbuf[0] == sbuf[0] )
		{
			return rbuf[1];
		}
		else return -1;
	}
	
	public int recvDot()
	{
		byte [] rbuf = new byte[3];
		byte [] sbuf = new byte[3];
		rbuf[0] = (byte) 0;
		rbuf[1] = (byte) 0;
		rbuf[2] = (byte) 0;
		try
		{
			ins.read(rbuf);
		}
		catch (IOException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		sbuf[0] = (byte) rbuf[0];
		sbuf[1] = (byte) intMyMap[rbuf[1]][rbuf[2]];
		
		try
		{
			outs.write(sbuf);
		}
		catch (IOException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		switch (sbuf[1])
		{
		case MAP_FLIGHT_HEAD:
			intMyMap[rbuf[1]][rbuf[2]] = MAP_SHOT_HEAD;
			//showInfo("我方被击落一架飞机");
			break;
			
		case MAP_EMPTY:
			intMyMap[rbuf[1]][rbuf[2]] = MAP_SHOT_EMPTY;
			//showInfo("对手打空");
			break;
			
		case MAP_FLIGHT_BODY:
			intMyMap[rbuf[1]][rbuf[2]] = MAP_SHOT_BODY;
			//showInfo("我方飞机被击中");
			break;
			

		default:
			break;
		}
		try
		{
			token = ins.readBoolean();
			if (token)
			{
				showInfo("获得控制权");
			}
		}
		catch (IOException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return (int) rbuf[0];
	}
	
	public void beginNewGame()
	{
		//开始游戏
		System.out.println("here is begin new Game");
		//这里需要一个建立蓝牙连接的模块,同时要完成先后手的决定
		//先手做服务器
		//isFirst = true;
		Thread newThread = new Thread(this);
		newThread.start();
		try
		{
			Thread.sleep(100);
		}
		catch (InterruptedException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		this.gameState = GAMESTATE_INIT;
		showInfo("先后手"+isFirst);
		//返回后开始绘制作战区地图
	}
	
	public void run()
	{
		if ( !initConnection(isFirst) )
		{
			showInfo("蓝牙连接建立失败");
			//连接失败处理
		}
	}
	
	public boolean initConnection(boolean isFirst)
	{
		if (isFirst)
		{
			session.setAsServerMode();
			token = true;
		}

⌨️ 快捷键说明

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