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

2014/6/9

how do you make a high score counter?

GP2 GP2

2014/6/9

#
Hello Greenfoot Community, I am trying to make a counter that displays the highest score(or in my case best time) achieved in my game. I have a variable called bestTime in my world class that stores the high score on my game. I tried not initializing it in the beginning and then initializing it after the player wins the game but everytime I restart the game it displays the same value for high score even if I get a better score. I guess since the default initialization for an int is 0 it is still initializing as 0 in the beginning. Can anybody help me? Here is the code for my 'world' class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;
/**
 * This is the GameWorld class. This class represents the world in which the game takes place.
 * At the moment this is the only gameworld class that has code in it. In the future I will have different levels which are the other GameWorlds.
 * 
 * @author (Glenn Price) 
 * @version (Version 0.02)
 */
public class GameWorld1 extends World//Class heading
{
    private int spawnCounter;//the rate at which enemies spawn in the world.
    private int bestTime;
    private String bestTimeHolder;
    public static final int ORIGINAL_BEST_TIME = 36000;
    public static final String ORIGINAL_BEST_TIME_HOLDER = "Nobody";
    private Player player1;
    private Counter keyCounter;
    private Counter scoreCounter;
    private Counter highScoreCounter; 
    /**
     * Constructor for objects of class GameWorld.
     * 
     */
    public GameWorld1()//construcotr sets the spawnCounter to 100, populates the world, and starts the game
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 800, 1);
        spawnCounter = 100;
        populateWorld();
        Greenfoot.start();
    }
    public void populateWorld()//creates the player, all of the keys, and one virus. The player and the original virus start in the same spot but the keys are spawned in random locations which makes the game different every time.
    {
        JOptionPane.showMessageDialog(null, "Instructions: \n \n Arrow Keys: Movement \n Q Key: fire arrow \n W Key: drop bomb \n Spacebar: pick up key \n Pick up all 9 keys to win. \n Be Careful!!! Bombs will destroy everything in range including keys and yourself");
        String inputstr = JOptionPane.showInputDialog("Hello, thank you for playing 9 keys.\n What is your Name?");
        player1 = new Player(inputstr);
        JOptionPane.showMessageDialog(null, "Nice to meet you" + " " + inputstr + "! \nI am sorry to tell you this but you are trapped in my system and the only way to get out is to collect the 9 keys in this world. \nViruses will try to attack you but I have equipped you with weapons that will aid you on your quest. \nGood luck!");
        Greenfoot.start();
        addObject(player1, 30, 30);
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Key(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
        addObject(new Virus(), 110, 110);
        
        
        keyCounter = new Counter("Keys Found: ");
        scoreCounter = new Counter(player1.getName() + "'s Time: ");
        highScoreCounter = new Counter("Best Time: " + bestTimeHolder + ": ");
        if(bestTime == 0)
        {
            highScoreCounter.setValue(ORIGINAL_BEST_TIME);
            bestTimeHolder = ORIGINAL_BEST_TIME_HOLDER;
        }
        
        else
        {
             highScoreCounter.setValue(bestTime);
        }
        
        addObject(keyCounter,63, 15 );
        addObject(scoreCounter,63,40);
        addObject(highScoreCounter,95,65);
        

    }
    
    public void act()//makes one virus spawn in a random location for every 100 calls of the act method
    {
        if(spawnCounter == 0)
        {
            addObject(new Virus(), Greenfoot.getRandomNumber(680) + 100 , Greenfoot.getRandomNumber(710) + 70 );
            spawnCounter = 100;
        }
        spawnCounter--;
        keyCounter.setValue(player1.getKeysFound());
        scoreCounter.add(1);
        if(keyCounter.getValue() ==9)
            {
                if(scoreCounter.getValue() < highScoreCounter.getValue())
                {
                    bestTime = scoreCounter.getValue();
                    bestTimeHolder = player1.getName();
                }
                Greenfoot.stop();
                JOptionPane.showMessageDialog(null, "YOU WIN!");
            }
    }
    
    public void clearHighScores()
    {
        bestTime = 0;
        bestTimeHolder = null;
    }
}
danpost danpost

2014/6/9

#
If you want to just have a high score for each session of playing the game, you can make the 'bestTime' and 'bestTimeHolder' fields 'static'. If you want to have the best times saved for future sessions, you can make use of the UserInfo storage availability.
GP2 GP2

2014/6/12

#
ok have looked into the UserInfo class and I'm a little confused on how it works do I have to make a .csv file or something?
danpost danpost

2014/6/12

#
GP2 wrote...
ok have looked into the UserInfo class and I'm a little confused on how it works do I have to make a .csv file or something?
No -- the UserInfo class of greenfoot does that for you. You only need to call the 'store' method on a modified (or un-modified) UserInfo object returned from 'UserInfo.getMyInfo()'. As the documentation states, however, you must first make sure that the user is logged in and storage server is available with 'if (UserInfo.isStorageAvailable())'.
You need to login to post a reply.