我正在嘗試構建一個簡單的pacman遊戲,而我剛剛開始。在上下移動時實現pacman遊戲問題
目前我的構造是這樣的:
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";
}
此構造建立董事會和吃豆子將設在中間,我用「V」爲標誌。
我嘗試創建兩個方法currenlty,上下移動。
這裏是設置:
我第一稱爲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
我打印出此到控制檯(I初始化的10×10的板):
Pacman按照預期向前移動了三步,並吃了三個點。我略作修改,並創建了一個向下移動方法:
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個步驟,但我得到了這樣的結果:
我遇到的麻煩是,我不知道如何找到Pacman最後的位置。向下移動方法簡單地創建了一個新的Pacman並向下移動了3個步驟,這不是我想要的。我怎樣才能解決這個問題?
你也可以使用1d數組而不是2d數組;你有一個'int pacmanPosition',+1向右移動,+10向下移動。 – Brian
@布萊恩我需要實現從左到右的運動後,我解決了我目前的問題,這就是爲什麼我選擇二維數組。 – OPK
我編輯了我的答案來演示一個板子:) – Brian