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

2019/5/5

make game over screen when my healthbar reaches to 0

gdrgnxxi gdrgnxxi

2019/5/5

#
My game mechanics: 1. When the falling object is on Y(599)/ at the edge, healthbar decreases please help me make game over screen when the healthbar is empty and also to stop the game.
gdrgnxxi gdrgnxxi

2019/5/5

#
//this is the code i put in my healthbar actor
//the gameover is an image

public class HealthBar extends Actor
{
    int health = 10;
    int healthBarWidth= 100;
    int healthBarHeight = 15;
    int pixelPerHealthPoint = (int)healthBarWidth/health;
    
    public void act(){
       }

       public HealthBar(){
        update();   
    }
    
    public void update(){
        setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight +2));
        GreenfootImage myImage = getImage();
        myImage.setColor(greenfoot.Color.WHITE);
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(greenfoot.Color.GREEN);
        myImage.fillRect(1,1, health*pixelPerHealthPoint, healthBarHeight);
    }
        public int loseHealth(int n){
           health = health-n;
           
           update();
           return health;
    }
        public void showGameover(){
        if (health == 0)
        {
                World realWorld = getWorld();
                RealGame levelone = (RealGame)realWorld;
                gameOver gameover = new gameOver();
                realWorld.addObject(gameover, realWorld.getWidth()/5, realWorld.getHeight()/5);
            }
        }  
}
gdrgnxxi gdrgnxxi

2019/5/5

#
should i put the gameover code in the healthbar actor or in the gameOver actor? it doesnt work :(
danpost danpost

2019/5/5

#
gdrgnxxi wrote...
should i put the gameover code in the healthbar actor or in the gameOver actor? it doesnt work :(
Show code of actor with health.
gdrgnxxi gdrgnxxi

2019/5/8

#
hello! it already worked but i used an image for the game over, is there any way to make the GAME OVER have its own world? like when healthbar is zero, it goes to game over world.
Super_Hippo Super_Hippo

2019/5/9

#
You already have the condition. So you can add:
Greenfoot.setWorld(new GameOver());
Then you need to have a World subclass named GameOver, put the image as the background or do whatever you want to do in that world.
You need to login to post a reply.