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

📄 netsnakemodel.java

📁 是一个贪吃蛇的游戏
💻 JAVA
字号:
package NetPlayer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.Arrays;
import java.util.LinkedList;
import javax.swing.JOptionPane;

class NetSnakeModel implements Runnable {
	NetSnake gs;
	boolean[][] matrixSnake;
	boolean[][] matrixFood;
	/*
	 * 蛇身(自己)的位置、分数
	 */
	LinkedList myNodeArray = new LinkedList();
	int myScore = 0;
	String mySpeed = "您现在是'龟'速哦!";
	/*
	 * 蛇身(对手)的位置、分数
	 */
	LinkedList youNodeArray = new LinkedList();
	int youScore = 0;

	Node food;
	static int maxX;
	static int maxY;

	/*
	 * 运行状态
	 */
	boolean myRunning = false;
	boolean youRunning = true;

	/*
	 * 开始移动方向
	 */
	int direction = 3;

	/*
	 * 两次移动间的睡眠时间(ms)
	 */
	int finalTime = 100;
	int timeInterval = finalTime;

	/*
	 * 速度因子
	 */
	double speedChangeRate = 0.5;

	/*
	 * 暂停状态
	 */
	boolean paused = false;

	int countMove = 0;

	/*
	 * 上下为偶数,左右为奇数 原因见changeDirection(int newDirection)
	 */
	public static final int UP = 2;
	public static final int DOWN = 4;
	public static final int LEFT = 1;
	public static final int RIGHT = 3;

	/*
	 * 流
	 */
	Socket s;
	ObjectOutputStream oos;
	ObjectInputStream ois;

	public NetSnakeModel(NetSnake gs, int XX, int YY, Socket s) {
		this.gs = gs;
		this.maxX = gs.canvasWidth / gs.nodeWidth;
		this.maxY = gs.canvasHeight / gs.nodeHeight;
		this.s = s;
		/*
		 * 初始matirx
		 */
		matrixSnake = new boolean[maxX][];
		for (int i = 0; i < maxX; ++i) {
			matrixSnake[i] = new boolean[maxY];
			Arrays.fill(matrixSnake[i], false);
		}
		matrixFood = new boolean[maxX][];
		for (int i = 0; i < maxX; ++i) {
			matrixFood[i] = new boolean[maxY];
			Arrays.fill(matrixFood[i], false);
		}

		/*
		 * 设置蛇开始状态的长度 maxX是 游戏界面的宽度/蛇的宽度
		 */

		int initArrayLength = maxX > 20 ? 3 : maxX / 2;
		for (int i = 0; i < initArrayLength; ++i) {
			int x = XX;
			int y = YY;
			myNodeArray.addLast(new Node(x, y));
			matrixSnake[x][y] = true;
		}
		try {
			oos = new ObjectOutputStream(s.getOutputStream());
			oos.writeObject("start");
			ois = new ObjectInputStream(s.getInputStream());
			int x = Integer.parseInt(ois.readObject().toString());
			int y = Integer.parseInt(ois.readObject().toString());
			matrixFood = (boolean[][]) ois.readObject();
			food = new Node(x, y);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * 改变方向
	 * 
	 * 判断(%2)是为了当按下方向命令与当前方向"水平"时,不改变方向 而按下方向命令与当前方向"垂直"时,改变方向 eg:当方向向上时
	 * 按下"向上"和"向下"时方向不变 按下"向左"和"向右"时才改变方向
	 */
	public void changeDirection(int newDirection) {
		if (direction % 2 != newDirection % 2) {
			direction = newDirection;
		}
	}

	public boolean moveOn() {
		Node n = (Node) myNodeArray.getFirst();
		int x = n.x;
		int y = n.y;

		switch (direction) {
		case UP:
			y--;
			break;
		case DOWN:
			y++;
			break;
		case LEFT:
			x--;
			break;
		case RIGHT:
			x++;
			break;
		}

		/*
		 * 判断是否在游戏界面内
		 */
		if ((0 <= x && x < maxX) && (0 <= y && y < maxY)) {
			if (matrixSnake[x][y]) {
				return false;
			} else if (matrixFood[x][y]) {
				/*
				 * 判断是否是食物 是食物就往第一个节点添加一点
				 */
				if (x == food.x && y == food.y) {
					myNodeArray.addFirst(food);
					/*
					 * 计算分数,分数跟移动格数有关 清空移动格数
					 */
					int scoreGet = (10000 - 200 * countMove) / timeInterval;
					myScore += scoreGet > 0 ? scoreGet : 10;
					countMove = 0;
					food = createFood();
					return true;
				}
			} else {
				myNodeArray.addFirst(new Node(x, y));
				matrixSnake[x][y] = true;
				n = (Node) myNodeArray.removeLast();
				matrixSnake[n.x][n.y] = false;
				countMove++;
				return true;
			}
		}
		return false;
	}

	public void getAllMsg() {
		try {
			oos = new ObjectOutputStream(s.getOutputStream());
			oos.writeObject("update");
			oos.writeObject(myNodeArray);
			// oos.writeObject(myRunning);

			ois = new ObjectInputStream(s.getInputStream());
			youNodeArray = (LinkedList) ois.readObject();
			youScore = Integer.parseInt(ois.readObject().toString());
			youRunning = (Boolean) ois.readObject();
			int x = Integer.parseInt(ois.readObject().toString());
			int y = Integer.parseInt(ois.readObject().toString());
			food = new Node(x, y);
			matrixFood = (boolean[][]) ois.readObject();

		} catch (Exception e) {
		}
	}

	public void run() {
		myRunning = true;
		while (myRunning && youRunning) {
			try {
				Thread.sleep(timeInterval);
			} catch (Exception e) {
				break;
			}
			if (moveOn()) {
				getAllMsg();
				gs.repaint();
			} else {
				// JOptionPane.showMessageDialog(null, "You Source:" + myScore,
				// "Game Over", JOptionPane.INFORMATION_MESSAGE);
				break;
			}
		}
		myRunning = false;
		try {
			oos = new ObjectOutputStream(s.getOutputStream());
			oos.writeObject("stop");
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (myScore > youScore)
			JOptionPane.showMessageDialog(null, "恭喜您,您胜利了!",
					"Game Over", JOptionPane.INFORMATION_MESSAGE);
		else if(myScore<youScore)
			JOptionPane.showMessageDialog(null, "很抱歉,您失败了!",
					"Game Over", JOptionPane.INFORMATION_MESSAGE);
		else 
			JOptionPane.showMessageDialog(null, "恭喜您,您们并列第一!",
					"Game Over", JOptionPane.INFORMATION_MESSAGE);
	}

	/*
	 * 随机创建一个食物
	 */
	public Node createFood() {

		int x = 0;
		int y = 0;
		try {
			oos = new ObjectOutputStream(s.getOutputStream());
			oos.writeObject("food");
			oos.writeObject(myScore + "");
			ois = new ObjectInputStream(s.getInputStream());
			x = Integer.parseInt(ois.readObject().toString());
			y = Integer.parseInt(ois.readObject().toString());
			matrixFood = (boolean[][]) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Node(x, y);
	}

	/*
	 * 加快移动速度
	 */
	public void speedUp() {
		try {
			timeInterval *= speedChangeRate;
			if (timeInterval <= finalTime / 4)
				timeInterval = finalTime / 4;

			if (timeInterval == finalTime / 4)
				mySpeed = "哦,您现在可是'神'速哦!";
			else if (timeInterval == finalTime / 2)
				mySpeed = "您现在的速度'一般'";
			else if (timeInterval == finalTime)
				mySpeed = "您现在是'龟'速哦!";
			gs.updateSpeed();
		} catch (Exception e) {
		}

	}

	/*
	 * 减慢移动速度
	 */
	public void speedDown() {
		try {
			timeInterval /= speedChangeRate;
			if (timeInterval >= finalTime)
				timeInterval = finalTime;

			if (timeInterval == finalTime / 4)
				mySpeed = "哦,您现在可是'神'速哦!";
			else if (timeInterval == finalTime / 2)
				mySpeed = "您现在的速度'一般'";
			else if (timeInterval == finalTime)
				mySpeed = "您现在可是'龟'速哦!";
			gs.updateSpeed();
		} catch (Exception e) {
		}
	}
}

class Node implements Serializable {
	int x;
	int y;

	Node(int x, int y) {
		this.x = x;
		this.y = y;
	}
}

⌨️ 快捷键说明

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