【发布时间】:2022-01-16 09:23:03
【问题描述】:
我正在尝试在终端中为学校作业创建游戏。没有弹出窗口或任何东西。问题是游戏本身的一个错误。 'x' 应该移动直到它撞到墙上,但它会卡在墙内。我只是在学习 java,对以后的帖子或编程的任何提示都表示赞赏。
import java.util.Scanner;
public class App {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int coordx = 1;
int coordy = 1;
while (true) {
// System.out.println("Move: ");
String move = input.nextLine();
switch (move) {
case "a":
while (!wall(coordy - 1, coordx)) {
coordx--;
}
break;
case "w":
while (!wall(coordy, coordx - 1)) {
coordy--;
}
break;
case "d":
while (!wall(coordy + 1, coordx)) {
coordx++;
}
break;
case "s":
while (!wall(coordy, coordx + 1)) {
coordy++;
}
break;
default:
break;
}
render(coordx, coordy);
}
}
public static void render(int chary, int charx) {
int[][] grid = new int[9][40];
for (int i = 0; i < 9; i++) {
System.out.print("\n");
for (int j = 0; j < 40; j++) {
if (i == charx && j == chary) {
grid[i][j] = 0;
System.out.print("x");
} else if (wall(i, j)) {
grid[i][j] = 2;
System.out.print("#");
} else {
grid[i][j] = 1;
System.out.print(" ");
}
}
}
}
public static boolean wall(int coordy, int coordx) {
if (coordx == 0 || coordx == 39 || coordy == 0 || coordy == 8) {
return true;
} else {
return false;
}
// return true;
}
}
【问题讨论】: