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

2019/3/19

How to make a scoring system?

Poseidon420 Poseidon420

2019/3/19

#
In my game, I need a scoring system but I'm not entirely sure how to create one. Could somebody please give me some advice. Thanks.
danpost danpost

2019/3/19

#
In your case, since you are using a game over world and presuming that you have multiple players, you could use something like this in your world class (written for two players, but easily modifiable for more):
public static final RED = 0, BLUE = 1; // continue for more players

private int[] scores = new int[2]; // size to number of players

public int getScore(int player)
{
    return scores[player);
}

public void addScore(int player, int amount)
{
    scores[player] += amount;
}

public int[] getScores()
{
    return scores;
}
With the above in a MyWorld world, from an Actor subclass, you can
// get a single score
int redScore = ((MyWorld)getWorld()).getScore(MyWorld.RED);

// add to a score
((MyWorld)getWorld()).addScore(MyWorld.BLUE, 10);

// or get both scores
int finalScores = ((MyWorld)getWorld()).getScores();
Nate2002 Nate2002

2019/3/19

#
What I usually do is this to show the scoreboard in the world. (Put this in your "My World" class)
 private int score = 0;
    public int time = 0;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 600, 1); 
        GreenfootImage back = getBackground();
        back.setColor(Color.BLACK);
        back.fill();
        prepare();
        showScore();
        score++;

    }

    public void act()
    {
     
        countTime();
    }

    public void countTime()
    {
        time++;

    }

    public void addScore(int points)
    {
        score = score + points;
        showScore();
    }


    public void showScore()
    {
        showText("Ship 1:  " + score, 75, 25);

    }
Here's how you would add the score (Put this in your ammunition class or whatever would touch the enemy to add score)
public void Stuff()
    {
        boolean kill = true;
        if(kill)
            if (isTouching(UFO.class))
            {
                kill = false;
                removeTouching(UFO.class);
                MyWorld world = (MyWorld)getWorld();
                getWorld().addObject(new Explosion(), getX(), getY());
                getWorld().removeObject(this);
                Greenfoot.playSound("Explosion copy.wav");
                world.addScore(50);
            }
        if(kill)
            if (isTouching(Arwing.class))
            {
                kill = false;
                removeTouching(Arwing.class);
                MyWorld world = (MyWorld)getWorld();
                getWorld().addObject(new Explosion(), getX(), getY());
                getWorld().removeObject(this);
                Greenfoot.playSound("Explosion copy.wav");
                world.addScore(-50);
            }
        if(kill)
            if (isTouching(UFO0.class))
            {
                kill = false;
                removeTouching(UFO0.class);
                MyWorld world = (MyWorld)getWorld();
                getWorld().addObject(new Explosion(), getX(), getY());
                getWorld().removeObject(this);
                Greenfoot.playSound("Explosion copy.wav");
                world.addScore(50);
            }
        if(kill)
            if (isTouching(Mario.class))
            {
                kill = false;
                removeTouching(Mario.class);
                MyWorld world = (MyWorld)getWorld();
                getWorld().addObject(new Explosion(), getX(), getY());
                getWorld().removeObject(this);
                world.addScore(200);
                Greenfoot.playSound("Mario 64 Falling Death Scream (320  kbps).mp3");
            }
        if(kill)
            if (isTouching(Mario2.class))
            {
                kill = false;
                removeTouching(Mario2.class);
                MyWorld world = (MyWorld)getWorld();
                getWorld().addObject(new Explosion(), getX(), getY());
                getWorld().removeObject(this);
                world.addScore(200);
                Greenfoot.playSound("Mario 64 Falling Death Scream (320  kbps).mp3");
            }
        if(kill)
            if (isTouching(Arwing1.class))
            {
                kill = false;
                removeTouching(Arwing1.class);
                MyWorld world = (MyWorld)getWorld();
                getWorld().addObject(new Explosion(), getX(), getY());
                getWorld().removeObject(this);
                Greenfoot.playSound("Explosion copy.wav");
                world.addScore(-50);
            }

    }
Poseidon420 Poseidon420

2019/3/19

#
Its a one player game and I am very confused at the lines of code there is. So many some more clarification of what the ifs and other stuff are doing. Thanks
danpost danpost

2019/3/20

#
Poseidon420 wrote...
Its a one player game and I am very confused at the lines of code there is. So many some more clarification of what the ifs and other stuff are doing. Thanks
Then, its more simple:
private int score;

public int getScore()
{
    return score;
}

public void addScore(int amt)
{
    score += amt;
}
Using in Actor subclass
// getting score
int score = ((MyWorld)getWorld()).getScore();

// changing score
((MyWorld)getWorld()).addScore(10);
Poseidon420 Poseidon420

2019/3/25

#
After i put this code in, my bullet won't shoot. Also where do i put the changing score bit?
danpost danpost

2019/3/25

#
Poseidon420 wrote...
After i put this code in, my bullet won't shoot.
Not due to this code. This has nothing to do with shooting.
where do i put the changing score bit?
In whatever Actor subclass whose instances get hit to score points.
Poseidon420 Poseidon420

2019/3/26

#
OK i fixed the shooting but the score doesn't go up
danpost danpost

2019/3/26

#
Poseidon420 wrote...
OK i fixed the shooting but the score doesn't go up
Show class where you put the code.
Poseidon420 Poseidon420

2019/3/26

#
public Bullet()
    {
        GreenfootImage g = new GreenfootImage("beeper.png");
        g.scale(50, 50);
        setImage(g);
    }
    public void act() 
    {
       int score = ((MyGame)getWorld()).getScore();
        Actor t;
       t = getOneObjectAtOffset(0, 0, Igloo.class);
       setLocation(getX() + 50, getY());
       if(t!=null)
        {
            ((MyGame)getWorld()).addScore(1);
            World w = getWorld();
            w.removeObject(t);
            getWorld().removeObject(this);
            
        }
        else if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
           
    }
Super_Hippo Super_Hippo

2019/3/27

#
How do you know that the score isn't increasing? Did you inspect the MyGame instance? My guess is that the bullet is moving too fast. Chances are the bullet is moving through the Igloo without actually touching it.
Poseidon420 Poseidon420

2019/3/27

#
Its alright i've fixed it now thanks guys
BaliEatz BaliEatz

2019/3/27

#
Poseidon420 wrote...
Its alright i've fixed it now thanks guys
np dog
You need to login to post a reply.