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

2012/12/9

Score keeper

sethjemery sethjemery

2012/12/9

#
I am currently taking an intro to programming course through my school and am having trouble making a score keeping mechanism. I've tried several different things but can not get anything to work. I am using one of Greenfoots scenarios "Little Crabs" that I got with the book and program. I am trying to make a visible scoreboard that will increase by 5 points whenever a crab eats a worm and reduces to 0 whenever a lobster hits a crab. If the lobster hits the crab when the score is at 0 then the game will be over. Any help would be great since I know nothing about programming.
Zamoht Zamoht

2012/12/9

#
It's a bit late here, so I'm not going to write any code to you for now, but you might want to look at the balloons scenario.
sethjemery sethjemery

2012/12/9

#
I looked into it and it doesnt work for my game. I founf a script that works but it doesn't appear on the screen. 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); } /** * Make the score board image. */ private void makeImage(String title, String prefix, int score) { GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT); image.setColor(new Color(255,255,255, 128)); image.fillRect(0, 0, WIDTH, HEIGHT); image.setColor(new Color(0, 0, 0, 128)); 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); } }
vonmeth vonmeth

2012/12/9

#
You need to create it, and add it to the world.
        ScoreBoard board = new ScoreBoard();
        addObject(board,getWidth()/2, getHeight()/2);
vonmeth vonmeth

2012/12/9

#
That is more for a Game Over ending score though you can, of course, modify it to suit your needs. The following, from the asteroids-3 scenario (Chapter 7) provided with Greenfoot, is a bit better suited for keeping a count.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Color;
import java.awt.Graphics;

/**
 * Counter that displays a text and number.
 * 
 * @author Michael Kolling
 * @version 1.0.1
 */
public class Counter extends Actor
{
    private static final Color textColor = new Color(255, 180, 150);
    
    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) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(textColor);

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

    public void add(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, 12);
    }
}
        scoreCounter = new Counter("Score: ");
        addObject(scoreCounter, 60, 380);
sethjemery sethjemery

2012/12/10

#
Where do I add the score counter? I made a new subclass in the world to do so. Is the correct? it gave me an identifier error at first so I added <> around the first scoreCounter. Now it says illegal start of type right before the =
sethjemery sethjemery

2012/12/10

#
ok I got the counter visible but now I need it to count for every worm that is eaten until I get to 10. also I need it to be moved to the top left corner and I need to change the color to black.
mufusion mufusion

2013/4/21

#
how can i change the counter +1 when an enemie is killed? in the asteroids 3 scenario i doesnt work the counter stays at 0.
danpost danpost

2013/4/21

#
Normally, you can use the following line after removing the enemy being killed (providing it is the one and only counter object in the world):
((Counter)getWorld().getObjects(Counter.class).get(0)).add(1);
If more than one are in the world:
((WorldName)getWorld).getScoreCounter().add(1);
// where 'getScoreCounter' is as follows (in your world class, which I have named 'WorldName')
public Counter getScoreCounter()
{
    return scoreCounter; 
}
mufusion mufusion

2013/4/21

#
i added line 1 where the enemie die
public void remove()
    {

        if (health == 0)
        {getWorld().removeObject(this);
         ((SpielWelt)getWorld).getScoreCounter().add(1);
        }

    }
and the other lines in my world (SpielWelt) but im not sure where in Spielwelt to add it. anyway i get an compile error in the enemies (gegner1) code : cannot find symbol - variable getWorld when i search on the internet it says that getWorld is followed by () and not ) but when i edit it to
((SpielWelt)getWorld().getScoreCounter().add(1));
it says " not a statement"
danpost danpost

2013/4/21

#
Try this:
public void remove()
{
    if (health == 0)
    {
        ((SpielWelt)getWorld()).getScoreCounter().add(1);
        getWorld().removeObject(this);
    }
}
Please note where the parenthesis are placed. Also, the order is necessary because if you remove 'this' from the world first 'getWorld()' will return 'null' and you will not be able to get the scoreCounter (actually, you will get a NullPointerException error).
mufusion mufusion

2013/4/21

#
Thank you wery much, without you my game would be half as finish as it is now
You need to login to post a reply.