Question

In: Computer Science

Make the pig win the game after eating at least 30 mushrooms. (It’s currently set to...

Make the pig win the game after eating at least 30 mushrooms. (It’s currently set to win at 15.) This will involve the act method in the Pig class.

Add one Barrel object at location (565, 350) in the world. This will involve the constructor of the PigWorld class.

Allow the user to drag-and-drop the barrel anywhere in the world by using the mouse. This will involve the act method of the Barrel class.

When the barrel is dropped, make all the mushrooms within a 75-unit radius become “stored” in the barrel. (Make sure that the mushrooms disappear from the world. The barrel doesn’t store Mushroom objects at all; instead, it only keeps a count of how many it has “stored”.) This will involve the act method of the Barrel class.

Each mushroom that the barrel stores on any given drop causes the barrel’s image to scale up by 5%. For example, if five mushrooms are stored on a drop, then the barrel’s image grows by 25% of its current size. This will involve the act method of the Barrel class.

The barrel keeps track of how many mushrooms it has stored, and it can store at most ten. This will involve a field in the Barrel class and the act method of the Barrel class.

When the barrel tries to store more than ten mushrooms, it has itself removed from the world. This will involve the act method of the Barrel class.

If the user right-clicks on the barrel, the pig “eats” all the mushrooms currently in the barrel and the barrel returns to its original size and original location. This will involve the act method and fields of the Pig class, the getMushrooms method of the Barrel class, and the reset method of the Barrel class. Remembering the original state of the barrel (location and image size) will involve fields of the Barrel class and the addedToWorld method of the Barrel class

Solutions

Expert Solution

Working code implemented in Java and appropriate comments provided for better understanding.

Here I am attaching code for these files:

  • Barrel.java
  • Mushroom.java
  • Pig.java
  • PigWorld.java

Barrel.java:

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

import java.util.List;

public class Barrel extends Actor
{
private int width;
private int height;
private int oldWidth;
private int oldHeight;
private int xcoord;
private int ycoord;
private int mushroomsEaten;
private final double SCALE_FACTOR = 1.05;
private final int MAX_SIZE = 25;
GreenfootImage img = getImage();

/**
* Automatically called by Greenfoot whenever a Barrel object
* is placed in a world. In this assigment, we use it to remember
* the initial state of this barrel - its (x,y) position and its
* original image size.
*/
public void addedToWorld(World world) {
oldWidth = img.getWidth();
oldHeight = img.getHeight();
width = oldWidth;
height = oldHeight;
xcoord = getX();
ycoord = getY();
  
}
  
/**
* Returns how many mushrooms this barrel has stored.
*/
public int getMushrooms() {
return mushroomsEaten;
}
  
/**
* Follows mouse drag-and-drop motion. Eats nearby mushrooms when
* dropped and increases its current image scale by 25%. If this barrel
* stores more than 10 mushrooms, this barrel has itself removed from
* this world.
*/
public void act() {
if (Greenfoot.mouseDragged(this)) {
MouseInfo mouse = Greenfoot.getMouseInfo();
setLocation(mouse.getX(), mouse.getY());
}
if (Greenfoot.mouseDragEnded(this)) {
List<Mushroom> mushrooms = getObjectsInRange(75, Mushroom.class);
for(Mushroom m : mushrooms) {
mushroomsEaten++;
width = (int)(width * SCALE_FACTOR);
height = (int)(height * SCALE_FACTOR);
img.scale(width, height);
getWorld().removeObject(m);
}
if(mushroomsEaten > 10) {
getWorld().removeObject(this);
}
}
  

}   
  
/**
* Returns this barrel to its original (x,y) location and its
* original image scale.
*/
public void reset() {
img.scale(oldWidth, oldHeight);
setLocation(xcoord, ycoord);
mushroomsEaten = 0;
}

}

Mushroom.java:

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Mushroom extends Actor
{
/**
* Moves one unit down the screen each act cycle.
* Stops the scenario if this mushroom reaches the bottom
* of the screen.
*/
public void act()
{
setLocation(getX(), getY() + 1);
if (getY() == 400) {
Greenfoot.stop();
}
}
}

Pig.java:

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Pig extends Actor
{
/** Keeps track of how many mushrooms this pig has eaten. */
private int shrooms;
  
/**
* Constructs a Pig object and initializes it as having
* eaten no mushrooms.
*/
public Pig() {
shrooms = 0;
}
  
/**
* Follows the mouse movement and eats mushrooms on mouse clicks.
* Stops the scenario once this pig has eaten at least 15 mushrooms.
*/
public void act()
{
if (Greenfoot.mouseMoved(null)) {
MouseInfo mouse = Greenfoot.getMouseInfo();
setLocation(mouse.getX(), mouse.getY());
}
if (Greenfoot.mouseClicked(null)) {
Mushroom m = (Mushroom) getOneIntersectingObject(Mushroom.class);
if (m != null) {
shrooms++;
getWorld().removeObject(m);
}
}
if (shrooms > 29) {
Greenfoot.stop();
}
if (Greenfoot.mouseClicked(null)) {
MouseInfo click = Greenfoot.getMouseInfo();
if(click.getButton() == 3){
Barrel barrel = (Barrel) getOneIntersectingObject(Barrel.class);
shrooms = shrooms + barrel.getMushrooms();
barrel.reset();
}
}
}
}

PigWorld.java:

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class PigWorld extends World
{

/**
* Builds a PigWorld object.
*
*/
public PigWorld()
{
super(600, 400, 1, false);
setPaintOrder(Barrel.class, Pig.class, Mushroom.class);
addObject(new Pig(), 400, 300);
addObject(new Barrel(), 565, 350);
}
  
/**
* Adds mushrooms at the top of the screen at given intervals.
*/
public void act() {
if (Greenfoot.getRandomNumber(100) < 3) {
addObject(new Mushroom(), Greenfoot.getRandomNumber(550), 0);
}
}
}


Related Solutions

The old saying that you should wait at least 30 minutes after eating before you swim...
The old saying that you should wait at least 30 minutes after eating before you swim is based on the idea that after a big meal, blood will be diverted away from your arms and legs, towards your stomach’s digestive tract. And if your limbs don’t get enough blood flow to function, you’re at risk of drowning. With a reduced blood flow, there is potentially less oxygen available to the working muscle and stomach, which is a potential cause of...
I roll a die 4 times. I win if a six appears. To make this game...
I roll a die 4 times. I win if a six appears. To make this game more interesting, I decide to add a second die and target the appearance of a double six. I reason as follows: a double six is one-sixth as likely as a six — 1/36 compared to 1/6. I should be able to increase the number of rolls by a factor of 6 (now 24 rolls) and still maintain the same probability of winning. Is this...
Play the game for at least 30-60 minutes and then answer the following question. Each question...
Play the game for at least 30-60 minutes and then answer the following question. Each question is worth 10 points. Remember to note the game that you played and analyzed. 1) What Aesthetics do you think the game achieves? What types of fun do you think the game speaks to? (use terms from this week's lecture to answer). 2) What are the core mechanics/rules of the game (cover as many of these as able)? 3) What dynamics are created from...
MIS/D3: Please make sure the answer should be in your own words with at least 30...
MIS/D3: Please make sure the answer should be in your own words with at least 30 sentences. 1. What are the business costs or risks of poof data quality? Support your discussion with at least 3 references. 2. What is data mining? Support your discussion with at least 3 references. 3. What is text mining? Support your discussion with at least 3 references.
D5/ISI: Please make sure the answer should be at least 30 sentences with your own words....
D5/ISI: Please make sure the answer should be at least 30 sentences with your own words. Research emerging enterprise network applications and describe 3 that you think are most interesting and briefly explain why you believe this.
You want to calculate a bonus if the sold price was at least equal to the listing price, and if the house sold within 30 days after being listed.
You want to calculate a bonus if the sold price was at least equal to the listing price, and if the house sold within 30 days after being listed.In cell L12, insert an IF function with a nested AND function to calculate a bonus. The AND function should ensure both conditions are met: Sold Price divided by the Listing Price is greater than or equal to 100% (cell L7) and the Days on Market are less than or equal to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT