结果展示
迷宫的大小为10*10,绿色的S代表起点(随机),黄色的P代表玩家(随机),红色的E代表终点(随机),灰色的0代表石块(随机)。玩家的任务是从起点出发,绕过石块,到达终点。使用键盘上的WASD键分别控制玩家的上下左右移动(切换为英文输入法)。
代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
public class MazeGameGUI extends JFrame {
private static final int SIZE = 10;
private static final char EMPTY = ' ';
private static final char START = 'S';
private static final char END = 'E';
private static final char PLAYER = 'P';
private static final char ROCK = '0';
private static char[][] maze = new char[SIZE][SIZE];
private static int playerX, playerY;
private static int startX, startY;
private static int endX, endY;
private JPanel mazePanel;
private JLabel[][] mazeLabels;
public MazeGameGUI() {
setTitle("Maze Game");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initializeMaze();
mazePanel = new JPanel(new GridLayout(SIZE, SIZE));
mazeLabels = new JLabel[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
mazeLabels[i][j] = new JLabel(" ", SwingConstants.CENTER);
mazeLabels[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
mazeLabels[i][j].setOpaque(true);
mazePanel.add(mazeLabels[i][j]);
}
}
updateMazeDisplay();
add(mazePanel, BorderLayout.CENTER);
// Add key listener for WASD movement
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W:
movePlayer('W');
break;
case KeyEvent.VK_S:
movePlayer('S');
break;
case KeyEvent.VK_A:
movePlayer('A');
break;
case KeyEvent.VK_D:
movePlayer('D');
break;
}
}
});
setVisible(true);
setFocusable(true); // Ensure the JFrame is focusable to capture key events
}
private void initializeMaze() {
Random random = new Random();
// Initialize the maze with empty spaces
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
maze[i][j] = EMPTY;
}
}
// Randomly place the start position
startX = random.nextInt(SIZE);
startY = random.nextInt(SIZE);
playerX = startX;
playerY = startY;
maze[startX][startY] = START;
// Randomly place the end position, ensuring it's not the same as the start position
do {
endX = random.nextInt(SIZE);
endY = random.nextInt(SIZE);
} while (endX == startX && endY == startY);
maze[endX][endY] = END;
// Randomly place rocks, ensuring they don't overlap with start or end positions
for (int i = 0; i < 5; i++) {
int rockX, rockY;
do {
rockX = random.nextInt(SIZE);
rockY = random.nextInt(SIZE);
} while ((rockX == startX && rockY == startY) ||
(rockX == endX && rockY == endY) ||
maze[rockX][rockY] == ROCK);
maze[rockX][rockY] = ROCK;
}
// Place the player at the start
maze[playerX][playerY] = PLAYER;
}
private void movePlayer(char direction) {
int newX = playerX;
int newY = playerY;
switch (direction) {
case 'W': // Up
newX--;
break;
case 'S': // Down
newX++;
break;
case 'A': // Left
newY--;
break;
case 'D': // Right
newY++;
break;
default:
return;
}
// Check if the new position is within the maze bounds and not a rock
if (newX >= 0 && newX < SIZE && newY >= 0 && newY < SIZE && maze[newX][newY] != ROCK) {
maze[playerX][playerY] = (playerX == startX && playerY == startY) ? START : EMPTY; // Clear old position
playerX = newX;
playerY = newY;
maze[playerX][playerY] = PLAYER; // Mark new position
updateMazeDisplay();
// Check if player reached the end
if (playerX == endX && playerY == endY) {
JOptionPane.showMessageDialog(this, "Congratulations! You've reached the end!");
System.exit(0);
}
}
}
private void updateMazeDisplay() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
char cell = maze[i][j];
switch (cell) {
case EMPTY:
mazeLabels[i][j].setBackground(Color.WHITE);
mazeLabels[i][j].setText(" ");
break;
case START:
mazeLabels[i][j].setBackground(Color.GREEN);
mazeLabels[i][j].setText("S");
break;
case END:
mazeLabels[i][j].setBackground(Color.RED);
mazeLabels[i][j].setText("E");
break;
case PLAYER:
mazeLabels[i][j].setBackground(Color.YELLOW);
mazeLabels[i][j].setText("P");
break;
case ROCK:
mazeLabels[i][j].setBackground(Color.GRAY);
mazeLabels[i][j].setText("0");
break;
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(MazeGameGUI::new);
}
}