i cant et my scoreboard to set the highscore of the game. Also i build shelters in the game too and i can only place one in the world when im supposed to get 3 per game. when the lobster hits the bottom of the screen the score goes up and when i shoot the bombs its supposed t kills the lobsters and the amount of bombs goes down. PLEASE HELPPPP
lobsters
public class Lobstrosity extends Actor
{
private int touching;
private int count=1;
private ScoreBoard score = new ScoreBoard();
/**
* Act - do whatever the Lostrosity wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setRotation(90);
move(1);
if (isAtEdge())
{
getWorld().addObject(new Lobstrosity(),Greenfoot.getRandomNumber(800)+1,
Greenfoot.getRandomNumber(240)+40);
setLocation(Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40);
((MyWorld)getWorld()).getScoreBoard().incScore(1);
//((MyWorld)getWorld()).getScoreBoard().getHighScore();
if(score.getScore() > score.getHighScore())
{
((MyWorld)getWorld()).getScoreBoard();
score.setHighScore(score.getScore());
score.resetForNewGame();
}
}
if (isTouching(Shelter.class)==true)
{
((Shelter)getOneIntersectingObject(Shelter.class)).reduceStrength();
getWorld().removeObject(this);
}
}
Player
public class Player extends Actor
{
private Shelter placeShelt= new Shelter(10);
int Ammo = 10;
int sheltersCount = 3;
int initialStrength=10;
private ScoreBoard score = new ScoreBoard();
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.isKeyDown("left"))
{
move(-1);
}
if (Greenfoot.isKeyDown("right"))
{
move(1);
}
if(isTouching(Lobstrosity.class) || getWorld().getObjects(Lobstrosity.class).isEmpty())
{
getWorld().removeObjects(getWorld().getObjects(null));
if(score.getScore() > score.getHighScore())
{
((MyWorld)getWorld()).getScoreBoard();
score.setHighScore(score.getScore());
score.resetForNewGame();
}
}
if (Greenfoot.isKeyDown("space") && placeShelt.getWorld() == null && placeShelt.hasHealth())
{
placeShelt();
}
/**
* fire the PoisonBomb
*/
if ("b".equals(Greenfoot.getKey()) && getWorld().getObjects(PoisonBomb.class).isEmpty())
{
if(Ammo>0)
{
PoisonBomb poisonbomb = new PoisonBomb();
getWorld().addObject(poisonbomb, getX(), getY());
Ammo--;
((MyWorld)getWorld()).getScoreBoard().decBombCount();
}
else
{
}
}
}
public void placeShelt()
{
if(((MyWorld)getWorld()).getScoreBoard().getShelterCount()>0)
{
getWorld().addObject(placeShelt, getX(), getY()-50);
((MyWorld)getWorld()).getScoreBoard().decShelterCount();
sheltersCount--;
}
else
{
}
}
}
myWorld
public class MyWorld extends World
{
// YOU MAY WISH/NEED TO ADD INSTANCE VARIABLE(S) HERE!!!
ScoreBoard scoreboard = new ScoreBoard();
int sheltersLeft = 3;
/**
* Constructor for objects of class MyWorld.
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
prepare();
act();
}
private void prepare()
{
Player player = new Player();
addObject(player, 400, 575);
addObject(new Lobstrosity(),400,Greenfoot.getRandomNumber(240)+40);
}
/**
* getScoreBoard gives us the Scoreboard found in the world
* NOTE ---> YOU ***MUST*** CODE THIS !!!
*
* @return the ScoreBoard object seen in the world
*/
public ScoreBoard getScoreBoard()
{
addObject(scoreboard,400, 10);
return scoreboard;
}
public void act()
{
if (Greenfoot.isKeyDown("n") && getObjects(Lobstrosity.class).isEmpty())
{
Player player = new Player();
addObject(player, 400, 575);
scoreboard = new ScoreBoard();
addObject(scoreboard,400, 10);
Lobstrosity lobstrosity = new Lobstrosity();
addObject(lobstrosity,400,Greenfoot.getRandomNumber(240)+40);
}
}
/**
* containsShelter tells us whether or not a shelter
* is already in the world.
* @return true if there is a shelter, false if not
*/
//public boolean containsShelter()
//{
//}
/**
* containsBomb tells us whether or not a bomb
* is already in the world.
* @return true if there is a bomb, false if not
*/
public boolean containsBomb()
{
// fairly inelegant and inefficient way to code this
// but avoids the need for code elsewhere.
return (!getObjects(PoisonBomb.class).isEmpty());
}
/**
* lobstrosityCount tells us how man lobstrosities are
* in the World.
* @return the number of lobstrosities in the world
*/
public int lobstrosityCount()
{
return getObjects(Lobstrosity.class).size();
}
}
ScoreBoard
public class ScoreBoard extends Actor
{
private int gameScore; // current game score
private int highScore; // high score so far
private int sheltersLeft; // number of shelters left
private int bombsLeft; // number of bombs left
// "global" representing the height (bottom to top) of a
// ScoreBoard
public static final int SCOREBOARD_HEIGHT = 25;
/**
* Constructor for ScoreBoard to build starting scoreboard.
*/
public ScoreBoard()
{
gameScore=0; // no game started yet, so score is 0
highScore=0; // no game started yet, so high score is 0
bombsLeft=10; // no game started yet, so all bombs available
sheltersLeft=3; // no game started yet, so all shelters available
}
// Greenfoot trick to draw image ater object added to world.
protected void addedToWorld(World toWorld)
{
redraw();
}
// redraws scoreboard, updating values displayed as we go
// we don't want this called from outside the class, so
// it is private in this case.
private void redraw()
{
// needs regular access to world, so store in a variable.
MyWorld placedIn = (MyWorld) getWorld();
// check to see if image is valid. If not, rebuild it.
GreenfootImage img = getImage();
if (img==null ||
img.getWidth()!= placedIn.getWidth() ||
img.getHeight()!=SCOREBOARD_HEIGHT)
{
img = new GreenfootImage(placedIn.getWidth(), SCOREBOARD_HEIGHT);
setImage(img);
}
// fill image with gray background
img.setColor(Color.LIGHT_GRAY);
img.fill();
// draw scores, etc. in black
img.setColor(Color.BLACK);
img.drawString("Score:"+ gameScore, 600, SCOREBOARD_HEIGHT-8);
img.drawString("High Score:"+ highScore, 400, SCOREBOARD_HEIGHT-8);
// draw in shelter and bomb stats
img.drawString("Shelters:"+ sheltersLeft, 100, SCOREBOARD_HEIGHT-8);
img.drawString("Bombs:"+ bombsLeft, 300, SCOREBOARD_HEIGHT-8);
}
/**
* incScore - adds something to score
* @param byVal - value to add to the score.
*/
public void incScore(int byVal)
{
gameScore+=byVal;
redraw();
}
/**
* getScore - returns the current score
* @return the current game score
*/
public int getScore()
{
return gameScore;
}
/**
* decBombCount - decrease the number of bombs by 1, but only
* if there are any bombs left
* @return true when there was at least 1 bomb left,
* false otherwise
*/
public boolean decBombCount()
{
// if there are bombs left
if (bombsLeft>0)
{
bombsLeft--; // reduce bomb count
redraw(); // redraw image to see updated bomb count
return true; // we succeeded!
}
return false; // not enough bombs to do this!
}
/**
* decShelterCount - decrease the number of shelters by 1, but
* only if there are any shelters left
* @return true when there was at least 1 shelter left,
* false otherwise
*/
public boolean decShelterCount()
{
// if there are shelters left
if (sheltersLeft>0)
{
sheltersLeft--; // now there's one less shelter
redraw(); // redraw to reflect change
return true; // we had a shelter left!
}
return false; // no shlters left!
}
/**
* getShelterCount returns how mny shelters are available
*
* @return the current number of shelters
*/
public int getShelterCount()
{
return sheltersLeft;
}
/**
* getHighScore returns the current value of high score
*
* @return the current high score
*/
public int getHighScore()
{
return highScore;
}
/**
* setHighScore gives high score a new value
*
* @param newScore the intended new value of high score
*/
public void setHighScore(int newScore)
{
highScore = newScore; // store the new high score
redraw(); // redisplay scoreboard
}
/**
* resetForNewGame resets the scoreboard data for a new game
* * note that this does NOT reset high score
*/
public void resetForNewGame()
{
// games start with 3 shelters, 10 bombs, and a score of 0
sheltersLeft=3;
bombsLeft=10;
gameScore=0;
redraw(); // redisplay the scoreboard.
}
}
shelter
public class Shelter extends Actor
{
private int strengthLeft; // remaining strength
private int strength = 10;
// dimensions of the shelter object. Note that these are
// private as there is no reason to utiize these outside
// of this class.
private static final int SHELTER_WIDTH = 225;
private static final int SHELTER_HEIGHT = 25;
public void act()
{
/**
* Constructor for a shelter.
*
* @param initialStrength is the starting strength of
* this shelter
*/
}
public boolean hasHealth()
{
return strengthLeft > 0;
}
public Shelter(int initialStrength)
{
strengthLeft = initialStrength; // remember initial strength
redraw(); // redraw the shelter
}
// redraw simply rebuilds the image for the shelter.
// note that methods outside this class should not call
// redraw, so it is private.
private void redraw()
{
// (re)build image if necessary
GreenfootImage img = getImage();
if (img==null ||
img.getWidth()!=SHELTER_WIDTH ||
img.getHeight()!=SHELTER_HEIGHT)
{
img = new GreenfootImage(SHELTER_WIDTH, SHELTER_HEIGHT);
setImage(img);
}
// set background to light gray
img.setColor(Color.LIGHT_GRAY);
img.fill();
// add text repressenting the strength left in black
img.setColor(Color.BLACK);
img.drawString(""+ strengthLeft, SHELTER_WIDTH/2-10, SHELTER_HEIGHT-8);
}
/**
* reduceStrngth reduces this shelter's strength by 1 unit
*/
public void reduceStrength()
{
reduceStrength(1); // use the other reduceStrength method
}
/**
* reduceStrength reduces the strength of the shelter by a
* requested amount.
* @param byVal is the amount to reduce this shelter's strength
*/
public void reduceStrength(int byVal)
{
strengthLeft-=byVal; // reduce the strength
// if shelter is still strong enough to function ...
if (strengthLeft>0)
redraw(); // ... redisplay the shelter
else // shelter not strong enough to function
getWorld().removeObject(this); // remove this shelter
}
}