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

2012/11/11

Counting multiples in scoreboard

kaciedy kaciedy

2012/11/11

#
In creating a crab game, I am wanting to display a scoreboard every time 10 worms have been eating, and count up the multiples (i.e. 10, 20, 30, and so on). I have only figured out enough to just display 10 and have no clue how I would let it keep counting, any pointers?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class Scoreboard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Scoreboard extends World
{
    private int wormsEaten;
    private CrabWorld ocean;
    
    /**
     * Constructor for objects of class Scoreboard.
     * 
     */
    public Scoreboard(int w)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        //ocean = cw;
        wormsEaten = w;
        GreenfootImage bg = getBackground(); //getting the background image
        Color c = new Color(Greenfoot.getRandomNumber(256), Greenfoot.getRandomNumber(256), Greenfoot.getRandomNumber(256));
        bg.setColor(c);    
        Font f = new Font("sample", Font.ITALIC, 20);//setting font style
        bg.setFont(f);//setting font
        bg.drawString("Worms EATEN =" + wormsEaten + " Press ENTER to continue playing!", 60, 210);
    }
    
    public void act()
    {
        if(Greenfoot.isKeyDown("enter"))
        {
            Greenfoot.setWorld(new CrabWorld());
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Animal
{
    private GreenfootImage image1 = new GreenfootImage("crab.png");
    private GreenfootImage image2 = new GreenfootImage("crab2.png");
    private int wormsEaten;
    private int count;

    
    public void act()
 
    {
        seeEatWorm();
        keyClickA();
        switchImage();
    }
 
    /**
     * 
     * When crab touches a worm it noms it.
     */
    public void seeEatWorm()
    
    {
        if(canSee(Worm.class))
        {
            eat(Worm.class);
            Greenfoot.playSound("slurp.wav");
            wormsEaten++;
            if(wormsEaten >= 1)
            {
                getWorld().addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));
            }
            
            if (wormsEaten == 10)
            {
                Greenfoot.playSound ("fanfare.wav");      
                Greenfoot.setWorld(new Scoreboard(wormsEaten));
            }
        }
    }
    
     /**
     *
     * Allows remote movement of crab of player 1.
     */
    
    public void keyClickA()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-4);
            move();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(4);
            move();
        }
    }
    
    /**
     * 
     */
    public void switchImage()
    {   
        if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("right"))
        if(getImage() == image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
}
danpost danpost

2012/11/11

#
Change line 39 in the Crab class to the following:
if (wormsEaten > 0 && wormsEaten - 10 * (wormsEaten / 10)== 0)
// or more simply
if (wormsEaten > 0 && wormsEaten % 10 == 0)
We ensure that we do not show the Scoreboard when wormsEaten is zero with 'wormsEaten > 0' and determine when to show with 'wormsEaten % 10 == 0', which means if dividing by ten leaves no remainder (hence 10, 20, 30, etc.).
kaciedy kaciedy

2012/11/11

#
My counter still resets to zero for some reason after the Scoreboard is introduced and the CrabWorld is reintroduced. I had attempted to do what you addressed for line 39, and it has the same reset issue I had. There isn't a place where I tell it to reset the counter, I'm guessing greenfoot did this automatically?
danpost danpost

2012/11/11

#
Oh, I understand what you are referring to. The reason it resets is because you are creating a NEW instance of the world when you exit the Scoreboard. Probably the easiest way to solve this is telling the new instance of the world what the score was:
// in the Scoreboard class code
// use this for your act method
public void act()
{
    if(Greenfoot.isKeyDown("enter"))
    {
        CrabWorld crabworld = new CrabWorld(); // creates the new crabworld object
        Crab crab = (Crab) crabworld.getObjects(Crab.class).get(0); // gets the crab object in the new world
        crab.setWormsEaten() = wormsEaten; // sets the value of 'wormsEaten' 
        Greenfoot.setWorld(crabworld); // starts the new world
    }
}
// and add this method in your Crab class
public void setWormsEaten(int wormCount)
{
    wormsEaten = wormCount;
}
BTW, I also realized that line 39 can be just
if (wormsEaten % 10 == 0)
because the code only executes when eating a worm and therefore the value of wormsEaten cannot be zero.
kaciedy kaciedy

2012/11/11

#
I get what you are saying. In line 8, by the 0 are you saying to keep the original crab and not replace it? Also, I run into an error with line 9 because of "setWormsEaten" - the error that reads is "method setWormsEaten in class Crab cannot be applied to given types; required: int, found: no arguments; reason: actual and formal argument lists differ in length" The int is defined when wormsEaten = wormCount is what I figure. I've never used "set" for part of my methods, is it one that is predefined that requires certain things? I know some do because I have ran into an error before because of using something that had defined requirements to execute it. Thank you so very much for your help.
danpost danpost

2012/11/11

#
In line 8: No. A new crab is created when the new crabworld is created. The new crab is what we are dealing with here. In line 9: That is my fault. It should be:
crab.setWormsEaten(wormsEaten);
Using 'set': it is not anything that is pre-defined; it does, however, help explain what the method does. Normally, when you see this in a method name, there will be a value or object that is passed to the method (in this case an 'int' value ).
kaciedy kaciedy

2012/11/11

#
I tried that and just didn't remove the =wormsEaten, which should have been obvious, but yeah it executes properly. Ok, thank you for helping clear that up it makes quite a bit of sense.
bonana bonana

2013/1/7

#
in this part:
bg.drawString("Worms EATEN =" + wormsEaten + " Press ENTER to continue playing!", 60, 210);
how can I insert a new line when I want to have the " Press ENTER to continue playing!" at the bottom of my scoreboard? and how can I use a different font? obviously you are printing your text in italics:
Font f = new Font("sample", Font.ITALIC, 20);
but how can I change it, for example in fat print? I tried some font names and just replacing ITALIC with another font name doesn't seem to work out...
danpost danpost

2013/1/7

#
In order to have to fonts on the same image, you need to draw the string images (plural) with different fonts for each one onto one main image and set that main image as the image of your object.
bonana bonana

2013/1/7

#
Could you please explain that further? And how do I insert new lines in my text?
danpost danpost

2013/1/7

#
To have multiple lines of text with the same font you can use the control sequence for the end line character '\n' as follows:
String text = "first line of text\nsecond line of text";
But to have two different fonts you will need to draw two images each with a different font and then draw them both on a main image as follows:
GreenfootImage imageOne = new GreenfootImage("first line of text", 24, Color.black, new Color(0, 0, 0, 0));
GreenfootImage imageTwo = new GreenfootImage("second line of text", 18, Color.black, new Color(0, 0, 0, 0));
int mainWide = Math.max(imageOne.getWidth(), imageTwo.getWidth());
int mainHigh = imageOne.getHeight() + imageTwo.getHeight();
GreenfootImage mainImg = new GreenfootImage(mainWide, mainHigh);
mainImg.drawImage(imageOne, (mainImg.getWidth()-imageOne.getWidth())/2, 0);
mainImg.drawImage(imageTwo, (mainImg.getWidth()-imageTwo.getWidth())/2, imageOne.getHeight());
setImage(mainImg);
bonana bonana

2013/1/8

#
The \n stuff doesn't work for me... :-( I used this code:
super(800, 600, 1);
        GreenfootImage bg = getBackground(); //getting the background image  
        Color c = new Color(Greenfoot.getRandomNumber(256), Greenfoot.getRandomNumber(256),
        Greenfoot.getRandomNumber(256));  
        bg.setColor(c);      
        Font f = new Font("sample", Font.ITALIC, 20);//setting font style  
        bg.setFont(f);//setting font
        bg.drawString("Controls:\nPlayer1:\nup: Arrow UP\ndown: Arrow DOWN\nright: Arrow RIGHT\nleft: Arrow LEFT", 60, 210);
it doesn't create a new line... and I just want to use a different font style than italic...
bonana bonana

2013/1/8

#
Ahhh... just managed to solve the new line problem! :-) I just changed the position of the text... I'm so stupid... -_-
bg.drawString("Controls:", 40, 60);
bg.drawString("Player1:", 40, 80);
//and so on and so on
bonana bonana

2013/1/9

#
still the font style to change... And I've got another problem with my own scoreboard. The scoreboard gets activated by actor z, whose score is not counted. The scoreboard should display the score of actor a, actor b and actor c. I tried to use a public static int scoreA / scoreB / scoreC in the classes of these actors and "actor a" + scoreA / "actor b" + scoreB / "actor c" + scoreC as a string in the scoreboard class, but it always shows a score of 0. Does anyone know how to fix it? (I'm sure you do ;-) )
You need to login to post a reply.