2

好的,我有游戏、鸟类和棋盘类。我的 Game 类中有管道创建、删除和移动代码。有人建议我不要创建 Pipe 类并创建一个管道对象。没有管道代码,我的游戏运行流畅,虽然没有管道出现。当我有管道代码时,程序会运行但不会出现窗口。我将向您展示 Game 构造函数和管道代码。

public Game(int a) {

    super("Game");
    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);

    this.board = new Board(this);
    add(board, BorderLayout.CENTER);

    bird = new Bird();
    bird.setArr();
    System.out.println("Bird made");

    /*setPipeHeight(a);
    setArr1();
    setArr2();
    System.out.println("Pipe made");
    */
    //It continues, but this is the section I am looking at
}

现在管道代码

public void setPipeHeight (int h){
    System.out.println("Set height");
    height = h;
    //Set coords
    setArr1();
    setArr2();
    /*for (int i = 0; i < arrX1.length; i++){
        for (int j = 0; j < arrY1.length; j++){

        }
    }*/
}

public void setArr1(){
    System.out.println("Set arr1");
    int x = (Board.numCols - 1) - width;
    for (int i = 0; i < width + 1; i++){
        for (int j = 0; j < height + 1; j = j + height){
            int h = height;
            while (h >= 0){
                Board.grid[x][h] = 3;
            }
        }
    }
}

public void setArr2(){
    System.out.println("Set arr2");
    int tillTop = Board.numRows - (height + separation);
    for (int i = 0; i < width + 1; i++){
        for (int j = 0; j < tillTop + 1; j = j + tillTop){
            int h = height;
            while (h >= 0){
                Board.grid[x][h] = 3;
            }
        }
    }
}

public void movePipe(){
    System.out.println("Move pipe");
    x--;
    setArr1();
    setArr2();
}

当我没有注释掉任何东西时,它说它正在运行,但没有创建一个窗口。在 Game 类中,当我注释掉这些行时,会出现窗口但小鸟不会向下移动。

/*setPipeHeight(a);
setArr1();
setArr2();
System.out.println("Pipe made");
*/

当我注释掉管道代码的其余部分时,我得到一个正常运行的游戏减去出现的管道

4

1 回答 1

3

while (h >= 0)在您的情况下是一个无限循环: h 在循环内未修改。

于 2014-04-04T12:43:24.313 回答