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

2013/10/27

How do i make a counter?

Aermus Aermus

2013/10/27

#
I have have two counters. One when the police caputers a thief and one when a thief brings a car into a garage. It is similair to the GTA game. How ever i made a score like de wombat has eaten a leave. I can see in the inspect how many thiefs a police has caputerd. How ever i want this score into my counter.. How do i do this??
private GreenfootImage polie;
    private GreenfootImage polie1;
    private GreenfootImage polie2;
    
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;

    private int direction;
    private int speed = 10;
    
    private int score; 
    
    
    
    
    
    
    
          
    public Politieauto_move()
    {
        setDirection(EAST);         
        polie = new GreenfootImage("auto1.png");
        polie1 = new GreenfootImage ("auto2.png");
        polie2 = new GreenfootImage ("auto10.png");
         
    }
        /**
     * Act - do whatever the Politieauto_move wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
                
        if (foundAutodief())
        {
            eatAutodief();            
        }
        if(canMove()) {
            move(3);
            if (getImage() == polie1){
                setImage(polie2);
            }else{
                setImage(polie1);
                            }
        }
            else {
            turnLeft();
        }   
        if (getOneIntersectingObject(Obstakel.class) !=null) 
        {
            move (-1);
            turn(Greenfoot.getRandomNumber(359));
        }
          if (getOneIntersectingObject(Burgerauto.class) !=null) 
        {
            move (-1);
            turn(Greenfoot.getRandomNumber(359));
        }
     }     
         /**
     * Check whether there is a autodief in the same cell as we are.
     */
    public boolean foundAutodief()
    {
        Actor autodief = getOneObjectAtOffset(0, 0, Autodief.class);
        if(autodief != null) {
            return true;
        }
        else {
            return false;
        }
    }    
    
        /**
     * Sets the direction we're facing. The 'direction' parameter must
     * be in the range [0..3].
     */
    public void setDirection(int direction)
    {
        if ((direction >= 0) && (direction <= 3)) {
            this.direction = direction;
        }
        switch(direction) {
            case SOUTH :
                setRotation(90);
                break;
            case EAST :
                setRotation(0);
                break;
            case NORTH :
                setRotation(270);
                break;
            case WEST :
                setRotation(180);
                break;
            default :
                break;
        }
    }
      /**
     * Test if we can move forward. Return true if we can, false otherwise.
     */
    public boolean canMove()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        switch(direction) {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        // test for outside border
        if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) {
            return false;
        }
        else if (x < 0 || y < 0) {
            return false;
        }
        return true;
    }
       /**
     * Turns towards the left.
     */
    public void turnLeft()
    {
        switch(direction) {
            case SOUTH :
                setDirection(EAST);
                break;
            case EAST :
                setDirection(NORTH);
                break;
            case NORTH :
                setDirection(WEST);
                break;
            case WEST :
                setDirection(SOUTH);
                break;
        }
    }
    /**
     * Move one cell forward in the current direction.
     */
    public void move()
    {
        if (!canMove()) {
            return;
        }
        switch(direction) {
            case SOUTH :
                setLocation(getX(), getY() + 1);
                break;
            case EAST :
                setLocation(getX() + 1, getY());
                break;
            case NORTH :
                setLocation(getX(), getY() - 1);
                break;
            case WEST :
                setLocation(getX() - 1, getY());
                break;
        }
    }
    
    /**
     * Eat a autodief.
     */
    public void eatAutodief()
    {
        Actor autodief = getOneObjectAtOffset(0, 0, Autodief.class);
        if(autodief != null) {
            // eat the leaf...
            getWorld().removeObject(autodief);
                 
           score = score + 1; 
           
           
         
                   }
    }
and this is the code for the counter, similiair to de crab game
private static final Color transparent = new Color (0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    
    /**
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();
        value = 0;
        target = 0;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target
     */
    public void act()
    { 
        if (value < target){
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }
    
    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }
    
    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }
    
    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Update the image on screen to show the current Value.
     */
    private void updateImage()
    { 
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage ("" + value, 48, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
You need to login to post a reply.