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

Write a program that plays a game called Catch-the-Creature. Use an image to rep

ID: 3818076 • Letter: W

Question

Write a program that plays a game called Catch-the-Creature. Use an image to represent the creature. Have the creature appear at a random location for a random duration, then disappear and reappear somewhere else. The goal is to “catch” the creature by pressing the mouse button while the mouse pointer is on the creature image. Create a separate class to represent the creature, and include in it a method that determines if the location of the mouse click corresponds to the current location of the creature. Display a count of the number of times the creature is caught as well as the number of misses.

CODE REQUIRED IN JAVA

Explanation / Answer

import javax.swing.*; public class Catch_The_Creature { public static void main(String[] args) { JFrame frame = new JFrame("Catch the Creature"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Creature panel = new Creature(); JOptionPane.showMessageDialog(frame, "Catch Pikachu!"); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } import java.awt.*; import java.util.Random; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class Creature extends JPanel { private final int WIDTH = 400, HEIGHT = 300; private final int DELAY=20, IMAGE_SIZE = 60; private ImageIcon image; private int pikachuX, pikachuY; private int x, y; private int catchCount=0; private static Random generator = new Random(); private Timer time; private ActionListener updater; private JLabel countLabel; public Creature() { image = new ImageIcon("image/pikachu.jpg"); time = new Timer(DELAY, updater); addMouseListener ((MouseListener) new MouseClickedListener()); setBackground (Color.green); setPreferredSize(new Dimension(1900,1000)); time.start(); } public boolean point(int x, int y) { if (x == pikachuX && y == pikachuY) { catchCount++; return true; } return false; } public int getCatchCount() { return catchCount; } private class MouseClickedListener extends MouseAdapter { public void mouseClicked(MouseEvent event) { point(event.getX(), event.getY()); } } try { BufferedImage image = ImageIO.read("image/pikachu.jpeg"); } catch (Exception ex){ ex.printStackTrace(); } public void paintComponent(Graphics page) { super.paintComponent(page); page.drawImage(image, x, y, heightYouWant, widthYouWant, this); page.drawString("Pikachus Captured: " + catchCount, 10, 35); setFont(new Font("Arial", Font.BOLD,35)); } public void actionPerformed(ActionEvent event) { time.setDelay(1000); x += pikachuX; y += pikachuY; if (x = WIDTH-IMAGE_SIZE) pikachuX = pikachuX * -1; if (y = HEIGHT-IMAGE_SIZE) pikachuY = pikachuY * -1; repaint(); } public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mousePressed(MouseEvent arg0) {} public void mouseReleased(MouseEvent arg0){} }
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