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

2019/4/16

Creating a Health Orb

Syes Syes

2019/4/16

#
Hey, I woud like to draw a Health bar in form of an Orb like seen in the Diablo games Is it possible to draw an Oval and fill it vertically to a certain extent or is there any other way I can achieve this? Thanks in advance.
Super_Hippo Super_Hippo

2019/4/16

#
Two ways here without too much thinking about efficiency.
//check for every pixel, x and y
if (<distance to middle> < <radius of orb> && y > <image height> - 1 - currentHealth/fullHealth)
{
    //fill this pixel
}
Or as an alternative
//image 1
create rectangle based on current health vs. full health

//image 2
create the orb

//image 3
fill all pixels which are filled in both image1 and image2
danpost danpost

2019/4/16

#
Create a blank image, the height of full health, and fill your full oval on it. Then create another image of current health height and draw the oval image on it starting full health height above the bottom of this image. Finally, create another full-sized blank image and draw the second image on it at health height. Use this final image for the health actor. These steps (except for the 1st one, whose image can be saved in a field, would need to be done any time the health value changes.
Syes Syes

2019/4/16

#
I looked into Super_Hippo´s second suggestion and found this tutorial about Compositing Graphics. Due to my lack of programming experience I couldn't figure out how to use the AlphaComposite funktion. If someone could provide a example code on how to use it in Greenfoot I would greatly appreciate it.
Super_Hippo Super_Hippo

2019/4/17

#
I only worked with the methods which come with the GreenfootImage class, so I have no clue. danpost's suggestion is far more efficient though.
danpost danpost

2019/4/17

#
I was thinking something like this
import greenfoot.*;

public class OrbHealth extends Actor
{
    static final int SIZE = 80;
    
    int health = 100;
    GreenfootImage orb;
    
    public OrbHealth(Color color)
    {
        orb = new GreenfootImage(SIZE, SIZE);
        orb.setColor(color);
        orb.fillOval(0, 0, SIZE, SIZE);
        updateImage();
    }
    
    private void updateImage()
    {
        GreenfootImage img = new GreenfootImage(SIZE, SIZE);
        int cutSize = SIZE*health/100;
        if (cutSize > 0)
        {
            GreenfootImage cutImg = new GreenfootImage(SIZE, cutSize);
            cutImg.drawImage(orb, 0, cutImage.getHeight()-SIZE);
            img drawImage(cutImg, 0, SIZE-cutSize);
        }
        setImage(img);
    }
    
    public void adjustHealth(int change)
    {
        health += change;
        if (health > 100) health = 100;
        if (health < 0) health = 0;
        updateImage();
    }
    
    public int getHealth()
    {
        return health;
    }
}
You need to login to post a reply.