Hellooo!!
im trying to make a game for greenfoot. Right now, the character shoots bomb at lobsters falling from the sky and they disappear and also is supposed to press space and make a "shelter" that can only be hit by 10 lobsters then disappear. ive gotten the shelter to appear but i cant seem to figuer out how to make the life decrease. Helppppppp!!!
My World code.
public class MyWorld extends World
{
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);
ScoreBoard scoreboard = new ScoreBoard();
addObject(scoreboard,400, 10);
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()
{
ScoreBoard scoreboard = new ScoreBoard();
return scoreboard;
}
public void act()
{
if (Greenfoot.isKeyDown("n") && getObjects(Lobstrosity.class).isEmpty())
{
Player player = new Player();
addObject(player, 400, 575);
ScoreBoard 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();
}
}
Shelter code
public class Shelter extends Actor
{
private int strengthLeft; // remaining strength
private int initialStrength = 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
*/
if (isTouching(Lobstrosity.class))
{
initialStrength--;
}
if (initialStrength==0)
{
getWorld().removeObject(this);
}
}
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
}
}
Player Class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends Actor
{
private Shelter placeShelt= new Shelter(10);
private int speedX;
private int speedY;
int Ammo = 10;
int sheltersLeft = 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()
{
int deltaX=getX();
int deltaY=getY();
if (Greenfoot.isKeyDown("left") )
{
deltaX-=1;
}
if (Greenfoot.isKeyDown("right") )
{
deltaX+=1;
}
setLocation(deltaX, deltaY);
if(isTouching(Lobstrosity.class) || getWorld().getObjects(Lobstrosity.class).isEmpty())
{
getWorld().removeObjects(getWorld().getObjects(null));
}
if ("space".equals(Greenfoot.getKey())&& getWorld().getObjects(Shelter.class).isEmpty())
//&& score.getShelterCount()>=0))
{
placeShelt();
}
/**
* fire the PoisonBomb
*/
if ("b".equals(Greenfoot.getKey()))
{
if(Ammo>0)
{
PoisonBomb poisonbomb = new PoisonBomb();
getWorld().addObject(poisonbomb, getX(), getY());
Ammo--;
}
else
{
}
}
}
public void placeShelt()
{
//Shelter shelter = new Shelter();
getWorld().addObject(placeShelt, getX(), getY()-50);
}
}
Lobster class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Lobstrosity extends Actor
{
private int touching;
private int count=1;
/**
* 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 (isTouching(Shelter.class))
//{
//removeTouching(Lobstrosity.class);
// getWorld().removeObject(this);
//}
if (isAtEdge())
{
getWorld().addObject(new Lobstrosity(),Greenfoot.getRandomNumber(800)+1,
Greenfoot.getRandomNumber(240)+40);
setLocation(Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40);
getWorld().addObject(new Lobstrosity(),Greenfoot.getRandomNumber(800)+1,
Greenfoot.getRandomNumber(240)+40);
setLocation(Greenfoot.getRandomNumber(800)+1, Greenfoot.getRandomNumber(240)+40);
}
if (isTouching(Shelter.class)==true)
{
getWorld().removeObject(this);
//placeShelt.reduceStrength(-1);
}
}
}

