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!
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);
    }
}
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.
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...
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.