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

📄 snake.java

📁 贪吃蛇网络版 运行平台 LINUX/UNIX Macintosh Windows Server 2003 Windows 2000 Windows XP Windows CE Windows 9*/
💻 JAVA
字号:
package logic;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JPanel;

import ui.StartUI;
import ui.WaitUI;

public class Snake extends JFrame implements Runnable{

	private static final long serialVersionUID = 1L;

	JPanel gameDisplay;

	SnakeCanvas gameBoard;

	private WorldMap worldMap;

	// those fields are used for setconnection

	private Socket s;

	private String host;

	private int port;

	private DataInputStream dis;

	private DataOutputStream dos;

	private int ID;

	private String mapName;
	
	private String[] playerName;

	// end of fields for setconnection

	// used for waitUI

	private boolean hasstart; // judge whether the host has pressed start
	
	private WaitUI waitUI; // waitUI

	// end of waitUI

	public Snake(String host, int port) {

		super("GreedySnake 1.0");

		this.host = host;
		this.port = port;
		
		//wait until host start the game
		//waiting();
		new Thread(this).start();
		setConnection();

		// gameBoard = new SnakeCanvas();
		gameBoard = new SnakeCanvas(s, ID, mapName);

		// 设置和服务器的网络连接
		// gameBoard.setConnection();
		// 初始化游戏地图
		worldMap = new WorldMap(gameBoard.getMapName());
		// 设置游戏地图
		gameBoard.setWorldMap(worldMap);
		// set players' names
		gameBoard.setPlayerNames(playerName);
		gameDisplay = (JPanel) getContentPane();
		// BackGroundCanvas bgc = new BackGroundCanvas();
		// gameDisplay.add(bgc, BorderLayout.CENTER);
		gameDisplay.add(gameBoard, BorderLayout.CENTER);

		BufferedImage cur = new BufferedImage(1, 1,
				BufferedImage.TYPE_4BYTE_ABGR);

		Cursor customCursor = Toolkit.getDefaultToolkit().createCustomCursor(
				cur, new Point(0, 0), "");
		setCursor(customCursor);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 根据地图设置窗体大小
		setSize(worldMap.getMapWidth(), worldMap.getMapHeight());
		setVisible(true);

		gameBoard.init();

	}

	/**
	 * set connection outside the canvas
	 * 
	 * @author hch
	 * @version 1.0
	 */

	private void setConnection() {
		try {
			s = new Socket(host, port);
			System.out.println("Connected to " + s.getInetAddress());
			dis = new DataInputStream(s.getInputStream());
			dos = new DataOutputStream(s.getOutputStream());

			ID = dis.read();
			mapName = dis.readUTF();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * our player should wait outside until the host press start button
	 * 
	 * @author hch
	 * @version 1.0
	 */
	public void waiting() {
		String information; // the information received from server
		String[] parsedInformation; // parsed infromation
		hasstart = false;
//		setConnection();

		if (ID != 0) // ID == 0, which means it's the host,
			waitUI = new WaitUI(); // so there is no need to initialize the waitUI
		 

		while (!(Thread.interrupted())) {
			try {
				information = dis.readUTF();
				hasstart = (information.equals("true")) ? true : false;

				/**
				 * if hasstart is true, that is, we should start the game then
				 * jump out of the loop else we should parse the information
				 */
				if (hasstart == true)
					break;
				else {
					parsedInformation = information.substring(1).split(":", -1);
					playerName = parsedInformation;
					// if the length of parsedInformation is 1, then
					// only one player exists, 2 : 2, ...
					if (ID != 0) {
						if (parsedInformation.length == 1)
							waitUI.setFirstName(parsedInformation[0]);
						else if (parsedInformation.length == 2) {
							waitUI.setFirstName(parsedInformation[0]);
							waitUI.setSecondName(parsedInformation[1]);
						} else if (parsedInformation.length == 3) {
							waitUI.setFirstName(parsedInformation[0]);
							waitUI.setSecondName(parsedInformation[1]);
							waitUI.setThirdName(parsedInformation[2]);
						}
					}

				}
			} catch (IOException e) {
				e.printStackTrace();
			}



		}
		
		if (ID != 0)
			waitUI.dispose();

	}

	public WorldMap getWorldMap() {
		return worldMap;
	}

	public void run() {
		// TODO Auto-generated method stub
		waiting();
	}
	




}

⌨️ 快捷键说明

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