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

2022/5/10

I need help, to put a "Game over" screen or an animation that says game over in greenfoot

2
3
4
5
MR.UWU MR.UWU

2022/6/2

#
What I want with this method is that when I eat gas, my life recovers little by little
MR.UWU MR.UWU

2022/6/3

#
hola
Spock47 Spock47

2022/6/3

#
Well, you can just add the call to recoverHealth at the end of the if-statement of catch_gas like shown in my last comment. Still, danpost's idea to center all health bar-related things into an inner class, is a good idea to keep the overview. For that, you can basically just move the complete Gasolina source code into the inner class and add the catch_gas method at the outer level:
public class Gasolina extends Actor
{
    
    private final Health health = new Health();
    
    @Override
    protected void addedToWorld(final World world)
    {
        world.addObject(health, 100, 30); // or 'addObject(bar, getX(), getY()-50);' if bar is to stay with player on screen
    }
    
    @Override 
    public void act()
    {
        catch_gas();
    }
    
    private void catch_gas()
    {
        final Actor gas = getOneIntersectingObject(Gas.class);
        if (gas != null)
        {
            getWorld().removeObject(gas);
            Greenfoot.playSound("gas.wav");
            health.recover();
        }
    }
    
    protected class Health extends Actor {

        private static final int MAX_HEALTH = 10;
        private static final int LOSE_HEALTH_PERIOD = 120; 
        
        private final int healthBarWidth = 40;
        private final int healthBarHeigth = 10;
        private final int pixelsPerHealthPoint = healthBarWidth / MAX_HEALTH;
        
        private int currentFrame = 0;
        private int health = MAX_HEALTH;
        
        @Override
        public void act()
        {
            checkForHealthLoss();
            update();
        }
    
        public void update()
        {
            setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeigth + 2));
            GreenfootImage myImage = getImage();
            myImage.setColor(Color.WHITE);
            myImage.drawRect(0,0, healthBarWidth + 1, healthBarHeigth + 1);
            myImage.setColor(Color.GREEN);
            myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeigth);
        }
    
        private void checkForHealthLoss() 
        {
            ++currentFrame;
            if (currentFrame == LOSE_HEALTH_PERIOD) 
            {
                lose();
                currentFrame = 0;
            }
        }
    
        public void lose()
        {
            health--; 
            if (health == 0)
            {
                endGame();
            }
        }
        
        public void recover()
        {
            health++;
        }
    
        private void endGame()
        {
            getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
            Greenfoot.stop();
        }
        
    }

}
You need to login to post a reply.
2
3
4
5