hello all!
I'm new to the forum, so forgive me if i'm asking too much or if i'm asking something unreasonable. For my APCS class, we were told to make one of three games. I choose the hardest of the three, pong. We were supplied with some basic code as an outline to start off of. Currently, i've created a game of pong were the left and right paddle move properly, but the ball does not bounce off of the right paddle correctly. If i manage to get the ball to the left side, if bounces off the paddle if the paddle is in its course. the ball sticks to the left or right wall if it hits it. it bounces off of the the top and bottom walls as it's supposed to. the code is attached below. There are six classes that i use for this. In the actual "pong" class (not the class "the game") the algorithm for detecting if the ball hits the right paddle is off (like i said before). Can anyone help? Cheers!
the algorithm that needs fixing is as follows:
if( (ball.getX() <= rightPaddle.getX() + rightPaddle.getWidth() + Math.abs(ball.getXSpeed()) && (ball.getY() >= rightPaddle.getY() && ball.getY() <= rightPaddle.getY() + rightPaddle.getHeight() || ball.getY() + ball.getHeight() >= rightPaddle.getY() && ball.getY() + ball.getHeight() < rightPaddle.getY() + rightPaddle.getHeight()))) { System.out.println("hit Right Paddle"); if(ball.getX() >= rightPaddle.getX() + rightPaddle.getWidth() - Math.abs(ball.getXSpeed())) { ball.setYSpeed(-ball.getYSpeed()); //System.out.println("changing y speed"); } else { ball.setXSpeed(-ball.getXSpeed()); //System.out.println("changing x speed"); } }
Ball.txt
Block.txt
Locatable.txt
paddle.txt
pong.txt
TheGame.txt