Monday, June 29, 2009

Java Greenfoot: Food Fabric

The ants scenario "ends" if there no more Food objects exist in the world. In such a case, the ants still continuously move and explore the world, but surely for nothing. We can help these poor ants by creating a new actor that is able to produce food : the FoodFabric.

Some basic considerations for this new actor are given as follows.
  • A FoodFabric object shall be represented by dynamic images to indicate that the object is dynamic i.e. can produce food.

  • The basic behaviours of a FoodFabric object are to produce Food objects and to maintain its visual appearance.

import greenfoot.*;  
import java.util.Random;
import java.util.List;

public class FoodFabric extends Actor
{
private static final Random randomizer =
AntWorld.getRandomizer();

private GreenfootImage[] images;
private int size=0;
private int increment=1;
private final static int IMAGE_COUNT= 8;

public FoodFabric() {
initialiseImages();
setImage(images[0]);
}

public void act() { ... }
public void initialiseImages() { ... }
private void generateFood() { ... }
private boolean randomChance() { ... }
}

The skeleton of the class is given above. It might remind us of the structure of the Explosion class in the lunarlander scenario. Indeed, the mechanism for image animation is quite similar to that in the Explosion class. The intialiseImages() method is also more-or-less similar to the method in the Explosion class. However, some image manipulations are done differently.
public void initialiseImages() {
if(images == null) {
GreenfootImage baseImage = new
GreenfootImage("images/circle.png");
GreenfootImage foodImage = new
GreenfootImage("images/center.png");
GreenfootImage scaledImage;
int maxSize = baseImage.getWidth();
int delta = maxSize / IMAGE_COUNT;
int size = 0;
images = new GreenfootImage[IMAGE_COUNT];
for(int i=0; i < IMAGE_COUNT; i ) {
size = size delta;
images[i] = new GreenfootImage(50,50);
scaledImage = new GreenfootImage(baseImage);
scaledImage.scale(size, size);
images[i].drawImage(scaledImage,
25-size/2,25-size/2);
images[i].drawImage(foodImage,10,10);
}
}
}

public void act() {
setImage(images[size]);

size = increment;
if((size>=IMAGE_COUNT)||(size<=0)) {
increment = -increment;
size = increment;
}
generateFood();
}



A FoodFabric object surrounded by some Food objects; the circle in the background grows and shrinks alternately.

The codes before generateFood() in the act() method are to change the appearance of the object. The generateFood() method will put Food objects at slightly randomized positions around the FoodFabric (see the codes below).
private void generateFood() {
int pos = randomizer.nextInt(8);
int x = getX(); int y = getY();
int xFood=x, yFood=y;
int xyOffset = 20;
int xDelta = randomizer.nextInt(10);
int yDelta = randomizer.nextInt(10);
switch(pos) {
// ...
}
if(randomizer.nextInt(2)==0) xFood += xDelta;
else xFood -= xDelta;
if(randomizer.nextInt(2)==0) yFood += yDelta;
else yFood -= yDelta;

if(randomChance(25)) {
List foods = getWorld().getObjectsAt(
xFood, yFood, Food.class);
if(foods.isEmpty()) {
getWorld().addObject(
new Food(), xFood, yFood);
}
}
}



A screenshot showing the ants scenario with a FoodFabric object.


Webpage Keywords : Java programming, Java game, Greenfoot scenario

No comments:

Post a Comment