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

2015/4/3

High Scores

1
2
ZOE ZOE

2015/4/3

#
Hello everyone , I'm trying to get my score in the game's end to be saved , but without using the userInfo, only when stop the game without reset , as this would make the score back to zero, but it is always to zero without being saved. In my world , I have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void saveScore()
   {
       ScoreCounter score = getScore();
       if (score.getPreviousScore() > highScore)
       {
           highScore = score.getPreviousScore();
       }
   }
    
   public int highScore()
   {
       return highScore;
   }
To get my highScore and insert it in the start menu, i have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void bestScore()
    {
        SnakeWorld world = (SnakeWorld) getWorld();
        int score = world.highScore();
 
        if(highScores[0] <= score)
        {
            highScores[0] = score;
        }
        else
        {
            if(highScores[1] <= score)
            {
                highScores[1] = score;
            }
            else
            {
                if(highScores[2] <= score)
                {
                    highScores[2] = score;
                }
            }
        }
    }
And to save my score at the end of the game, I call for saveScore method in the snake. Someone can help me?
danpost danpost

2015/4/3

#
ZOE wrote...
I call for saveScore method in the snake.
This (the game over routine in the snake class) is something that might be useful in resolving your issue. Also note that your 'bestScore' method just replaces values at the different locations. If you replace the best score, the previous best score is no longer there. That is, you need to shift all lower scores down one spot and insert the new score.
ZOE ZOE

2015/4/3

#
how I do that? I'm a bit lost! I try some different ways and I cann't find one that can help me!
danpost danpost

2015/4/3

#
danpost wrote...
ZOE wrote...
I call for saveScore method in the snake.
This (the game over routine in the snake class) is something that might be useful in resolving your issue.
What I meant was that you need to show the code of the snake class -- especially the part that detects game over.
ZOE ZOE

2015/4/4

#
In the snake class, i have this;
1
2
3
4
5
6
7
8
9
10
11
12
13
public void checkForObstacles()
    {
        Actor wood = getOneIntersectingObject(Obstacles.class);
        if(wood != null)
        {
            Greenfoot.playSound("GameOver.wav");
            getWorld().addObject(new GameOver(),getWorld().getWidth()/2, getWorld().getHeight()/2);
            Greenfoot.stop();
 
            ((SnakeWorld) getWorld()).saveScore();
            getWorld().addObject(new Home(), 35, 365);
        }
    }
danpost danpost

2015/4/4

#
You could possibly add another constructor in the SnakeWorld class to pass the high scores to the new world and override the 'started' method to start the new game.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// in SnakeWorld class
public SnakeWorld(int[] scores)
{
    this(); // calls the 'public SnakeWorld()' constructor
    highScores = scores; // retains the previous high score list
}
 
/** starts a new game if last game was over, passing the high scores to the new world */
public void started()
{
    if (!getObjects(GameOver.class).isEmpty())
    {
        Greenfoot.setWorld(new SnakeWorld(highScores));
    }
}
ZOE ZOE

2015/4/4

#
I just have a question . In your code , the highscores is my array created a subclass of the actor , which lets you create a table that is inserted into the start menu. So i need to create something like that to get my highScores list in the snakeWorld? I create this in my subclass of the table.
1
2
3
4
public int [] getList()
    {
        return highScores;
    }
And in my snakeWorld, i should do something like that?
1
2
3
4
public ScoresTable getHighScoresList()
    {
        return thisHighScores;
    }
This allows me to run your code by entering :
1
2
3
4
5
6
public SnakeWorld(int[] scores)
{
    this();
    ScoresTable highScores = getHighScoresList();
    highScores.getList() = scores;
}
Something like that, right?
danpost danpost

2015/4/4

#
I cannot be sure exactly how as yet. Please show the ScoresTable class code so I have a better idea of what should be done.
danpost danpost

2015/4/4

#
If you have a 'thisHighScores' field that holds a ScoresTable object in your SnakeWorld class (which it appears you do), the you should be able to do this:
1
2
3
4
5
public SnakeWorld(ScoresTable scoresTable)
{
    this();
    thisHighScores = scoresTable;
}
Then line 13 in the 'started' method above would be:
1
Greenfoot.setWorld(new SnakeWorld(thisHighScores));
ZOE ZOE

2015/4/4

#
In my ScoresTables, i have something like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
private int [] highScores;
 
    public ScoresTable()
    {
        highScores = new int[3];
         
        makeBox(150,200);
        printText(20, "Scores", 40, 27);
        printText(16, "" + highScores[0], 40, 80);
        printText(16, "" + highScores[1], 40, 120);
        printText(16, "" + highScores[2], 40, 160);
    }
 
    public void bestScore()
    {
        SnakeWorld world = (SnakeWorld) getWorld();
        int score = world.highScore();
 
        if(highScores[0] <= score)
        {
            highScores[0] = score;
        }
        else
        {
            if(highScores[1] <= score)
            {
                highScores[1] = score;
            }
            else
            {
                if(highScores[2] <= score)
                {
                    highScores[2] = score;
                }
            }
        }
    }
     
    public int [] getList()
    {
        return highScores;
    }
danpost danpost

2015/4/4

#
Try my last suggestion.
ZOE ZOE

2015/4/5

#
I did as your last suggestion, but as when the snake loses should go back to the start menu, because that's where it shows the table of high score, i did something like thar in Start menu:
1
2
3
4
5
6
7
8
9
10
public ScoresTable getHighScoresList()
{
    return thisHighScores;
}
 
public StartMenu(ScoresTable scoresTable)
{
    this();
    thisHighScores = scoresTable;
}
And in SnakeWorld i just do that:
1
2
3
4
5
6
7
public void started()
    {
        if(!getObjects(GameOver.class).isEmpty())
        {
            Greenfoot.setWorld(new StartMenu(thisHighScores));
        }
    }
ZOE ZOE

2015/4/5

#
But my high score table continues to zeros.
danpost danpost

2015/4/5

#
The changes you made look to be okay. However, your ScoresTable class code does not. Lines 7 through 11 appear to be what displays the scores; however, being that these lines are in the constructor of the class and immediately follow the creation of the highScores array, it is no wonder it will display all zero. There is no code elsewhere in the class (that was provided) that would update the display of the scores; so, I fear that you are creating and using NEW ScoresTable objects when going back to the StartMenu world.
danpost danpost

2015/4/5

#
I suggest the you post the entire ScoresTable class so that it can be dealt with first.
There are more replies on the next page.
1
2