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

2013/12/12

How to set the score equal to zero after reset?

1
2
jlin55 jlin55

2013/12/12

#
I created a game where a seal eats all the grapes in the ocean. The seal must avoid the birds to be eaten. My concern is how can I reset the score to zero each time without clicking the compile button? I need this project finished by Tuesday.
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
For the ocean as background:
public class ocean extends World
{
 
    /**
     * Constructor for objects of class ocean.
     *
     */
    public ocean()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        addObject(new seal(), 25, getHeight()/2);
        addObject(new grapes(), getWidth() - 40, getHeight()/2);
        addObject( new grapes(), (Greenfoot.getRandomNumber(450)), (Greenfoot.getRandomNumber(450)));
        addObject( new grapes(), (Greenfoot.getRandomNumber(450)), (Greenfoot.getRandomNumber(450)));
        addObject(new score(), 40, getHeight()/2);
        populateWorld();
         
    }
public void populateWorld()
    {
        
        for (int i=1; i<4; i++)
        {
            addObject( new bird(), (Greenfoot.getRandomNumber(450)), (Greenfoot.getRandomNumber(450))) ;
        }
           for (int i= 1; i< 10; i++)
            {
                addObject( new grapes(), (Greenfoot.getRandomNumber(500)), (Greenfoot.getRandomNumber(500)));
            }
             
        }
}
 
The Seal for the user:
 
   public static int score = 0;
   public void act()
    {
        checkForArrowKeys(5);
         
    }   
     public void checkForArrowKeys (int adjust)
    {
              if ( Greenfoot.isKeyDown("left") )
                  {
                      setLocation(getX() - adjust, getY());
                  }
              if ( Greenfoot.isKeyDown("right") )
                  {
                      setLocation(getX() + adjust, getY());
                  }   
               if ( Greenfoot.isKeyDown("down") )
                  {
                      setLocation(getX() , getY()+ adjust);
                  }
               if ( Greenfoot.isKeyDown("up") )
 
 
   The Bird to eat Seal:
 
             public class bird extends Actor
{
    /**
     * Act - do whatever the bird wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        randomMove(  5  );
        tryToCatch( seal.class      );
          
    
    public void randomMove (int maxAmount)
    {
        setLocation(getX() + (-maxAmount + Greenfoot.getRandomNumber(2 * maxAmount + 1)) , getY() + (-maxAmount + Greenfoot.getRandomNumber(2 * maxAmount + 1))  );
    }  
 public void tryToCatch(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0,clss);
        if ( actor !=null )
        {
            getWorld().removeObject (actor);
        
         
    }
 
The Grapes:
 
public class grapes extends Actor
{
    /**
     * Act - do whatever the grapes wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean flag = false;
    public void act()
    {
        checkForseal();
    }   
    public void checkForseal()
    {
        seal theseal = null;
        theseal = (seal)getOneIntersectingObject(seal.class);
        if(theseal !=null)
        {
            if (flag == false)
            {
                seal.score++;
                flag = true;
                getWorld().removeObject(this);
    }
}
}
}
 
And Finally my Score (the purple ball):
 
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;
public class score extends Actor
{
    /**
     * 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 act()
    {
         updatescore();
          
    }   
       public void updatescore()
    {
        GreenfootImage image = new GreenfootImage(145, 35);
        image.setColor(new Color(0, 0, 0, 160));
        image.fillRect(0, 0, 145, 35);
        Font font = new Font ("Arial", Font.PLAIN, 30);
        image.setFont(font);
        image.setColor(Color.WHITE);
        image.drawString("score: " + seal.score, 5, 27);
        setImage(image);
}
 
}
Gevater_Tod4711 Gevater_Tod4711

2013/12/12

#
The value is saved when you reset the game because the variable score (line 38) is declared static. I think you never used this variable in a static context so you could try just deleting the static and change it to public int score. If this doesn't work please tell me so I can give you the right code.
jlin55 jlin55

2013/12/12

#
I deleted the static, but the grapes class code source comes up and on line 110, (seal.score++;), it says non-static variable score cannot be referenced from a static context.. What does it mean?
Gevater_Tod4711 Gevater_Tod4711

2013/12/12

#
Oh I didn't see that. That is a static access of the variable. But if you just change seal.score++; to theseal.score++; it should work.
jlin55 jlin55

2013/12/13

#
i did that change, but the score class code comes up and says, the seal.score (line 142) is non-static variable score cannot be referenced from a static context.
danpost danpost

2013/12/13

#
Make the 'score' field 'static' in your 'seal' class (again). Then, just set its value to zero in the constructor of your 'ocean' world class.
jlin55 jlin55

2013/12/13

#
what is the code and where do i put it in the ocean world? the error is still there.
danpost danpost

2013/12/13

#
It would be 'seal.score = 0;' and it would go in your 'public ocean()' constructor.
jlin55 jlin55

2013/12/13

#
just did that change, but the score class pops up and says non-static variable score cannot be referenced from a static context on line 142 again, and all of the code is highlighted.
danpost danpost

2013/12/13

#
danpost wrote...
Make the 'score' field 'static' in your 'seal' class (again). Then, just set its value to zero in the constructor of your 'ocean' world class.
The first line was not executed.
jlin55 jlin55

2013/12/13

#
Sorry, I just decided to redo starting from yesterday danpost and gevater_Tod4711. Going back to the first solution that gevater_Tod4711 gave me, I deleted the static to public int = 0; for the seal class, but the score class pops up, instead of the grapes class last time, with an error on line (142) that says non-static variable score cannot be referenced from a static context, then I did the theseal.score++; in the score and still doesn't work. Then I added the seal.score=0; in the ocean constructor and the error in the score class says cannot find symbol-variable theseal.
danpost danpost

2013/12/13

#
If you are not going to make the field 'static', then you are going to need to get a reference to the seal object that is in your world.
1
2
3
4
5
if (!getWorld().getObjects(seal.class).isEmpty())
{
    seal theseal = (seal)getWorld().getObjects(seal.class).get(0);
    theseal.score++;
}
Line 1 makes sure a seal is in your world. Line 3 gets a reference to that seal in your world. Line 4 increments the 'score' field for that seal. If that seal is removed from the world and replaced with a 'new' seal, then your score may be lost (the 'new' seal will be created with its own 'score' field with a starting value of zero).
jlin55 jlin55

2013/12/17

#
Thanks, finally got it. One thing that I would like to have in my game is a game over sign for either birds eat the seal, or seal eats all the grapes.
danpost danpost

2013/12/17

#
1
2
3
4
5
6
7
8
9
// in world class 'act' method or a method it calls
if (getObjects(seal.class).isEmpty())
{
    // birds eat the seal game over sign
}
if (getObects(grape.class).isEmpty())
{
    // seal eats all the grapes game over sign
}
The signs can be by removing all actors from the world and adding a game over actor or by setting a new game over world.
jlin55 jlin55

2013/12/19

#
which class do i put the code into?
There are more replies on the next page.
1
2