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

2020/4/26

How do I make a highscore?

1
2
3
HairyChickens HairyChickens

2020/4/26

#
So I've just finished with my scoring system. MASSIVE credits to danpost for helping me. But now I want to have a high score in my scoreboard. My idea is to have an actor that checks the two scores from my two players and sets the highest score. If there are higher scores then that replaces the previous highest score. Below is my code for Player 1's score. Player 2's code is the same but Player 1 is replaced with Player 2
1
2
3
4
5
6
7
8
9
10
11
12
13
public void increaseScore()
{
    player1score++;
    showStatus();
}
public void showStatus()
{
    getWorld().showText("Player 1 Score : "+player1score, 93,865);
}
protected void addedToWorld(World world)
{
    showStatus();
}
Let me know if more of my code is needed. Thanks.
danpost danpost

2020/4/26

#
Being this is a two player game, you won't be able to use UserInfo storage as you can only modify the info of the one logged in. This, in turn, means high scores will not work on any web page (this site or any other). That means you are restricted to local high scores, writing them to a file, unless you just want high scores within a single session (once you close the program, nothing is retained)
HairyChickens HairyChickens

2020/4/27

#
danpost wrote...
Being this is a two player game, you won't be able to use UserInfo storage as you can only modify the info of the one logged in. This, in turn, means high scores will not work on any web page (this site or any other). That means you are restricted to local high scores, writing them to a file, unless you just want high scores within a single session (once you close the program, nothing is retained)
If single sessions work with both players then i would like that.
danpost danpost

2020/4/27

#
HairyChickens wrote...
If single sessions work with both players then i would like that.
Show game world and high scores world class codes.
HairyChickens HairyChickens

2020/4/27

#
I don't have a high score world because I want the high score to be shown similarly to how my scores are shown : getWorld().showText("Player 1 Score : "+player1score, 93,865);
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public class GameWorld extends World
{
    private Player_1 player_1;
    private Player_2 player_2;
 
    /**
     * Constructor for objects of class GameWorld.
     *
     */
    public GameWorld()
    {   
        // Create a new world with 1650x900 cells with a cell size of 1x1 pixels.
        super(1650, 900, 1);
        prepare();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Pluto pluto = new Pluto();
        addObject(pluto,69,72);
        Earth earth2 = new Earth();
        addObject(earth2,1562,826);
        player_1 = new Player_1();
        player_1.turn(-90);
        addObject(player_1,1510,820);
        player_2 = new Player_2();
        player_2.turn(-90);
        addObject(player_2,1621,820);
        Asteroid asteroid = new Asteroid();
        addObject(asteroid,344,223);
        Asteroid asteroid2 = new Asteroid();
        addObject(asteroid2,746,91);
        Asteroid asteroid3 = new Asteroid();
        addObject(asteroid3,646,305);
        Asteroid asteroid4 = new Asteroid();
        addObject(asteroid4,269,582);
        Asteroid asteroid5 = new Asteroid();
        addObject(asteroid5,216,394);
        Asteroid asteroid6 = new Asteroid();
        addObject(asteroid6,621,521);
        Asteroid asteroid7 = new Asteroid();
        addObject(asteroid7,392,787);
        Asteroid asteroid8 = new Asteroid();
        addObject(asteroid8,925,725);
        Asteroid asteroid9 = new Asteroid();
        addObject(asteroid9,1009,496);
        Asteroid asteroid10 = new Asteroid();
        addObject(asteroid10,906,317);
        Asteroid asteroid11 = new Asteroid();
        addObject(asteroid11,1209,69);
        Asteroid asteroid12 = new Asteroid();
        addObject(asteroid12,1566,254);
        Asteroid asteroid13 = new Asteroid();
        addObject(asteroid13,620,783);
        Asteroid asteroid14 = new Asteroid();
        addObject(asteroid14,1253,285);
        removeObject(asteroid5);
        asteroid.setLocation(231,292);
        WaterBlob waterblob = new WaterBlob();
        addObject(waterblob,811,675);
        WaterBlob waterblob2 = new WaterBlob();
        addObject(waterblob2,770,387);
        WaterBlob waterblob3 = new WaterBlob();
        addObject(waterblob3,376,417);
        WaterBlob waterblob4 = new WaterBlob();
        addObject(waterblob4,135,754);
        WaterBlob waterblob5 = new WaterBlob();
        addObject(waterblob5,384,89);
        WaterBlob waterblob6 = new WaterBlob();
        addObject(waterblob6,966,91);
        Timer timer = new Timer();
        addObject(timer,73,66);
        WaterBlob waterblob7 = new WaterBlob();
        addObject(waterblob7,1378,393);
        WaterBlob waterblob8 = new WaterBlob();
        addObject(waterblob8,1327,610);
        Scorepanel scorepanel = new Scorepanel();
        addObject(scorepanel,240,887);
        scorepanel.setLocation(240,882);
        Barrier barrier = new Barrier();
        addObject(barrier,1649,887);
        BackgroundMusic backgroundmusic = new BackgroundMusic();
        addObject(backgroundmusic,69,86);
        Alien alien = new Alien();
        addObject(alien,55,55);
        alien.setLocation(70,66);
    }
 
    public void plutoTouched1()
    {
        if (player_1.player1score > player_2.player2score)
        {
            addObject(new Player_1_Wins(),getWidth()/2, getHeight()/2);
            Greenfoot.playSound("Victory.mp3");
            Greenfoot.stop();
        }
    }
 
 
    public void plutoTouched2()
    {
        if (player_2.player2score > player_1.player1score)
        {
            addObject(new Player_2_Wins(), getWidth()/2,getHeight()/2);
            Greenfoot.playSound("Victory.mp3");
            Greenfoot.stop();
        }
    }
danpost danpost

2020/4/27

#
Add:
1
private static int high_1, high_2;
to your GameWorld class. In the two "touched" methods in the same class, compare winner's score to the respective high value and if it is greater, set the new high value and update their display.
HairyChickens HairyChickens

2020/4/27

#
danpost wrote...
In the two "touched" methods in the same class, compare winner's score to the respective high value and if it is greater, set the new high value and update their display.
Not exactly sure what I need to type for that.
danpost danpost

2020/4/27

#
HairyChickens wrote...
Not exactly sure what I need to type for that.
Try something. I told you where and what.
HairyChickens HairyChickens

2020/4/27

#
Ok this is what I originally put after reading your reply.
1
2
3
4
5
6
7
8
9
10
11
12
13
public void plutoTouched1()
{
    if (player_1.player1score > player_2.player2score)
    {
        addObject(new Player_1_Wins(),getWidth()/2, getHeight()/2);
        Greenfoot.playSound("Victory.mp3");
        if (player_1.player1score > high_1)
        {
            showStatus();
        }
        Greenfoot.stop();
    }
}
1
2
3
4
5
public void showStatus()
{
    showText("Highscore : "+high_1, 100,100);
    showText("Highscore : "+high_2, 100,100);
}
But like I said I wasn't sure if this is right because the high score wasn't showing. It showed a 0. I have a feeling that I missed something really obvious. I'll see if i can think of it before you reply.
danpost danpost

2020/4/27

#
You just need to set the new high before line 9:
1
high_1 = player_1.player1score;
HairyChickens HairyChickens

2020/4/28

#
Still shows up as zero when a player wins.
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
43
44
public class GameWorld extends World
{
    private Player_1 player_1;
    private Player_2 player_2;
    private int high_1, high_2;
}
 
    public void plutoTouched1()
    {
        if (player_1.player1score > player_2.player2score)
        {
            addObject(new Player_1_Wins(),getWidth()/2, getHeight()/2);
            Greenfoot.playSound("Victory.mp3");
            if (player_1.player1score > high_1)
            {
                high_1 = player_1.player1score;
                showStatus();
            }
            Greenfoot.stop();
        }
    }
     
    public void showStatus()
    {
        showText("Highscore : "+high_1, 100,100);
        showText("Highscore : "+high_2, 100,100);
    }
 
    public void plutoTouched2()
    {
        if (player_2.player2score > player_1.player1score)
        {
            addObject(new Player_2_Wins(), getWidth()/2,getHeight()/2);
            Greenfoot.playSound("Victory.mp3");
            if (player_2.player2score > high_2)
            {
                high_2 = player_2.player2score;
                showStatus();
            }
            Greenfoot.stop();
        }
    }
     
}
danpost danpost

2020/4/28

#
HairyChickens wrote...
Still shows up as zero when a player wins. << Code Omitted >>
No wonder. Line 5 is not how I suggested it be.
HairyChickens HairyChickens

2020/4/28

#
danpost wrote...
HairyChickens wrote...
Still shows up as zero when a player wins. << Code Omitted >>
No wonder. Line 5 is not how I suggested it be.
Same thing happens with the suggested line. That was me experimenting with it and I forgot to put the correct line when I replied.
danpost danpost

2020/4/28

#
HairyChickens wrote...
Same thing happens with the suggested line. That was me experimenting with it and I forgot to put the correct line when I replied.
From what I can tell, it should work.
HairyChickens HairyChickens

2020/4/28

#
danpost wrote...
HairyChickens wrote...
Same thing happens with the suggested line. That was me experimenting with it and I forgot to put the correct line when I replied.
From what I can tell, it should work.
Is there maybe an alternative way of doing a highscore?
There are more replies on the next page.
1
2
3