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

2020/11/9

How does one make a simple scoreboard?

MrSkyPanda MrSkyPanda

2020/11/9

#
Please make it as simple as possible
danpost danpost

2020/11/9

#
MrSkyPanda wrote...
Please make it as simple as possible
What all do you want it to display?
MrSkyPanda MrSkyPanda

2020/11/9

#
i want to display the score based off of how many coins one player has grabbed
danpost danpost

2020/11/9

#
MrSkyPanda wrote...
i want to display the score based off of how many coins one player has grabbed
So like "Game Over" and "Score: 143"?
MrSkyPanda MrSkyPanda

2020/11/9

#
yes
danpost danpost

2020/11/9

#
MrSkyPanda wrote...
yes
I used a less simple version of the following class in one of my games:
import greenfoot.*;

public class Panel extends Actor
{
    private String message;
    
    public Panel(String text)
    {
        message = text; // saves text to be displayed
    }

    public void addedToWorld(World w)
    {
        GreenfootImage base = new GreenfootImage(w.getWidth(), w.getHeight()); // world sized image 
        base.setColor(new Color(84, 84, 84, 128);
        base.fill(); // fills in background color
        GreenfootImage txtImg = new GreenfootImage(message, 80, new Color(0, 48, 48, 192), new Color(0, 0, 0, 0)); // text image
        base.drawImage(txtImg, (w.getWidth()-txtImg.getWidth())/2, (w.getHeight()-txtImg.getHeight())/2); // add text image to base image
        setImage(base); // display image
    }
}
Then you can use:
addObject(new Panel("Game Over\nScore: "+score), getWidth()/2, getHeight()/2);
MrSkyPanda MrSkyPanda

2020/11/11

#
Thank you! Is there a way to only show the score when one of the players reaches say for example 10 coins? Also where am i supposed to put the last code you gave me? I assumed it was in the world but it just gave m an error
danpost danpost

2020/11/11

#
MrSkyPanda wrote...
Is there a way to only show the score when one of the players reaches say for example 10 coins? Also where am i supposed to put the last code you gave me? I assumed it was in the world but it just gave m an error
If you keep references to your players in the world:
private Player player1, player2;
your act method in world could have:
int winner = 0, score = 0;
int p1score = player1.getScore();
int p2score = player2.getScore();
if (p1score >= 10) { winner = 1; score = p1score; }
if (p2score >= 10) { winner = 2; score = p2score; }
if (winner > 0) addObject(new Panel("Game Over\nPlayer "+winner+" wins\nScore: "+score), getWidth()/2, getHeight()/2);
Since you provided no code to go on and because you had an error above, lines 2 and 3 were coded as if your scores were with your players.
MrSkyPanda MrSkyPanda

2020/11/11

#
Thanks i figured it ot!
You need to login to post a reply.