【问题标题】:Java terminal gameJava终端游戏
【发布时间】: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;
    }
}

【问题讨论】:

    标签: java terminal console


    【解决方案1】:

    我非常喜欢这样的小游戏,很高兴能帮助像你这样的人在他们的冒险投掷编程的早期阶段。

    我认为你有一些 while contidions 混淆了。例如:

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

    据我了解你的游戏应该是:

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

    还要检查你的游戏循环中的其他条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      相关资源
      最近更新 更多