Question

I need to make the game impossible to win, so I used this code to move AI paddle.

My code may be slightly unreadable at first, so here's the theory behind it: the paddle always hits the ball with the middle point, unless the ball is closer to the edge than half of the paddle length, then the paddle stops to move with one of its ends touching top or bottom window frame.

if (ball.getY() < getHeight() - HEIGHT / 2
                    && ball.getY() > HEIGHT / 2) { 
                paddleRight.setLocation(getWidth() - 3 * WIDTH, ball.getY()
                        - HEIGHT / 2);
                image2.setLocation(getWidth() - 3 * WIDTH, ball.getY() - HEIGHT
                        / 2);
                image2.sendToFront();
            } else if (ball.getY() < HEIGHT / 2) {
                paddleRight.setLocation(getWidth() - 3 * WIDTH, 0);
                image2.setLocation(getWidth() - 3 * WIDTH, 0);
                image2.sendToFront();
            } else {
                paddleRight.setLocation(getWidth() - 3 * WIDTH, getHeight()
                        - HEIGHT);
                image2.setLocation(getWidth() - 3 * WIDTH, getHeight() - HEIGHT);
                image2.sendToFront();
            }

My ball also speeds up randomly during the game:

boolean bool = rand.nextBoolean();
            if (bool)
                if (dx > 0)
                    dx += 1;
                else
                    dx -= 1;
            else if (dy > 0)
                    dy += 0.5;
                 else
                    dy -= 0.5;

ball movement consist of X and Y axis movement

And at some specific speed, if the paddle gets to one of the corners it starts to blink back and forth between top and bottom corner. I can't find the reason for that in my code.

Full code here

Was it helpful?

Solution

Your if statement does not correctly handle conditions where the ball is exactly at boundaries between conditions. You have:

if (ball.getY() < getHeight()-HEIGHT/2 && ball.getY() > HEIGHT/2) { 
    ...
} else if (ball.getY() < HEIGHT/2) {
    ...
} else { 
    ...
}

But what happens when ball.getY() == HEIGHT/2? In this case, it will fall through to the third block, but this is not the desired behavior - it will briefly jump to the third condition when ball.getY() == HEIGHT/2, hence flicker when the ball's Y position is at that edge. Instead, make a simple change to your second condition:

if (ball.getY() < getHeight()-HEIGHT/2 && ball.getY() > HEIGHT/2) { 
    ...
} else if (ball.getY() <= HEIGHT/2) { // note <= instead of <
    ...
} else { // ball.getY() >= getHeight()-HEIGHT/2
    ...
}

This should take care of the transition cases.


By the way, unrelated, you should use curly braces in nested if statements like the one you have, e.g.:

if (bool) {
    if (dx > 0)
        dx += 1;
    else
        dx -= 1;
} else {
    if (dy > 0)
        dy += 0.5;
    else
        dy -= 0.5;
}

While your original logic happens to have the same effect in this case, your original code, despite its slightly misleading indentation, actually had this structure:

if (bool)
   ...
else if (dy > 0)
   ...
else // !bool && dy <= 0
   ...

That probably doesn't reflect the actual intent of that if and, even though it happens to work here, adding the curly braces is a good habit to get into because it can prevent potential subtle logic errors in more complex code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top