Greenfoot: Wombat Lab

Directions for Loading Wombat Scenario:
We are going to load the Wombat scenario which is located in the folder Chapter 1 which is inside the folder Book Scenarios. You should have already loaded Book Scenarios into your Greenfoot Stuff folder. If you have not loaded the Book Scenarios folder yet (i.e. you are far ahead or far behind), follow the directions given here:


https://drive.google.com/drive/folders/0B5rct-XawetHNDVyRUtBY2ZyUXM


Then open the Greenfoot app on your PC. On the top tabs, go to Scenarios → Open.


Then navigate to: Greenfoot Stuff → Book Scenarios → Chapter 1 → Leaves-and-Wombats


Then click Open. Now you have loaded the Wombat scenario.


Before proceeding with the assignments, below, your instructor will lead you through the process of how to create a BoxWombat that moves in a square pattern. You will need to modify this BoxWombat code to create RectangleWombat which is the last (and by far most difficult) of your assignments, below.


IMPORTANT: Remember to place the following line at the beginning of each Wombat class you develop. Note that greenfoot is all lowercase in this import statement.

import greenfoot.*;


HINT: In the Wombat-derived classes, try to avoid overriding the act() method. Instead, override methods like turn(), move(), etc.

Assignments:


You must create new classes derived from the Wombat class to implement the following:


  1. DeadWombat: This wombat should just sit there and NOT do anything when asked to move or turn. (So there are two methods that you need to override and suppress).

  2. TurningWombat: This type of Wombat stays in the same place and simply turns 30° clockwise each time it moves.

  3. HandsomeWombat: Replace the picture of the Wombat with one you find on Google images. Make sure you have not violated copyright rules in doing so. (When searching for the image under Google images, click on:

    Search Tools → Usage Rights → Labeled for Reuse.  

    Then pick one of the pictures to use for your HandsomeWombat. Be sure to set the image inside the HandsomeWombat zero-argument constructor. Do this by overriding the zero-argument Wombat() constructor. (Do not use the compiler directive @Override when doing so as this directive is not allowed when overriding constructors). To get a hint of how to change an image, look at the examples given for BiPolarWombat.

  4. BiPolarWombat: Switches between two different images (“wombat.png” and “wombat-left.png”) of Wombats each time it moves.


Hints: Add the following code to the beginning of your BiPolarWombat class to set up
the two images:

public class BiPolarWombat extends Wombat {
//new instance variables for BiPolarWombat class

private GreenfootImage image1;

    private GreenfootImage image2;

   

// override the zero-argument Wombat constructor

    public BiPolarWombat() {

        super(); // call the zero-argument Wombat constructor

        image1 = new GreenfootImage("wombat.png"); // initialize

        image2 = new GreenfootImage("wombat-left.png");

    }

}


Then override the move() method to not only move like a regular wombat (call super.move()) but also to change the picture each time the BiPolarWombat moves.

You can change the current picture of the wombat to be image1  like this:


setImage(image1);

There is also a method called getImage() which can be used to figure out which is the current image. You should use an if-then-else statement to keep changing the image of the Wombat inside the move() method. (Calculate what the current image is and then switch to the other image).

  1. RightTurningWombat: Normally, a Wombat turns to the left when it encounters the edge of the World. Your job is to make a Wombat that turns to the right when it gets to the edge. To do this, you will need to override the Wombat’s turnLeft() method.

  2. DizzyWombat: The Wombat should move in a random direction each time it moves. Hint: After running the greenfoot command:

    int x = Greenfoot.getRandomNumber(20);

    Integer x will then contain a random number between 0 and 19 inclusive.

Hint: Do you think it would be better/easier to use the random number generator
to move the creature or to turn the creature?

  1. TrappedWombat: Create a new class called Net. Assign the picture “Backgrounds→ Bathroom-tile.jpg”  to this class. As its name implies, Net objects do not do much but just sit there and wait for an unsuspecting wombat to walk on top of it. Add a few Net objects to your scenario. If your TrappedWombat happens to fall into (walk on top of) a Net object, it is trapped and should no longer move (or turn). One net can capture more than one TrappedWombats.


Hints: The following code can be used to “detect a collision” between
a TrappedWombat and a net:

Actor a = getOneObjectAtOffset(0, 0, Net.class);

if (a != null) {

// we have a collision

}

  1. RectangleWombat: Create a new class called RectangleWombat that moves in a rectangle. The length and width of the rectangle should be specified in the contructor call when the RectangleWombat is created. It is important (critical) that your RectangleWombat only moves one square (or turn in place) when it is its turn to move.

    It is ok if your RectangleWombat “freaks out” if it gets too close to the world edge. Just start the creature near the center of the board so this does not become an issue.


To test your code, create two RectangleWombats and have them draw their corresponding rectangular paths at the same time. (Demonstrate that they take turns moving).