I have put the interclass problems and possible solution code below.
1. Make the person walk randomly around the screen as part of the act method. Look in the API at the Greenfoot class for methods that will allow you to generate random numbers. Note that when you call a static method the syntax is ClassName.methodName(args).
public class Person extends Actor
{
/**
* Act - do whatever the Person wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setLocation(getX()+Greenfoot.getRandomNumber(3)-1,getY()+Greenfoot.getRandomNumber(3)-1);
}
}
2. Make the person walk in circles. You will do this by having the move based on the rotation of the person and by changing the rotation with each call to act. To get the move from the rotation will require using the trig functions sine and cosine. These can be called with Math.sin(angle) and Math.cos(angle). One catch is that the sine and cosine functions want radians and rotation is in degrees.
public class Person extends Actor
{
/**
* Act - do whatever the Person wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
double angle=getRotation()*Math.PI/180; // You could use 3.14159 instead of Math.PI
double dx=1.5*Math.cos(angle);
double dy=1.5*Math.sin(angle);
setLocation(getX()+(int)dx,getY()+(int)dy);
setRotation(getRotation()+10);
}
}
Putting this into the normal city makes the person go in something of a square.
To get a smooth circle you can change the a line in the City class so that insteadof reading
super(15,15,50) it reads something like super(150,150,5)
then change the two places above that say 1.5 to 15.
3. Have the city start off with ten houses at random locations. See 1. for how to get random numbers.
public class City extends World
{
/**
* Constructor for objects of class City.
*
*/
public City()
{
super(15, 15, 50);
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
addObject(new Building(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
}
}
4. Make the person reproduce. Each time act is called have a new person appear at a random location on the screen. See 1. for how to get random numbers. (Hint: if you do this, don't do Run. Hit Act multiple time instead.)
public class Person extends Actor
{
/**
* Act - do whatever the Person wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
getWorld().addObject(new Person(),Greenfoot.getRandomNumber(15),Greenfoot.getRandomNumber(15));
}
}
This code can be made a bit more robust with the following change so that we aren't hard coding in the size of the screen. A variable is used to remember the word so that we don't have to type getWorld() over and over.
public class Person extends Actor
{
/**
* Act - do whatever the Person wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
World w=getWorld();
w.addObject(new Person(),Greenfoot.getRandomNumber(w.getWidth()),Greenfoot.getRandomNumber(w.getHeight()));
}
}