I’m working on a Pong game using Java and LWJGL and having trouble with my AI paddle logic. When I use fixed movement speed, everything works perfectly and the paddle respects screen boundaries. But as soon as I add random speed variation, the boundary checking breaks.
Working version with fixed speed:
public void movePaddle(int ballPosition, int screenHeight, int paddleSize) {
if (ballPosition < this.position + height / 2) {
if (this.position <= 0) {
System.out.println("Hit top wall");
position = 0;
} else {
position -= 2;
}
}
if (ballPosition > this.position + height / 2) {
if (this.position >= screenHeight - paddleSize) {
System.out.println("Hit bottom wall");
position = screenHeight - paddleSize;
} else {
position += 2;
}
}
}
Broken version with random speed:
public void movePaddle(int ballPosition, int screenHeight, int paddleSize) {
if (ballPosition < this.position + height / 2) {
if (this.position <= 0) {
System.out.println("Hit top wall");
position = 0;
} else {
if(random.nextInt(2) + 1 == 1){
position -= 2;
} else {
position -= 3;
}
}
}
// bottom movement logic stays the same
}
The random version works once but then the paddle goes off-screen and ignores boundaries completely. The console still shows the correct position values though. I want to make the AI sometimes move faster or slower for variety. What am I doing wrong here?