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

2014/3/21

Infinite Scoreboards

miniclausen1995 miniclausen1995

2014/3/21

#
Hey I am having problems with my scoreboard. When the game ends, it runs the method "gameOver()", but the program wont stop. This is the game over method
    public void gameOver()
    {
        
        addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.stop();
    }
Here is the scoreboard
    import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)  
    import java.awt.Color;  
    import java.awt.Font;  
    import java.util.Calendar;  
      
    /** 
     * The ScoreBoard is used to display results on the screen. It can display some 
     * text and several numbers. 
     *  
     * @author M Kolling 
     * @version 1.0 
     */  
    public class ScoreBoard extends Actor  
    {  
        public static final float FONT_SIZE = 48.0f;  
        public static final int WIDTH = 400;  
        public static final int HEIGHT = 300;  
          
        /** 
         * Create a score board with dummy result for testing. 
         */  
        public ScoreBoard()  
        {  
            
            this(100);  
            
        }  
      
        /** 
         * Create a score board for the final result. 
         */  
        public ScoreBoard(int score)  
        {  
            
            makeImage("Game Over", "Score: ", score);  
            
        }  
      
        /** 
         * Make the score board image. 
         */  
        private void makeImage(String title, String prefix, int score)  
        {
            GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);  
            
            image.setColor(new Color(255,255,255, 128));  
            image.fillRect(0, 0, WIDTH, HEIGHT);  
            image.setColor(new Color(0, 0, 0, 128));  
            image.fillRect(5, 5, WIDTH-10, HEIGHT-10);  
            Font font = image.getFont();  
            font = font.deriveFont(FONT_SIZE);  
            image.setFont(font);  
            image.setColor(Color.WHITE);  
            image.drawString(title, 60, 100);  
            image.drawString(prefix + score, 60, 200);  
            setImage(image);  
            
        }  
    }  
How can I make my game stop?
danpost danpost

2014/3/21

#
What class is the gameOver method in and what is the code of the method that calls it? Also, what class is the method that calls it in? When giving classes, give what they extends also -- through to Actor, World or other.
miniclausen1995 miniclausen1995

2014/3/21

#
The gameOver method is in the world class
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List; 

/**
 * A simple world with flying birds.
 * 
 * @author mik
 * @version 2008-07-11
 */

public class TestWorld extends World

{
    Counter counter = new Counter("Score: ");
    Gun gun = new Gun();
    static public int BPM = 4;
    static public int BPM2 = BPM / 4;
    /**
     * Constructor for objects of class TestWorld.
     */
    public TestWorld()
    {    
        
        super(850, 500, 1);
        
        //Sværhedsgrader
        Difficulty d = new Difficulty();
        Hard h = new Hard();
        Medium med = new Medium();
        Easy e = new Easy();
        
        //sværhedsgrader
        addObject(d, 335, 480);
        addObject(h, 500, 480);
        addObject(med, 450, 480);
        addObject(e, 400, 480);
        
        setPaintOrder( Easy.class, Medium.class, Hard.class, ScoreBoard.class, Gun.class, Target.class, Counter.class, Bird2.class, Bird.class);

        populate();
        
        prepare();
    }

    public void gameOver()
    {
        
        addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.stop();
    }

    public void countBird()
    {
        counter.add(1);
    }

    public void countBird2()
    {
        counter.add(20);
    }
    public void populate ()
    {
        addObject (counter, 750, 480);
        
    }

    public void act()
    {
        
        if (Greenfoot.getRandomNumber(100) < BPM) {  // 4% chance
            createNewBird();
        }
        if (Greenfoot.getRandomNumber(100) < BPM2) {  // 4% chance
            createNewBird2();
        }
    }

    private void createNewBird()
    {
        addObject (new Bird(), 830, Greenfoot.getRandomNumber(400)+50);
    }

    private void createNewBird2()
    {
        addObject (new Bird2(), 830, Greenfoot.getRandomNumber(400)+50);
    }

    List Target = getObjects(Target.class); 
    {
        if (Target.size() == 0) addObject(new Target(), 425, 250);
        
    }

    List Gun = getObjects(Gun.class); 
    {
        if (Gun.size() == 0) addObject(new Gun(), 50, 300);
    }
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
    }
}

The object, that runs the method is the bird class
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;  
import java.util.List; 
import java.awt.Font;  
import java.util.Calendar; 
/**
 * Write a description of class Bee here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bird extends AnimatedActor
{
    
    Counter counter = new Counter("Score: ");
    int i = Greenfoot.getRandomNumber(8);
    public Bird()
    {
        super("bird", ".png", 10);
    }
    
    /**
     * Fly to the left. When we hit the edge, disappear.
     */
    public void act() 
    {
        
        if (i < 5)
        {
            i = 5;
        }
        
        super.act();  // this takes care of the image animation
        move(-i);
        if (atWorldEdge()) {
            ((TestWorld) getWorld()).gameOver();
            getWorld().removeObject(this);
            
        }
        
    }

    
    public void pop() 
    {
        Greenfoot.playSound("pop.wav");
        Blood blood = new Blood();
        getWorld().addObject(blood, getX(), getY());
        for(int i = 0; i > 0; i++)  
            {  
                getWorld().removeObject(blood);  
                //add something?  
            }
        getWorld().removeObject(this);
    }
    }
    

danpost danpost

2014/3/21

#
Somewhere around line 36 of the Bird class (in the 'atWorldEdge' 'if' block), add the following line:
System.out.println("At edge of world");
Compile and run till you believe the game should be over (the bird hits the left edge). Then, report whether the Terminal window appeared with anything or not. If it does not appear, please post the code for the 'atWorldEdge' method. Oh, and is this a scrolling world?
miniclausen1995 miniclausen1995

2014/3/21

#
The terminal window printed several times (one for each bird that passed) As far as I know, the world is not scrolling. The objects are moving, while the world is standing still
danpost danpost

2014/3/21

#
Ok, move the 'System.out.println' statement to the 'gameOver' method of the TestWorld class (line 47), changing the text to " 'gameOver' method executing"
miniclausen1995 miniclausen1995

2014/3/21

#
Alright, done. The text in put into the terminal window, but the game is still running
danpost danpost

2014/3/21

#
Would you mind uploading the scenario, please. There must be something somewhere that is causing this for you. But, it would be hard to find without a first-hand look at it. Make sure, if you do decide to upload it, to check the checkbox to 'Publish source code'.
miniclausen1995 miniclausen1995

2014/3/21

#
Here u go http://www.greenfoot.org/scenarios/11145
danpost danpost

2014/3/22

#
Sorry for the late reply. My internet service was down for a while (over 9 hours). I will look at it now.
danpost danpost

2014/3/22

#
It does not help that you have 'Greenfoot.start();' in the 'act' method of your Medium class; and your Hard class; and your Easy class.
miniclausen1995 miniclausen1995

2014/3/22

#
Oh I see. Thank you so much
You need to login to post a reply.