I need this question to be answered in the next three hours. My hero has a hunger meter, it increases every time act is called. When it hits a certian value, he starves and its game over. I have a hunger counter that shows up on screen. The variable increases in value, but the counter does not change. I need the counter to update itself every time the value of hunger increases, but I'm having trouble. When the hero eats food, hunger decreases. I need the counter to update with that info too. Here is the code I have. It may look long, but do not read it all, only pay attention to the things that have to do with hunger
In World Class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class BigWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class BigWorld extends World
{
private Level _currentLevel;
private Score _score;
private Hunger _hunger;
private boolean isGameOver;
private boolean isGameRunning;
private int numEnemies;
private int moar;
GreenfootSound backgroundmusic = new GreenfootSound("bubbleman.mp3");
/**
* Constructor for objects of class BigWorld.
*
*/
public BigWorld()
{
// Create a new world with 800x600 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
_currentLevel = new Level1();
_currentLevel.prepare(this);
_score = new Score();
addObject(_score, 78, 34);
_hunger = new Hunger();
addObject(_hunger, 78, 64);
Hero hero = new Hero();
addObject(hero, 400, 311);
isGameOver = false;
isGameRunning = false;
numEnemies = getObjects( Enemy.class ).size();
prepare();
}
public void started()
{
backgroundmusic.playLoop();
}
public void changeScore(Integer newScore) {
_score.setScore(newScore);
}
public void changeHunger(Integer newHunger) {
_hunger.setHunger(newHunger);
}
public void isLevelUp() {
if(getObjects(Food1.class).size() == 0) {
_currentLevel = _currentLevel.nextLevel();
_currentLevel.prepare(this);
}
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
public Score getScore(){
return _score;
}
public Hunger getHunger(){
return _hunger;
}
private void prepare()
{
Score score = new Score();
addObject(score, 277, 81);
score.setLocation(69, 36);
removeObject(score);
Hunger hunger = new Hunger();
addObject(hunger, 277, 100);
hunger.setLocation(69, 50);
}
public void act()
{
if (Greenfoot.getRandomNumber(1000) < 1) {
addObject (new Enemy(), 411, 558);
//moar = moar + 1;
}
if (Greenfoot.getRandomNumber(1000) < 2) {
addObject (new Enemy2(), 200, 100);
//moar = moar + 1;
}
if ( ! isGameOver ) {
int numEnemies = getObjects( Enemy.class ).size();
}
if ( !isGameRunning && numEnemies > 0 ) {
isGameRunning = true;
} else if ( isGameRunning && numEnemies == 3 ) {
isGameOver = true;
onEndGame();
Greenfoot.stop();
}
}
public void onEndGame()
{
System.out.println( "Too many enemies! GAME OVER!" );
Greenfoot.stop();
}
}
In Hero Class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Hero here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Hero extends Actor
{
private int hunger;
private int health;
private int fed;
private int crash;
private int anothercrash;
private String lastEnemyHit;
//stuff for third version of score
private Score _score;
public Hero() {
hunger = 0;
health = 25;
lastEnemyHit = null;
}
/**
* Third version of score
*/
public Hero(Score score) {
fed = 0;
_score = score;
}
/**
* Act - do whatever the Hero wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private void checkForHunger() {
for(int hunger = 0; hunger < 4000; hunger++){
//first
world.changeHunger(hunger);
//second
Hunger hunger = world.getHunger();
hunger.incrementHunger();
//third
_hunger.incrementHunger();
}
}
public void act()
{
checkForHunger();
if (Greenfoot.isKeyDown("left")) {
setRotation(180);
move(15);
}
if (Greenfoot.isKeyDown("right")) {
setRotation(0);
move(15);
}
if (Greenfoot.isKeyDown("down")) {
setRotation(90);
move(15);
}
if (Greenfoot.isKeyDown("up")) {
setRotation(270);
move(15);
}
if (getOneIntersectingObject(Enemy.class) !=null) {
health = health - 1;
crash = crash + 1;
Greenfoot.playSound("Hit.wav");
}
if (getOneIntersectingObject(Enemy2.class) !=null) {
health = health - 1;
anothercrash = anothercrash + 1;
Greenfoot.playSound("Hit.wav");
}
//if (crash == 2) {
//crash = 0;
//fed = 0;
//}
//if (anothercrash == 2) {
//anothercrash = 0;
//fed = 0;
//}
if (health == 0) {
((BigWorld) getWorld()).backgroundmusic.stop();
Greenfoot.stop();
}
if (getOneIntersectingObject(Mine.class) !=null) {
Greenfoot.playSound("boom.wav");
((BigWorld) getWorld()).backgroundmusic.stop();
Greenfoot.stop();
}
if (getOneIntersectingObject(Food1.class) !=null) {
hunger = hunger -1000;
fed = fed + 1;
}
if(lastEnemyHit == "Enemy.class"){
for (int fed = 0; fed < 10; fed++)
{
int worldX = Greenfoot.getRandomNumber(getWorld().getWidth());
int worldY = Greenfoot.getRandomNumber(getWorld().getHeight());
getWorld().addObject(new Food1(), worldX, worldY);
}
fed = 0;
}
if (hunger == 4000) {
((BigWorld) getWorld()).backgroundmusic.stop();
getWorld().removeObject(this);
Greenfoot.stop();
}
if (hunger == 1000) {
getImage().setTransparency(200);
}
if (hunger == 2000) {
getImage().setTransparency(150);
}
if (hunger == 3000) {
getImage().setTransparency(100);
}
}
private void hitFoes() {
}
private void checkForFood() {
Actor collidedWith;
collidedWith = getOneIntersectingObject(Food1.class);
//Actor collideWith = getOneIntersectingObject(Feather.class);
BigWorld world = (BigWorld)getWorld();
if(collidedWith != null) {
//turn(180);
if(hunger >= 2000) {
getWorld().removeObject(collidedWith);
fed = fed + 1;
hunger = hunger - 1000;
//first version of changing score
world.changeScore(fed);
//Second version of changing score
Score score = world.getScore();
score.incrementScore();
//Third
_score.incrementScore();
//here is where we would call isLevelUp
world.isLevelUp();
}
}
}
}
Hunger meter class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Score here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Hunger extends Actor
{
private Integer _hunger;
public Hunger() {
_hunger = 0;
GreenfootImage image = new GreenfootImage(100, 20);
image.setColor(java.awt.Color.BLACK);
image.drawString("Hunger: " + _hunger, 0, 10);
setImage(image);
}
private void updateHunger() {
GreenfootImage image = getImage();
image.clear();
image.setColor(java.awt.Color.BLACK);
image.drawString("Hunger: " + _hunger, 0, 10);
setImage(image);
}
public void incrementHunger() {
_hunger++;
updateHunger();
}
public void addToHunger(Integer amount) {
_hunger = _hunger + amount;
updateHunger();
}
public void setHunger(Integer amount) {
_hunger = amount;
updateHunger();
}
/**
* Act - do whatever the Score wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
//incrementScore();
}
}

