A quick intro to Greenfoot
This introduction provides a quick step into using Greenfoot. It assumes that you are somewhat familiar with programming. It's aim is on brevity, rather than coverage of details.
You can find more information in the Wombats Tutorial.
Also available in French.
| Go to the download page to download and install Greenfoot. When you open Greenfoot for the first time, select Open tutorial and tutorial scenario for a brief introduction. |
 |
The tutorial scenario is called Wombats. (Wombats are native Australian animals.) On the right hand side, we see some classes involved in this scenario. Right-click (MacOS: ctrl-click) the Wombat class (the box labelled Wombat) and select new Wombat(). You will get a wombat icon. Drag it into the world (the large empty brownish area of the main window). Now click the 'Act' button. The tutorial explains more interactions with objects. We will move on to other things now which are not covered in the tutorial.

a) create object |

b) move to world |

c) place |
Download the crabs scenario and unzip it. In Greenfoot, select Scenario | Open and then select the crabs folder you've unzipped. Create a crab in the same way we created a wombat, place it in the world, and click run. Nothing will happen; we now need to program the crab to move.

We will now make the crab do something. Open the editor for the crab by selecting Open editor from its menu. In the editor, you will see an empty act method:
public void act()
{
}
Insert the command move(); in the body of the act method:
public void act()
{
move();
}
Close the editor and click the Compile all button. Then place a crab into the world again and try clicking the Act button. Also click Run and play with the Speed slider.
|
 |
Now we can make the crab move. We can also make it turn. Modify your code (using the editor) to include a turn command. Note that the turn method takes a parameter for the number of degrees to turn. We use 5 degrees in our example.
public void act()
{
move();
turn(5);
}
Compile, place the crab in the world again, and try it out (click Run). Try placing multiple crabs.
The move and turn methods come from the Animal class. Let's find out what other methods we've got available to us.
Open the editor of the Animal class, and then switch to Documentation view. You can do this by changing the pop-up menu at the top right of the editor from Source Code to Documentation. |
 |
Inspecting the existing methods, we see that we have a method called 'atWorldEdge()' to check whether we are near the edge of the world. Using this, we can modify our code to turn only when we reach the world edge.
public void act()
{
if (atWorldEdge()) {
turn(13);
}
move();
}
Place multiple crabs into the world and try it out.
We now want to add a new class (another type of animal). Select New subclass from the Animal class's menu. Type Worm as the class name.
You see that there are already some images prepared, including one for a sand worm. Select that image, and then click Ok. You can then compile again and place the worm into the world. |
 |
Next, we want to control the crab, so that we can make the crab catch the worms. The first step is to be able to control the crab with the keyboard. Every key on the keyboard has a name, e.g. "left" for the left cursor key. Here is how we can add keyboard control.
public void act()
{
if (Greenfoot.isKeyDown("left")) {
turn(-7);
}
if (Greenfoot.isKeyDown("right")) {
turn(7);
}
move();
}
Now, we want the crab to eat the worms when it comes across them. We can use the 'canSee' and 'eat' methods from the Animal class. Compile, place a crab and some worms into the world, and try it out.
public void act()
{
if (Greenfoot.isKeyDown("left")) {
turn(-7);
}
if (Greenfoot.isKeyDown("right")) {
turn(7);
}
move();
if (canSee(Worm.class)) {
eat(Worm.class);
}
}
If we want to make this scenario a little more interesting, we can add another creature that chases the crab. Let's add a lobster that runs around randomly and tries to eat the crab. Add the lobster as before, and write the following code in its act method.
public void act()
{
if (atWorldEdge()) {
turn(17);
}
move();
if (Greenfoot.getRandomNumber(100) > 90) {
turn(Greenfoot.getRandomNumber(90)-45);
}
if (canSee(Crab.class)) {
eat(Crab.class);
}
}
Now add a crab, three lobsters and a few worms to the world and see whether you can get all worms before the lobsters catch you!
The last step for today is to add some sound to the little game we have created. In the crab class, at the place in the code where the crab eats the worm, place a statement to play a sound. Two sound files, names "slurp.wav" and "au.wav" are provided with this scenario.
if (canSee(Worm.class)) {
eat(Worm.class);
Greenfoot.playSound("slurp.wav");
}
Similarly, add a statement to play the "au.wav" sound in the Lobster class where the lobster eats the crab.
To find out what other methods are available to do things in Greenfoot, use the 'Greenfoot Class Documentation' item from the Help menu. The best way, maybe, to get started is to look at some other scenarios. Several are included in the Greenfoot installation, and more are available from the Scenarios page and the Greenfoot Gallery.
Doing more
This quick introduction is a very much abbreviated version of parts of the first two chapters of the Greenfoot book. In this book, this example is introduced and discussed in much more detail, followed by many other programming projects which lead you through developing a variety of games and simulations, such as Asteroids, an ants simulation, a piano, a planet/gravity simulation, and more. For more information, please see the bookweb site.
|