我正在尝试制作PacMan游戏但尚未完成。到目前为止,我已经创建了下面的代码,没有错误。但是,它没有运行。到目前为止,我只想要出现黑色背景屏幕,墙壁和食物。
尝试运行代码时出错:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 25
at Board.loadBoard(Board.java:92)
at Board.<init>(Board.java:71)
at PacManGUI.<init>(PacManGUI.java:11)
at PacManGame.main(PacManGame.java:8)
请帮助我成为编码的初学者并且不知道为什么会发生这种情况
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* This class represents the game board and includes methods to handle
keyboard events and game actions
*/
public class Board extends JPanel implements KeyListener, ActionListener {
private static final ImageIcon WALL = new ImageIcon("images 3/StdWall.bmp");
private static final ImageIcon FOOD = new ImageIcon("images 3/StdFood.bmp");
private static final ImageIcon DOOR = new ImageIcon("images 3/Black.bmp");
//Timer for game movement
private Timer gameTimer = new Timer(250, this);
//Timer for PacMan animation
private Timer animateTimer = new Timer(50, this);
//Array to hold the game board characters from the text file
private char[][] maze = new char[25][27];
//Array to hold the game board images
private JLabel[][] cell = new JLabel[25][27];
//PacMan object
private PacMan pacMan;
//Array of Ghost objects
private Ghost[] ghost = new Ghost[3];
//Track amount of food on board
private int pellets=0;
//Track game score (1pt per food item eaten)
private int score=0;
//Steps for animating Pacman's chomp
private int pStep;
//Construct the game board including the layout, background, PacMan and ghosts
//and calls the loadBoard method
public Board() {
//Set the layout (grid), background (black)
setLayout(new GridLayout (25, 27));
setBackground(Color.BLACK);
//Create PacMan and the ghosts
pacMan = new PacMan();
ghost[0] = new Ghost(0);
ghost[1] = new Ghost(1);
ghost[2] = new Ghost(2);
//Load the maze
loadBoard();
}
//Loads the maze onto the screen from a text file
private void loadBoard() {
int r = 0;
Scanner input;
try {
input = new Scanner(new File ("maze.txt"));
while(input.hasNext()) {
maze[r]= input.nextLine().toCharArray();
for (int c = 0; c<maze [r].length; c++) {
cell[r][c] = new JLabel();
if (maze [r][c] == 'W') {
cell[r][c].setIcon(WALL);
}
else if (maze[r][c]=='F') {
cell[r][c].setIcon(FOOD);
pellets++;
}
else if (maze[r][c]=='P') {
cell[r][c].setIcon(pacMan.getIcon());
pacMan.setRow(r);
pacMan.setColumn(c);
pacMan.setDirection(0);
}
else if (maze[r][c]=='0'||maze[r][c]=='1'||maze[r][c]=='2'){
int gNum = (int)(maze[r][c])-48;
cell[r][c].setIcon(ghost[gNum].getIcon());
ghost[gNum].setRow(r);
ghost[gNum].setColumn(c);
}
else if (maze[r][c]=='D');{
cell[r][c].setIcon(DOOR);
add(cell[r][c]);
}
r++;
}
input.close();
}
}
catch(FileNotFoundException e) {
System.out.println("File not found");
}
}
//Open the maze text file for input
//Cycle through all the rows in the maze file reading one row at a time
//Read the next line from the maze file
//For each row cycle through all the columns
//If the symbol is a wall then assign a wall picture to the current square on the screen
//Otherwise if the symbol is a food item then assign a food picture to the current square on the screen and keep track of the number of items
//Otherwise if the symbol is PacMan then assign the closed left image and set PacMan's row, column, and direction (left)
//Otherwise if the symbol is a Ghost then assign the appropriate ghost image and set the ghost's row, column
//Otherwise if the symbol is a door then assign a door
//Add the current cell to the board panel
//Increment the row
//Close the maze text file
public void actionPerformed(ActionEvent e) {
}
public void keyTyped(KeyEvent key) {
}
public void keyPressed(KeyEvent key) {
}
public void keyReleased(KeyEvent key) {
}
}