出于某种奇怪的原因,当我尝试在游戏中移动我的角色时,交换“移动”的数组将不起作用,当我尝试为游戏中的前两个移动打印角色的当前位置时,没有任何显示。只是总体上表现得很奇怪。有人可以向我解释我做错了什么,或者至少我怎样才能做得更好?谢谢。
代码:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Adventure {
private Scanner in_file;
private int x_size, y_size, px, py, dx, dy;
private char[][] level;
private boolean game;
public Adventure() {
x_size = y_size = 20;
level = new char[x_size][y_size];
px = py = 1;
dx = dy = 0;
game = true;
}
public static void main(String []args) {
Adventure adv = new Adventure();
adv.load_game();
adv.main_game();
System.out.println("ADVENTURE");
}
public void load_game() {
try {
in_file = new Scanner(new File("level.txt"));
} catch (FileNotFoundException e) {
System.err.println("ERROR: Level could not be loaded!");
System.exit(1);
}
for (int i = 0; i < level.length; i++ ) {
for (int j = 0; j < level[i].length; j++) {
if (in_file.hasNextInt()) {
int nextInt = in_file.nextInt();
if (nextInt == 0)
level[i][j] = '-';
else if (nextInt == 1)
level[i][j] = '#';
}
}
}
in_file.close();
}
public void new_game() {
}
public void main_game() {
Scanner key = new Scanner(System.in);
Adventure adv_move = new Adventure();
level[px][py] = 'Q';
while (game) {
for (int i = 0; i < level.length; i++) {
for (int j = 0; j < level[i].length; j++) {
System.out.print(level[i][j]);
}
System.out.println();
}
System.out.println();
System.out.print("Enter a letter choice to move ->");
String temp = key.nextLine();
if (temp.equals("w")) {
dx = -1;
dy = 0;
}
else if (temp.equals("a")) {
dy = -1;
dx = 0;
}
else if (temp.equals("s")) {
dx = 1;
dy = 0;
}
else if (temp.equals("d")) {
dy = 1;
dx = 0;
}
else
dx = dy = 0;
System.out.println();
adv_move.move_check();
}
}
public void move_check() { //DYSFUNCTIONAL ARRAY!!!!
System.out.println(level[px][py]);
// System.out.println(level[px+dx][py+dy]);
if (level[px+dx][py+dy] != '#') {
level[px][py] = '-';
px += dx;
py += dy;
level[px][py] = 'Q';
}
else
System.out.print("Move invalid.\n");
}
public void save_game() {
}
}