How to create a new world that already has objects in it -- Greenfoot programming
Find other posts in the same category: |


This video tutorial tells you how to make a new Greenfoot world that already has objects in it. (11.5 minutes)

If you have trouble accessing the video, you can probably figure out how by reading the script below.

Here is the code used in the video tutorial. Today is Dec. 16, 2008. The web page does not include the source code for this tutorial. So I typed in the code for the class CrabWorld. (I did not type in the comments.)

If you are making this by editing the original Greenfoot Crab tutorial, you will also need to make a new Animal subclass called Leaf (or this code will return an error saying there is no class called Leaf)

import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage

import java.util.Random;

public class CrabWorld extends World
* Create the crab world (the beach). Our world has a size
* of 560x460 cells, where every cell is just 1 pixel.

private static final int NUM_LEAVES = 30;
public CrabWorld()
addObject ( new Crab(), getWidth()/2, getHeight()/2 );
public void createLeaves(int numLeaves)
for (int i=0; i<numLeaves; i++)
int x = Greenfoot.getRandomNumber (getWidth());
int y = Greenfoot.getRandomNumber (getHeight());
addObject ( new Leaf(), x, y);

Next Book Page

Previous Book Page

To see posts on the same topic, click the links below the name of the author at the top of this page.