This site requires JavaScript, please enable it in your browser!
Greenfoot back
PrezHawkeye
PrezHawkeye wrote ...

2013/11/22

Heath hearts problem

PrezHawkeye PrezHawkeye

2013/11/22

#
I'm trying to get my game to reset after the player loses a heart and when it reaches 0 the game ends. I also need the game to remember the score so that it will leave off where it was when the player died. My actor died with the eat method from the crabworld game. Also how do i increase the size of my health bar?

public class HaleyCenter extends World
{
    GreenfootImage title = new GreenfootImage("TitleScreen.png");
    GreenfootImage lose = new GreenfootImage("LoseScreen.png");
    GreenfootImage win = new GreenfootImage("Winscreen.png");
    GreenfootSound music = new GreenfootSound("danger_zone.mp3");
    Actor aubie = new Aubie();
    int gamePlayMode = 0;
   
    public Health hearts = new Health();
    private Score score = new Score();
    
    /**
     * Constructor for objects of class HaleyCenter.
     * 
     */
    public HaleyCenter()
    {    
        super(800, 500, 1); 
        populate();
        spawn();
        addObject(new TitleScreen(), 400, 250);
    }
    
    public Score getScore()
    {
        return score;
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown("enter"))
        {
            gamePlayMode = 1;
            music.stop();
        }
        
        if (gamePlayMode != 0)
        {
            createWorld();
            score = new Score();
            addObject(score, 50, 25);
            addObject(hearts, 40, 480);
            music.play();
        }
        
        gameOver();
    }
 public void gameOver()
    {
        gamePlayMode = 0;
        if (getObjects(Zombies.class).isEmpty())
        {
            setBackground(win);
            removeObjects(getObjects(Aubie.class));
            removeObjects(getObjects(Professor.class));
            removeObjects(getObjects(Student.class));
            removeObjects(getObjects(Wall.class));
            removeObjects(getObjects(desk.class));
           
            if (Greenfoot.isKeyDown("enter"))
            {
                Greenfoot.setWorld(new HaleyCenter());
                music.stop();
            }
        }
        
        gamePlayMode = 0;
        if (getObjects(Aubie.class).isEmpty() && !getObjects(Zombies.class).isEmpty())
        {
            setBackground(lose);
            removeObjects(getObjects(Aubie.class));
            removeObjects(getObjects(Professor.class));
            removeObjects(getObjects(Student.class));
            removeObjects(getObjects(Wall.class));
            removeObjects(getObjects(desk.class));
            
            if (Greenfoot.isKeyDown("enter"))
            {
                Greenfoot.setWorld(new HaleyCenter());
                music.stop();
            }
        }
        
        if (Greenfoot.isKeyDown("shift"))
        {
           Greenfoot.stop();
           music.stop();
        }
    }

/////////////////////////////////////////////////////
public class Health extends Characters
{
    GreenfootImage[] hearts = {new GreenfootImage("hearts0.png"),
                               new GreenfootImage("hearts1.png"),
                               new GreenfootImage("hearts2.png"),
                               new GreenfootImage("hearts3.png")};
    int health = 3;
    
    public Health()
    {
        updateImage();
    }
    
    private void updateImage()
    {
        setImage(hearts[health]);
    }
    
    public void updateHealth(int value)
    {
        health += value;
        if (health > 3) health = 3;
        if (health == 0) Greenfoot.stop();
        else updateImage();
    }
}
//////////////////////////////////////////////////////
public class Score extends Characters
{
   private int scoreCount = 0;  
  
   public void act() 
   { 
      String text = "Score: " + ((int)(scoreCount));  
      int fontSize = 20;  
      Color textColor = Color.BLACK,  
      bgColor = new Color(0, 0, 0, 0);  
      setImage(new GreenfootImage(text, fontSize, textColor, bgColor));       
   }  
  
   public void updateScore(int score) 
   {  
      scoreCount += score;
   } 
   
}
//////////////////////////////////////////////////////
within my actor class:
public void health()
    {
        if (getWorld().getObjects(Aubie.class).isEmpty())
        {
           HaleyCenter world = (HaleyCenter)getWorld();
           world.hearts.updateHealth(-1);
        }
    }
//////////////////////////////////////////////////
You need to login to post a reply.