This site requires JavaScript, please enable it in your browser!
Greenfoot back
akiki
akiki wrote ...

2020/12/4

Use List<> to add the new asteroid when all asteroids have been cleared

akiki akiki

2020/12/4

#
I am trying to use List<> to get the asteroids in the background, but it does not work. What did I miss? Can anyone tall me. Thank you.
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 = 3;

    /**
     * Create the space and all objects within it.
     */
    public Space() 
    {
        super(600, 500, 1);
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
        createStars(300);
        
        Rocket rocket = new Rocket();
        addObject(rocket, getWidth()/2 + 100, getHeight()/2);
        
        addAsteroids(startAsteroids);
        
        scoreCounter = new Counter("Score: ");
        
        addObject(scoreCounter, 60, 480);

        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);
        }
    }
    
    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    {
        addObject(new ScoreBoard(scoreCounter.getValue()), getWidth()/2, getHeight()/2);
    }
    
    public void countScore(int score)
    {
        scoreCounter.add(score);
    }
    
    public void addNewAsteroids()
    {
        Space space = this;
        List<Asteroid> myAsteroid = space.getObjects(Asteroid.class);
        if(myAsteroid.isEmpty())
        {
            space.addAsteroids(1);
        }
        
    }
    
}
danpost danpost

2020/12/4

#
akiki wrote...
I am trying to use List<> to get the asteroids in the background, but it does not work. What did I miss?
I am quite sure your problem was in trying to assign the list to myAsteroids on line 84. Maybe this would work there:
List<Asteroid> myAsteroid = (List<Asteroid>)space.getObjects(Asteroid.class);
If not, this would be sufficient:
List myAsteroid = space.getObjects(Asteroid.class);
However, more simply put is the following:
public void addNewAsteroids()
{
    if (this.getObjects(Asteroid.class).isEmpty())
    {
        this.addAsteroids(++startAsteroids);
    }
}
I took the liberty of increasing the number of asteroids per wave.
akiki akiki

2020/12/4

#
I found my problem. I forgot to insert "import java.util.List;". When I use it, the program runs properly. I think use List<>must use "import java.util.List". Thank you for your help, danpost.
danpost danpost

2020/12/4

#
akiki wrote...
I found my problem. I forgot to insert "import java.util.List;". When I use it, the program runs properly. I think use List<>must use "import java.util.List".
My "simply put" way does not require the import.
akiki akiki

2020/12/4

#
Yes, your "simply put" does not require the import. I did not say it clear, sorry about that.
danpost danpost

2020/12/4

#
akiki wrote...
Yes, your "simply put" does not require the import. I did not say it clear, sorry about that.
Actually, it is still not required to import it, when coded as follows:
java.util.List myAsteroid = space.getObjects(Asteroid.class);
akiki akiki

2020/12/5

#
Thank you. You teach me one more way to write the code. I like this simplified way.
You need to login to post a reply.