code]code for life counter:
import greenfoot.*;
import java.awt.Color;
/**
* Write a description of class LifeCounter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class LifeCounter extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
/**
*
*/
public LifeCounter()
{
background = getImage();
value = 3;
target = 3;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
if (value > target) {
value--;
updateImage();
}
else if (value < target) {
value++;
updateImage();
}
}
/**
* Add a new score to the current counter value.
*/
public void add(int score)
{
target += score;
}
// you could add this message to the class
public void subtract(int score)
{
target -= score;
}
/**
* Return the current counter value.
*/
public int getValue()
{
return value;
}
/**
* Set a new counter value.
*/
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
main character:
import greenfoot.*;
/**
* a bob in the game will eat the Bobfood on your command.
*
* @author (A.T.P)
* @version (2.4.2)
*/
public class Bob extends Beings
{
private int points = 0;
private Score Score;
// instance field to add
LifeCounter lifeCounter = new LifeCounter();
// methods to add
public LifeCounter getLifeCounter()
{
return lifeCounter;
}
public Bob(Score pointScore)
{
Score = pointScore;
}
/**
* Act - do whatever the Bob wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(3);
checkKeys();
tryToEat();
}
/**
* once reached world edge you must turn away
*/
public void turnAtWorldEdge()
{ if (atWorldEdge())
{
turn(4);
}
}
/**
* check whether the keys are being pressed or not if they are follow the command
*/
public void checkKeys()
{
if( Greenfoot.isKeyDown("left"))
{
turn(-6);
}
if( Greenfoot.isKeyDown("right"))
{
turn(6);
}
if( Greenfoot.isKeyDown("up"))
{
move(5);
}
if( Greenfoot.isKeyDown("down"))
{
move(-5);
}
}
/**
* Check whether we can see Bobfood.if we can,eat it.
*/
void tryToEat()
{
if(canSee(BobFood.class))
{
eat(BobFood.class);
Greenfoot.playSound("slurp.wav");
Score.add(5);
createNewBobFood();
}
}
/**
* add new BobFood into the world at a random location
*/
private void createNewBobFood()
{
BobFood newBobFood;
newBobFood=new BobFood();
World world;
world=getWorld();
int worldWidth = world.getWidth();
int worldHeight = world.getHeight();
int x = Greenfoot.getRandomNumber(worldWidth);
int y = Greenfoot.getRandomNumber(worldHeight);
world.addObject(newBobFood,x,y);
}
private int getLivesCounter()
{
LifeCounter lives = ((BobWorld)getWorld()).getLivesCounter();
lives.add(-1);
if (lives.getValue() == 0) gameOver();
}
}
Need help with life counter wrote...
iv given you a list of code plz guide me to where the code is and shouldnt be and where it must be.
i also need help with what i am missing in this code
and if u could post what i need or what more info i should be giving ;)code for world:
import greenfoot.*;
/**
* all history of bobsworld.
*
* @author (A.T.P)
* @version (2.4.2)
*/
public class BobWorld extends World
{
/**
* Constructor for objects of class BobWorld.
*
*/
public BobWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(700, 500, 1);
prepare();
}
LifeCounter lifeCounter = new LifeCounter();
private LifeCounter LifeCounter = new LifeCounter();
public LifeCounter getLifeCounter()
{
return lifeCounter;
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
addObjectBob();
Bob bob = new Bob();
addObject(Bob,x1 = 121,y1 = 186);
addObject(Bob.getLifeCounter(),x2 = 40,y2 = 70);
Score score = new Score();
addObject(score,45,15);
Bob bob = new Bob(score);
addObject(bob, 121, 186);
bob.setLocation(94, 251);
bob.setLocation(106, 232);
bob.setLocation(67, 185);
bob.setLocation(72, 162);
bob.setLocation(76, 177);
bob.setLocation(75, 177);
bob.setLocation(75, 175);
bob.setLocation(85, 204);
BobFood bobfood = new BobFood();
addObject(bobfood, 468, 205);
bob.setLocation(86, 204);
bob.setLocation(100, 191);
bob.setLocation(98, 191);
bob.setLocation(134, 168);
bob.setLocation(95, 206);
bobfood.setLocation(451, 210);
BobEater bobeater = new BobEater();
addObject(bobeater, 81, 140);
BobEater bobeater2 = new BobEater();
addObject(bobeater2, 74, 334);
bob.setLocation(201, 235);
bobfood.setLocation(451, 181);
bob.setLocation(202, 235);
bob.setLocation(203, 235);
bob.setLocation(207, 216);
bobeater.setLocation(84, 142);
bobeater.setLocation(85, 150);
bobeater2.setLocation(76, 315);
BobFood bobfood2 = new BobFood();
addObject(bobfood2, 465, 46);
BobFood bobfood3 = new BobFood();
addObject(bobfood3, 468, 314);
BobFood bobfood4 = new BobFood();
addObject(bobfood4, 286, 95);
BobFood bobfood5 = new BobFood();
addObject(bobfood5, 306, 289);
BobFood bobfood6 = new BobFood();
addObject(bobfood6, 326, 201);
BobFood bobfood7 = new BobFood();
addObject(bobfood7, 552, 103);
BobFood bobfood8 = new BobFood();
addObject(bobfood8, 541, 263);
BobFood bobfood9 = new BobFood();
addObject(bobfood9, 139, 34);
BobFood bobfood10 = new BobFood();
addObject(bobfood10, 181, 340);
BobFood bobfood11 = new BobFood();
addObject(bobfood11, 69, 213);
BobFood bobfood12 = new BobFood();
addObject(bobfood12, 182, 124);
Instructions instructions = new Instructions();
addObject(instructions, 272, 395);
instructions.setLocation(282, 389);
bobeater2.setLocation(76, 304);
BobEater bobeater3 = new BobEater();
addObject(bobeater3, 73, 312);
BobEater bobeater4 = new BobEater();
addObject(bobeater4, 389, 100);
BobEater bobeater5 = new BobEater();
addObject(bobeater5, 545, 202);
bobeater3.setLocation(76, 312);
bobeater3.setLocation(346, 329);
bobeater3.setLocation(344, 329);
score.setLocation(44, 15);
score.setLocation(27, 18);
score.setLocation(27, 24);
removeObject(bobfood6);
removeObject(bobfood);
removeObject(bobfood9);
bobfood3.setLocation(469, 313);
removeObject(bobfood3);
bobfood4.setLocation(286, 98);
removeObject(bobfood4);
bobfood12.setLocation(198, 87);
bobfood5.setLocation(306, 260);
bobfood2.setLocation(396, 179);
bobfood2.setLocation(405, 167);
bobfood8.setLocation(491, 312);
bobfood8.setLocation(491, 310);
bobfood7.setLocation(543, 57);
bobfood10.setLocation(154, 340);
bobfood11.setLocation(56, 214);
bobfood11.setLocation(56, 215);
bobfood5.setLocation(307, 257);
bobfood2.setLocation(434, 132);
bobfood2.setLocation(416, 116);
bobeater4.setLocation(402, 115);
bobeater4.setLocation(378, 98);
bobfood2.setLocation(421, 136);
bobfood2.setLocation(421, 135);
instructions.setLocation(361, 489);
bobfood8.setLocation(573, 404);
bobfood5.setLocation(326, 395);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(154, 340);
bobfood10.setLocation(50, 426);
bobfood11.setLocation(67, 217);
bobfood2.setLocation(378, 245);
bobeater3.setLocation(378, 363);
bobeater2.setLocation(86, 306);
bobeater.setLocation(154, 80);
bobeater3.setLocation(381, 411);
bobeater3.setLocation(384, 430);
bobeater2.setLocation(110, 341);
score.setLocation(52, 23);
score.setLocation(51, 22);
score.setLocation(50, 16);
}
}

