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

2018/4/24

Trouble Keeping Score

studentProgrammer studentProgrammer

2018/4/24

#
I'm making a game for my computer programming class and I can't really figure out what to do about the score.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class DinosaurWorld extends World
{
    public DinosaurWorld()
    {    
        super(560, 560, 1); 
        populateWorld();
        getBackground().drawImage(new GreenfootImage("Instructions:", 20, null, null), 35, 1);
        getBackground().drawImage(new GreenfootImage("Move the dinosaur using the space bar", 18, null, null), 40, 15);
        getBackground().drawImage(new GreenfootImage("and turn using the up and down arrow keys.", 18, null, null), 50, 30);
    }
    int eggcounter = 0;
    public void act() 
    {    
        getBackground().drawImage(new GreenfootImage("Score="+eggcounter, 20, null, null), 500, 500);
        //when predator eats egg, eggcounter++
        if (getObjects(Egg.class).isEmpty())
            {
                Greenfoot.stop();
            }
    }
    public void populateWorld()
    {
        addObject(new predator(), 300, 300);
        while (getObjects(Egg.class).size() < Greenfoot.getRandomNumber(11)){
            addObject(new Egg(), Greenfoot.getRandomNumber(500), Greenfoot.getRandomNumber(500));
        }
    }
}
I want to make it so that when my predator class eats the egg class it will add something to the eggcounter.
danpost danpost

2018/4/24

#
studentProgrammer wrote...
I want to make it so that when my predator class eats the egg class it will add something to the eggcounter.
Try this:
public void removeObject(Actor actor)
{
    super.removeObject(actor);
    if (actor instanceof Egg) eggcounter++;
}
studentProgrammer studentProgrammer

2018/4/25

#
It didn't work. I think it might be because my method to make the eggs disappear is in my actor predator.
danpost danpost

2018/4/25

#
studentProgrammer wrote...
It didn't work. I think it might be because my method to make the eggs disappear is in my actor predator.
If you are using getWorld().removeObject(...) to remove the Egg objects one at a time, then the provided code should work.
studentProgrammer studentProgrammer

2018/4/26

#
I'm using this
public void collectEgg()
    {
        if ( canSee(Egg.class))
        {
            eat(Egg.class);
        }
    }
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
I put it inside of the actor predator. It's the same way the crab in the crabWorld game eats the worms.
danpost danpost

2018/4/26

#
After eating an egg, pause the scenario, right click on world, select Inspect and sse what the value of eggcounter is. Report back what you find and why you think it did not work.
studentProgrammer studentProgrammer

2018/4/26

#
All it says is No Fields
studentProgrammer studentProgrammer

2018/4/26

#
I put the egg counter in the actor class predator. I just don't have a way to display the score since the variable is from the actor class and not the world class.
studentProgrammer studentProgrammer

2018/4/26

#
I found a thing that did it!
You need to login to post a reply.