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: 3818520 • 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

Caught and Miss counts will be displayed on console or terminal or cmd.

Please give 5 star if I really did what you exactly want. Please comment if any problem. Always happy to help. Thankyou

Creature.java

class Creature
{
   private int xPos,yPos;//x and y positions of creature in JFrame
   private int hitCount,missCount;//caught and miss counts
   public Creature(int position[])
   {
       this.xPos = position[0];
       this.yPos = position[1];
   }
   //getting position of creature
   public int[] getPosition()
   {
       int position[] = new int[2];
       position[0] = xPos;
       position[1] = yPos;
       return position;
   }
   //setting position of creature
   public void setPosition(int point[])
   {
       xPos = point[0];
       yPos = point[1];
   }
   //checks whether mouse pointer's position is same as creature's position
   public boolean checkHit(int xMouse,int yMouse)
   {
       if(this.xPos<=xMouse && xMouse<=this.xPos+10 && this.yPos<=yMouse && yMouse<=this.yPos+10)
       {
           hitCount++;
           return true;
       }
       else
       {
           missCount++;
           return false;
       }
   }
   //prints hit and miss counts
   public void getHitAndMissCounts()
   {
       System.out.println("Caught "+hitCount+" times! Missed "+missCount+" times!!!");
   }
}

CatchTheCreature.java

import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
class CatchTheCreature extends JFrame implements MouseListener,Runnable
{
   private Creature mumu;//"name" of creature object
   private JLabel creatureImage;//image of creature
   private int time;//random time to change position of creature
   public CatchTheCreature()
   {
       time = getRandomTime();
       //a thread to run continuously to reposition the creature in the JFrame
       Thread t = new Thread(this,"Continuous running");
       t.start();
      
       //assigning some random position to creature to sit
       int position[] = new int[2];
       position = getRandomPosition();
       mumu = new Creature(position);
      
       //displaying image of creature in JFrame
       ImageIcon imageIcon = new ImageIcon("smallCreature.png"); // load the image to a imageIcon
       creatureImage = new JLabel(imageIcon);
      
       int pos[] = new int[2];
       pos = mumu.getPosition();
       creatureImage.setBounds(pos[0],pos[1],10,10);
      
       setLayout(null);
       add(creatureImage);
      
       setResizable(false);
       setSize(500,500);
       addMouseListener(this);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
   }
   //to generate random x and y positions from 1 to 490
   public int[] getRandomPosition()
   {
       Random random = new Random();
       int position[] = new int[2];
      
       position[0] = random.nextInt(490)+1;
       position[1] = random.nextInt(490)+1;
      
      
       return position;
   }
   //produces random time(numbers) from 1 to 5
   public int getRandomTime()
   {
       Random random = new Random();
       int time = random.nextInt(5)+1;
      
       return time;
   }
   public static void main(String a[])
   {
       CatchTheCreature game = new CatchTheCreature();
   }
  
   public void mouseExited(MouseEvent me)
   {
      
   }
   public void mouseEntered(MouseEvent me)
   {
      
   }
   public void mouseReleased(MouseEvent me)
   {
      
   }
   public void mousePressed(MouseEvent me)
   {
      
   }
   public void mouseClicked(MouseEvent me)
   {
       int x=me.getX();
       int y=me.getY();
      
       //if we catch creature, then we have to change position of creature dynamically.
       if(mumu.checkHit(x,y))
       {
          
           setCreatureObjectPosition();
           setCreatureImageToPositionOfCreatureObject();
           //System.out.println("Changed!!!");
       }
       //printing counts
       mumu.getHitAndMissCounts();
      
   }
  
   public void run()
   {
       while(true)
       {
           try
           {
               Thread.currentThread().sleep(time*1000);
              
               this.setVisible(true);
               setCreatureObjectPosition();
               setCreatureImageToPositionOfCreatureObject();
               time = getRandomTime();
           }
           catch(Exception e)
           {
               System.out.println("Some error in thread");
           }
       }
   }
   //assinging random position to creature
   public void setCreatureObjectPosition()
   {
       int position[] = new int[2];
       position = getRandomPosition();
       mumu.setPosition(position);
   }
   //setting image position to creature's position
   public void setCreatureImageToPositionOfCreatureObject()
   {
       remove(creatureImage);
       int pos[] = new int[2];
       pos = mumu.getPosition();
       ImageIcon imageIcon = new ImageIcon("smallCreature.png"); // load the image to a imageIcon
       creatureImage = new JLabel(imageIcon);
       creatureImage.setBounds(pos[0],pos[1],10,10);
       add(creatureImage);
       repaint();
       this.setVisible(true);
   }
}

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