From 00289d5ffabf9895a4d7c1cbaf8c813af1db1cb1 Mon Sep 17 00:00:00 2001 From: yangxvlin <1768528843@qq.com> Date: Wed, 31 Mar 2021 23:05:58 +1100 Subject: [PATCH] finish Q5d --- src/Ball.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Ball.java b/src/Ball.java index ae30fca..4621ba3 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; -- GitLab