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

2012/10/6

Scoreboard for Crab world scenario

jmwalker jmwalker

2012/10/6

#
Simple source code needed for a scoreboard to count worms eaten by the crab and crabs eaten by the lobster. Also Final scoreboard to show Game Over.
-nic- -nic-

2012/10/6

#
could i have a look at the code you have allready?
Game/maniac Game/maniac

2012/10/9

#
Create a new class call it anything you want then open it and delete everything inside then paste this and compile:
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 GameOver 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 GameOver()
    {
        this(100);
    }

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

    /**
     * Make the score board image.
     */
    private void makeImage(String title, String prefix, String prefix2, 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 + score1, 60, 200);
        image.drawString(prefix2 + score2, 60, 250);
        setImage(image);
    }
}
In your world paste this:
int crabScore=0; //the Crabs score
int lobScore=0; //the Lobsters score

public void gameEnd()
{
    addObject(new GameOver(crabScore, lobScore),getWidth()/2,getHeight()/2);
}
in your Lobsters eat method paste:
Worldn worldn = (Worldn) getWorld();//Change Worldn to the name of your world and worldn is just your worlds name but with all lower case letters
worldn.lobScore++;
in your Crabs eat method past:
Worldn worldn = (Worldn) getWorld();//Change Worldn to the name of your world and worldn is just your worlds name but with all lower case letters
worldn.crabScore++;
all you need to do now it paste this into your method which ends the game:
gameEnd();
danpost danpost

2012/10/9

#
Not the simplest, but Game/maniac's suggestion of using M Kolling's code for a final scoreboard would work, provided lines 23 and 37 of the GameOver class compiled. Since the GameOver constructor has two parameters, line 23 needs changed to something like
this(100, 15);
Line 37 is also missing the other score. It needs changed to
private void makeImage(String title, String prefix, String prefix2, int score, int score2)
Game/maniac Game/maniac

2012/10/9

#
You mean:
private void makeImage(String title, String prefix, String prefix2, int score1, int score2)
danpost danpost

2012/10/9

#
Yes, thank you.
Game/maniac Game/maniac

2012/10/9

#
your welcome
jevan jevan

2012/11/11

#
when i tried that it said that the class game over should be declared in a file in java
sethjemery sethjemery

2012/12/9

#
I tried it and cannot get it to work.
Fratica Fratica

2012/12/17

#
Cannot find import java.util.Calendar keep geting an error code. Help plz. thanks
Game/maniac Game/maniac

2012/12/17

#
try import java.util.calendar
You need to login to post a reply.