Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Catch Game where the user tries to click on a moving target. To draw the

ID: 3766828 • Letter: W

Question

Write a Catch Game where the user tries to click on a moving target. To draw the game space, extend the JPanel class. In this class, draw your target. (My example uses a square. You can use any shape you like). Create a Timer object in this class. When the timer fires, the target moves to a new random location. Add a MouseListener to listen for mouse clicks. If the user clicks into the target, update a "hit count" that is displayed on the screen. Have a control panel with radio buttons that specify how hard the game should be. To make the game harder, the target should move faster. The control panel will need to communicate with your other class.

Explanation / Answer

// A new target is selected if (Mouse.isButtonDown(0)) { targetX = Mouse.getX(); targetY = Mouse.getY(); } // Player is not standing on the target if (targetX != X || targetY != Y) { // Get the vector between the player and the target int pathX = targetX - X; int pathY = targetY - Y; // Calculate the unit vector of the path double distance = Math.sqrt(pathX * pathX + pathY * pathY); double directionX = pathX / distance; double directionY = pathY / distance; // Calculate the actual walk amount double movementX = directionX * speed; double movementY = directionY * speed; // Move the player X = (int)movementX; Y = (int)movementY; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote