我正在尝试制作迷你迷宫游戏。我有一个文本文件,其中包括用于草的墙壁和空白的#。我在阅读文件和行atm时遇到问题。我尝试使用不同的方法,但我不熟悉阅读文件和如何阅读行。
public class Map {
private Scanner m;
private Image wall,grass;
private int lines;
private char Map[][];
public Map(){
ImageIcon img = new ImageIcon("/Users/parischrysanthou/Downloads/handpainted_wall2.jpg");
wall = img.getImage();
img = new ImageIcon(("/Users/parischrysanthou/Downloads/grass20.png"));
grass = img.getImage();
openFile();
readFile();
closeFile();
}
public int getLines() { return lines; }
public Image getGrass(){
return grass;
}
public Image getWall() {
return wall;
}
public char getMap(int x, int y){
char index = Map[x][y];
return index;
}
public void openFile(){
try {
File f = new File("/Users/parischrysanthou/Downloads/maze21-2.txt");
Scanner m = new Scanner(f);
while (m.hasNextLine()) {
m.nextLine();
lines++;
}
Map = new char [lines][lines];
}
catch(Exception e){
System.out.println("Error loading file");
}
}
public void readFile() {
m = new Scanner("/Users/parischrysanthou/Downloads/maze21-2.txt");
while (m.hasNextLine()) {
for (int i = 0; i < lines; i++) {
String s = m.nextLine();
for (int j = 0; j < lines; j++) {
Map[i][j] = s.charAt(j);
}
}
}
}
public void closeFile(){
m.close();
}
}
显然我的文件是作为字符串读取而不是读取内容。我尝试使用BufferedReader,但我无法使用hasNextLine。任何帮助表示赞赏。
的示例