I'm trying to place fly's in the worlds using a buffer amount based on the fly s image width. This is what I have so far:
import greenfoot.*;
public class BugWorld extends World
{
//Fields for values that can be used anywhere in the class. Feel free to play with these!
private final int NUMBER_OF_Flies = 5;
private final int NUMBER_OF_SPIDERS = Greenfoot.getRandomNumber(50);
private static final int WORLD_WIDTH = 600;
private final static int WORLD_HEIGHT = 400;
private GreenfootImage spider;
/**
* Constructor for objects of class BugWorld.
*/
public BugWorld()
{
super(WORLD_WIDTH, WORLD_HEIGHT, 1);
//Instantiate the spider GreenfootImage
GreenfootImage drawedImage = new GreenfootImage("spider.png");
drawSpidersOnBackground();
PopulateWorldWithFlies();
}
private void drawSpidersOnBackground()
{
int buffer = spider.getWidth();
int x,y;
int count = 0;
while(count < NUMBER_OF_SPIDERS)
{
x= Greenfoot.getRandomNumber(getWidth()-(2*buffer)) + buffer;
y = Greenfoot.getRandomNumber(getHeight()-(2*buffer)) + buffer;
drawSpiderAt(x,y);
count++;
}
}
private void PopulateWorldWithFlies()
{
int count = 0;
while(count < NUMBER_OF_Flies)
{
placeFly();
count++;
}
}
private void drawSpiderAt(int x, int y)
{
//Turn the spider image 90 degrees clockwise from its current orientation
spider.rotate(90);
//Make the spider image a random transparency value between 20 and 200
spider.setTransparency(Greenfoot.getRandomNumber(181)+20);
getBackground().drawImage(spider, x, y);
}
public void placeFly()
{
//Create variables that will only be available in this method
int x ,y;
int buffer;
int turnAmount = Greenfoot.getRandomNumber(360) - 180; //range: -180 to 179
//Create a Fly actor to place in the world
Fly fly = new Fly();
buffer = Fly.getImage().getWidth();
//Set the x and y coordinates for placing the fly in a random location in the world
x = Greenfoot.getRandomNumber(getWidth()-(2*buffer)) + buffer;
y = Greenfoot.getRandomNumber(getHeight()-(2*buffer)) + buffer;
//Add the fly to the world
addObject(fly,x,y);
//Turn the fly by the random amount calculated earlier
fly.turn(turnAmount);
}
}

