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

2015/1/13

Displaying Score at the End of Game

elementa elementa

2015/1/13

#
HI, so at the end of my game I would like to be able to display my ScoreBoard with the player's final score on it. I'm not quite sure how I can carry this over? *the end screen of my game is a separate world called EndWorld, and currently has nothing in it. This is my code for my ScoreBoard class:
import greenfoot.*;  
import java.awt.Color;
import java.awt.Graphics;

{
    private int score = 0;
    private final int WIDTH = 80;      
    private final int HEIGHT = 20;      
    private String text = "Score: ";

    // This constructor creates the starting image for the Scoreboard 
    public ScoreBoard()
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
        image.setColor(Color.WHITE);
        image.fill();
        image.setColor(Color.BLACK);
        image.drawRect(0, 0 , WIDTH - 1, HEIGHT - 1);
        setImage(image);
        updateDisplay();
        
    }
    
    // This method reinstates the variable of score by adding the gained points with the current score. 
    public void add(int value)
    {
        score = score + value;
        updateDisplay();
    }
    
    // This method resets the score in the ScoreBoard back to 0. 
    public void reset()
    {
        score = 0;
        updateDisplay();
    }
    
    // This method gets the score from the actor CorrectDoor and returns it back. 
    public int getScore()
    {
        return score;
    }

    // This method updates the display of the ScoreBoard with the scores from the CorrectDoor class. 
    public void updateDisplay()
    {
        // x and y relative to the image. baseline of leftmost character.
        int x = 5;
        int y = 15;
        // "Repaint" the score display
        GreenfootImage image = getImage();
        image.setColor(Color.WHITE);
        image.fillRect(1, 1, WIDTH-2, HEIGHT-2);    // "erase" the display
        image.setColor(Color.BLACK);
        image.drawString(text + score, x, y);
        setImage(image);
    }
}
danpost danpost

2015/1/13

#
You have one place in your code where you have access to both the EndGame world AND the score -- that is when you initially create the EndGame world and before you set it as the active world. That code would be even more important to show than what you gave (although what you provided is useful in how to code the solution).
elementa elementa

2015/1/13

#
Um, I'm not completely sure where this line of code is in the ScoreBoard class?
danpost danpost

2015/1/13

#
I doubt very much that you would be changing worlds from the ScoreBoard class. I pretty much said it would be elsewhere in my last post.
You need to login to post a reply.