我正在尝试构建一个简单的吃豆子游戏,而我才刚刚开始。

目前,我的构造函数如下所示:

static String[][] board;
static int pacmanBornHeight;
static int pacmanBornWidth;

public PacmanKata(int height, int width) {

    board = new String[height][width];
    pacmanBornHeight = (int) Math.floor(height / 2);
    pacmanBornWidth = (int) Math.floor(width / 2);

    for (int i = 0; i < boardHeight; i++) {
        for (int j = 0; j < boardWidth; j++) {
            board[i][j] = "*";
        }
    }
    board[pacmanBornHeight][pacmanBornWidth] = "V";
}


这个构造函数设置了板子,pacman将位于中间,我使用“ V”作为符号。

我尝试创建两种方法,上下移动。

这是设置:

我首先调用了tickUp方法:

public void tickUp(int steps) {
    int counter = 1;
    int timer = 0;
    for (int loop = 0; loop < steps; loop++) {
        board[pacmanBornHeight - counter][pacmanBornWidth] = "V";
        for (int innerTimer = 0; innerTimer < counter; innerTimer++) {
            board[pacmanBornHeight - innerTimer][pacmanBornWidth] = " ";
        }
        counter++;
        timer++;
    }


    for (int i = 0; i < boardHeight; i++) {
        for (int j = 0; j < boardWidth; j++) {
            System.out.print(board[i][j]);
        }
        System.out.println();
    }
    System.out.println("-------------------------");
} //end going UP


然后将其打印到控制台(我初始化了一个10 x 10板):

java - 上下移动pacman游戏问题-LMLPHP

吃豆子如预期的那样向上移动了三个台阶,吃了三个点。我稍加修改并创建了下移方法:

public void tickDown(int steps) {
    int counter = 1;
    int timer = 0;
    for (int loop = 0; loop < steps; loop++) {
        board[pacmanBornHeight + counter][pacmanBornWidth] = "V";
        for (int innerTimer = 0; innerTimer < counter; innerTimer++) {
            board[pacmanBornHeight + innerTimer][pacmanBornWidth] = " ";
        }
        counter++;
        timer++;
    }

    for (int i = 0; i < boardHeight; i++) {
        for (int j = 0; j < boardWidth; j++) {
            System.out.print(board[i][j]);
        }
        System.out.println();
    }
    System.out.println("-------------------------");
}//end tickDown


现在,我打电话给tickDown,并要求它向下移动3个步骤,但结果如下:

java - 上下移动pacman游戏问题-LMLPHP

我遇到的麻烦是,我不知道如何找到吃豆子的最后一个位置。下移方法只是创建了一个新的吃豆人,并向下移动了3个步骤,这不是我想要的。我怎样才能解决这个问题?

最佳答案

更改tickUptickDown方法以保存Pacman的新位置:

public void tickDown(int steps) {
    int counter = 1;
    int timer = 0;
    for (int loop = 0; loop < steps; loop++) {
        for (int innerTimer = 0; innerTimer < counter; innerTimer++) {
            board[pacmanBornHeight + innerTimer][pacmanBornWidth] = " ";
        }
        pacmanBornHeight += counter;
        //Allow for wraparounds:
        if (pacmanBornHeight > board.length) {
            pacmanBornHeight = 0;
        }
        board[pacmanBornHeight][pacmanBornWidth] = "V";
        timer++;
    }

    for (int i = 0; i < boardHeight; i++) {
        for (int j = 0; j < boardWidth; j++) {
            System.out.print(board[i][j]);
        }
        System.out.println();
    }
    System.out.println("-------------------------");
}//end tickDown


我移动了循环,以将板子中的空格写到外循环的开始处。这样,您将根据吃豆子的起始位置获得空间。将空格写入数组后,您将更新Pacman的位置并将其写入数组。

编辑:

这是显示如何将一维数组用作电路板的示例:

public class PacmanKata {
    static String[] board;
    static int pacmanPosition;
    static int boardHeight;
    static int boardWidth;

    public static void main(String[] args) {

        PacmanKata kata = new PacmanKata(10,10);
        kata.tickUp(7);
        kata.tickRight(9);
    }

    public PacmanKata(int height, int width) {
        boardHeight = height;
        boardWidth = width;
        board = new String[height*width];
        int offset = (width + 1) % 2;
        pacmanPosition = (int) Math.floor((height + offset)*width/2);

        for (int i = 0; i < board.length; i++) {
            board[i] = "*";
        }
        board[pacmanPosition] = "V";
    }

    private void printBoard() {
        for (int i = 0; i < board.length; i++) {
            System.out.print(board[i]);
            if ((i+1) % boardWidth == 0) {
                System.out.println();
            }
        }
        System.out.println("-------------------------");
    }

    public void tickUp(int steps) {
        int counter = -1 * boardHeight;
        for (int loop = 0; loop < steps; loop++) {
            //Current position = ' '
            board[pacmanPosition] = " ";
            //Pacman's position changes:
            pacmanPosition += counter;
            //Allow for wraparounds:
            if (pacmanPosition < 0) {
                pacmanPosition += board.length;
            }
            //Update the board with Pacman's new position:
            board[pacmanPosition] = "V";
        }

        printBoard();
    }//end tickUp

    public void tickRight(int steps) {
        int counter = 1;
        for (int loop = 0; loop < steps; loop++) {
            //Current position = ' '
            board[pacmanPosition] = " ";
            //Pacman's position changes:
            pacmanPosition += counter;
            if (pacmanPosition % boardWidth == 0) {
                pacmanPosition -= boardWidth;
            }
            //Update the board with Pacman's new position:
            board[pacmanPosition] = "V";
        }

        printBoard();
    }//end tickUp
}

10-06 09:52