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/5/29

#
hello someone knows how to make a life bar that is spent slowly. if anyone knows please send me the code
danpost danpost

2022/5/29

#
MR.UWU wrote...
how to make a life bar that is spent slowly.
A life bar is the same as a health bar or progress bar. A bar can have any minimum, maximum and range -- but they are all basically the same. Please refer to my Value Display Tutorial scenario, under non-textual displaying of values, for the bar itself. The code running the bar is dependent on your needs. The internal value (outside the codes for or class of the bar itself) used for running the bar does not have to be exactly what the bar displays. You could start with an internal value of ten thousand, with the bar showing full at 10. As the value decreases, the bar can be made to show only the thousands that it is currently at.
MR.UWU MR.UWU

2022/5/29

#
private Actor valueBar; private int value = 3600; private int maxValue = value; /** * Constructor for objects of class MyWorld. */ public MyWorld() { super(600, 400, 1); valueBar= new SimpleActor(); updateValueDisplay(); addObject(valueBar, 80,20); } public void adjustValue(int amount) { value += amount; if(value<0)value=0; if(value>maxValue) value = maxValue; updateValueDisplay(); } private void updateValueDisplay() { int wide = 100; int high = 12; GreenfootImage fullImg = new GreenfootImage(wide,high); fullImg.setColor(Color.GREEN); fullImg.fill(); GreenfootImage colorBar = new GreenfootImage(wide,high); int percentage=wide*value/maxValue; colorBar.drawImage(fullImg,percentage-wide,0); GreenfootImage image = new GreenfootImage(wide+4,high+4); image.setColor(Color.WHITE); image.fill(); image.setColor(Color.BLACK); image.drawRect(0,0,wide+3,high+3); image.drawImage(colorBar,2,2); valueBar.setImage(image); } }
MR.UWU MR.UWU

2022/5/29

#
look I did this example but it gives me an error valueBar = new SimpleActor, and what I want to do is a life bar that is spent slowly using the colors of greenfoot and creating an actor class called life
danpost danpost

2022/5/29

#
Did you create a SimpleActor class that extends Actor? What was the error? With a SimpleActor class, you do not need a class called life.
MR.UWU MR.UWU

2022/5/30

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HealthBar here. * * @author (your name) * @version (a version number or a date) */ public class HealthBar extends Actor { int health = 20; int healthBarWidth = 80; int healthBarHeigth = 10; int pixelsPerHealthPoint = (int)healthBarWidth/health; /** * Act - do whatever the HealthBar wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public HealthBar() { update(); } public void act() { update(); /*if(pixelsPerHealthPoint== 10) { }*/ } 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.RED); myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeigth); } public void loseHealth() { health--; } }
MR.UWU MR.UWU

2022/5/30

#
look I made that example of a life bar but it doesn't decrease, as I would do for it to decrease slowly
Spock47 Spock47

2022/5/30

#
MR.UWU wrote...
look I made that example of a life bar but it doesn't decrease, as I would do for it to decrease slowly
The problem is that "pixelsPerHealthPoint" changes when health changes, but it needs to be a fixed value (depending on the original health value, not the current (potentially reduced) health):
private static final int MAX_HEALTH = 20;
private int health = MAX_HEALTH;
private int pixelsPerHealthPoint = healthBarWidth / MAX_HEALTH;
Live long and prosper, Spock47
MR.UWU MR.UWU

2022/5/31

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HealthBar here. * * @author (your name) * @version (a version number or a date) */ public class HealthBar extends Actor { int healthBarWidth = 80; int healthBarHeigth = 10; private static final int MAX_HEALTH = 20; private int health = MAX_HEALTH; private int pixelsPerHealthPoint = healthBarWidth / MAX_HEALTH; /** * Act - do whatever the HealthBar wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public HealthBar() { update(); } public void act() { 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.RED); myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeigth); } public void loseHealth() { health--; } }
MR.UWU MR.UWU

2022/5/31

#
look I already did it but it does not wear life
Spock47 Spock47

2022/5/31

#
The class works fine for me. So, let's check whether the calls to the class are working correct, too. Please show where you call the "loseHealth" method.
MR.UWU MR.UWU

2022/6/1

#
int healthBarWidth = 80; int healthBarHeigth = 10; private static final int MAX_HEALTH = 20; private int health = MAX_HEALTH; private int pixelsPerHealthPoint = healthBarWidth / MAX_HEALTH; /** * Act - do whatever the HealthBar wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public HealthBar() { update(); } public void act() { 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.RED); myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeigth); } public void loseHealth() { health--; }
MR.UWU MR.UWU

2022/6/1

#
yes it's fine but it doesn't do what I want, which is that the life bar is spent slowly. if you know how to do it can you pass me the code if you can
Spock47 Spock47

2022/6/1

#
For being spent slowly, the loseHealth method has to be called repeatedly. This can be done best from the act method. The act method of any actor gets called repeatedly. But the act method of any actor runs about 60 times per second. Therefore, you don't want to call the loseHealth method every time (because then the health would be down to 0 after a third of a second). Instead you only want to call loseHealth e.g. every second, which would mean every 60th frame (since act is called 60 times a second). Let's define a constant for this:
// time period (in frames) for losing one health point (higher values = slower decrease in health)
private static final LOSE_HEALTH_PERIOD = 60; 
Now, you have to 1. Remember how many frames have been run since the last health loss and 2. Check whether it equals the number of frames defined above and in that case call the loseHealth method.
// Frames run since the last health loss
private int currentFrame = 0;

public void act() {
    checkForHealthLoss();
}

private void checkForHealthLoss() {
    ++currentFrame;
    if (currentFrame == LOSE_HEALTH_PERIOD) {
        this.loseHealth();
        currentFrame = 0;
    }
}
You can put this source code directly into the health bar.
MR.UWU MR.UWU

2022/6/1

#
ok thank you very much it works for me and now as I do for when I eat a heart you recover a little of the lost life
There are more replies on the next page.
1
2
3
4
5