2015-12-02 40 views
1

我正在嘗試構建一個簡單的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的板):

enter image description here

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個步驟,但我得到了這樣的結果:

enter image description here

我遇到的麻煩是,我不知道如何找到Pacman最後的位置。向下移動方法簡單地創建了一個新的Pacman並向下移動了3個步驟,這不是我想要的。我怎樣才能解決這個問題?

+0

你也可以使用1d數組而不是2d數組;你有一個'int pacmanPosition',+1向右移動,+10向下移動。 – Brian

+0

@布萊恩我需要實現從左到右的運動後,我解決了我目前的問題,這就是爲什麼我選擇二維數組。 – OPK

+0

我編輯了我的答案來演示一個板子:) – Brian

回答

0

更改tickUptickDown的方式來保存你的吃豆子的新位置:

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的起始位置獲得空間。一旦你將空間寫入數組,你更新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 
} 
0

pacmanBornWidthpacmanBornHeight領域的相反,你應該有一個字段吃豆子當前位置(所有領域不應是靜態的):

String[][] board; 
java.awt.Point pacmenPos; 

public PacmanKata(int height, int width) { 

    board = new String[height][width]; 
    pacmanPos = new Point((int) width/2, (int) height/2); 

    for (int i = 0; i < boardHeight; i++) { 
     for (int j = 0; j < boardWidth; j++) { 
      board[i][j] = "*"; 
     } 
    } 
    board[pacmanPos.x][pacmanPos.y] = "V"; 
} 

現在取代的pacmanBornWidth所有出現和pacmanBornHeightpacmanPos.xpacmanPos.y

而在你tickUptickDown方法,只需更新吃豆子位置:

public void tickUp(int steps) { 
    ... 
    pacmanPos.translate(0, steps); 
    ... 
} 

public void tickDown(int steps) { 
    ... 
    pacmanPos.translate(0, -steps); 
    ... 
} 

這也將工作一樣,如果你添加tickLefttickRight方法:

public void tickLeft(int steps) { 
    ... 
    pacmanPos.translate(-steps, 0); 
    ... 
} 

public void tickRight(int steps) { 
    ... 
    pacmanPos.translate(steps, 0); 
    ... 
} 
+0

使用一個'pacmanPos'變量是一個好主意,但是您的解決方案掩蓋了用Pacman吃掉藥片的空間來更新板子。 – Brian