This site requires JavaScript, please enable it in your browser!
Greenfoot back
WannaBeADeveloper
WannaBeADeveloper wrote ...

2020/10/9

Two players from the same keyboard

WannaBeADeveloper WannaBeADeveloper

2020/10/9

#
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();
            }
        }
    }
}
RcCookie RcCookie

2020/10/9

#
Please also post the world class code
danpost danpost

2020/10/9

#
WannaBeADeveloper wrote...
I tried to copy the same class and change the inputs from the pressKey but it didn't nothing..
It would be better to have just one class where the input key values are variable, using a constructor with input key parameters.
You need to login to post a reply.