diff --git a/src/Ball.java b/src/Ball.java index ae30fca349df4139dfeac332bfe2ede545224adc..4621ba3e9ac9534d2c5a38714ab0923a561c436e 100644 --- a/src/Ball.java +++ b/src/Ball.java @@ -48,6 +48,18 @@ public class Ball { } public void update() { + // (d) Extend the code to make the ball choose a random diagonal direction, and move in that direction. + // - When the ball reaches the left and right edges of the window, it should reverse horizontal direction. + // - Similarly, when the ball reaches the top and bottom edges of the window, it should reverse vertical direction. + // You can change the step size of player and ball when simulating. + // When the ball reaches the left and right edges of the window, it should reverse horizontal direction. + if (x < 0 || x > Window.getWidth()) { + directionX *= -1; + } + // Similarly, when the ball reaches the top and bottom edges of the window, it should reverse vertical direction. + if (y < 0 || y > Window.getHeight()) { + directionY *= -1; + } // (a) x += directionX * STEP_SIZE;