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

2017/5/3

Greenfoot chapter 4

Ethan Ethan

2017/5/3

#
Hi, I have recently started a programming course. I've probably spent about 3-4 hours with Greenfoot. Can someone please explain to me what is happening in the following code? I am completely lost. Any help will be appreciated. import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage /** * The CrabWorld is the place where crabs and other creatures live. * It creates the initial population. */ public class CrabWorld extends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Crab crab = new Crab(); addObject(crab, 231, 203); Worm worm = new Worm(); addObject(worm, 445, 137); Worm worm2 = new Worm(); addObject(worm2, 454, 369); Worm worm3 = new Worm(); addObject(worm3, 368, 466); Worm worm4 = new Worm(); addObject(worm4, 129, 488); Worm worm5 = new Worm(); addObject(worm5, 254, 388); Worm worm6 = new Worm(); addObject(worm6, 106, 334); Worm worm7 = new Worm(); addObject(worm7, 338, 112); Worm worm8 = new Worm(); addObject(worm8, 150, 94); Worm worm9 = new Worm(); addObject(worm9, 373, 240); Worm worm10 = new Worm(); addObject(worm10, 509, 55); Lobster lobster = new Lobster(); addObject(lobster, 334, 65); Lobster lobster2 = new Lobster(); addObject(lobster2, 481, 481); Lobster lobster3 = new Lobster(); addObject(lobster3, 79, 270); } }
danpost danpost

2017/5/3

#
import greenfoot.*; // makes all classes in the greenfoot package accessible

public class CrabWorld extends World // declares a World type class named CrabWorld
{
    public CrabWorld() // declares the class constructor block (creates, or instantiates, a CrabWorld object)
    {
        super(560, 560, 1); // supplies World constructor with information to create the world with (width, height and cell size) 
        prepare(); // executes the 'prepare' method 
    }

    private void prepare() // declares a method called 'prepare'
    {
        Crab crab = new Crab(); // declares a variable reference to a crab object, names it 'crab' and assigns a new instance of the Crab class to it
        addObject(crab, 231, 203); // adds the Crab object referenced by 'crab' into the world at location (231, 203)
        Worm worm = new Worm(); // declares a variable reference to a Worm object, names it 'worm' and assigns a new Worm instance to it
        addObject(worm, 445, 137); // adds the Worm object referenced by 'worm' into the world at location (445, 137)
        // the last two lines are repeated 9 times (added to world at different locations)
        Lobster lobster = new Lobster(); // declares a variable reference to a Lobster object, names it 'lobster' and assigns a new Lobster object to it
        addObject(lobster, 334, 65); // adds the Lobster object referenced by 'lobster' into the world at location (334, 65)
        // the last two lines are repeated 2 times (added to world at different locations)
    }
}
Basically, all code currently in the class is executed during world construction, and only then. Currently, that would be when the project is compiled or reset, or when you manually create a new CrabWorld. All, if any, actions performed during run-time must currently be in your Actor subclasses.
Ethan Ethan

2017/5/4

#
Thanks!
You need to login to post a reply.