The Greenfoot Programmers' ManualVersion 0.3for Greenfoot 1.0
Michael Kölling
University of Kent
Copyright © M Kölling, 2006 This document may be reproduced in hard copy. Mirroring of electronic
versions on sites other
1 IntroductionThis manual is an introduction to programming in Greenfoot. It starts from the start: We first discuss how to create a new scenario, then how to create worlds and actors, and so on. This may not be the order in which you approach your own personal Greenfoot programming experience. In fact, probably the most common way for people to start programming in Greenfoot is by modifying an existing scenario. In that case, you already have a scenario, a world, and one or more actor classes. Feel free to jump right into the middle of this manual and start reading there. You may, for instance, be interested in generating actor images in a certain way, or in dealing with collisions. The secitons in this manual were written with the goal that they can be read independently - there is no strong need to read everything in order. Every now and then, when it is useful to refer to examples, we will use the 'wombats', 'ants' and 'lunarlander' scenarios as examples. All these scenarios are included in the standard Greenfoot distribution. You can find them in the 'scenarios' folder. Have fun!
2 Creating a new scenario
Doing this is easy: choose 'New' from the 'Project' menu, and select a location and name for your scenario. Greenfoot will create a folder with that name that contains all files associated with your scenario.
Both 'World' and 'Actor' are abstract classes - that is: you cannot create any objects of them. Especially, there is no world object at the moment, because we have no finished world class to make a world object from. As a result, even if we had an actor object now, we could not place it anywhere, because we cannot create a world. In order to progress from here, we need to create subclasses (that is: special cases) of World and of Actor. In other words: we have to define our own world, and then we have to define one or more of our own actors. That's what we'll start in the next section.
3 Using the API
You can also select the 'API Documentation' function from Greenfoot's Help menu (right) to open the API documentation in a web browser. Greenfoot provides four classes that you shoudl know about. They are World, Actor, Greenfoot and GreenfootImage. The World and Actor classes serve as superclasses for our own world and actor classes - we have seen them in the class display. 'GreenfootImage' is a class that provides images and image drawing methods for us to use with our worlds and actors. 'Greenfoot' is a class that gives us access to the Greenfoot framework itself, such as pausing the execution or setting the speed. All of these classes will be mentioned more below. While you are programming in Greenfoot, it is often a good idea to have the API available to us, either printed out or in a web browser window.
4 Creating a world
After choosing a name for your new world and clicking Ok, you should see the new world in the class display. Your new world class will have a skeleton that is valid and compiles. You can now compile your scenario, and you will notice that the new world is automatically visible in the world view. This is one of the built-in features of Greenfoot: whenever a valid world class exists, Greenfoot will, after every compilation, create one world object and show it in the world display. In theory, you could have multiple world subclasses in a single Greenfoot scenario. It is non-deterministic, though, which of those world will then be automatically created. You could manually create a new world using the world's constructor, but in practice we usually have only a single world subclass in every scenario. World size and resolutionLet us have a closer look at the code inside the new world class. The default skeleton looks something like this: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class MyWorld extends World
{
/**
* Create a new world with 20x20 cells
* with a cell size of 10x10 pixels.
*/
public MyWorld() {
super(20, 20, 10);
}
}
The first things we have to decide are the size of our world and the size of its cells. There are essentially two kinds of worlds: those with large cells, where every actor object occupies only one single cell (that is: the cell is large enough to hold the complete actor), and those with small cells, where actors span multiple cells. You have probably seen examples of both. 'wombats', for example, is one of the first kind, whereas 'ants' is of the second kind. (The wombats and ants scenarios are distributed as examples together with Greenfoot. If you have not looked at them, and are not sure what we are discussing here, it would be a good idea to look at them now.) We can specify the world size and cell size in the super call in the constructor. Assume we change it to super(10, 8, 60); In this case, we will get a world that is 10 cells wide, 8 cells high, and where every cell is 60x60 pixels in size. The signature of thr World class constructor (which we are calling here) is public World(int worldWidth, int worldHeight, int cellSize) All positioning of actors in the world is in terms of cells. You cannot place actors between cells (although an actor's image can be larger than a cell, and thus overlap many cells). Large cell / small cell trade-offThe trade-off to be made when choosing a world's cell size is between smooth motion and ease of collision detection. One result of the fact that actors can only be placed in cells is that worlds with large cells give you fairly course-grained motion. The wombats scenario, for instance uses a 60-pixel sized cell, and thus, when wombats move one step forward, their image on screen moves by 60 pixels. On the other hand, in worlds with large cells, where the actors are completely contained inside a cell, detecting other actors at one actor's position is a bit simpler - we do not need to check for overlapping images, but simply for the presence of other actors in the same cell. Finding actos in neighbouring cells is also easy. We will discuss this in more detail in the section 'Detecting other objects (collisions)', below. If you want smoother motion, you should use a smaller cell size. the 'ants' scenario, for example, uses 1-pixel cells, ginving the ants the ability to move in small steps. Thsi can also be combined with large actor objects. The 'lunarlander' scenario, for instance, uses a large actor (the rocket) on a 1-pixel cell world. This gives the actor very smooth motion, since it's location can be changed in one-pixel intervalls. World background imagesMost of the time, we want our world to have a background image. This relatively easy to do. First, you have to make of find a suitable background image. (There are some at the end of the Greenfoot Image Collection on the Greenfoot web site.) Place the image file into the 'images' folder inside your scenario folder. Once the image file is there, it is available to your Greenfoot scenario. Then you can set the world's background image with the line: setBackground("myImage.jpg");
where "myImage.jpg" should be replaced with the correct image file name. For example, assume we have placed the image "sand.jpg" into our scenario's 'images' folder, then our world's constructor might look like this: public MyWorld()
{
super(20, 20, 10);
setBackground("sand.jpg");
}
The background will, by default, be filled with the image by tiling the image across the world. To get smooth looking background, you should use an image whose right edge fits seamlessly to the left, and the bottom to the top. Alternatively, you can use a single image that is large enough to cover the whole world. If you want to paint the background programmatically you can easily do so instead of using an image file. The world always has a background image. By default (as long as we do not specify anythign else) it is an image that has the same size as the world and is completely transparent. We can retrieve the image object for the world's background image and perform drawign operations on it. For example: GreenfootImage background = getBackground(); background.setColor(Color.BLUE); background.fill(); These instructions will fill the entire bakground image with blue. A third option is to combine the two: You can load an image file, then draw onto it, and use the modified file as a background for the world: GreenfootImage background = new GreenfootImage("water.png");
background.drawString("WaterWorld", 20, 20);
setBackground(background);
Background images are typically set only once in the constructor of the world, although there is nothing that stops you from changing the world's background dynamically at other times while your scenario is running. Showing tilesSometimes, when you have worlds with large grid sizes, you want to make the grid visible. The 'wombats' scenario does this - you can see the grid painted on the background of the world. There is no special function in Greenfoot to do this. This is done simply by having the grid painted on the image that is used for the world background. In the case of 'wombats', the world has 60-pixel cells, and the background image is a 60x60 pixel image (to match the cell size) that has a one pixel line at the left and top edges darkened a bit. The effect of this is a visible grid when the tiles are used to fill the world background. The grid can also be drawn programatically onto the background image in the world constructor. Here is an example: private static final Color OCEAN_BLUE = new Color(75, 75, 255); 5 Creating new actorsThis section discusses some of the characteristics of actor classes, and what to consider when writing them. All classes that we want to act as part of our scenario are subclasses of the built-in 'Actor' class. We can create new actor classes by selecting 'New subclass...' from the Actor class's popup menu. The following dialogue lets us specify a name and an image for our new class. The name must be a valid Java class name (that is: contain only letters and numbers). The class image will be the default image for all objects of that class. Class imagesEvery class has an assiciated image that serves as the default image for all objects of that class. Every object can later alter its own image, so that individual objects of the class may look different. This is further discussed in the section 'Dealing with images', below. If objects do not set images explicitly, they will receive the class image. The image selector in the dialogue shows two groups of images: project images and library images. The library images are included in the Greenfoot distribution, the project images are stored in the 'images' folder inside your scenario (project) folder. If you have your own images that you like to use for your class, you have two options:
InitialisationAs with most Java objects, the normal initialisation of the object happens in the object's constructor. However, there are some initialisation tasks that cannot be finished here. The reason is that, at the time the object is constructed, it has not been entered into the world yet. The order of events is:
During step 1, the object's constructor is executed. Since the object is not in the world at this time, methods such as getWorld(), getX() and getY() cannot be called in the constructor (when you're not in the world, you do not have a location). So, if we want to do anything as part of our initialisation that needs access to the world (such as create other objects in the world, or set our image depending on neighbouring objects), it cannot be done in the constructor. Instead, we have a second initialisation method, called 'addedToWorld'. Every actor class inherits this method from class 'Actor'. The signature of this method is public void addedToWorld(World world) This method is automatically called when the actor has been added to the world. So, if we have work to do at the time the object has been added, all we have to do is to define an 'addedToWorld' method in our class with this signature and place our code there. For example: public class Rabbit extends Actor
{
private GreenfootImage normalImage;
private GreenfootImage scaredImage;
public Rabbit()
{
normalImage = new GreenfootImage("rabbit-normal.png");
scaredImage = new GreenfootImage("rabbit-scared.png");
}
public void addedToWorld(World world)
{
if(isNextToFox()) {
setImage(scaredImage);
}
else {
setImage(normalImage);
}
}
private boolean isNextToFox()
{
... // calls getWorld() to check neighbours in world
}
}
In this example, the intention is that a rabbit, when placed into the world, looks 'normal' if it is not next to a fox, but looks scared when placed next to a fox. To do this, we have two image files ("rabbit-normal.png" and "rabbit-scared.png"). We can load the images in the Rabbit's constructor, but we cannot select the image to show, since this involves checking the world, which is not accessible at the time the constructor executes. When a user places an object into the world, things happen in this order:
In our example above, by the time the addedToWorld method is called, the object is in the world and has a location. So we can now call our own isNextToFox() method (which presumably makes use of the world and our location). The setLocation method will be called every time the location of the object changes. The addedToWorld method is called only once. The 'Lander' class in the 'lunarlander' scenario (from the Greenfoot sample scenarios) shows another example of using this method.
6 Making things moveEvery time a user clicks the 'Act' button on screen, the 'act' method of each object in the world will be called. The 'act' method has the following signature: public void act() Every object that is active (i.e. is expected to do something) should implement this method. The effect of a user clicking the 'Run' button is nothing more than a repeated (very fast) click on the 'Act' button. In other words, our 'act' method will be called over and over again, as long as the scenario runs. To make object move on screen, it is enough to modify the object's location. Three attributes of each actor become automatically and immediately visible on screen when you change them. They are:
If we change any of these attributes, the appearance of the actor on screen will change. The Actor class has methods to get and set any of these.
|
![]() |
![]() |
Without the diagonal: |
With the diagonal: |
A method related in functionality to the getNeighbours methods:
List leaves = getObjectsInRange(2, Leaf.class);
This method call will return all objects (of the class Leaf and subclasses) that have a location that is within 2 cells. If the distance between two actors is exactly 2, it is considered to be in range. See the picture below for an illustration of this
![]() |
getObjectsInRange(2, Leaf.class); |
Sometimes it is not precise enough to use the cell location to determine collisions. Greenfoot has a few methods that allows to check whether the graphical representations of actors overlap.
Actor leaf = getOneIntersectingObject(0, 0, Leaf.class);
Be aware that these method calls requires more computation than the cell based methods and might slow down your program if it contains many actors.
both?
(to be written)
(to be written)
(to be written)