public void lookForWorm() { if ( canSee(Worm.class) ) { if (Greenfoot.getRandomNumber(4) < 0) { eat(Worm.class); wormsEaten = wormsEaten + 1; } if ( wormsEaten == 4) { Greenfoot.playSound("ouch.wav"); Greenfoot.stop(); } }


public void lookForWorm() { if ( canSee(Worm.class) ) { if (Greenfoot.getRandomNumber(4) < 0) { eat(Worm.class); wormsEaten = wormsEaten + 1; } if ( wormsEaten == 4) { Greenfoot.playSound("ouch.wav"); Greenfoot.stop(); } }
public class Lobster extends Animal { /** * Do whatever lobsters do. */ public void act() { turnAtEdge(); randomTurn(); move(); lookForCrab(); lookForWorm(); } /** * Check whether we are at the edge of the world. If we are, turn a bit. * If not, do nothing. */ public void turnAtEdge() { if ( atWorldEdge() ) { turn(17); } } /** * Randomly decide to turn from the current direction, or not. If we turn * turn a bit left or right by a random degree. */ public void randomTurn() { if (Greenfoot.getRandomNumber(100) > 90) { turn(Greenfoot.getRandomNumber(90)-45); } } /** * Try to pinch a crab. That is: check whether we have stumbled upon a crab. * If we have, remove the crab from the game, and stop the program running. */ public void lookForWorm() { if ( canSee(Worm.class) ) { if (Greenfoot.getRandomNumber(4) < 0) { eat(Worm.class); wormsEaten = wormsEaten + 1; } if ( wormsEaten == 4) { Greenfoot.playSound("ouch.wav"); Greenfoot.stop(); } } } public void lookForCrab() { if ( canSee(Crab.class) ) { eat(Crab.class); } } }
private int wormsEaten;