import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Zombie here. * * @author (your name) * @version (a version number or a date) */ public class Zombie extends Actor { /** * Act - do whatever the Zombie wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { followSurvivor(); int num = 0; lookForSurvivor(); } public void followSurvivor() { int dist = 325; Actor closest = null; if(!getObjectsInRange(dist, Survivor.class).isEmpty()) { for (Object obj: getObjectsInRange(dist, Survivor.class)) { Actor guy = (Actor) obj; int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY()); if (closest == null || guyDist< dist) { closest = guy; dist = guyDist; move(2); } } turnTowards(closest.getX(),closest.getY()); } else { if ( Greenfoot.getRandomNumber (100) < 10) turn(Greenfoot.getRandomNumber(90)-45); move(1); } } public void lookForSurvivor() { if (canSee(Survivor.class) ) { eat(Survivor.class); } } public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0,0, clss); return actor != null; } public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0,0, clss); if(actor != null) { getWorld().removeObject(actor); Greenfoot.stop(); } } }