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

2018/10/23

How to increase points by more than one?

ananyagarg ananyagarg

2018/10/23

#
I was wondering if there is a way you could increase your score counter by more than one? For example, when a snake eats a cherry, I want it to increase by one, but when it eats a blueberry, I want it to increase by 5. This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Score extends Actor
{
    private int score = 0;
     
    public Score(){
       updateImage();
    }
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void updateImage()
    {
      setImage(new GreenfootImage("Score : "+ score,20,greenfoot.Color.BLACK, greenfoot.Color.WHITE));
    }
    public void addScoreCherries(){
       score++;
       updateImage();
    }
}
danpost danpost

2018/10/23

#
ananyagarg wrote...
I was wondering if there is a way you could increase your score counter by more than one? For example, when a snake eats a cherry, I want it to increase by one, but when it eats a blueberry, I want it to increase by 5. << Code Omitted >>
Just add an addScoreBlueberries method to the class with similar (but not exactly the same) code to addScoreCherries
ananyagarg ananyagarg

2018/10/23

#
danpost wrote...
ananyagarg wrote...
I was wondering if there is a way you could increase your score counter by more than one? For example, when a snake eats a cherry, I want it to increase by one, but when it eats a blueberry, I want it to increase by 5. << Code Omitted >>
Just add an addScoreBlueberries method to the class with similar (but not exactly the same) code to addScoreCherries
How would you code to add 5 points instead of one?
danpost danpost

2018/10/23

#
ananyagarg wrote...
How would you code to add 5 points instead of one?
1
2
3
4
// either
score = score + 5;
// or
score += 5;
You need to login to post a reply.