Making a scoreboard in Greenfoot
Find other posts in the same category: |

Contents

I am trying to learn how to make a scoreboard.

Here is the code for the scoreboard from the Greenfoot game called Wombat and Sheep, written by rabbithacking.
http://greenfootgallery.org/scenarios/486

This is a good example of a scoreboard.

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 M Kolling
* @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;

/**
* Create a score board with dummy result for testing.
*/
public ScoreBoard()
{
this(100);
}

/**
* Create a score board for the final result.
*/
public ScoreBoard(int score)
{
makeImage("Game Over", "Score: ", score);
}

/**
* Create a score board for any message.
*/
public ScoreBoard(String txt)
{
makeTxt(txt);
}

/**
* Write anything on the board
*/
private void makeTxt(String txt)
{
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.WHITE);
image.drawString(txt, 60, 100);
setImage(image);
}

/**
* 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.WHITE);
image.drawString(title, 60, 100);
image.drawString(prefix + score, 60, 200);
setImage(image);
}
}

And here is the code from the class Counter

import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Font;

/**
* Counter that displays a number.
*
* @author Michael Kolling
* @version 1.0.1
*/
public class Counter extends Actor
{
private int value = 0;
private int target = 0;
private String text;
private int stringLength;

public Counter()
{
this("");
}

public Counter(String prefix)
{
text = prefix;
stringLength = (text.length() + 2) * 16;

setImage(new GreenfootImage(stringLength, 24));
GreenfootImage image = getImage();
Font font = image.getFont();
image.setFont(font.deriveFont(24.0F)); // use larger font

updateImage();
}

public void act() {
if(value < target) {
value++;
updateImage();
}
else if(value > target) {
value--;
updateImage();
}
}

public void add(int score)
{
target += score;
}

public void subtract(int score)
{
target -= score;
}

public int getValue()
{
return value;
}

/**
* Make the image
*/
private void updateImage()
{
GreenfootImage image = getImage();
image.clear();
image.drawString(text + value, 1, 18);
}
}


To see posts on the same topic, click the links below the name of the author at the top of this page.