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

2016/4/29

Trying to display White Cell count on WBC

DC97Cobra DC97Cobra

2016/4/29

#
Hello all. I'm taking a class and one of the requests was to display the count of white cells, red cells, and bacteria, and viruses. For the life of me, I cannot get it to display the count. attached is the code of the Bloodstream and White Cell. Once I have this figured out, I know that I can get the other counts. Any help would be much appreciated!
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * The bloodstream is the setting for our White Blood Cell scenario. It's a place
 * where blood cells, bacteria and viruses float around.
 *
 * @author Michael Kölling
 * @version 1.0
 */
public class Bloodstream extends World
{
    private int countWC;
    private int countWC2;
    private int score;
    private int time;
     
    /**
     * Constructor: Set up the staring objects.
     */
    public Bloodstream()
    {   
        super(780, 360, 1);
        setPaintOrder(Border.class);
        prepare();
        score = 0;
        time = 2000;
        //countWC = 0;
        showScore();
        showWCount();
        showTime();
    }
 
    /**
     * Create new floating objects at irregular intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Bacteria(), 779, Greenfoot.getRandomNumber(360));
        }
         
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 0);
        }
         
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 359);
        }
         
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Virus(), 779, Greenfoot.getRandomNumber(360));
        }
         
        if (Greenfoot.getRandomNumber(100) < 6)
        {
            addObject(new RedCell(), 779, Greenfoot.getRandomNumber(360));
        }
        countTime();
        wCount();
    }
     
    /**
     * Count number of White Cells in Bloodstream.
     */
    public int wCount()
    {
        int countWC;
        countWC = getObjects(WhiteCell.class).size();
        return countWC;
         
         
    }// end countWhiteCellsInBloodstream
     
    /**
     * Add Count
     */
    public void addCountWC(int countWC)
    {
        countWC2 = countWC;
        showWCount();
    }
     
    /**
     * Display number of White Cells in Bloodstream.
     */
    private void showWCount()
    {
       showText("White Cell Count: " + countWC2, 663, 45);
    }
         
    /**
     * Add some points to our current score. (May be negative.)
     * If the score falls below 0, game's up.
     */
    public void addScore(int points)
    {
        score = score + points;
        showScore();
        if (score < 0)
        {
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
        }
    }
     
    /**
     * Show our current score on screen.
     */
    private void showScore()
    {
        showText("Score: " + score, 80, 25);
    }
 
    /**
     * Count down the game time and display it. Stop the game
     * with a winning message when time is up.
     */
    private void countTime()
    {
        time--;
        showTime();
        if (time == 0)
        {
            showEndMessage();
            Greenfoot.stop();
        }
    }
 
    /**
     * Show the remaining game time on screen.
     */
    private void showTime()
    {
        showText("Time: " + time, 700, 25);
    }
     
    /**
     * Show the end-of-game message on screen.
     */
    private void showEndMessage()
    {
        showText("Time is up - you win!", 390, 150);
        showText("Your final score: " + score + " points", 390, 170);
    }
     
    /**
     * Prepare the world for the start of the program. In this case: Create
     * a white blood cell and the lining at the edge of the blood stream.
     */
    private void prepare()
    {
        WhiteCell whitecell = new WhiteCell();
        addObject(whitecell, 128, 179);
         
        Lining lining = new Lining();
        addObject(lining, 126, 1);
        Lining lining2 = new Lining();
        addObject(lining2, 342, 5);
        Lining lining3 = new Lining();
        addObject(lining3, 589, 2);
        Lining lining4 = new Lining();
        addObject(lining4, 695, 5);
        Lining lining5 = new Lining();
        addObject(lining5, 114, 359);
        Lining lining6 = new Lining();
        Lining lining7 = new Lining();
        addObject(lining7, 295, 353);
        Lining lining8 = new Lining();
        Lining lining9 = new Lining();
        Lining lining10 = new Lining();
        addObject(lining10, 480, 358);
        Lining lining11 = new Lining();
        addObject(lining11, 596, 359);
        Lining lining12 = new Lining();
        addObject(lining12, 740, 354);
         
        Border border = new Border();
        addObject(border, 0, 180);
        Border border2 = new Border();
        addObject(border2, 770, 180);
    }
}
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * This is a white blood cell. This kind of cell has the job to catch bacteria and
 * remove them from the blood.
 *
 * @author Michael Kölling
 * @version 1.0
 */
public class WhiteCell extends Actor
{
    /**
     *
     */
     
    public void WhiteCell()
    {
    }
     
    /**
     * Act: move up and down when cursor keys are pressed.
     */
    public void act()
    {
        checkKeyPress();
        checkCollision();
    }
     
    /**
     * Check whether a keyboard key has been pressed and react if it has.
     */
    private void checkKeyPress()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-8);
        }
         
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+8);
        }
         
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+4, getY());
        }
         
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-4, getY());
        }
    }
     
    /**
     * Check whether we are touching a bacterium or virus. Remove bacteria.
     * Game over if we hit a virus.
     */
    private void checkCollision()
    {
        if (isTouching(Bacteria.class))
        {
            Greenfoot.playSound("slurp.wav");
            removeTouching(Bacteria.class);
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.addScore(20);
        }
 
        if (isTouching(Virus.class))
        {
            removeTouching(Virus.class);
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.addScore(-100);           
        }
    }
}
danpost danpost

2016/4/29

#
Line 63 gets the white blood cell count, but does nothing with it (does not set it to a variable or use it in a method call). Maybe you need to set 'countWC2' to the returned value and then call the 'showWCount' method so the value can be updated on the screen.
DC97Cobra DC97Cobra

2016/4/29

#
I figured it out with the help of a fellow classmate. See the code below.
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
/**
 * The bloodstream is the setting for our White Blood Cell scenario. It's a place
 * where blood cells, bacteria and viruses float around.
 *
 * @author Michael Kölling
 * @version 1.0
 */
public class Bloodstream extends World
{
    private int countWC;
    // private int countWC2; // Not needed. Removed from code.
    private int score;
    private int time;
      
    /**
     * Constructor: Set up the staring objects.
     */
    public Bloodstream()
    {   
        super(780, 360, 1);
        setPaintOrder(Border.class);
        prepare();
        score = 0;
        time = 2000;
        // countWC = 0; Not needed. Removed from code.
        showScore();
        // showWCount(); // Incorrect Location. Moved to public void act.
        showTime();
    }
  
    /**
     * Create new floating objects at irregular intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Bacteria(), 779, Greenfoot.getRandomNumber(360));
        }
          
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 0);
        }
          
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 359);
        }
          
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Virus(), 779, Greenfoot.getRandomNumber(360));
        }
          
        if (Greenfoot.getRandomNumber(100) < 6)
        {
            addObject(new RedCell(), 779, Greenfoot.getRandomNumber(360));
        }
        countTime();
        wCount();
        showwCount(); // Added to code.
    }
      
    /**
     * Count number of White Cells in Bloodstream.
     */
    public void wCount() // Changed public int wCount() to public void wCount()
    {
        // int countWC; // Note required. Removed from code.
        countWC = getObjects(WhiteCell.class).size();
        // return countWC; // Not required. Removed from code.
          
    }// end countWhiteCellsInBloodstream
      
    // /**
    //  * Add Count
    //  * Not needed. Removed subclass from code.
    //  */
    // public void addCountWC(int countWC)
    // {
    //     countWC2 = countWC;
    //     showWCount();
    // } // end addcountWC
     
    /**
     * Display number of White Cells in Bloodstream.
     */
    private void showWCount()
    {
       showText("White Cell Count: " + countWC, 663, 45); // Changed countWC2 to countWC
    }
          
    /**
     * Add some points to our current score. (May be negative.)
     * If the score falls below 0, game's up.
     */
    public void addScore(int points)
    {
        score = score + points;
        showScore();
        if (score < 0)
        {
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
        }
    }
      
    /**
     * Show our current score on screen.
     */
    private void showScore()
    {
        showText("Score: " + score, 80, 25);
    }
  
    /**
     * Count down the game time and display it. Stop the game
     * with a winning message when time is up.
     */
    private void countTime()
    {
        time--;
        showTime();
        if (time == 0)
        {
            showEndMessage();
            Greenfoot.stop();
        }
    }
  
    /**
     * Show the remaining game time on screen.
     */
    private void showTime()
    {
        showText("Time: " + time, 700, 25);
    }
      
    /**
     * Show the end-of-game message on screen.
     */
    private void showEndMessage()
    {
        showText("Time is up - you win!", 390, 150);
        showText("Your final score: " + score + " points", 390, 170);
    }
      
    /**
     * Prepare the world for the start of the program. In this case: Create
     * a white blood cell and the lining at the edge of the blood stream.
     */
    private void prepare()
    {
        WhiteCell whitecell = new WhiteCell();
        addObject(whitecell, 128, 179);
          
        Lining lining = new Lining();
        addObject(lining, 126, 1);
        Lining lining2 = new Lining();
        addObject(lining2, 342, 5);
        Lining lining3 = new Lining();
        addObject(lining3, 589, 2);
        Lining lining4 = new Lining();
        addObject(lining4, 695, 5);
        Lining lining5 = new Lining();
        addObject(lining5, 114, 359);
        Lining lining6 = new Lining();
        Lining lining7 = new Lining();
        addObject(lining7, 295, 353);
        Lining lining8 = new Lining();
        Lining lining9 = new Lining();
        Lining lining10 = new Lining();
        addObject(lining10, 480, 358);
        Lining lining11 = new Lining();
        addObject(lining11, 596, 359);
        Lining lining12 = new Lining();
        addObject(lining12, 740, 354);
          
        Border border = new Border();
        addObject(border, 0, 180);
        Border border2 = new Border();
        addObject(border2, 770, 180);
    }
And the code that was cleaned up and submitted...
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * The bloodstream is the setting for our White Blood Cell scenario. It's a place
 * where blood cells, bacteria and viruses float around.
 *
 * @author Michael Kölling
 * @version 1.0
 *
 * HW12 (4/28/2015)
 * R01 (20 points) Display the number of white cells
 * R02 (20 points) Display the number of bacteria
 * R03 (20 points) Display the number of red cells
 * R04 (20 points) Display the number of viruses
 */
public class Bloodstream extends World
{
    private int score;
    private int time;
    private int countWC; //User - Stores the White Cell Count. This satisfies R01.
    private int countRC; //User - Stores the Red Cell Count. This satisfies R03.
    private int countV; //User - Stores the Virus Count. This satisfies R04.
    private int countB; //User - Stores the Bacteria Count. This satisfies R02.
     
    /**
     * Constructor: Set up the staring objects.
     */
    public Bloodstream()
    {   
        super(780, 360, 1);
        setPaintOrder(Border.class);
        prepare();
        score = 0;
        time = 2000;
        showScore();
        showTime();
    }
 
    /**
     * Create new floating objects at irregular intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Bacteria(), 779, Greenfoot.getRandomNumber(360));
        }
         
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 0);
        }
         
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 359);
        }
         
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Virus(), 779, Greenfoot.getRandomNumber(360));
        }
         
        if (Greenfoot.getRandomNumber(100) < 6)
        {
            addObject(new RedCell(), 779, Greenfoot.getRandomNumber(360));
        }
        countTime();
        wCount(); //User - White Cell Count. This satisfies R01.
        rCount(); //User - Red Cell Count. This satisfies R03.
        vCount(); //User - Virus Count. This satisfies R04.
        bCount(); //User - Bacteria Count. This satisfies R02.
        showwCount(); //User - Shows the White Cell Count. This satisfies R01.
        showrCount(); //User - Shows the Red Cell Count. This satisfies R03.
        showvCount(); //User - Shows the Virus Count. This satisfies R04.
        showbCount(); //User - Shows the Bacteria Count. This satisfies R02.
    }
     
    /**
     * User - Count White Cells in Bloodstream. This satisfies R01.
     */
    public void wCount()
    {
        countWC = getObjects(WhiteCell.class).size();       
    } // end White Cell Count
     
    /**
     * User - Count Red Cells in Bloodstream. This satisfies R03.
     */
    public void rCount()
    {
        countRC = getObjects(RedCell.class).size();
    } // end Red Cell Count
     
    /**
     * User - Count Viruses in Bloodstream. This satisfies R04.
     */
    public void vCount()
    {
        countV = getObjects(Virus.class).size();
    } // end Virus Count
     
    /**
     * User - Count Bacteria in Bloodstream. This satisfies R02.
     */
    public void bCount()
    {
        countB = getObjects(Bacteria.class).size();
    } // end Bacteria Count
     
    /**
     * User - Show White Cell Count. This satisfies R01.
     */
    private void showwCount()
    {
         showText("White Cell: " + countWC, 700, 50);
    } // end show White Cell Count
     
    /**
     * User - Show Red Cell Count. This satisfies R03.
     */
    private void showrCount()
    {
         showText("Red Cell: " + countRC, 700, 70);
    } // end show Red Cell Count
     
    /**
     * User - Show Virus Count. This satisfies R04.
     */
    private void showvCount()
    {
         showText("Viruses: " + countV, 700, 90);
    } // end show Virus Count
     
    /**
     * User - Show Bacteria Count. This satisfies R02.
     */
    private void showbCount()
    {
         showText("Bacteria: " + countB, 700, 110);
    } // end show Bacteria Count
     
    /**
     * Add some points to our current score. (May be negative.)
     * If the score falls below 0, game's up.
     */
    public void addScore(int points)
    {
        score = score + points;
        showScore();
        if (score < 0)
        {
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
        }
    }
     
    /**
     * Show our current score on screen.
     */
    private void showScore()
    {
        showText("Score: " + score, 80, 25);
    }
 
    /**
     * Count down the game time and display it. Stop the game
     * with a winning message when time is up.
     */
    private void countTime()
    {
        time--;
        showTime();
        if (time == 0)
        {
            showEndMessage();
            Greenfoot.stop();
        }
    }
 
    /**
     * Show the remaining game time on screen.
     */
    private void showTime()
    {
        showText("Time: " + time, 700, 25);
    }
     
    /**
     * Show the end-of-game message on screen.
     */
    private void showEndMessage()
    {
        showText("Time is up - you win!", 390, 150);
        showText("Your final score: " + score + " points", 390, 170);
    }
     
    /**
     * Prepare the world for the start of the program. In this case: Create
     * a white blood cell and the lining at the edge of the blood stream.
     */
    private void prepare()
    {
        WhiteCell whitecell = new WhiteCell();
        addObject(whitecell, 128, 179);
         
        Lining lining = new Lining();
        addObject(lining, 126, 1);
        Lining lining2 = new Lining();
        addObject(lining2, 342, 5);
        Lining lining3 = new Lining();
        addObject(lining3, 589, 2);
        Lining lining4 = new Lining();
        addObject(lining4, 695, 5);
        Lining lining5 = new Lining();
        addObject(lining5, 114, 359);
        Lining lining6 = new Lining();
        Lining lining7 = new Lining();
        addObject(lining7, 295, 353);
        Lining lining8 = new Lining();
        Lining lining9 = new Lining();
        Lining lining10 = new Lining();
        addObject(lining10, 480, 358);
        Lining lining11 = new Lining();
        addObject(lining11, 596, 359);
        Lining lining12 = new Lining();
        addObject(lining12, 740, 354);
         
        Border border = new Border();
        addObject(border, 0, 180);
        Border border2 = new Border();
        addObject(border2, 770, 180);
    }
}
You need to login to post a reply.