Hi guys I need some help displaying the my players Health value, I have the health in the player as 'public int health = 200;' and once that reaches zero it's game over. How would I go aboit getting this value and displaying it on the world? Would I need to use something similar to the score code I'm using?
Thanks :)
Player Code:
Score Code:
public class Player1 extends Cymeriadau
{
private GreenfootImage image1 = new GreenfootImage("PlayerImage1.png");
private GreenfootImage image2 =new GreenfootImage("PlayerImage2.png");
private int shotTimer = 0;
private int counter = 0;
public int health = 200;
private Score score;
public void act()
{
move(0);
if( atWorldEdge() ) {
move(0);
}
if(Greenfoot.isKeyDown("left") ) {
move(-5);
switchImage();
}
if( Greenfoot.isKeyDown("right") ) {
move(5);
switchImage();
}
if( Greenfoot.isKeyDown("up") ) {
setLocation(getX(), getY() - 4);
switchImage();
}
if( Greenfoot.isKeyDown("down") ) {
setLocation(getX(), getY() + 4);
switchImage();
}
if (shotTimer >0) {
shotTimer = shotTimer -1;}
else if (Greenfoot.isKeyDown("space") )
{
getWorld().addObject(new Bullet1(getRotation()), getX(), getY());
shotTimer = 10;
Greenfoot.playSound("Gun_Shoot2.wav");
}
checkHealth();
}
public void switchImage()
{
counter = counter + 1;
if( counter == 5 ) {
setImage( image1 );
} else if( counter == 10 ) {
setImage( image2 );
counter = 0;
}
}
public Player1(Score pointcounter)
{
score = pointcounter;
}
public void setHealth(int points)
{
health += points;
}
public int getHealth()
{
return health;
}
public void checkHealth()
{
if (health <0) {
getWorld().removeObject(this);
Greenfoot.setWorld(new GameOver());
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Graphics;
/**
* Counter that displays a text and number.
*
* @author Michael Kolling
* @version 1.0.1
*/
public class Score extends Actor
{
private static final Color textColor = new Color(248, 248, 255);
public static int value = 0;
public static int target = 0;
private String text;
private int stringLength;
public Score()
{
this("Score= ");
}
public Score(String prefix)
{
text = prefix;
stringLength = (text.length() + 5) * 10;
setImage(new GreenfootImage(stringLength, 16));
GreenfootImage image = getImage();
image.setColor(textColor);
updateImage();
}
public void act() {
if(value < target) {
value++;
updateImage();
}
else if(value > target) {
value--;
updateImage();
}
}
public void add(int score)
{
target += score;
}
public int getValue()
{
return value;
}
/**
* Make the image
*/
private void updateImage()
{
GreenfootImage image = getImage();
image.clear();
image.drawString(text + value, 1, 12);
}
}