Study the Catch a Creature program and substitute your own creature for the one
ID: 3850597 • Letter: S
Question
Study the Catch a Creature program and substitute your own creature for the one that is displayed, (20 points) in java //******************************************************************** // Creature.java Author: Lewis/Loftus // // Solution to Programming Project 9.12 //******************************************************************** import javax.swing.*; import java.awt.*; import java.util.Random; public class Creature { private int creatureX, creatureY, clickCount, catchCount; private ImageIcon creature; private Random gen; //----------------------------------------------------------------- // Creates the creature. //----------------------------------------------------------------- public Creature(int initialX, int initialY) { creature = new ImageIcon("happyFace.gif"); creatureX = initialX; creatureY = initialY; clickCount = catchCount = 0; gen = new Random(); } //----------------------------------------------------------------- // Moves the creature to a random location within the play area. //----------------------------------------------------------------- public void move(Dimension area) { creatureX = gen.nextInt(area.width - creature.getIconWidth()); creatureY = gen.nextInt(area.height - creature.getIconHeight()); } //----------------------------------------------------------------- // Returns true if point (x , y) is in the creature and increments // the catch count, else returns false. //----------------------------------------------------------------- public boolean pointInMe(int x, int y) { clickCount++; if (x >= creatureX && x <= creatureX + creature.getIconWidth()) { if (y >= creatureY && y <= creatureY + creature.getIconHeight()) { catchCount++; return true; } else return false; } else return false; } //----------------------------------------------------------------- // Returns the number of catches. //----------------------------------------------------------------- public int getCatchCount() { return catchCount; } //----------------------------------------------------------------- // Returns the number of misses. //----------------------------------------------------------------- public int getMissCount() { return clickCount - catchCount; } //----------------------------------------------------------------- // Draws the creature on the specified component. //----------------------------------------------------------------- public void draw(Component panel, Graphics page) { creature.paintIcon(panel, page, creatureX, creatureY); } }
Explanation / Answer
The code is given below :
package cybermoo;
/**
* Acts as a base class for all living entities.
* Encapsulates common logic between player characters and non-player character
* @author Shane
*/
public class Creature {
private String name;
private String description;
private int health;
private int maxHealth;
private Creature target;
@Override
public String toString() {
return getName();
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the health
*/
public int getHealth() {
return health;
}
/**
* @param health the health to set
*/
public void setHealth(int health) {
this.health = health;
}
/**
* @return the maxHealth
*/
public int getMaxHealth() {
return maxHealth;
}
/**
* @param maxHealth the maxHealth to set
*/
public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}
/**
* @return the target
*/
public Creature getTarget() {
return target;
}
/**
* @param target the target to set
*/
public void setTarget(Creature target) {
this.target = target;
}
}