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

2013/7/21

How to make a Crab each time it moves it uses 1/20 of a Worm worth of energy

1
2
3
4
danpost danpost

2013/7/22

#
regba wrote...
No it is not before, it is either or. Here are the choices: Once a Crab has reached zero Worms eaten, it dies and disappears from the world. Stop the game once all the Worms have been eaten or all the Crabs have died.
Well, the first line is R6. But, the second is for R8. Please explain where you require help and show what code you have for it so far.
regba regba

2013/7/22

#
I thought I was pretty much done until navydoc6436 say it is not correct. here are my codes
public class Crab extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int wormsEaten; // R3.
    
    /**
     * Create a crab and initialize its two images.
     */
    public Crab()
    {
        image1 = new GreenfootImage("crab.png");
        image2 = new GreenfootImage("crab2.png");
        setImage(image1);
        wormsEaten = 0;
        wormsEaten= Greenfoot.getRandomNumber(7) +5; //R3. Random number between 5-11.
    }
        
    /** 
     * Act - do whatever the crab wants to do. This method is called whenever
     *  the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        //checkKeypress();
        lookForWorm();
        move();
        movement();
        switchImage();
        randomTurn();// R7 
    }
    
    public void randomTurn()
    {
     if (Greenfoot.getRandomNumber(100) < 10 ) // R7 
        {
            turn (Greenfoot.getRandomNumber(90)-45);// R7
        }   
    }
    
    /**
     * Alternate the crab's image between image1 and image2.
     */
    public void switchImage()
    {
        if (getImage() == image1) 
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
            
    /**
     * Check whether a control key on the keyboard has been pressed.
     * If it has, react accordingly.
     */
    //public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("left")) 
        {
            turn(-4);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            turn(4);
        }
    }
    
    /**
     * Check whether we have stumbled upon a worm.
     * If we have, eat it. If not, do nothing. If we have
     * eaten eight worms, we win.
     */
    public void lookForWorm()
    {
        if ( canSee(Worm.class) && wormsEaten < 7 ) // R4
        {
            eat(Worm.class);
            Greenfoot.playSound("slurp.wav");
            
            wormsEaten = wormsEaten + 1;
            if (wormsEaten > 6) // R4
            {  
                move();
                    
               //Greenfoot.playSound ("fanfare.wav");
                

            }  
            
           // if (wormsEaten == 8) 
            {
                //Greenfoot.playSound("fanfare.wav");
                //Greenfoot.stop();
            }
            
            
        }
    }
    
    
    private int moveCount = 0; 
    private void movement()  // R5
{  
    move(1);  
    moveCount++;  
    if (moveCount == 20)  
    {  
             
moveCount = 0;  
wormsEaten = wormsEaten - 1;  
if (wormsEaten == 0)  //R6
{  
    getWorld().removeObject(this);  
}    
    }  
}  
}
danpost danpost

2013/7/22

#
Other than not having the crabs turn when at the edges of the world, the code looks pretty good. Right now, the crabs travel in a not-so-smooth way across the screen to the edge of the world and then mainly hug the edge. When fixing this, it might require moving 'randomTurn();' in the act method to before 'lookForWorm();' (using 'atWorldEdge' requires the actor be in the world). The only thing left is R8 (whose code should be in your world class act method).
regba regba

2013/7/22

#
Yes the worldedge is not a requirement in this exercise that is why I didn't include it, but you're right I could have. here is the R8
public void act()
    {
      if ( countCrabsInTheWorld() <= 0 || countWormsInTheWorld() <= 0 )  
      {                                                                  
         Greenfoot.stop();
      }
    } 
    
    public int countCrabsInTheWorld()
    {      
     int crabCount;
      
    crabCount = getObjects(Crab.class).size();
           
    return crabCount;  
    }
    
    public int countWormsInTheWorld()
    {      
     int wormsCount;
      
      wormsCount = getObjects(Worm.class).size();
           
     return wormsCount; 
    }
danpost danpost

2013/7/22

#
A more to the point method would be:
public void act()
{
    if (getObjects(Crab.class).isEmpty() || getObjects(Worm.class).isEmpty())
        greenfoot.stop();
}
(without the need for the 'count...InTheWorld' methods).
regba regba

2013/7/22

#
So should I get rid of everything and just include these five live lines you mention? for R8
danpost danpost

2013/7/22

#
That is not necessary (although, you could).
regba regba

2013/7/22

#
Thanks a lot danpost
You need to login to post a reply.
1
2
3
4