In: Computer Science
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
Working code implemented in Java and appropriate comments provided for better understanding.
Here I am attaching code for these files:
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);
}
}
}