• greenfoot programming help
  • Introduction to Greenfoot Programming Environment:

    Greenfoot is a software tool designed to let beginners get experience with object-oriented programming. Greenfoot model consists of a World class (represented by a rectangular screen area) and any number of actor objects that are present in the world and can be programmed to act independently. In Greenfoot environment the world and actors are represented by Java objects and defined by Java classes. It offers methods to easily program these actors, including method for movement, collision detection, changes of appearance and rotation. One of the goals of Greenfoot is a design that explicitly visualizes important concepts of object-oriented programming.

    New feature of Greenfoot include:

    • Scope highlighting
    • New navigation view
    • Better Find and Replace
    • Code completion

Greenfoot Programming Code

/**Turn in a random direction.
public void turnRandom()
//get a random number between 0 and 3...
int turns= Greenfoot.getRandomNumber(4);
//...an trun left that many times.
for(int i=0; i<turns; i++){
Then we modify 'act()' to make use of turnRandom. The act () method currently reads:
public void act()
else if(canMove()){
public void act()
    setLocation (getX() + 4, getY());
    // Moves the object 4 cells to the right
    setRotation (getRotation() + 2);
    // Rotates the object 2 degrees clockwise
An example of a simple act method in Greenfoot
Actor a = getOneIntersectingObject(Asteroid.class);
if (a != null) {
    // we have hit an asteroid!

An example of simple collision detection

greenfoot code greenfoot programming environment