There are two ways that my scenario ends: 1) Patrick collects 100 shamrocks or 2) Patrick is poisoned by a mushroom. I've managed to get the counter and scoreboard to cooperate when Patrick collects a hundred shamrocks. At this point in the game the scenario stops and the scoreboard comes up showing the correct number from the counter (the counter keeps track of shamrocks collected). My problem now is getting the scoreboard to come up when Patrick is poisoned by a mushroom. I've tried the same code, altered the same code, rearranged the same code (!) that I've used in class Patrick, but cannot get it to work. Any help would be greatly appreciated. Here is a copy of the terminal error that appears as well as classes Mushroom, Patrick and PatrickWorld:
THIS IS THE TERMINAL ERROR I GET WHEN PATRICK IS EATEN BY A MUSHROOM
at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.NullPointerException
at Mushroom.tryToEatPatrick(Mushroom.java:76)
at Mushroom.act(Mushroom.java:29)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.NullPointerException
at Mushroom.tryToEatPatrick(Mushroom.java:76)
at Mushroom.act(Mushroom.java:29)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.NullPointerException
at Mushroom.tryToEatPatrick(Mushroom.java:76)
at Mushroom.act(Mushroom.java:29)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
java.lang.NullPointerException
at Mushroom.tryToEatPatrick(Mushroom.java:76)
at Mushroom.act(Mushroom.java:29)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
MUSHROOM CLASS
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Poisonous mushrooms float around and will end game if they touch Patrick.
*
* @author ()
* @version (1.0)
*/
public class Mushroom extends Animal
{
private int shamrocksEaten;
private Counter counter;
/**
* Act - do whatever the Mushroom wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(2);
randomTurn();
turnAtEdge();
tryToEatPatrick();
}
/**
* With a 20% probability, turn a bit right or left.
*/
public void randomTurn()
{
if (Greenfoot.getRandomNumber(100) < 20 )
{
turn(Greenfoot.getRandomNumber(40)-30 );
}
}
/**
* If we reach the edge of the world, turn a little bit.
*/
public void turnAtEdge()
{
if(atWorldEdge())
{
turn(7);
}
}
/**
* Check whether we can see Patrick. If we can, poison/eat him.
*/
void tryToEatPatrick()
{
if (canSee(Patrick.class))
{
eat(Patrick.class);
Greenfoot.playSound("SadTrombone.wav");
Greenfoot.stop();
PatrickWorld patrickworld = (PatrickWorld) getWorld();
//patrickworld.removeObject(this);
patrickworld.gameOver(counter.getValue());
}
}
PATRICK CLASS
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Patrick is on a mission to collect as many Shamrocks as he can. For every ten Shamrocks he collects, he will
* receive a Keg of beer. Patrick can be controlled by using the up, down, left and right keys. He is able to
* collect/eat Shamrocks and food items that appear from time to time.
*
* @author ()
* @version (2.0)
*/
public class Patrick extends Animal
{
private int shamrocksEaten;
private Counter counter;
/**
* Create Patrick with a reference to the counter in the game.
*/
public Patrick(Counter pointCounter)
{
counter = pointCounter;
}
/**
* Act - do whatever Patrick wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeys();
lookForShamrock();
lookForBurger();
lookForFries();
lookForPizza();
lookForMuffin();
}
public Patrick()
{
shamrocksEaten = 0;
}
/**
* This method is called when the game is over to display the final score
*/
public void gameOver()
{
getWorld().addObject(new ScoreBoard(counter), getWorld().getWidth()/2, getWorld().getHeight()/2);
}
/**
* Check whether we can see a Shamrock. If we can, collect/eat it.
*/
public void lookForShamrock()
{
if ( canSee(Shamrock.class) )
{
eat(Shamrock.class);
counter.add(1);
Greenfoot.playSound("Bamboo.wav");
createNewShamrock();
createNewKeg();
}
}
/**
* Create a new Keg and another Mushroom for every 10 shamrocks collected.
*/
private void createNewKeg()
{
if (counter.getValue() == 9 )
{
getWorld().addObject(new Keg(), 80, 455);
Greenfoot.playSound("Popcorn.wav");
getWorld().addObject(new Mushroom(), Greenfoot.getRandomNumber(480), Greenfoot.getRandomNumber(600));
}
if (counter.getValue() ==19 )
{
getWorld().addObject(new Keg(), 110, 455);
Greenfoot.playSound("Popcorn.wav");
getWorld().addObject(new Burger(), Greenfoot.getRandomNumber(480), Greenfoot.getRandomNumber(600));
getWorld().addObject(new Mushroom(), Greenfoot.getRandomNumber(480), Greenfoot.getRandomNumber(600));
}
if (counter.getValue() == 100)
{
getWorld().addObject(new Keg(), 350, 455);
Greenfoot.playSound("ThatsAllFolks.wav");
Greenfoot.stop();
PatrickWorld patrickworld = (PatrickWorld) getWorld();
// patrickworld.removeObject(this);
patrickworld.gameOver(counter.getValue());
}
}
}
PATRICKWORLD
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
* @author ()
* @version (2.0)
*/
public class PatrickWorld extends World
{
private int counter;
/**
* Constructor for objects of class PatrickWorld.
*
*/
public PatrickWorld()
{
super(600, 480, 1);
prepare();
createMushrooms(5);
createShamrocks(10);
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Counter counter = new Counter();
addObject(counter, 30, 450);
Patrick patrick = new Patrick(counter);
addObject(patrick, 307, 179);
}
/**
* Create 15 Shamrocks and place them at random coordinates in the world.
*/
private void createShamrocks(int number)
{
for(int s = 0; s < number; s++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
addObject(new Shamrock(), x, y);
}
}
/**
* Create 5 Mushrooms and place them at random coordinates in the world.
*/
private void createMushrooms(int number)
{
for(int m = 0; m < number; m++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
addObject(new Mushroom(), x, y);
}
}
/**
* This method is called when the game is over to display the final score.
*/
public void gameOver(int score)
{
addObject (new ScoreBoard(score), getWidth() / 2, getHeight() /2 );
}
}

