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

2013/11/22

Keeping Score

pr2alede pr2alede

2013/11/22

#
I have a game which goes over two levels. The premise of the game is that a crab collects worms in a given time, if he collects 5 worms he moves on to the next level. Is there a way I could keep a record of the time it took to collect all 5 worms when the level is ended and then add this to the amount of time it took in the second level. This score will then be displayed when the game is completed. Here's the code I use for the timer
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A simple counter with graphical representation as an actor on screen.
 * 
 * @author mik
 * @version 1.0
 */
public class Timer extends Actor
{
    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 Timer()
    {
        background = getImage();  // get image from class
        value = 1000;
        target = 0;
        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.
     */
    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, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    if (value==0){
        Greenfoot.playSound("gameover.wav"); 
        Greenfoot.setWorld(new GameOver()); 
        Greenfoot.stop();
 }
        
}
}
Thank you
SPower SPower

2013/11/22

#
You can use static variables: a variable in a class, instead of an object:
// you can put this in any class
private static int currentScore;

public static int getCurrentScore() {
return currentScore;
}

public static void setCurrentScore(int s) {
currentScore = s;
}
and then when you change the score, you say:
<nameOfClassWhereScoreIsStored>.setScore(100);
// without the <> signs
danpost danpost

2013/11/22

#
Have two field in your world, one for the current time and one for total combined time. Call them 'time' and 'totalTime'; and make them 'public' fields. Then when one level is ended and you need to move on to the next, use something like this:
Level oldLevel = (Level)getWorld();
Level newLevel = new Level();
newLevel.totalTime = oldLevel.totalTime+oldLevel.time;
Greenfoot.setWorld(newLevel);
You need to login to post a reply.