【问题标题】:Strangely Malfunctioning Flappy Bird Code奇怪的故障 Flappy Bird 代码
【发布时间】:2014-04-04 12:40:36
【问题描述】:

好的,我有 Game、Bird 和 Board 课程。我的 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");
*/

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

【问题讨论】:

    标签: java arrays loops flappy-bird-clone


    【解决方案1】:

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

    【讨论】:

    • 谢谢,。但我将其设置为使 h 每次迭代都减少。如果最大 h 可以是 21(我在 Board 类中设置),那将如何导致无限循环
    • Board.grid[x][h] = 3; h 在哪里减少?
    • 现在我添加了 h--;对于 setArr1() 和 h++;对于 setArr2() 都在他们尊重的 while 循环的末尾。我不认为这不起作用,但我可以打开一个运行游戏的窗口。我没有收到任何错误,但它仍然没有工作
    猜你喜欢
    • 2018-01-10
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多