I'm in progress with an app that I have put a class crab to eat worms and the same time to be chaced by a lobster and I want to add another crab to handle by another player from the same keyboard.... I tried to copy the same class and change the inputs from the pressKey but it didn't nothing... The code is the below one:
public class Crab extends Animal
{
private GreenfootImage image1;
private GreenfootImage image2;
private int wormsEaten;
/*Create a crab and intalize its two images. */
public Crab()
{
image1 = new GreenfootImage("crab.png");
image2 = new GreenfootImage("crab2.png");
setImage(image1);
wormsEaten = 0;
}
public void act()
{
checkKeypress();
move ();
lookForWorm();
switchImage();
}
public void checkKeypress()
{
if (Greenfoot.isKeyDown ("left"))
{
turn(-4);
}
if (Greenfoot.isKeyDown ("right"))
{
turn(4);
}
}
/**Alternate the crab's image between image1 and image2. */
public void switchImage()
{
if (getImage() == image1)
{
setImage(image2);
}
else
{
setImage(image1);
}
}
public void lookForWorm()
{
/* Checks if the class crab has been eaten the worm*/
if (canSee (Worm.class))
{
eat (Worm.class);
Greenfoot.playSound("slurp.wav");
wormsEaten = wormsEaten + 1;
if(wormsEaten == 8)
{
Greenfoot.playSound("fanfare.wav");
Greenfoot.stop();
}
}
}
}
