I am making a space game where rockets shoot bullets at asteroids. I have two functional scoreboards in my world: one for the number of bullets fired and another for the number of asteroids hit. I also want to make a third, however it always displays zero no matter how many bullets have been fired and how many asteroids have been hit.
The code for the Accuracy class is:
I realize that the actual accuracy calculation could be condensed quite a bit. It was expanded in an effort to fix the problem.
The code for the Scoreboard class is:
public class Accuracy extends Scoreboard{
private int accuracy;
private int asteroids;
private int hundredAsteroids;
private int bullets;
public Accuracy(){
super("Accuracy",0);
asteroids=AsteroidsHit.score;
bullets=BulletsFired.score;
}
/**
* Act - do whatever the Accuracy wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(bullets!=0){ //prevents division by 0
hundredAsteroids=asteroids*100;
accuracy=hundredAsteroids/bullets;
score=accuracy;
printScore();
asteroids=AsteroidsHit.score;
bullets=BulletsFired.score;
}
}
}public class Scoreboard extends Actor
{
protected static int score;
private String text;
public Scoreboard(String label, int startingScore)
{
GreenfootImage img = new GreenfootImage(300,40);
img.setColor(new Color(255,255,255));
Font f=new Font("Tahoma",true,false,24);
img.setFont(f);
img.drawString(label + ": " + startingScore,5,35);
setImage(img);
text = label;
score = startingScore;
}
public void changeScore(int howMuch)
{
score = score + howMuch;
GreenfootImage img = getImage();
img.clear();
img.drawString(text + ": " + score,5,35);
}
public void printScore(){
GreenfootImage img = getImage();
img.clear();
img.drawString(text + ": " + score,5,35);
}
public int getScore()
{
return score;
}
}

