ok, I have created a mobile application for a java enabled phone. I want to make the classic pong game with one paddle, and a number of blocks to break. I have managed to get the ball bouncing around the screen and off the block using a set of if statements checking the location of the ball against the location of the paddle and walls. So the next step is to get ball bouncing off the blocks. So i have a Block() class and within the initialisation I have managed to make an array of blocks to appear on the screen. So the collision detection is next. I have tried the following code but the ball just misses the blocks altogether, I don't understand why the ball is not bouncing off the blocks. The code is on the ball thread, I am simply trying to check the location of the ball against each blocks location. Any help would be great.
public void moveBall() {
while (true) {
ballX += incrementX;
ballY += incrementY;
//hits sides
if (ballX < 0 || ballX > (getWidth()-10)) {
incrementX = -incrementX;
}
//hits paddle or roof
if (ballY < 0 || ballY > (getHeight()-25) && ballX > paddleX && ballX < (paddleX +50)) {
incrementY = -incrementY;
}
//goes out past the paddle
if (ballY >= getHeight()) {
lives -= 1;
ballX = ((getWidth()/2)-5);
ballY = ((getHeight()/2)-5);
}
//hits a block
for (int i = 0; i < 14; i++) {
if ((ballY < blockArray[i].getY() + 10) && (ballX > blockArray[i].getX()) && (ballX < blockArray[i].getX()+30)) {
incrementY = -incrementY;
}
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}