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

2014/6/17

Help add a scoreboard

zachfreeman zachfreeman

2014/6/17

#
i need help adding a scoreboard to my game, but i have no idea how to do it. Can anyone help?
lordhershey lordhershey

2014/6/17

#
You would use the UserInfo class to save the score (the person needs to be logged in for this to work) you would take a blank image, get its image and draw the user's name and score to it. If you get stuck there I can cough up a small example at the end of the day if someone else does not do it first.
zachfreeman zachfreeman

2014/6/17

#
how do you input the score?
lordhershey lordhershey

2014/6/17

#
I would keep an integer somewhere where points were added, then when I get the UserInfo object I would put that score in the object in one of it's variables and save it.
zachfreeman zachfreeman

2014/6/17

#
ok Thanks could you post a small example
lordhershey lordhershey

2014/6/17

#
Sure, I can do that, but I will have to wait until I get home.
lordhershey lordhershey

2014/6/18

#
Ok here is a score box I have, it simply will list the name and score of the top 10 people - I do not do the nearby stuff because I am lazy:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
import java.awt.*;
import java.awt.image.*;

/**
 * Write a description of class HighScoreActor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HighScoreActor extends Actor
{
    BufferedImage bi = null;
    Graphics2D g = null;
    int w = 0;
    int h = 0;
    private Font myFont = new Font("TimesRoman", Font.BOLD, 18);

    public HighScoreActor()
    {
        GreenfootImage gi = getImage();
        bi = gi.getAwtImage();
        g = bi.createGraphics();
        w = bi.getWidth();
        h = bi.getHeight();
        g.setFont(myFont);
        g.setBackground(new Color(255,255,255,0));

        g.clearRect(0,0,w,h);
        g.setColor(Color.YELLOW);

        
        if(UserInfo.isStorageAvailable())
        {
            int y = 18;
            int advance = 24;
            java.util.List<UserInfo> list =  UserInfo.getTop(10);

            for(UserInfo user : list)
            {
                String stuff = user.getUserName() + " " + user.getScore();
                g.drawString(stuff,0,y);
                y += advance;
            }
        }
        //g.drawString(score,0,18);
    }

    public void act() 
    {
        // Add your action code here.
    }    
}
You cannot see it here, but there is an image (a green square) assigned to this actor (used for placement) that is 297px by 229px, this code will take the image, clear its contents and fill it with the top 10 people. To Save a Score (any score - you do not have to check if it is the top 10) you do something like this:
if(UserInfo.isStorageAvailable())
        {
            UserInfo me = UserInfo.getMyInfo();
            if(null == me)
            {
                System.out.println("Not Signed in");
            }
            else
            {
                if(me.getScore() < Score.getScore())
                {
                    me.setScore(Score.getScore());
                    me.store();
                }
            }
        }
above Score is a static class with static methods, the games score is kept there, this code above retrieves the saved score (if any) and compares it to the new score. If the new score is greater than the old one then we save it with the me.store() call.
geoffreyguilcapi geoffreyguilcapi

2014/6/18

#
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Font; import java.util.Calendar; /** * The ScoreBoard is used to display results on the screen. It can display some * text and several numbers. * * @author (your name) (Geoffrey) * @version 1.0 */ public class ScoreBoard extends Actor { public static final float FONT_SIZE = 48.0f; public static final int WIDTH = 400; public static final int HEIGHT = 300; public ScoreBoard() { this(100); } /** * Create a score board for the final result. */ public ScoreBoard(int score) { makeImage("Game Over", "Score: ", score); } /** * Make the score board image. */ private void makeImage(String title, String prefix, int score) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 160)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(255, 255, 255, 100)); image.fillRect(5, 5, WIDTH-10, HEIGHT-10); Font font = image.getFont(); font = font.deriveFont(FONT_SIZE); image.setFont(font); image.setColor(Color.RED); image.drawString(title, 60, 100); image.drawString(prefix + score, 60, 200); setImage(image); } } if this helps
You need to login to post a reply.