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.
}
}
Player
public class Player extends Actor
{
int Ammo = 10;//number of bombs per game
int sheltersCount = 3;//number of shelters per game
int initialStrength = 10;//how much heath the shelters have
private ScoreBoard score = new ScoreBoard();//the score of the scorebaord in the world
/**
* 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);//moves player to the left
}
if (Greenfoot.isKeyDown("right"))
{
move(1);//moves player to the right
}
if(isTouching(Lobstrosity.class) )//|| getWorld().getObjects(Lobstrosity.class).isEmpty())
{
getWorld().removeObjects(getWorld().getObjects(null));//removes all characters
if(score.getScore() > score.getHighScore())
{
score.setHighScore(score.getScore());//sets the highscore
score.resetForNewGame();//resets the score
((MyWorld)getWorld()).getScoreBoard();//sets the scoreboard
}
}
if (Greenfoot.isKeyDown("space") && getWorld().getObjects(Shelter.class).isEmpty() )
{
placeShelt();//adds a shelter only if there is none in the world already
}
if ("b".equals(Greenfoot.getKey()) && getWorld().getObjects(PoisonBomb.class).isEmpty())
{
//shoots the bomb is b is pressed
if(Ammo>0)
{
PoisonBomb poisonbomb = new PoisonBomb();//adds a new bomb
getWorld().addObject(poisonbomb, getX(), getY());//adds a new bomb where the b is pressed
Ammo--;//subtracts from the ammo
((MyWorld)getWorld()).getScoreBoard().decBombCount();//decrease the number of boms on the scoreboard
}
else
{
//nothing because we're out of ammo
}
}
}
/**
* adds a shelter to the world only if theres enough left
*/
public void placeShelt()
{
if (sheltersCount>0)
{
getWorld().addObject(new Shelter(10), getX(), getY()-50);//adds a new shelter
((MyWorld)getWorld()).getScoreBoard().decShelterCount();//decreases the shelter count in th scoreboard
sheltersCount--;//subtracts from the shelter count
}
else
{
//nothing because we're out of shelters
}
}
}
my world
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();
}
}
lobster
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);
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);
}
}
}
when the lobster hits the bottom of the screen the score goes up