Greenfoot: Wombats

Things to do:

Explore the Wombat Scenario

Load the Wombats scenario. Right click the title 'wombatWorld' and choose 'Populate'. Click the Act button and observe what happens. Click Run and experiment with speed. How many times does the simulation run when the Act button is clicked? How does the simulation behave when the Run button is clicked?

Right click the WombatWorld class icon and choose Open editor. What is the function of the words "public class WombatWorld extends World"? (Explain every word.) What general name is given to a line like this?

Name three other classes from the world around us and give an example of a member of each one.

The line 'public WombatWorld()' defines a particular type of function. What name is given to this? What does it do when the program runs?

What does the line 'super(8, 8, 60)' do?

Try changing the dimensions of the playing area to e.g. 10, 10 and the size of each square to 50. Compile the new code and run it. What effects does this have on the scenario?

What do these lines do: 'Wombat w1= new Wombat(); addObject(w1, 3, 3);' ?

What is happening in the function randomLeaves()?

The Wombat Class

Right click the wombat actor icon and choose Set Image. You can change the image of the wombat e.g. to another animal.

Change the leaf image to another such as a banana (note that you will have to click Reset to implement the change).

Right click the Wombat actor and choose new Wombat. Place the new object in one of the grid squares. Run the program and note that it has the same behaviour as the other wombats.

Populate the world with one or three wombats and five or six leaves.

Right click the Wombat class icon and choose Open Editor.

Explain the meaning of "public class Wombat extends Actor". (Explain every word.)

Explain the meaning of "private static final int EAST = 0". (Explain every word.)

Explain the meaning of "public wombat()". What is the purpose of this line?

What does 'setDirection(EAST)' do?

Look at the function setDirection(). What is correspondence between the compass directions and the degrees of rotation used by setRotation()?

What word is used to describe the items EAST... and leavesEaten?

Explain the meaning of "public void act()". What does this function do?

Explain the meaning of "public boolean foundLeaf()". What does this function do?

What does the function eatLeaf() do? What decides whether a leaf is eaten?

What alternative name is given to functions such as act() and eatLeaf()?

Extension:

What does the function move() do? What determines the movement of a wombat? For all four directions, explain how the movement changes according to the direction.

Explain what this line does: Actor leaf getOneObjectAtOffset(0, 0, Leaf.class)

Explain how the function move() and canMove() work together.

Explain how the functions turnLeft() and setDirection() work together.

The Leaf Class

Have a look at the code for the Leaf class: how does it compare to that for the Wombat?

Changes to Wombats

Use the tutorial guide to help you with these exercises.

Make the wombats rotate in the opposite direction (clockwise). Note that when you change the code you will have to compile it - the class icon takes on a striped background to show that the code has changed. The best way to compile the code is to click Compile in the editor; the Compile all button in the main window compiles all the code and takes longer.

Make the wombat change image when it is moving west and north so that it is not upside down. (Use setImage() in setDirection().)

What function is available to generate a random number? Make the wombats change to a random direction.

Extension:

Add a new object to the scenario e.g. a rock. To do this, right click the Actor class and choose New subclass. Note that the new class icon is striped until you compile it. The wombats will go through the new object. Amend the code so that the wombats are forced to go round the rock.

Own Scenario 1: BugWorld

Use the Creating a New Scenario and Making Things Move videos to help you with these exercises.

Create a new scenario.  Right click the World icon, choose New subclass, enter a name and choose a background image.

Use the editor to experiment with screen and cell dimensions. The screen size is found by multiplying the x and y values by the cell size e.g. (8, 8, 60) gives a screen size of 480 x 480; e.g. (600, 400, 2) gives a screen size of 1200 x 800).

Experiment with background images.

Right click the Actors icon and choose New subclass. Set the image to a bug (ladybird).

Experiment with different sizes of cell to make the bug move smoothly.