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

2020/4/22

How to increase my score by increments of 1000

HairyChickens HairyChickens

2020/4/22

#
So I am currently making this game for a school project and I wanted to increase my score by increments of 1000 because one of the objectives of my game is to collect money. I wanted the score to increase by 1000 because the money is worth $1000. Right now with my code it increases by 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Player_1 extends Actor
{
    private int score;
 
    public void act()
    {
        collectMoney();
    }
 
    public void increaseScore()
    {
        score++;
        showStatus();
    }
 
    public void collectMoney()
    {
        if (isTouching(Money.class))
        {
            removeTouching(Money.class);
            increaseScore();
            World world = getWorld();
            world.addObject(new Money(), Greenfoot.getRandomNumber(1690), Greenfoot.getRandomNumber(900));
        }
    }
 
    public void showStatus()
    {
        getWorld().showText("Player 1 Score : $"+score, 70,560);
        getWorld().showText("Player 1 Death : "+score2, 70,540);
    }
If there is any way of doing this? Also, I realised that the score doesn't show until I collect money. Is there a way of showing the score before I collect any money? eg. Player 1 Score: $0 Thanks
danpost danpost

2020/4/22

#
You can make use of the addedToWorld(World) method and call showStatus from there to have it initially show. You can also keep your scoring as is and just display 1000 times the score. Handling the score will not work well that way (as per my suggestion) if any transaction is not by a multiple of 1000. In that case, the increaseScore method will need to be modified. Giving it the amount to add via a parameter would be the way to go.
HairyChickens HairyChickens

2020/4/22

#
Thanks for the quick reply but do you mind showing me exactly what I should type for the addedToWorld method and the multiply 1000. This is my first time playing with coding and I'm not familiar it yet :)
danpost danpost

2020/4/22

#
HairyChickens wrote...
Thanks for the quick reply but do you mind showing me exactly what I should type for the addedToWorld method and the multiply 1000. This is my first time playing with coding and I'm not familiar it yet :)
The method would look like this:
1
2
3
4
protected void addedToWorld(World world)
{
    showStatus();
}
An example of multiplying would be taking "score" in line 29 and replacing it with "(score*1000)".
HairyChickens HairyChickens

2020/4/22

#
danpost wrote...
HairyChickens wrote...
Thanks for the quick reply but do you mind showing me exactly what I should type for the addedToWorld method and the multiply 1000. This is my first time playing with coding and I'm not familiar it yet :)
The method would look like this:
1
2
3
4
protected void addedToWorld(World world)
{
    showStatus();
}
An example of multiplying would be taking "score" in line 29 and replacing it with "(score*1000)".
Thank you so much!!!!
You need to login to post a reply.