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

1
2
3
4
5
MR.UWU MR.UWU

2022/6/1

#
hola
MR.UWU MR.UWU

2022/6/1

#
@Spock47 ahora como hago para cuando me como un corazon recuperas un poco de la vida perdida
Spock47 Spock47

2022/6/1

#
1. You need a method in health bar to recover life:
public void recoverHealth() {
    ++life;
}
2. When your player founds a heart, he has to call that method and remove the heart from the world:
final Actor heart = getOneIntersectingObject(Heart.class);
if (heart != null) {
    getWorld().removeObject(heart);
    healthBar.recoverHealth();
}
3. As you can see, the player needs a reference to the health bar, so that player can call the recoverHealth method. The cleanest way to do that is to give the health bar as parameter to the constructor of Player. A short version, that relies on having exactly one health bar (and implicitly one player), is to ask the world for the only HealthBar object present directly before the call to recoverHealth:
final HealthBar healthBar = getWorld().getObjects(HealthBar.class).get(0);
MR.UWU MR.UWU

2022/6/1

#
private void catch_gas() { if(isTouching(Gas.class)) { removeTouching(Gas.class); Greenfoot.playSound("gas.wav"); } } }
MR.UWU MR.UWU

2022/6/1

#
I have this method to remove the heart that is in the world now how do I do it so that with this method I recover a little life
MR.UWU MR.UWU

2022/6/1

#
this code is in the character
MR.UWU MR.UWU

2022/6/1

#
en le player
danpost danpost

2022/6/1

#
A health bar can be completely contained in the player class by using an inner class. A stripped down example follows:
import greenfoot.*;

public class Player extends Actor
{
    private final int maxHealth = 3600; // adjust as needed
    private Bar bar;
    private int health;
    
    protected void addedToWorld(World world)
    {
        if (bar == null) bar = new Bar();
        world.addObject(bar, 100, 30); // or 'addObject(bar, getX(), getY()-50);' if bar is to stay with player on screen
    }
    
    public void act()
    {
        if (/** any condition the causes health to change */)
        {
            // any non-bar actions here (like removing an object)
            bar.add(-10);  // adjust value as needed; a negative value decreases health
        }
        // repeat for each condition that changes health
        
        // at end of act
        if (health == 0) getWorld().removeObject(this);
    }
    
    /** Bar class */
    protected class Bar extends Actor
    {
        public Bar()
        {
            add(maxHealth); // start bar with full health
        }
        
        protected void add(change)
        {
            // adjust health
            health += change;
            if (health < 0) health = 0;
            if (heatlh > maxHealth) health = maxHealth;

            // determine bar width (percentage of health)
            int pct = health*100/maxHealth;
            
            // create and set new image for bar
            GreenfootImage image = new GreenfootImage(104, 24);
            image.drawRect(0, 0, 103, 23);
            image.drawRect(1, 1, 101, 21);
            /**
               * Could replace the last 2 lines with the following
               *
               *    image.fill();
               *    image.setColor(Color.WHITE);
              *    image.fillRect(2, 2, 102, 22);
             */
            if (pct != 0)
            {
               GreenfootImage indicator = new GreenfootImage(pct, 20);
               indicator.setColor(Color.GREEN);
               indicator.fill();
               image.drawImage(indicator(2, 2);
            }
            setImage(image);
         }
     }
}
Notice that the Bar class, here, is within the Player class. It is NOT a class on its own extending Actor.
MR.UWU MR.UWU

2022/6/2

#
yo lo que quiero es con el método de catch_gas cuando el jugador se coma una vida se recupere un poco este es el metodo que yo quiero usar @danpost @Spock47
MR.UWU MR.UWU

2022/6/2

#
private void catch_gas() { if(isTouching(Gas.class)) { removeTouching(Gas.class); Greenfoot.playSound("gas.wav"); } } }
MR.UWU MR.UWU

2022/6/2

#
hola
danpost danpost

2022/6/2

#
MR.UWU wrote...
<< Code Omitted >>
With the inner class codes above, this would suffice:
private void catch_gas()
{
    if (isTouching(Gas.class))
    {
        removeTouching(Gas.class);
        Greenfoot.playSound("gas.wav");
        bar.add(10); // adjust change (value) as needed
    }
}
MR.UWU MR.UWU

2022/6/2

#
public class Gasolina extends Actor { int healthBarWidth = 40; int healthBarHeigth = 10; private int currentFrame = 0; private int health = MAX_HEALTH; private int pixelsPerHealthPoint = healthBarWidth / MAX_HEALTH; private static final int MAX_HEALTH = 10; private static final int LOSE_HEALTH_PERIOD = 120; /** * Act - do whatever the Gasolina wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { update(); checkForHealthLoss(); } 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); } public void loseHealth() { health--; } private void checkForHealthLoss() { ++currentFrame; if (currentFrame == LOSE_HEALTH_PERIOD) { this.loseHealth(); currentFrame = 0; if (health == 0) endGame(); } } private void endGame() { getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2); Greenfoot.stop(); } }
MR.UWU MR.UWU

2022/6/2

#
I already made the life bar with this code
MR.UWU MR.UWU

2022/6/2

#
private void catch_gas() { if(isTouching(Gas.class)) { removeTouching(Gas.class); Greenfoot.playSound("gas.wav"); } }
There are more replies on the next page.
1
2
3
4
5