ironphoenix20 wrote...
yes, i get that but how do i actually test if the bar changes color? and i still dont understand what the first two parameters of the fillRect() method are
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* HealthBar generates and maintains a custom-sized image of a Health Bar
* in green and red (red being missing HP).
*
* Accepts any size - use Constants.
*
* NOT FINISHED! Forgive my lack of commenting...
*
* @author Jordan Cohen
* @version 0.0.1 (2012)
*/
public class HealthBar extends Actor
{
private double hp = 100;
private double currHp = 100;
private double damage = 5;
private double xCoor;
private double yCoor;
// Declare Instance Variables
private double currPercentHP;
private int redBarSize;
private int greenBarSize;
// Declare Instance Images
private GreenfootImage bar;
private GreenfootImage greyBar;
// Some constants - can be changed to suit size of related objects
private final int HP_BAR_WIDTH = 150;
private final int HP_BAR_HEIGHT = 20;
private final int OFFSET = 36;
// Declare Instance Objects
private Actor target;
// Declare some Color objects
private Color myGreen = new Color (0, 255, 0);
private Color myRed = new Color (255, 0, 0);
private Color myGrey = new Color (169, 169, 169);
/**
* Main constructor - this is only called by the other Constructors
* and is not intended to be called directly.
*/
public HealthBar()
{
bar = new GreenfootImage(HP_BAR_WIDTH, HP_BAR_HEIGHT);
greyBar = new GreenfootImage(100,100);
bar.setColor(myGreen);
bar.fill();
//bar.setColor(myGrey);
//bar.fill();
//greyBar.setColor(myGrey);
//greyBar.fill();
/*if (currHP == maxHP)
{*/
this.setImage(bar);
//this.setImage(greyBar);
}
/*else
{
this.setImage(bar);
}*/
/**
* Constructor that takes one int - for objects starting with max hit points.
* This will set both current and maximum hit points to the same value.
*/
public HealthBar(int inMaxHP)
{
this(); // Calst he Main constructor (above)
hp = inMaxHP;
currHp = inMaxHP;
}
/**
* Constructor takes an int for current and max hitpoints and also takes in an
* Actor, which this HP Bar will follow - Whenever the Actor moves, so will this
* hp bar. If the Actor is removed from the World, this HP bar will destroy itself.
*
* NOTE: This is the Constructor used in the Bug simulation
*/
public HealthBar (int inMaxHP, Actor target)
{
this(inMaxHP);
this.target = target;
}
/**
* Constructor that takes in a different value for current and max HP, ideal for
* when a new health bar is needed for an Actor that doesn't have full HP.
*/
public HealthBar(int inMaxHP, int inCurrHP)
{
this();
hp = inMaxHP;
currHp = inCurrHP;
}
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
/*if (target.getWorld() != null)
{
setLocation (target.getX(), target.getY() - OFFSET);
}
else
getWorld().removeObject(this);*/
setLocation(200,200);
}
/**
* update Method:
*
* Expects new current HP
*
* Returns true if HP has changed (needs an update)
* Returns false if HP has not changed (to avoid excessive processing)
*/
public void update (int newCurrHP)
{
for(int i=0; i<5; i++)
{
currPercentHP = (double) currHp / hp;
greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
bar.setColor(myGreen);
bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
bar.setColor(myGrey);
bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
//greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
this.setImage(bar);
}
//return true;
/*// Don't do anything if current HP hasn't changed
if (newCurrHP != currHP)
{
if (newCurrHP == maxHP)
{
this.setImage(blank);
//return false;
}
else
{
// Redraw HP bar w/ appropriate amount of green and red based on
// current HP divided by max HP
currHP = newCurrHP;
currPercentHP = (double) currHP / maxHP;
greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
redBarSize = HP_BAR_WIDTH - greenBarSize;
//Troubleshooting code:
//System.out.println("CurrHP: " + currHP + " curr%HP: " + currPercentHP);
//System.out.println("GreenBar: " + greenBarSize + " RedBar: " + redBarSize);
bar.setColor(myGreen);
bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
bar.setColor(myRed);
bar.fillRect(greenBarSize, 0, redBarSize, HP_BAR_HEIGHT);
this.setImage(bar);
//return true;
}
}
//return false;*/
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* HealthBar generates and maintains a custom-sized image of a Health Bar
* in green and red (red being missing HP).
*
* Accepts any size - use Constants.
*
* NOT FINISHED! Forgive my lack of commenting...
*
* @author Jordan Cohen
* @version 0.0.1 (2012)
*/
public class HealthBar extends Actor
{
private double hp = 100;
private int currHp = 100;
private double damage = 5;
private double xCoor;
private double yCoor;
// Declare Instance Variables
private double currPercentHP;
private int redBarSize;
private int greenBarSize;
// Declare Instance Images
private GreenfootImage bar;
private GreenfootImage greyBar;
// Some constants - can be changed to suit size of related objects
private final int HP_BAR_WIDTH = 150;
private final int HP_BAR_HEIGHT = 20;
private final int OFFSET = 36;
// Declare Instance Objects
private Actor target;
// Declare some Color objects
private int red=0;
private int green=255;
private Color myGreen = new Color (red, green, 0);
private Color myRed = new Color (255, 0, 0);
private Color myGrey = new Color (169, 169, 169);
/**
* Main constructor - this is only called by the other Constructors
* and is not intended to be called directly.
*/
public HealthBar()
{
bar = new GreenfootImage(HP_BAR_WIDTH, HP_BAR_HEIGHT);
greyBar = new GreenfootImage(100,100);
bar.setColor(myGreen);
bar.fill();
//bar.setColor(myGrey);
//bar.fill();
//greyBar.setColor(myGrey);
//greyBar.fill();
/*if (currHP == maxHP)
{*/
this.setImage(bar);
//this.setImage(greyBar);
}
/*else
{
this.setImage(bar);
}*/
/**
* Constructor that takes one int - for objects starting with max hit points.
* This will set both current and maximum hit points to the same value.
*/
public HealthBar(int inMaxHP)
{
this(); // Calst he Main constructor (above)
hp = inMaxHP;
currHp = inMaxHP;
}
/**
* Constructor takes an int for current and max hitpoints and also takes in an
* Actor, which this HP Bar will follow - Whenever the Actor moves, so will this
* hp bar. If the Actor is removed from the World, this HP bar will destroy itself.
*
* NOTE: This is the Constructor used in the Bug simulation
*/
public HealthBar (int inMaxHP, Actor target)
{
this(inMaxHP);
this.target = target;
}
/**
* Constructor that takes in a different value for current and max HP, ideal for
* when a new health bar is needed for an Actor that doesn't have full HP.
*/
public HealthBar(int inMaxHP, int inCurrHP)
{
this();
hp = inMaxHP;
currHp = inCurrHP;
}
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
/*if (target.getWorld() != null)
{
setLocation (target.getX(), target.getY() - OFFSET);
}
else
getWorld().removeObject(this);*/
setLocation(200,200);
update();
}
/**
* update Method:
*
* Expects new current HP
*
* Returns true if HP has changed (needs an update)
* Returns false if HP has not changed (to avoid excessive processing)
*/
public void update (/*int newCurrHP*/)
{
if (hit()){
red+=damage;
green-=damage;
greenBarSize-=damage;
//barLength+=damage;
//remake the bars
myGreen= new Color(red, green, 0);
//currPercentHP = (double) newCurrHP / hp;
//greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
bar.setColor(myGreen);
bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
bar.setColor(myGrey);
bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
//greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
this.setImage(bar);
}
//return true;
/*// Don't do anything if current HP hasn't changed
if (newCurrHP != currHP)
{
if (newCurrHP == maxHP)
{
this.setImage(blank);
//return false;
}
else
{
// Redraw HP bar w/ appropriate amount of green and red based on
// current HP divided by max HP
currHP = newCurrHP;
currPercentHP = (double) currHP / maxHP;
greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
redBarSize = HP_BAR_WIDTH - greenBarSize;
//Troubleshooting code:
//System.out.println("CurrHP: " + currHP + " curr%HP: " + currPercentHP);
//System.out.println("GreenBar: " + greenBarSize + " RedBar: " + redBarSize);
bar.setColor(myGreen);
bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
bar.setColor(myRed);
bar.fillRect(greenBarSize, 0, redBarSize, HP_BAR_HEIGHT);
this.setImage(bar);
//return true;
}
}
//return false;*/
}
public boolean hit()
{//checks to see if hit by dangerous item
return true;
}
}
public void update (/*int newCurrHP*/)
{
if (hit()){
red+=damage;
green-=damage;
greenBarSize-=damage;
//barLength+=damage;
//remake the bars
if (red<256 && green>0)
{
myGreen= new Color(red, green, 0);
}
else
getWorld().removeObject(this);
//currPercentHP = (double) newCurrHP / hp;
//greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
bar.setColor(myGreen);
bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
bar.setColor(myGrey);
bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
//greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
this.setImage(bar);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* HealthBar with two sections to show HP remaining
* left section decreases and increases in size and
* changes gradually from green to red to show
* remaining HP percentage and grey section to show HP lost
*
*
* Accepts any size - use Constants
*
* @author Deejesh Subramanian and Tishko Araz
* @version 0.0.1 (2017)
*/
public class HealthBar extends Actor
{
private double hp = 100;
private int currHp = 100;
private double damage = 5;
private double xCoor;
private double yCoor;
// Declare Instance Variables
private double currPercentHP;
private int greenBarSize=255;
// Declare Instance Images
private GreenfootImage bar;
private GreenfootImage greyBar;
// Some constants - can be changed to suit size of related objects
private final int HP_BAR_WIDTH = 255;
private final int HP_BAR_HEIGHT = 20;
private final int OFFSET = 36;
// Declare Instance Objects
private Actor target;
// Declare some Color objects
private int red=0;
private int green=255;
private Color myGreen = new Color (red, green, 0);
private Color myRed = new Color (255, 0, 0);
private Color myGrey = new Color (169, 169, 169);
/**
* Main constructor - creates a new bar image that is default grey and sets it to the HealthBar actor
*/
public HealthBar()
{
bar = new GreenfootImage(HP_BAR_WIDTH, HP_BAR_HEIGHT);
greyBar = new GreenfootImage(100,100);
bar.setColor(myGrey);
bar.fill();
//bar.setColor(myGrey);
//bar.fill();
//greyBar.setColor(myGrey);
//greyBar.fill();
/*if (currHP == maxHP)
{*/
this.setImage(bar);
//this.setImage(greyBar);
}
/*else
{
this.setImage(bar);
}*/
/**
* Constructor that takes one int - for objects starting with max hit points.
* This will set both current and maximum hit points to the same value.
*
* @param inMaxHP the maximum hit points the actor is allowed to have
*/
public HealthBar(int inMaxHP)
{
this(); // Calst he Main constructor (above)
hp = inMaxHP;
currHp = inMaxHP;
}
/**
* Constructor takes an int for current and max hitpoints and also takes in an
* Actor, which this HP Bar will follow - Whenever the Actor moves, so will this
* hp bar. If the Actor is removed from the World, this HP bar will destroy itself.
*
* NOTE: This is the Constructor used in the Bug simulation
*
* @param inMaxHP the maximum hit points the actor is allowed to have
* @param target the Actor the HealthBar is associated with
*/
public HealthBar (int inMaxHP, Actor target)
{
this(inMaxHP);
this.target = target;
}
/**
* Constructor that takes in a different value for current and max HP, ideal for
* when a new health bar is needed for an Actor that doesn't have full HP.
*
* @param inMaxHP the maximum hit points the actor is allowed to have
* @param inCurrHP the current HP of an actor that starts out with less than max HP
*/
public HealthBar(int inMaxHP, int inCurrHP)
{
this();
hp = inMaxHP;
currHp = inCurrHP;
}
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
/*if (target.getWorld() != null)
{
setLocation (target.getX(), target.getY() - OFFSET);
}
else
getWorld().removeObject(this);*/
//setLocation(200,200);
update();
}
/**
* update Method:
*
* Expects new current HP
*
* changes length and color of green section of bar based
* on new current HP
*/
public void update ()
{
if (hit()&&green>0){
decrease();
}
//else if (hit() && greenBarSize<=100)
//{
// increase();
//}
if (heal()&&green==0){
increase();
}
//return true;
if (currHp<=20){
}
/*// Don't do anything if current HP hasn't changed
if (newCurrHP != currHP)
{
if (newCurrHP == maxHP)
{
this.setImage(blank);
//return false;
}
else
{
// Redraw HP bar w/ appropriate amount of green and red based on
// current HP divided by max HP
currHP = newCurrHP;
currPercentHP = (double) currHP / maxHP;
greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
redBarSize = HP_BAR_WIDTH - greenBarSize;
//Troubleshooting code:
//System.out.println("CurrHP: " + currHP + " curr%HP: " + currPercentHP);
//System.out.println("GreenBar: " + greenBarSize + " RedBar: " + redBarSize);
bar.setColor(myGreen);
bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
bar.setColor(myRed);
bar.fillRect(greenBarSize, 0, redBarSize, HP_BAR_HEIGHT);
this.setImage(bar);
//return true;
}
}
//return false;*/
}
/**
* Checks to see if target actor has been hit and lost HP
*
* @return boolean True if damage to target actor detected, otherwise false
*/
public boolean hit()
{//checks to see if hit by dangerous item
return true;
}
/**
* Returns true if target actor has been healed and gained HP
*
* @return boolean True if target actor has been healed, otherwise false
*/
public boolean heal()
{//checks to see if gained hp
return true;
}
public void decrease()
{
red+=damage;
green-=damage;
greenBarSize-=damage;
//barLength+=damage;
//remake the bars
if (red<256 && green>=0)
{
myGreen= new Color(red, green, 0);
}
else
greenBarSize=0;
//redBarSize=255;
//increase();
//currPercentHP = (double) newCurrHP / hp;
//greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
bar.setColor(myGreen);
bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
bar.setColor(myGrey);
bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
//greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
this.setImage(bar);
}
public void increase()
{
red-=damage;
green+=damage;
greenBarSize+=damage;
//barLength+=damage;
//remake the bars
if (red>=0 && green<256)
{
myGreen= new Color(red, green, 0);
}
else
greenBarSize=255;
//decrease();
//currPercentHP = (double) newCurrHP / hp;
//greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
bar.setColor(myGreen);
bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
bar.setColor(myGrey);
bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
//greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
this.setImage(bar);
}
}