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

2015/2/20

Creating a Score Counter

GabrahamCAPS GabrahamCAPS

2015/2/20

#
Hello, I need some help with my code. I am trying to make the Score Counter go up every time my fish eats a cherry. I am not sure if I am putting the code in the correct place or not. I would greatly appreciate an explanation if possible.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class Counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }   
     
    public void addScore()
    {
        score++;
    }
}
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Fish1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Fish extends Player
{
    public int cherriesEaten;
    public Fish()
    {
        cherriesEaten = 0;
    }
     
    /**
     * Act - do whatever the Fish1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       if (foundCherry()) {
           eatCherry();
        }
        
       if (Greenfoot.isKeyDown("left"))
       {
           setLocation(getX() - 10, getY());
       }
       if (Greenfoot.isKeyDown("right"))
       {
           setLocation(getX() + 5, getY());
       }
       if (Greenfoot.isKeyDown("up"))
       {
           setLocation(getX(), getY() - 5);
       }
       if (Greenfoot.isKeyDown("down"))
       {
           setLocation(getX(), getY() + 5);
       }
    }
     
    public boolean foundCherry()
    {
        Actor cherry = getOneObjectAtOffset(0, 0, Cherry.class);
        if(cherry != null) {
            return true;
        }
        else {
            return false;
        }
    }
     
    public void eatCherry()
    {
        Actor cherry = getOneObjectAtOffset(0, 0, Cherry.class);
        if(cherry != null) {
            World myWorld = getWorld();
            getWorld().removeObject(cherry);
            Ocean ocean = (Ocean)myWorld;
            Counter counter = ocean.getCounter();
            counter.addScore();
        }
    }
     
    public int getCherriesEaten()
    {
        return cherriesEaten;
    }
}
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
32
33
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * An Ocean Experience
 *
 * @author Gabe Piper
 * @version 1.0.1
 */
public class Ocean extends World
{
    Counter counter = new Counter();
    public Ocean()
    {   
        super(600, 400, 1, false);
    }
     
    public Counter getCounter()
    {
        return counter;
    }
     
    public void act()
    {
        if(Greenfoot.getRandomNumber(150) < 1)
        {
            addObject(new Seal(), getWidth(), Greenfoot.getRandomNumber(getHeight()));
        }
        if(Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Cherry(), getWidth(), Greenfoot.getRandomNumber(getHeight()));
        }
    }
}
danpost danpost

2015/2/20

#
What, exactly, are you not sure about? Are you experiencing unwanted behavior? if so, what? I can say that, at first glance, everything seems to be alright (for what it is); but, I have no clue if it is what you want.
GabrahamCAPS GabrahamCAPS

2015/2/20

#
Well, I am expecting the score counter to add 1 to the score every time my fish eats a cherry. Its not doing that.
danpost danpost

2015/2/20

#
GabrahamCAPS wrote...
Well, I am expecting the score counter to add 1 to the score every time my fish eats a cherry. Its not doing that.
How do you know it is not doing that? What did you do to find out?
GabrahamCAPS GabrahamCAPS

2015/2/20

#
I played the game. I was controlling my fish. I ran into a cherry. I ate the cherry. The score on the screen did not show add 1 to the score.
danpost danpost

2015/2/20

#
If you are manually adding a Counter object into the world (I put it this way because I do not see anywhere in your code where one is added to the world), then, of course, it will not increase when your fish eats a cherry. The Counter object manually placed into the world is NOT the Counter object that is being increased by the Fish object when it eats a cherry. The one being increase is not in the world; so, you do not see it increasing.
GabrahamCAPS GabrahamCAPS

2015/2/20

#
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
32
33
34
35
36
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * An Ocean Experience
 *
 * @author Gabe Piper
 * @version 1.0.1
 */
public class Ocean extends World
{
    Counter counter = new Counter();
    public Ocean()
    {   
        super(600, 400, 1, false);
        addObject(new Counter(), 546, 37);
        addObject(new Fish(), 51, 192);
    }
     
    public Counter getCounter()
    {
        return counter;
    }
         
    public void act()
    {
        if(Greenfoot.getRandomNumber(150) < 1)
        {
            addObject(new Seal(), getWidth(), Greenfoot.getRandomNumber(getHeight()));
        }
        if(Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Cherry(), getWidth(), Greenfoot.getRandomNumber(getHeight()));
        }
    }
     
}
I added it in... Still not working. I am sorry if if I am not understanding. I have been working on this problem for quite a few days now.
danpost danpost

2015/2/20

#
Again, the Counter object in the world is not the one the Fish uses. You create one on line 11 and a different one on line 15.
GabrahamCAPS GabrahamCAPS

2015/2/23

#
danpost wrote...
Again, the Counter object in the world is not the one the Fish uses. You create one on line 11 and a different one on line 15.
So what should I do? Should I make something in the Fish Class? Or should I make a preparation method in the World Class that adds everything in the world?
GabrahamCAPS GabrahamCAPS

2015/2/23

#
danpost wrote...
Again, the Counter object in the world is not the one the Fish uses. You create one on line 11 and a different one on line 15.
Nevermind, I see what you were saying. I have no idea why I created a second Counter when adding an object. I think it was because I wasn't understanding the code. Thank you so very much. I appreciate it. Thanks for bearing with me. I changed line 15 to just say "counter" and not "New Counter()"
You need to login to post a reply.