对于蚂蚁来说,
这个世界包含巢穴、食物,和空间。
巢穴就是AntHill,AntHill包括setLocation和显示情况。
食物的抽象就是Foot,食物有显示颜色三种,会被蚂蚁带走,并因此逐渐减少。
显示情况是很固定的。
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
 
/**
 * The world where ants live.
 *
 * @author Michael Kolling
 * @explaner WEITAO
 * @version 1.1
 */
public class AntWorld extends World
{
    public static final int SIZE = 640;
 
    /**
     * Create a new world. It will be initialised with a few ant hills
     * and food sources
     */
    public AntWorld()
    {
        super(SIZE, SIZE, 1);
        setPaintOrder(Ant.class, Pheromone.class, AntHill.class, Food.class);
        setup1();
    }
 
    /**
     * Create world contents: one ant hill and food.
     */
    public void setup1()
    {
        removeObjects(getObjects(null));  // remove all existing objects
        addObject(new AntHill(70), SIZE / 2, SIZE / 2);
        addObject(new Food(), SIZE / 2, SIZE / 2 - 260);
        addObject(new Food(), SIZE / 2 + 215, SIZE / 2 - 100);
        addObject(new Food(), SIZE / 2 + 215, SIZE / 2 + 100);
        addObject(new Food(), SIZE / 2, SIZE / 2 + 260);
        addObject(new Food(), SIZE / 2 - 215, SIZE / 2 + 100);
        addObject(new Food(), SIZE / 2 - 215, SIZE / 2 - 100);
    }
 
    /**
     * Create world contents: two ant hills and food.
     */
    public void setup2()
    {
        removeObjects(getObjects(null));  // remove all existing objects
        addObject(new AntHill(40), 546, 356);
        addObject(new AntHill(40), 95, 267);
 
        addObject(new Food(), 80, 71);
        addObject(new Food(), 291, 56);
        addObject(new Food(), 516, 212);
        addObject(new Food(), 311, 269);
        addObject(new Food(), 318, 299);
        addObject(new Food(), 315, 331);
        addObject(new Food(), 141, 425);
        addObject(new Food(), 378, 547);
        addObject(new Food(), 566, 529);
    }
 
    /**
     * Create world contents: two ant hills and food.
     */
    public void setup3()
    {
        removeObjects(getObjects(null));  // remove all existing objects
        addObject(new AntHill(40), 576, 134);
        addObject(new AntHill(40), 59, 512);
 
        addObject(new Food(), 182, 84);
        addObject(new Food(), 39, 308);
        addObject(new Food(), 249, 251);
        addObject(new Food(), 270, 272);
        addObject(new Food(), 291, 253);
        addObject(new Food(), 339, 342);
        addObject(new Food(), 593, 340);
        addObject(new Food(), 487, 565);
    }
}
 

返回