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

📄 mazegame.java

📁 一个迷宫游戏,最大特色是可以随机生成迷宫,可以遍历迷宫得到答案,可以在迷宫中添加追赶者.随机生成迷宫算法和遍历算法比较经典,可提供参考.
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
					&& horizontal[playerRow + 1][playerColumn] == Wall.notExist) {
				playerRow += 1;
				succeed = true;
			}
			break;
		}

		repaint();// 走完一步不要忘记重画!

		return succeed;
	}

	/**
	 * 开启追赶者
	 */
	public void chaseOn() {
		this.chase = true;
		timer.start();
		repaint();
	}

	/**
	 * 关闭追赶者
	 */
	public void chaseOff() {
		this.chase = false;
		timer.stop();
		repaint();
	}

	/**
	 * 玩家拆墙,若超过了限制次数或本来就没墙的则拆墙无效
	 * 
	 * @param direction
	 *            1--拆玩家当前方格位置的左墙,2--拆上墙,3--拆右墙,4--拆下墙
	 */
	public void tearDown(int direction) {
		boolean succeed = false;

		if (this.tearDownAllowed != 0) {// 若小于0则说明玩家设定拆墙不限次数,若大于0则说明还可以拆墙
			switch (direction) {
			case 1:
				if (vertical[playerRow][playerColumn] == Wall.exist) {
					vertical[playerRow][playerColumn] = Wall.notExist;
					succeed = true;
				}
				break;
			case 2:
				if (horizontal[playerRow][playerColumn] == Wall.exist) {
					horizontal[playerRow][playerColumn] = Wall.notExist;
					succeed = true;
				}
				break;
			case 3:
				if (vertical[playerRow][playerColumn + 1] == Wall.exist) {
					vertical[playerRow][playerColumn + 1] = Wall.notExist;
					succeed = true;
				} 
				break;
			case 4:
				if (horizontal[playerRow + 1][playerColumn] == Wall.exist) {
					horizontal[playerRow + 1][playerColumn] = Wall.notExist;
					succeed = true;
				} 
				break;
			}
			if (this.tearDownAllowed != -1 && succeed)
				this.tearDownAllowed--;// 是限制次数且成功拆了一个墙
		}

		repaint();// 拆完墙或建完墙后不要忘记重画!
	}

	/**
	 * 获取当前可拆墙剩余次数
	 * 
	 * @return 当前可拆墙剩余次数
	 */
	public int tearDownAllowedTimes() {
		return tearDownAllowed;
	}

	/**
	 * 可变范围拆墙,拆除一个矩形区域内的所有墙:以玩家当前位置为中心方格,以width为宽度半径,以height为高度半径
	 * 注意:若给定矩形区域不完全在迷宫区域内则拆墙无效
	 * 
	 * @param widthHalf
	 *            矩形宽度半径
	 * @param heightHalf
	 *            矩形高度半径
	 */
	public void variableTearDown(int widthHalf, int heightHalf) {
		boolean notBeyond;

		notBeyond = playerRow - heightHalf >= 0
				&& playerRow + heightHalf + 1 <= this.height
				&& playerColumn - widthHalf >= 0
				&& playerColumn + widthHalf + 1 <= this.width;

		if (notBeyond) {// 未超出迷宫范围
			// 矩形区域内的水平墙全拆除
			for (int i = playerRow - heightHalf; i <= playerRow + heightHalf
					+ 1; i++)
				for (int j = playerColumn - widthHalf; j <= playerColumn
						+ widthHalf; j++)
					horizontal[i][j] = Wall.notExist;

			// 矩形区域内的竖直墙全拆除
			for (int i = playerRow - heightHalf; i <= playerRow + heightHalf; i++)
				for (int j = playerColumn - widthHalf; j <= playerColumn
						+ widthHalf + 1; j++)
					vertical[i][j] = Wall.notExist;
		}

		repaint();// 再次强调:拆完墙不要忘记重画!

	}

	/**
	 * 保护生效:玩家四面是墙;保护失效:玩家四面无墙
	 * 
	 * @param protect
	 *            true为保护生效;false为保护失效
	 */
	public void protectEnabled(boolean protect) {
		if (protect) {
			horizontal[playerRow][playerColumn] = Wall.exist;
			horizontal[playerRow + 1][playerColumn] = Wall.exist;
			vertical[playerRow][playerColumn] = Wall.exist;
			vertical[playerRow][playerColumn + 1] = Wall.exist;
		} else {
			horizontal[playerRow][playerColumn] = Wall.notExist;
			horizontal[playerRow + 1][playerColumn] = Wall.notExist;
			vertical[playerRow][playerColumn] = Wall.notExist;
			vertical[playerRow][playerColumn + 1] = Wall.notExist;
		}

		repaint();
	}

	/**
	 * 返回当前的游戏结果(考虑到玩家不动却被追赶成功的情况,不推荐在每次玩家移动时作出判断)
	 * (建议在外部类中定义一个定时器,每隔10毫秒就判断一次玩家的输赢状态,方便处理)
	 * 
	 * @return 返回-1表示当前玩家输了(追赶者为"开"且玩家碰到追赶者)
	 *         返回0表示当前迷宫游戏仍在进行中;返回1表示当前玩家赢了(玩家到达终点且此刻并没有碰到追赶者)
	 */
	public int currentResult() {
		boolean chased = false;// 判断是否被追到

		for (Chaser chaser : chasers)
			if (chase == true && playerRow == chaser.getRow()
					&& playerColumn == chaser.getColumn())
				chased = true;

		if (chased)// 追赶者为"开"且玩家碰到追赶者
			currentResult = -1;
		else if (playerRow == this.height - 1 && playerColumn == this.width - 1)// 玩家到达终点且此刻并没有碰到追赶者
			currentResult = 1;

		return currentResult;
	}

	/**
	 * 当前提示,会根据玩家在迷宫的当前位置给出提示 (考虑到玩家不动却被追赶成功的情况,不推荐在每次玩家移动时给出提示)
	 * (建议在外部类中定义一个定时器,每隔10毫秒就调用一次本方法返回当前提示)
	 * 
	 * @return 当前提示,会根据玩家在迷宫的当前位置给出提示.
	 */
	public String currentTip() {
		String tip = "暂 无 提 示";
		boolean chaserNear = false;// 判断追赶者是否在附近

		for (Chaser chaser : chasers)
			if (Math.abs(playerRow - chaser.getRow()) < 2
					&& Math.abs(playerColumn - chaser.getColumn()) < 2)
				chaserNear = true;

		if (playerRow == 0 && playerColumn == 0)// 玩家在起点
			tip = "你 在 起 点";
		else if (this.currentResult() == 1)// 玩家到达终点
			tip = "恭 喜 到 达 终 点!";
		else if (chaserNear)// 追赶者和玩家相差几个方格
			tip = "当 心! 追 赶 者 似 乎 很 接 近 了!";
		else if (checks[playerRow][playerColumn] == Check.notMain) // 玩家当前位置不在主路径上
			tip = "你 走 的 路 似 乎 不 对";
		else if (checks[playerRow][playerColumn] == Check.main)// 玩家当前位置在主路径上
			tip = "你 走 的 路 似 乎 是 正 确 的";

		return tip;
	}

	/**
	 * 帮助信息:迷宫的规则.注意:按键功能由外部类定义并告诉玩家.
	 * 
	 * @return 帮助信息:迷宫的规则
	 */
	public String help() {
		String help;

		help = "规 则 : 玩 家 是 蓝 点, 碰 到 红 点 就 输,走 到 右 下 角 (终 点) 就 赢.";

		return help;
	}

	/**
	 * 追赶者的定时移动
	 * 
	 * @author 山
	 * 
	 */
	private class TimerListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			for (Chaser chaser : chasers)
				// 追赶者追赶玩家
				chaser.chase();

			repaint();// 追赶者移动后不要忘记重画!
		}
	}

	/**
	 * 定义一个追赶者以及其追赶操作
	 * 
	 * @author 山
	 * 
	 */
	private class Chaser {
		private int row, column;// 追赶者的所在方格的行数和列数,可在Maze类中访问

		/**
		 * 新的追赶者
		 * 
		 * @param row
		 *            新建追赶者所在方格的行数
		 * @param column
		 *            新建追赶者所在方格的列数
		 */
		private Chaser(int row, int column) {
			this.row = row;
			this.column = column;
		}

		/**
		 * 以追赶者为中心,玩家的方位有8个:上,左上,右等,追赶者就是根据这些来追赶
		 */
		private void chase() {
			if (playerRow == this.row && playerColumn < this.column)// 玩家在追赶者左边
				this.move(8);
			else if (playerRow == this.row && playerColumn > this.column)// 玩家在追赶者右边
				this.move(4);
			else if (playerRow < this.row && playerColumn == this.column)// 玩家在追赶者上面
				this.move(2);
			else if (playerRow > this.row && playerColumn == this.column)// 玩家在追赶者下面
				this.move(6);
			else if (playerRow < this.row && playerColumn < this.column)// 玩家在追赶者左上方
				this.move(1);
			else if (playerRow < this.row && playerColumn > this.column)// 玩家在追赶者右上方
				this.move(3);
			else if (playerRow > this.row && playerColumn < this.column)// 玩家在追赶者左下方
				this.move(7);
			else if (playerRow > this.row && playerColumn > this.column)// 玩家在追赶者右下方
				this.move(5);
		}

		/**
		 * 追赶者移动
		 * 
		 * @param direction
		 *            1--左上方,2--上方,3--右上方,4--右方,5--右下方,6--下方,7--左下方,8--左方
		 * @return 成功走了一步则返回true;否则返回false
		 */
		private boolean move(int direction) {
			boolean succeed = false;

			switch (direction) {
			case 1:
				succeed = this.move(8);
				if (!succeed)
					succeed = this.move(2);
				break;

			case 2:
				if (row != 0 && horizontal[row][column] == Wall.notExist)// 不在第一行且没有墙阻挡
				{
					row -= 1;
					succeed = true;
				}
				break;

			case 3:
				succeed = this.move(4);
				if (!succeed)
					succeed = this.move(2);
				break;

			case 4:
				if (column != width - 1
						&& vertical[row][column + 1] == Wall.notExist) {
					column += 1;
					succeed = true;
				}
				break;

			case 5:
				succeed = this.move(4);
				if (!succeed)
					succeed = this.move(6);
				break;

			case 6:
				if (row != height - 1
						&& horizontal[row + 1][column] == Wall.notExist) {
					row += 1;
					succeed = true;
				}
				break;

			case 7:
				succeed = this.move(8);
				if (!succeed)
					succeed = this.move(6);
				break;

			case 8:
				if (column != 0 && vertical[row][column] == Wall.notExist) {
					column -= 1;
					succeed = true;
				}
				break;
			}

			return succeed;
		}
		
		/**
		 * 
		 * @return 追赶者当前方格的行数
		 */

		public int getRow() {
			return row;
		}
		
		/**
		 * 
		 * @return 追赶者当前方格的列数
		 */

		public int getColumn() {
			return column;
		}
	}
}

⌨️ 快捷键说明

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