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

📄 mazenetwork.java

📁 在eclipse的环境下开发的迷宫对战游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                //是否在游戏状态    
                if (disp == frmHall) {
                    ti.setUser(1, null);
                } else {
                    if (un1!=null && un1.equals(curUserName)) {
                        canMaze.isRunning = false;
                        display.setCurrent(frmHall);
                    }
                }
            }
        }
    }
    
    private void handleStartGame() {
        if ((sc == null) || (dis == null) || (dos == null)) {
            return;
        }
        
        int row = 0;
        int col = 0;
        int[][] map = null;
        
        //读取地图信息
        try {
            row = dis.readInt();
            col = dis.readInt();
            map = new int[row][col];
            for(int m=0;m<map.length;m++) {
                for(int n=0;n<map[0].length;n++) {
                    map[m][n] = dis.readInt();
                }
            }
        } catch(IOException ioe) {
            return;
        }
        canMaze = new MazeGameCanvas(this, map);
        canMaze.addCommand(cmdExit);
        canMaze.setCommandListener(this);
        canMaze.initialize();
        display.setCurrent(canMaze);
    }
    
    private void handleMove() {
        if ((sc == null) || (dis == null) || (dos == null)) {
            return;
        }
        
        int carX = 0;
    	int carY = 0;
        //读取位置信息
        try {
        	carX = dis.readInt();
        	carY = dis.readInt();
        } catch(IOException ioe) {
            return;
        }
        canMaze.otherCar.setPosition(carX*MazeGameCanvas.CELL_HEIGHT, 
        		carY*MazeGameCanvas.CELL_WIDTH);
    }
    
	//----------------------------------------接收和处理服务器命令-结束
	
	
	
	//------------------------------------------------------登录处理开始
	/**
	 * 处理登录请求
	 *
	 */
	private void requestLogin() {
		new Thread() {
			public void run() {
				// 登录游戏服务器
				String un = tfUserName.getString();
				String ps = tfPass.getString();
				if (un != null) {
					open();
					if (doLogin(un, ps)) {
						curUserName = un;
						displayGameHall();
						requestUserList();
					} else {
						displayErrorAlert();
					}
				}
			}
		}.start();		
	}
	
	/**
	 * 进行登录游戏服务器操作
	 * @param username 登录用户名
	 * @param password 密码
	 * <p>
	 * @return 如果登录成功,返回true,否则返回false
	 */
	private boolean doLogin(String username, String password) {
		if ((sc == null) || (dis == null) || (dos == null)) {
			return false;
		}
		boolean  ret = false;
        isSendingCmd = true;
		try {
			byte[] un = username.getBytes("UTF-8");
			byte[] ps = password.getBytes();
			
			//发送CMD_LOGIN命令
			dos.writeByte(MazeProtocol.CMD_LOGIN);
			dos.writeInt(un.length);
			dos.write(un);
			dos.writeInt(ps.length);
			dos.write(ps);
			dos.flush();
			
			//接收响应
			ret = (dis.readByte() == MazeProtocol.RESP_OK)?true:false;
		} catch (Exception e) {
			e.printStackTrace();
		}
        isSendingCmd = false;
		return ret;
	}
	
	/**
	 * 请求用户列表
	 *
	 */
	private void requestUserList() {
		if ((sc == null) || (dis == null) || (dos == null)) {
			return;
		}		
		
        isSendingCmd = true;
		try {
			//发送CMD_USERLIST命令
			dos.writeByte(MazeProtocol.CMD_USERLIST);
			dos.flush();
			
			//接收用户列表
			int user_num = dis.readInt();
			for(int i=0;i<user_num;i++) {
				int table_num = dis.readInt(); //与Item索引对应
				byte pos = dis.readByte();
				int size = dis.readInt();
				byte[] data = new byte[size];
				dis.read(data);
				String un = new String(data, "UTF-8");
				
				if (table_num < frmHall.size()) {
					TableItem ti = (TableItem)frmHall.get(table_num);
					ti.setUser(pos, un);
				}
			}
			dos.writeByte(MazeProtocol.RESP_OK);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
			displayErrorAlert();
		}
        isSendingCmd = false;
	}
	//------------------------------------------------------登录处理结束

	
	//------------------------------------------------------退出处理开始
	private void requestExit() {
		if ((sc == null) || (dis == null) || (dos == null)) {
			return;
		}
        
        isSendingCmd = true;
		try {
			//发送CMD_EXIT命令
			dos.writeByte(MazeProtocol.CMD_EXIT);
			dos.flush();
            
            if (dis.readByte() == MazeProtocol.RESP_OK) {
                if (display.getCurrent() == frmHall) {
                    isSendingCmd = false;
                    notifyDestroyed();
                }
            }
		} catch (IOException e) {
			e.printStackTrace();
		}	
        isSendingCmd = false;
	}
    
    private void requestEndGame() {
        if ((sc == null) || (dis == null) || (dos == null)) {
            return;
        }
        
        isSendingCmd = true;
        try {
            //发送CMD_ENDGAME命令
            dos.writeByte(MazeProtocol.CMD_ENDGAME);
            dos.flush();
            if (dis.readByte() == MazeProtocol.RESP_OK) {
                canMaze.isRunning = false;
                display.setCurrent(frmHall);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }   
        isSendingCmd = false;
    }
	//-----------------------------------------------------退出处理结束
	
	
	
	//-------------------------------------------------起立、坐下处理开始
	private boolean requestStandUp() {
		if ((sc == null) || (dis == null) || (dos == null)) {
			return false;
		}
		boolean ret = false;
		
        isSendingCmd = true;
		
        for(int i=0;i<frmHall.size();i++) {
            TableItem ti = (TableItem)frmHall.get(i);
            for(int j=0;j<2;j++) {
                String un = ti.getUser(j); 
                if (un != null && un.equals(curUserName) ) {
                    try {
                        dos.writeByte(MazeProtocol.CMD_STAND);
                        dos.writeInt(ti.number);
                        dos.writeByte((byte)j);
                        byte[] data = curUserName.getBytes("UTF-8");
                        dos.writeInt(data.length);
                        dos.write(data);
                        dos.flush();
                        
                        if (dis.readByte() == MazeProtocol.RESP_OK) {
                            ret = true;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        displayErrorAlert();
                        ret = false;
                    }
                    ti.setUser(j, null);
                }
            }
        }
        isSendingCmd = false;
		return ret;
	}

    
    private boolean requestSitDown(int number, 
            byte pos) {
        if ((sc == null) || (dis == null) || (dos == null)) {
            return false;
        }
        boolean ret = false;
         
        requestStandUp();
        isSendingCmd = true; 
        
        try {
            dos.writeByte(MazeProtocol.CMD_SIT);
            dos.writeInt(number);
            dos.writeByte(pos);
            byte[] data = curUserName.getBytes("UTF-8");
            dos.writeInt(data.length);
            dos.write(data);
            dos.flush();
            
            if (dis.readByte() == MazeProtocol.RESP_OK) {
                ret = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            displayErrorAlert();    
            ret = false;
        }
        isSendingCmd = false;
        return ret;
    }
	//-------------------------------------------------起立、坐下处理结束
	
    /**
     * 请求开始游戏
     */
    private void requestStartGame() {
        if ((sc == null) || (dis == null) || (dos == null)) {
            return ;
        }
        
        isSendingCmd = true; 
        
        //查找用户的位置,只有在游戏大厅就座
        //并且对手存在时才发送开始命令
        for(int i=0;i<frmHall.size();i++) {
            TableItem ti = (TableItem)frmHall.get(i);
            String u1 = ti.getUser(0);
            String u2 = ti.getUser(1);
            if (u1 == null || u2 == null) {
                return;
            } else {
                if (u1.equals(curUserName) ||
                        u2.equals(curUserName)) {
                    try {
                        dos.writeByte(MazeProtocol.CMD_STARTGAME);
                        dos.flush();
                    } catch (Exception e) {
                        e.printStackTrace();
                        displayErrorAlert();    
                    }                            
                }
            }
        }
        isSendingCmd = false;
    }
    
    
    void requestMove(int carX, int carY) {
        if ((sc == null) || (dis == null) || (dos == null)) {
            return ;
        }   
        
        isSendingCmd = true;
        try {
            dos.writeByte(MazeProtocol.CMD_MOVE);
            dos.writeInt(carX);
            dos.writeInt(carY);
            dos.flush();
            
            if (dis.readByte() != MazeProtocol.RESP_OK) {
                displayErrorAlert();
            }
        } catch (Exception e) {
            e.printStackTrace();
            displayErrorAlert();    
        }
        isSendingCmd = false;
    }
    
    void requestWin() {
        if ((sc == null) || (dis == null) || (dos == null)) {
            return ;
        }   
        
        isSendingCmd = true;
        try {
            dos.writeByte(MazeProtocol.CMD_WIN);
            dos.flush();
            
            if (dis.readByte() != MazeProtocol.RESP_OK) {
                displayErrorAlert();
            }
        } catch (Exception e) {
            e.printStackTrace();
            displayErrorAlert();    
        }    
        isSendingCmd = false;
    }
    

	public void commandAction(Command c, Displayable d) {
		if (c == cmdLogin) {
			//重新登录系统
			displayLoginForm(null);
		} else if (c == cmdPlay) {
            requestStartGame();     //请求开始游戏
        } else if (c == cmdExit) {
			if (d == frmLogin) {
				notifyDestroyed();
			} else if (d==frmHall){
				requestExit();  //请求退出游戏客户端
			} else {
                requestEndGame();   //请求提前介绍游戏
            }
		} else if (c == cmdOK) {
			if (d == frmLogin) {
                requestLogin(); //请求登录
			}
		}
	}
	
	/**
	 * 当选择某个桌子时,执行该函数
	 */
	public void commandAction(Command c, Item item) {
		TableItem ti = (TableItem)item;
        for(int i=0;i<2;i++) {
    		String user = ti.getUser(i);
    		
            //只有在座位为空时才可以坐下
            if (user == null) {
                if (requestSitDown(ti.number, (byte)i)) {
                    ti.setUser(i, this.curUserName);
                    break;
                }          
            } else {
    		    if (user.equals(this.curUserName)) {
    				//在这个位置就座,此时应该起立
    				requestStandUp();
                    break;
    			}
    		}
        }
	}

}

⌨️ 快捷键说明

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