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

2016/8/18

Help to show score at end of the game

BEASTYLAD BEASTYLAD

2016/8/18

#
Hi all and thanks in advance!! I am altering a very simple asteroid game ( Think the original code was from a rescource website) to be able to then teach children how to make it. I am looking to show the final score at the end of the game when the game has finihsed. I cannot seem to put the score in the setImage at the end even though it does increase throughout the game. Any help please. Timer which when it hits 20 i want it to do the above:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
            import java.awt.Color;
             
            public class Timer extends Counter
            {
                /**
                 * Act - do whatever the Timer wants to do. This method is called whenever
                 * the 'Act' or 'Run' button gets pressed in the environment.
                 */
                        private int time = 20;
                        private int count = 65;
                        public void act() 
                {
                    // Add your action code here.
                    if(time == 0)
                    {
                        display2();
                        Greenfoot.stop();
                        
                        return;
                    }
                     
                    if(counter())
                    {
                        time--;
                        count = 65;
                    }
                    display();
                   
                    
                }    
                 
                        private boolean counter()
                {
                    if(count > 0)
                    {
                        count--;
                    }
                    return count == 0;
                }
                 
                private void display()
                {
                    setImage(new GreenfootImage("Game Ends : " + time, 30, Color.WHITE, Color.BLACK));
                }
                
                public void display2()
                {
                    
                         setImage(new GreenfootImage("Game Over : " + "" , 50, Color.WHITE, Color.BLACK));
                    }
                    
                    
                public void setTime()
                {
                    time = 20;
                }
                 
                public boolean isTimeUp()
                {
                    return time == 0;
                }
                 
            }
Here is my counter for the score:
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
{
    /**
     * 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() 
    {
        // Add your action code here.
    }   
    
    public Counter()
    
    {
        setImage(new GreenfootImage("0",20,Color.WHITE,Color.BLACK));
    }
    public int totalScore = 0;
    public int finalScore;
    public void countScore(int amount)
    {
        totalScore += amount;
        setImage(new GreenfootImage("" + totalScore,20,Color.WHITE,Color.BLACK));
       
    }
    
    public void finalScore( )
    {
        
        
        setImage(new GreenfootImage("" + finalScore,20,Color.WHITE,Color.BLACK));

    }
}
and the score is actually increased by the number in hitAnAsteroid(); below:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Shot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shot extends Actor
{
    private Rocket myShip;
    
    public Shot(Rocket myShip)
    {
        this.myShip = myShip;
    }
    
    void hitAnAsteroid()
        {
        Space spaceWorld = (Space) getWorld(); //get a reference to the world
        Counter counter = spaceWorld.getCounter();//get a reference to the counter
        counter.countScore(5);
    }
    void hitAnAsteroid2()
        {
        Space spaceWorld = (Space) getWorld(); //get a reference to the world
        Counter counter = spaceWorld.getCounter();//get a reference to the counter
        counter.countScore(1);
    }
    
    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int yPosition = getY();
  
        if(yPosition >0)
        {        
        yPosition = yPosition - 5;
        setLocation(getX(),yPosition);
        Actor rock = getOneIntersectingObject(Rock.class);
        Actor pretendRock = getOneIntersectingObject(PretnedRock.class);
        if(rock != null)
        {
            hitAnAsteroid();
            getWorld().removeObject(rock);
            getWorld().removeObject(this);
        
        }
        
        if(pretendRock != null)
        {
            hitAnAsteroid2();
            getWorld().removeObject(pretendRock);
            getWorld().removeObject(this);
        
        }
        
        }
        else
        {
            getWorld().removeObject(this);
            
        }    
    
}

}

danpost danpost

2016/8/18

#
There is no code given that shows where the value of 'finalScore' is updated in any way or where the 'finalScore' method is called from. However, it appears that the image of the final score is no different than that of the image of the Counter object when displaying the 'totalScore' value.
BEASTYLAD BEASTYLAD

2016/8/19

#
Ok so I have removed the finalscore method and I have replaced it with :
  public Counter()
    {
    setImage( new GreenfootImage("0",50,Color.RED,Color.BLACK));
    
    }
so that it dispalys 0 when I start the game. my world code then looks like:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
     private Counter theCounter;
     
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,295,358);
        theCounter = new Counter();
        addObject(theCounter,50,300);
        Timer theTimer = new Timer();
        addObject(theTimer,150,350);
    }
    
    public Counter getCounter()
    {
        return theCounter;
    }
    
    public void act()
    {
        
    if(Greenfoot.getRandomNumber(100)<3)   
       { 
        addObject(new Asteroid(),0,10);
    }
    
    
       
}
}
so where exactly is my score beign updated. Sorry thought I was getting the hang of this but it seems like im not. Thanks
danpost danpost

2016/8/19

#
BEASTYLAD wrote...
Ok so I have removed the finalscore method and I have replaced it with : < Code Omitted > so that it dispalys 0 when I start the game.
You already had that in your code (see lines 20 through 24 of the Counter class given above) The color of the text is different, however (red instead of white). If you want to display the final score in red, you do not want to use a constructor construct, but a method to call on the object:
// change the first line to:
public void finalDisplay()
and call that method on the 'theCounter' object of the Space class when the game ends.
my world code then looks like: < Code Omitted > so where exactly is my score beign updated.
Lines 22 and 28 of the Shot class call the 'countScore' method of the Counter class on the Counter object held by the 'theCounter' field of the Space class.
BEASTYLAD BEASTYLAD

2016/8/19

#
Ok so as i want to now display what the final score was when the timer reaches zero in the display2() in the timer:
// Add your action code here.
                    if(time == 0)
                    {
                        display2();
                        
                        Greenfoot.stop();
                        
                        return;
                    }


public void display2()
                {
                    setImage(new GreenfootImage("Game Over : " + "This is where i want my final score to show", 50, Color.WHITE, Color.BLACK));
                }
how do I do this from the score ??
danpost danpost

2016/8/19

#
BEASTYLAD wrote...
Ok so as i want to now display what the final score was when the timer reaches zero in the display2() in the timer: < Code Omitted > how do I do this from the score ??
Well, you would get a reference to the Counter object the same way you did in the Shot class, with:
Space spaceWorld = (Space) getWorld(); //get a reference to the world
Counter counter = spaceWorld.getCounter();//get a reference to the counter
Then, you can use
counter.totalScore
where you want the final score to show in the 'display2' method.
BEASTYLAD BEASTYLAD

2016/8/19

#
Thank you very much, i understand now when i think of it in the same way that the shot was complete. Thank you again.
You need to login to post a reply.