Java终端游戏



我正试图在终端中为学校作业创建一个游戏。没有弹出的窗户或任何东西。问题出在游戏本身。"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;
}
}

我非常喜欢这样的小游戏,能在冒险初期帮助像你这样的人进行编程真是太棒了。

我想你有一些时间contidion混淆了。例如:

case "a":
while (!wall(coordy - 1, coordx)) {
coordx--;

据我所知,你的游戏应该是:

case "a":
while (!wall(coordy, coordx - 1)) {
coordx--;

还可以查看游戏大厅中的其他条件。

相关内容

最新更新