can you check if i did it right way or not?
7 Fix the score counting. You have seen that there already is a score counter,
but it is not being used yet. The counter is defined in class Counter, and a counter object is
being created in the Space class. You will have to do roughly the following: Add a method to
the Space class named something like countScore—this should add a score to the score
counter; and call this new method from the Asteroid class whenever an asteroid gets hit (you may want to have different scores for splitting the asteroid and removing the last little piece).
//SPace.java
import greenfoot.*;
/**
* Space. Something for rockets to fly in.
*
* @author Michael Kölling
* @version 1.2
*/
public class Space extends World
{
private Counter scoreCounter;
private int startAsteroids = 2;
/**
* Create the space and all objects within it.
*/
public Space()
{
super(600, 500, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addAsteroids(startAsteroids);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 480);
createStars(300);
Explosion.initializeImages();
ProtonWave.initializeImages();
}
/**
* Add a given number of asteroids to our world. Asteroids are only added into
* the left half of the world.
*/
private void addAsteroids(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
/**
* Crete a given number of stars in space.
*/
private void createStars(int number)
{
GreenfootImage background = getBackground();
for(int i=0; i < number; i++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
int color = 120 - Greenfoot.getRandomNumber(100);
background.setColor(new Color(color,color,color));
background.fillOval(x, y, 2, 2);
}
}
public void countScore(int count)
{
scoreCounter.add(count);
}
/**
* This method is called when the game is over to display the final score.
*/
public void gameOver()
{
addObject(new ScoreBoard(999), getWidth()/2, getHeight()/2);
}
public void add()
{
startAsteroids++;
addAsteroids(startAsteroids);
}
}
//Counter.java
import greenfoot.*;
/**
* A Counter class that allows you to display a numerical value on screen.
*
* The Counter is an actor, so you will need to create it, and then add it to
* the world in Greenfoot. If you keep a reference to the Counter then you
* can adjust its value.
*
* @author Neil Brown and Michael Kölling
* @version 1.1
*/
public class Counter extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
private String prefix;
public Counter()
{
this(new String());
}
/**
* Create a new counter, initialised to 0.
*/
public Counter(String prefix)
{
background = getImage(); // get image from class
value = 0;
target = 0;
this.prefix = prefix;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
if (value < target) {
value++;
updateImage();
}
else if (value > target) {
value--;
updateImage();
}
}
/**
* Add a new score to the current counter value. This will animate
* the counter over consecutive frames until it reaches the new value.
*/
public void add(int score)
{
target += score;
}
/**
* Return the current counter value.
*/
public int getValue()
{
return target;
}
/**
* Set a new counter value. This will not animate the counter.
*/
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
/**
* Sets a text prefix that should be displayed before
* the counter value (e.g. "Score: ").
*/
public void setPrefix(String prefix)
{
this.prefix = prefix;
updateImage();
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
if (text.getWidth() > image.getWidth() - 20)
{
image.scale(text.getWidth() + 20, image.getHeight());
}
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
