Why do I get an infinite loop?
I am trying to make a ball game that will simply make some bricks and such and destroy them when they get hit by the ball. The ball, paddle, brick, and such are all using their own classes so I don't have a huge mess but my code is pretty lengthy but I can post it here if need be. I have this bit of code that I'm not sure why it does this. I have the ball starting off inside the window at x = 100 and y = 400 and the window is about 600 by 600 I think but the point of the matter is, the ball starts off in the window. I wanted to make it to where if you hit the bottom, the game stops, but I can't get it to stop incorrectly issuing out the you lose code. I notice in my netbeans, it starts up, then it says you lose three times before the game even starts. I don't understand why. Can someone here please explain?
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bouncing.ball;
/**
*
* @author Marlin
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
public class BouncePanel extends JPanel implements KeyListener {
//location of bouncing ball
Ball ball;
Paddle paddle;
LinkedList<Brick> brickList;
private int x;
private int y;
private int dx = 5;
private int dy = 5;
int i = 0;
BouncePanel() {
ball = new Ball();
paddle = new Paddle();
brickList = new LinkedList<Brick>();
makeABunchOfBricks();
x = 100;
y = 50;
AnimationThread animethread = new AnimationThread(this);
animethread.start();
//keyboard stuff
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics g) {
//clear screen
super.paintComponent(g);
//draw the ball
ball.draw(g);
//draw the paddle
paddle.draw(g);
//draw the bricks
for (Brick b : brickList) {
b.draw(g);
}
}
public void update() throws InterruptedException {
ball.update();
paddle.update();
//check for wall collisions
if (ball.x <= 0 || ball.x + ball.getWidth() >= getWidth()) {
ball.dx = -1 * ball.dx;
}
if (ball.y <= 0) {
ball.dy = -1 * ball.dy;
}
if (ball.y + ball.getHeight() >= getHeight())
{
System.out.println("You Lose!!!");
}
//check for paddle collision
if (ball.intersects(paddle)) {
ball.dy = -1 * ball.dy;
if (paddle.dx == 10) {
ball.dx = 8;
}
if (paddle.dx == -10) {
ball.dx = -8;
}
if (paddle.dx == 0 && ball.dx == 8) {
ball.dx = 4;
}
if (paddle.dx == 0 && ball.dx == -8) {
ball.dx = -4;
}
}
//check for brick collisions
for (Brick b : brickList) {
if (ball.intersects(b) && !b.destroyed) {
b.destroy();
ball.dy = -1 * ball.dy;
}
}
repaint();
}
public void makeABunchOfBricks() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
brickList.add(new Brick(i * 60, j * 30, 50, 20));
}
}
}
//key listener methods
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == 'd') {
paddle.dx = 10;
}
if (e.getKeyChar() == 'a') {
paddle.dx = -10;
}
}
public void keyReleased(KeyEvent e) {
paddle.dx = 0;
paddle.dy = 0;
}
public void keyTyped(KeyEvent e) {
}
}
Re: Why do I get an infinite loop?
How can the code be tested? I don't see a main() method to start the execution.
Try debugging the code by adding calls to the println() method. Print out the values of all the variables that control the program's execution as they are changed and used so that you can see what the computer sees which will help you understand why the code is doing what it is doing.
Re: Why do I get an infinite loop?
Hmm....I could try that but not until later today but like I mentioned, for some reason when I added a println to the method in question, it sometimes goes infinite or it will output the code three times in a row even thought, from what I can see, the conditions are not being met.
Here is the main method.
I didn't include it here cause I didn't know if it was needed.
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bouncing.ball;
/**
*
* @author Marlin
*/
import javax.swing.*;
public class Driver {
public static void main(String[] args) {
//create a frame
JFrame frame = new JFrame("Fun bouncing ball");
//create a panel, put in frame
BouncePanel panel = new BouncePanel();
frame.getContentPane().add(panel);
//set frame size, make visible
frame.setSize(600, 600);
frame.setVisible(true);
}
}
Re: Why do I get an infinite loop?
Quote:
when I added a println to the method in question, it sometimes goes infinite or it will output the code three times in a row even thought, from what I can see, the conditions are not being met.
Please post the debug output that shows what is being traced and how the values changed as the coded executed.
The posted code doesn't have any printlns in it for debugging. How are you debugging the code?
The posted code does not compile because of missing class definitions. It can not be executed for testing unless it compiles.
Re: Why do I get an infinite loop?
Ok so I did some println on the code but the reason it gives you that error is cause I didn't include the classes since it's somewhat numerous. I notice that when I start the program, for some reason, the getHeight takes a few cycles to initialize. For like three or four cycles, getHeight is at 0 then it sets itself 600 like it is suppose to or to whatever height i set to it initialize.