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

2019/2/1

Change background in leaves and wombats

Ravens52 Ravens52

2019/2/1

#
Is there a way to stop the background changing back to the original when you try to save the world?
import greenfoot.*;  // imports Actor, World, Greenfoot, GreenfootImage

/**
 * A world where wombats live.
 * 
 * @author Michael Kölling
 * @version 2.0
 */
public class WombatWorld extends World
{
    /**
     * Create a new world with 10x10 cells and
     * with a cell size of 60x60 pixels.
     */
    public WombatWorld() 
    {
        super(10, 10, 60);        
        setBackground("cell.jpg");
        setPaintOrder(Wombat.class, Leaf.class);  // draw wombat on top of leaf
        prepare();
    }

    /**
     * Populate the world with a fixed scenario of wombats and leaves.
     */    
    public void populate()
    {
        addObject(new Wombat(), 7, 1);
        addObject(new Wombat(), 6, 6);
        addObject(new Wombat(), 1, 7);
        randomLeaves(20);
    }

    /**
     * Place a number of leaves into the world at random places.
     * The number of leaves can be specified.
     */
    public void randomLeaves(int howMany)
    {
        for (int i=0; i<howMany; i++) {
            Leaf leaf = new Leaf();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(leaf, x, y);
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        Wombat wombat = new Wombat();
        addObject(wombat,1,2);
        Wombat wombat2 = new Wombat();
        addObject(wombat2,1,7);
        Leaf leaf = new Leaf();
        addObject(leaf,4,2);
        Leaf leaf2 = new Leaf();
        addObject(leaf2,5,5);
        Leaf leaf3 = new Leaf();
        addObject(leaf3,4,7);
    }
}
danpost danpost

2019/2/1

#
Remove line 18.
You need to login to post a reply.