LWJGL Pong Paddle AI Boundary Detection Problem with Random Movement

I’m having trouble with my Pong game AI and hope someone can help me figure this out.

I wrote some code for controlling the computer paddle in my LWJGL Pong game. The basic version works perfectly - the paddle follows the ball and stops correctly at screen edges:

public void controlPaddle(int ballPosition, int SCREEN_HEIGHT, 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 >= SCREEN_HEIGHT - paddleSize) {
            System.out.println("Hit bottom wall");
            position = SCREEN_HEIGHT - paddleSize;
        } else {
            position += 2;
        }
    }
}

This works great. The paddle moves up and down, hits the boundaries, prints messages, and stays within screen limits.

But when I try to add random speed variation, everything breaks:

public void controlPaddle(int ballPosition, int SCREEN_HEIGHT, 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;
            }
        }
    }
    if (ballPosition > this.position + height / 2) {
        if (this.position >= SCREEN_HEIGHT - paddleSize) {
            System.out.println("Hit bottom wall");
            position = SCREEN_HEIGHT - paddleSize;
        } else {
            position += 2;
        }
    }
}

With this change, the paddle works once but then completely ignores boundaries and moves off screen. The console shows negative positions and values way bigger than screen height. I just wanted to make the AI sometimes move faster or slower for variety.

I thought maybe nested if statements were the problem so I tried restructuring but got the same result. What am I missing here?

Your random movement is messing with the boundary detection. Here’s what’s happening: when you add random speed variation, the paddle overshoots the boundary by different amounts, breaking your position checks. Your working code moves the paddle exactly 2 pixels per frame, so this.position <= 0 catches it every time. But with random speeds of 2 or 3 pixels, the paddle jumps past position 0 into negative values before the check kicks in. Fix this by updating your boundary logic. Don’t just check if position equals the boundary - clamp the position within valid bounds after every movement. Calculate the new position first, then force it to stay within screen coordinates before updating the paddle. This way, whether the paddle moves 2 or 3 pixels, it’ll never actually go off-screen even if the math would normally put it there.

you’re checking boundaries before moving, but the random speed messes up the logic. store the speed in a variable first, then check boundaries with that speed. try int speed = random.nextInt(2)+2; if(position - speed < 0) position = 0; else position -= speed; - that’ll fix the overshoot.