Skip to content
Snippets Groups Projects
Commit 00289d5f authored by yangxvlin's avatar yangxvlin
Browse files

finish Q5d

parent 23b5a04d
No related branches found
No related tags found
No related merge requests found
...@@ -48,6 +48,18 @@ public class Ball { ...@@ -48,6 +48,18 @@ public class Ball {
} }
public void update() { 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) // (a)
x += directionX * STEP_SIZE; x += directionX * STEP_SIZE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment