How would I make a healthbar deteriorate over time? Am I required to make a separate class for a clock?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class HealthBar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HealthBar extends ScrollingActor
{
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int health = 100;
int healthBarWidth = 100;
int healthBarHeight = 30;
int pixelsPerHealthPoint = (int) healthBarWidth/health;
public HealthBar(){
act();
}
public int getHealth()
{
return health;
}
public void act()
{
update();
if (Greenfoot.isKeyDown("up")) {
setLocation(getX(), getY()-5);
}
if (Greenfoot.isKeyDown("down")) {
setLocation(getX(), getY()+5);
}
if (Greenfoot.isKeyDown("left")) {
setLocation(getX()-5, getY());
}
if (Greenfoot.isKeyDown("right")) {
setLocation(getX()+5, getY());
}
}
public void update(){
setImage(new GreenfootImage(healthBarWidth+2, healthBarHeight +2));
GreenfootImage myImage = getImage();
myImage.setColor(Color.WHITE);
myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
myImage.setColor(Color.GREEN);
myImage.fillRect(1, 1, health * pixelsPerHealthPoint, healthBarHeight);
}
public void loseHealthPoisonousBerries(){
health-=7;
}
public void addHealthHive(){
if(health<=85){
health+=15;
}
else{
health=100;
}
}
public void loseHealthBees(){
health-=50;
}
public void addHealthBerries(){
if(health <= 95){
health+=5;
}
if(health == 96){
health+=4;
}
if(health == 97){
health+=3;
}
if(health == 98){
health+=2;
}
if(health == 99){
health+=1;
}
}
public void addHealthFish(){
if(health <=93){
health+=7;
}
if(health == 94){
health+=6;
}
if(health == 95){
health+=5;
}
if(health == 96){
health+=4;
}
if(health == 97){
health+=3;
}
if(health == 98){
health+=2;
}
if(health == 99){
health+=1;
}
}
}
