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

2019/4/18

My counter is counting up. But I need it to count the leaves my wombat eats.

Rawlfish Rawlfish

2019/4/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target= 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
Rawlfish Rawlfish

2019/4/18

#
It counts up forever. The wombat eats the leaves and everything is fine. But I need the counter to count each leaf that is eaten. Then make a separate timer that does what I accidentally got my counter to do.
Rawlfish Rawlfish

2019/4/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * This is an example of one way to create a maze.
 * The  Bug can not cross into or through the maze walls.
 * This is done by using a square image object called "wallRect" that does nothing.
 * The effect of being impassible is actually created in the Bug class tryMove() method.
 * 
 * @author (Brian Myers)
 * @version (February 2010)
 */
public class mazeWorld extends World
{
    private Counter actCounter;
    public void act()
    {
        actCounter.setValue(actCounter.getValue() +1);
    }

    /**
     * Constructor for objects of class mazeWorld.
     * 
     */

    public mazeWorld()
    {    

        super(800, 800, 1);
        wallRect block ;
        actCounter = new Counter();
        addObject(actCounter, 689, 51);
        // make a horizontal wall 17 units long 
        for (int i = 3 ; i < 25 ; i++)
        {
            block = new wallRect() ;
            addObject(block, 30 * i, 150) ;
        }

        // make a horizontal wall 17 units long 
        for (int i = 3 ; i < 25 ; i++)
        {
            block = new wallRect() ;
            addObject(block, 30 * i, 620) ;
        }        

        // make a vertical wall 14 unts long
        for (int i = 5 ; i < 20 ; i++)
        {
            block = new wallRect() ;
            addObject(block, 90, 32 * i) ;
        }

        // make a vertical wall 4 unts long starting at (360, 100)
        for (int i = 5 ; i < 9 ; i++)
        {
            block = new wallRect() ;
            addObject(block, 548, 40 * i) ;
        }  

        // make a vertical wall 4 unts long starting at (360, 300)
        for (int i = 15; i < 20 ; i++)
        {
            block = new wallRect() ;
            addObject(block, 548, 30 * i) ;
        }    

        // INNER RECTANGLE

        // make a horizontal wall 8 units long starting at point (140,320)
        //   for (int i = 7 ; i < 15 ; i++)
        // { 
        //      block = new wallRect() ;
        //       addObject(  block,   29 * i,   360) ;
        //  }

        // make a horizontal wall 8 units long starting at point (140,180)
        //      for (int i = 7 ;              i < 15 ;                  i++)
        // { 
        //       block = new wallRect() ;
        // }

        // make a right vertical wall 8 unts long starting at (300, 180)
        for (int i = 15; i < 17 ; i++)
        {
            block = new wallRect() ;

            addObject(block,   300,   23 * i) ;

        } 

        // Character object        

        //Bug bug = new Bug() ;
        //addObject(bug,100,480) ;

        // Cherries object        
        // Cherries cherries = new Cherries() ;
        // addObject(cherries,250,250);  

        // Lizard object        

        //Lizard lizard = new Lizard() ;
        //addObject(lizard,110,150) ;

        prepare();
    }

    //     // called when the Bug reaches home. removes walls from the world.
    //     public void gameOver()
    //     {
    // 
    //         removeObjects(getObjects(wallRect.class)); 
    //         //removeObjects(getObjects(Lizard.class)); 
    //     }    

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Wombat wombat = new Wombat();
        addObject(wombat, 216, 510);
        //         Counter counter = new Counter();
        //         addObject(counter, 689, 51);
        Leaf leaf = new Leaf();
        addObject(leaf, 320, 504);
        Leaf leaf2 = new Leaf();
        addObject(leaf2, 431, 501);
        Leaf leaf3 = new Leaf();
        addObject(leaf3, 478, 470);
        Leaf leaf4 = new Leaf();
        addObject(leaf4, 482, 372);
        Leaf leaf5 = new Leaf();
        addObject(leaf5, 473, 302);
        Leaf leaf6 = new Leaf();
        addObject(leaf6, 469, 261);
    }
}
This is my world subclass mazeWorld
danpost danpost

2019/4/18

#
I would think the code to your Wombat class would be quite pertinent. Please provide its codes.
Rawlfish Rawlfish

2019/4/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * Wombat. A Wombat is controlled by the up, down, left and right
 * arrow keys. The Wombat can only move north, south, east or west.
 * 
 * Wombats try to find leaves to eat but must go around rocks that get in the way.
 * 
 *  */
public class Wombat extends Actor
{
    // constants that hold the four directions:
    private static final int EAST = 0;
    private static final int WEST = 180;
    private static final int NORTH = 270;
    private static final int SOUTH = 90;
    
    // variable to keep track of the wombat's current direction
    private int direction;
    private Counter counter;
    
    public Wombat(Counter pointCounter)
    {
        counter = pointCounter;
    }
    
    /**
     * Constructor in used to review the concept of constructors
     */
    public Wombat()
    {
        direction = EAST;
        setRotation( direction );
        
    }
    
    /**
     * Do whatever the wombat likes to to just now.
     */
    public void act()
    {
        checkKeys();
        lookForFood();
    }
    
    /**
     * Moves the wombat according to which keys are pressed
     */
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("up")) {
           direction = NORTH;
           setRotation( direction );
           move();
        }
      if (Greenfoot.isKeyDown("right")) {
            direction = EAST;
            setRotation( direction );
            move();
        }
         if (Greenfoot.isKeyDown("down")) {
             direction = SOUTH;
             setRotation( direction );
             move();
        }
       if (Greenfoot.isKeyDown("left")) {
            direction = WEST;
            setRotation( direction );
            move();
        }

    }
    
    /**
     * Search for food
     */
    public void lookForFood()
    {
        Actor leaf = getOneIntersectingObject( Leaf.class );
        if (leaf != null) {
            getWorld().removeObject(leaf);
          
        }
    }
    
    /**
     * Returns the number of leaves the wombat has eaten
     */
 /*   public int getLeavesEaten()
    {
       return numOfLeaves;
    }
 */   
    /**
     * Moves the wombat forward one location
     */
    public void move()
    {
        if (canMove()) {
            switch( getRotation() ) {
            case EAST:
                setLocation( getX() + 1, getY() );
                break;
            case SOUTH:
                setLocation( getX(), getY()+1 );
                break;
            case WEST:
                setLocation( getX() - 1, getY() );
                break;
            case NORTH:
                setLocation( getX(), getY() - 1);
                break;
            }
        }
    }
     
    /**
     * Determines if a Rock is located in front of the Wombat
     */
    public boolean canMove()
    {
        switch( getRotation() ) {
            case EAST:
                return getOneObjectAtOffset(25,0,wallRect.class)==null;
            case SOUTH:
                return getOneObjectAtOffset(0,25,wallRect.class)==null;
            case WEST:
                return getOneObjectAtOffset(-25,0,wallRect.class)==null;
            case NORTH:
                return getOneObjectAtOffset(0,-25,wallRect.class)==null;
            }
        return true;
    }

}
danpost danpost

2019/4/18

#
Rawlfish wrote...
<< Code Omitted >>
Okay, first, in your prepare method, change the first 4 lines to the following:
Counter counter = new Counter();
addObject(counter, 689, 51);
Wombat wombat = new Wombat(counter);
addObject(wombat, 216, 510);
-- that will get the counter passed to the wombat. Now, in the Wombat class, insert the following line immediately before line 24:
this();
-- this, pun intended, will allow the other constructor to set the direction/rotation of the wombat. Finally, uncomment lines 89 thru 93 and add the following at line 83:
counter.setValue(counter.getValue()+1);
Please note that your other counter is still there (behind the leaf counter). Just drag them to where you want and save the world.
Rawlfish Rawlfish

2019/4/19

#
Everything was going smooth until I compiled it and for return numOfLeaves; in line 91. It said cannot find symbol - Variable numOfLeaves
Rawlfish Rawlfish

2019/4/19

#
I commented it back out and the counter works. Thanks a lot for your help.
danpost danpost

2019/4/19

#
Rawlfish wrote...
Everything was going smooth until I compiled it and for return numOfLeaves; in line 91. It said cannot find symbol - Variable numOfLeaves
True, there is no numOfLeaves variable. You could change numOfLeaves to counter.getValue().
You need to login to post a reply.