行业知识
基于JAVA的简单迷宫游戏
2026-12-05 01:45:34
来源:互联网转载
一、实验要求
1. 迷宫游戏是非常经典的游戏,在该题中要求随机生成一个迷宫,并求解迷宫。
2. 要求游戏支持玩家走迷宫,和系统走迷宫路径两种模式。玩家走迷宫,通过键盘方向键控制,并在行走路径上留下痕迹;系统走迷宫路径要求基于 A* 算法实现,输出走迷宫的最优路径并显示。
3. 设计交互友好的游戏图形界面。
二、项目设计
1.使用Java语言
2.开发环境:IntelliJ IDEA Community Edition 2021.2
三、项目实现及结果分析
算法
1.创建一个Lattice的类,将迷宫视为由一个个格子组成,格子之间的边为迷宫的墙。将迷宫视为在这些格子上构造的树,每个格子都是树上的一个节点。 则任意两个格子间有且只有一条无环的路径。迷宫的构建过程即是在这些格子上随机构造一棵树的过程。
// 格子class Lattice { static final int INTREE = 1; static final int NOTINTREE = 0; private int x = -1; // 格子的位置,在第几行 private int y = -1; // 第几列 private int flag = NOTINTREE; // flag,标识格子是否已加入树中 private Lattice father = null; // 格子的父亲节点 public Lattice(int xx, int yy) { x = xx; y = yy; } public int getX() { return x; } public int getY() { return y; } public int getFlag() { return flag; } public Lattice getFather() { return father; } public void setFather(Lattice f) { father = f; } public void setFlag(int f) { flag = f; }} 2.对游戏进行初始化,用来重新开始游戏。以及重新开始的判断条件。
// 初始化游戏,重开一局时使用 private void init() { for (int i = 0; i NUM - 1 || x 4.创建树,用来画出迷宫。
// 构建随机树,创建迷宫 private void createMaze() { // 随机选一个格子作为树的根 Random random = new Random(); // 深度优先遍历 Stack s = new Stack(); Lattice p = maze[0][0]; Lattice neis[] = null; s.push(p); while (!s.isEmpty()) { p = s.pop(); p.setFlag(Lattice.INTREE); neis = getNeis(p); int ran = Math.abs(random.nextInt()) % 4; for (int a = 0; a fy ? j : fy) * width), sy = padding + ((i > fx ? i : fx) * width), dx = (i == fx ? sx : sx + width), dy = (i == fx ? sy + width : sy); if (sx != dx) { sx++; dx--; } else { sy++; dy--; } g.drawLine(sx, sy, dx, dy); } // 画迷宫 protected void paintComponent(Graphics g) { super.paintComponent(g); // 画NUM*NUM条黑线 for (int i = 0; i = 0; j--) { Lattice f = maze[i][j].getFather(); if (f != null) { int fx = f.getX(), fy = f.getY(); clearFence(i, j, fx, fy, g); } } } // 画左上角的入口 g.drawLine(padding, padding + 1, padding, padding + width - 1); int last = padding + NUM * width; // 画右下角出口 g.drawLine(last, last - 1, last, last - width + 1); // 画小球 g.setColor(Color.RED); g.fillOval(getCenterX(ballY) - width / 3, getCenterY(ballX) - width / 3, width / 2, width / 2); if (drawPath == true) { drawPath(g); } } 5.用深度优先算法求解迷宫。
private void drawPath(Graphics g) { Color PATH_COLOR = Color.red; if (drawPath == true) { g.setColor(PATH_COLOR); } else { g.setColor(this.getBackground()); } Lattice p = maze[NUM - 1][NUM - 1]; while (p.getFather() != null) { p.setFlag(2); p = p.getFather(); } g.fillOval(getCenterX(p) - width / 3, getCenterY(p) - width / 3, width / 2, width / 2); p = maze[0][0]; while (p.getFather() != null) { if (p.getFlag() == 2) { p.setFlag(3); } g.drawLine(getCenterX(p), getCenterY(p), getCenterX(p.getFather()), getCenterY(p.getFather())); p = p.getFather(); } g.setColor(PATH_COLOR); p = maze[NUM - 1][NUM - 1]; while (p.getFather() != null) { if (p.getFlag() == 3) { break; } g.drawLine(getCenterX(p), getCenterY(p), getCenterX(p.getFather()), getCenterY(p.getFather())); p = p.getFather(); } } 四、源代码
package maze; import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.Random;import java.util.Stack;import javax.swing.*; /* 迷宫由一个一个格子组成,格子之间的边为迷宫的墙。 * 将迷宫视为在这些格子上构造的树,每个格子都是树上的一个节点。 * 则任意两个格子间有且只有一条无环的路径。 * 迷宫的构建过程即是在这些格子上随机构造一棵树的过程。 * */ // 格子class Lattice { static final int INTREE = 1; static final int NOTINTREE = 0; private int x = -1; // 格子的位置,在第几行 private int y = -1; // 第几列 private int flag = NOTINTREE; // flag,标识格子是否已加入树中 private Lattice father = null; // 格子的父亲节点 public Lattice(int xx, int yy) { x = xx; y = yy; } public int getX() { return x; } public int getY() { return y; } public int getFlag() { return flag; } public Lattice getFather() { return father; } public void setFather(Lattice f) { father = f; } public void setFlag(int f) { flag = f; }} public class Maze extends JPanel { private static final long serialVersionUID = -8300339045454852626L; private int NUM, width, padding; // NUM:迷宫大小;width:每个格子的宽度和高度 private Lattice[][] maze; private int ballX, ballY; // 球的位置,在第几行第几列格子上 private boolean drawPath = false; // flag,标识是否画出路径 Maze(int m, int wi, int p) { NUM = m; width = wi; padding = p; maze = new Lattice[NUM][NUM]; for (int i = 0; i 结果