2

Okay, I have Game, Bird, and Board classes. I have pipe creation, deletion, and movement code in my Game class. I was advised to not make a Pipe class and to create a pipe object. Without the pipe code, my game runs smoothly, although no pipes appear. When I have the pipe code, the program runs but does not make a window appear. I'll show you the Game constructor and the pipe code.

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
}

Now the pipe code

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();
}

When I do not comment out anything, it says it's running, but does not create a window. In the Game class, when I comment out these lines, the window appears but the bird does not move downward.

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

When I comment out the rest of the pipe code, I get a functioning game minus the pipes appearing

TanMath
  • 598
  • 1
  • 9
  • 27
GrimThor3
  • 171
  • 1
  • 10

1 Answers1

3

while (h >= 0) is an infinite loop in your case: h is not modified within the loop.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • thanks,. but I set it so that h decreases every iteration. How would that cause an infinite loop if the maximum h can be is 21 (I set that in the Board class) – GrimThor3 Apr 04 '14 at 14:33
  • ``Board.grid[x][h] = 3;`` where does ``h`` decrease? – Jean Logeart Apr 04 '14 at 15:19
  • now I added h--; for setArr1() and h++; for setArr2() both at the end of their respected while loops. I do not mine that this does not work but that I get to be able to open a window with the game running on it. I did not get any errors but it still did not work – GrimThor3 Apr 05 '14 at 19:12