Olá gente. Estou fazendo um Pong, já tenho as raquetes, a bola o fundo, a k=linha do meio. O negócio é o seguinte: a bola tem dois ints assim: int dx, dy; e eu quero que quando bola toque na raquete esquerda, ela vá para a direita, (esse problema só ocorre na raquete esquerda, que na classe abaixo, coloquei pad, e o nome do seu retângulo é Pad1). Eu até coloquei assim:
if((ball).intersects(Pad1)){
dx = 1;
Para que a bola vá para a direita, na classe:
package pong;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Ball {
private Paddleft pad = new Paddleft();
private Paddleright pad2 = new Paddleright();
private Image image;
int x, dx, y, dy, w, h;
public Ball(){
try{
image = ImageIO.read(getClass(). getResource("res//ball.jpg"));
} catch(IOException e) {
throw new RuntimeException(
"Não foi possível carregar a imagem " + e);
}
this.w = image.getWidth(null);
this.h = image.getHeight(null);
x = 675;
y = 220;
}
public void move(){
x += dx;
y += dy;
if(y < 0){
y = 0;
}
if(y > 393){
y = 393;
}
}
public Image getImage() {
return image;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rectangle getBounds(){
return new Rectangle(10, 10);
}
public void checkforCollisions(){
Rectangle ball = this.getBounds();
Rectangle Pad1 = pad.getBounds();
Rectangle Pad2 = pad2.getBounds();
if((ball).intersects(Pad1)){
dx = 1;
}
if((ball).intersects(Pad2)){
dx = -1;
}
}
}