加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
AntHill.java 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
luyanfei78 提交于 2020-04-29 09:21 . initial project.
import greenfoot.*;
/**
* A hill full of ants.
*
* @author Michael Kölling
* @version 1.1
*/
public class AntHill extends Actor
{
/** Number of ants that have come out so far. */
private int ants = 0;
/** Total number of ants in this hill. */
private int maxAnts = 40;
/** Counter to show how much food have been collected so far. */
private Counter foodCounter;
/**
* Constructor for ant hill with default number of ants (40).
*/
public AntHill()
{
}
/**
* Construct an ant hill with a given number of ants.
*/
public AntHill(int numberOfAnts)
{
maxAnts = numberOfAnts;
}
/**
* Act: If there are still ants left inside, see whether one should come out.
*/
public void act()
{
if(ants < maxAnts)
{
if(Greenfoot.getRandomNumber(100) < 10)
{
getWorld().addObject(new Ant(this), getX(), getY());
ants++;
}
}
}
/**
* Record that we have collected another bit of food.
*/
public void countFood()
{
// if we have no food counter yet (first time) -- create it
if(foodCounter == null)
{
foodCounter = new Counter("Food: ");
int x = getX();
int y = getY() + getImage().getWidth()/2 + 8;
getWorld().addObject(foodCounter, x, y);
}
foodCounter.increment();
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化