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?
/* * 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) { } }