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

2020/4/21

How would I display score from another world class?

DaRafster DaRafster

2020/4/21

#
I have a return method in the world-class Galaxy. This return method returns the object score which holds information of the total score. I would like to access the method through the game over world, but how would I do that? Here is what I tried to do in my game over class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends World
{
    GreenfootImage over = new GreenfootImage("Game Over Pop Up.png");
    private Score theScore;
    public GameOver()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 900, 1); 
        prepare();
        
        theScore = new Score();
    }
    
    public void prepare()
    {
        Picture gameOver = new Picture(over, 1, 1);
        addObject(gameOver, getWidth()/2, 350);
        
        TryAgain tryAgain = new TryAgain();
        addObject(tryAgain, getWidth()/2, getHeight()/2 + 30);
        
        Exit exit = new Exit();
        addObject(exit, getWidth()/2, getHeight()/2 + 125);
        
        
        
        // What I attempted 
        
        Galaxy galaxyWorld = (Galaxy) getWorld(); // can't use getWorld() in world class
        Score score = galaxyWorld.getScore(); 
        int totalScore = score.returnTotalScore();
        
        GreenfootImage finalScore = new GreenfootImage("FINAL SCORE: " + totalScore, 20, Color.WHITE, new Color(0,0,0,0));
        getBackground().drawImage(finalScore, getWidth() - 30, getHeight() - 70);
        
    }
    
}
And here is my galaxy class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Galaxy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Galaxy extends World
{
    private GreenfootImage title = new GreenfootImage("Space Rush.png");
    private GreenfootSound chillwave = new GreenfootSound("Background Music.mp3");
    private GreenfootImage bg = getBackground();
    private Score score;
    private Timer timer; 
    private Spacecraft spacecraft;
    private HealthBar healthBar;
    private Picture logo;
    private int asteroidCounter, speedCounter, healthCounter = 0;
    private int randomNumberA;
    public Galaxy()
    {    
        super(1000, 900, 1); 

        logo = new Picture(title,2,2);
        timer = new Timer();
        spacecraft = new Spacecraft();
        score = new Score();
        healthBar = new HealthBar(300, 20, spacecraft);

        prepare();
        Greenfoot.setSpeed(50);
    }

    private void prepare()
    {
        int width = getWidth();
        int height = getHeight();

        addObject(logo, width/2, height/10);                 
        addObject(timer, width/2 - width/3, getHeight()/15);        
        addObject(spacecraft,width/5,height/2);        
        addObject(score, width/2 + width/3, height/15);        
        addObject(healthBar, width/2, getHeight() - height/22);

        drawImage();
        shootInstructions();
    }

    /**
     * Draws and fills in rectangles which are used to signify the boundaries of the game
     */
    private void drawImage()
    {
        int width = getWidth();

        bg.setColor(Color.WHITE);
        bg.drawRect(0, 115, width, 15);
        bg.drawRect(0, 800, width, 15);
        bg.setColor(new Color(83, 64, 34));    
        bg.fillRect(0, 116, width, 14);
        bg.fillRect(0, 801, width, 14);
    }

    /**
     * Displays what keys to press on the screen in order to fire their weapon
     */
    private void shootInstructions()
    {
        GreenfootImage instructions = new GreenfootImage("\"Press 1 to fire Sphere\"", 20, Color.WHITE, new Color(0,0,0,0));
        getBackground().drawImage(instructions, 30, getHeight() - 70);
        GreenfootImage instructions2 = new GreenfootImage("\"Press 2 to fire Magnet\"", 20, Color.WHITE, new Color(0,0,0,0));
        getBackground().drawImage(instructions2, 30, getHeight() - 50);        
        GreenfootImage instructions3 = new GreenfootImage("\"Hold 3 to fire Ray\"", 20, Color.WHITE, new Color(0,0,0,0));
        getBackground().drawImage(instructions3, 42, getHeight() - 30);
    }

    /**
     * Adds score over time, the longer the player survives, the more score they obtain
     */
    public void scoreOverTime()
    {
        if(timer.getTimeElapsed() > 0 && timer.getInitialCount() <= 0)
        {
            score.addScore(1);
        }
    }

    /**
     * Returns score
     */
    public Score getScore()
    {
        return score;
    }

    public Spacecraft getSpacecraft()
    {
        return spacecraft;
    }
    
    /**
     * The more time elapsed, the more often asteroids will spawn
     */
    public void controllingDifficulty()
    {
        int timeElapsed = timer.getTimeElapsed();
        if(timeElapsed >= 0 && timeElapsed < 15) randomNumberA = Greenfoot.getRandomNumber(50); // 1 in 50 chance every act cycle an asteroid will spawn
        else if(timeElapsed >= 15 && timeElapsed < 30)randomNumberA = Greenfoot.getRandomNumber(40); // 1 in 40 chance
        else if (timeElapsed >= 30 && timeElapsed < 45)randomNumberA = Greenfoot.getRandomNumber(35); // 1 in 35 chance
        else if (timeElapsed >- 45 && timeElapsed < 60)randomNumberA = Greenfoot.getRandomNumber(30); // 1 in 30 chance
        else if (timeElapsed >= 60)
        {
            randomNumberA = Greenfoot.getRandomNumber(20); // 1 in 20 chance
            Greenfoot.setSpeed(55); // game gets faster
        }
        else if(timeElapsed >= 90) Greenfoot.setSpeed(60); // EVEN faster
    }
    
    /**
     * Adds asteroids beyond the world's x-axis in a randomized manner
     */
    public void addingAsteroid()
    {
        controllingDifficulty();
        asteroidCounter++; 
        if(randomNumberA < 1 && asteroidCounter > 10 )
        {
            addObject(new Asteroid(), getWidth(), getRandomNumber(160, 760));
            asteroidCounter = 0;
        }
    }

    /**
     * Adds speed powerups beyond world's x-axis in a randomized manner
     */
    public void addingSpeedPowerup()
    {
        speedCounter++;
        if(Greenfoot.getRandomNumber(500) < 1 && speedCounter > 350)
        {
            addObject(new Speed(), getWidth(), getRandomNumber(160, 760));
            speedCounter = 0;
        }
    }

    /**
     * Adds health powerups beyond the world's x-axis in a randomized manner
     */
    public void addingHealthPowerup()
    {
        healthCounter++;
        if(Greenfoot.getRandomNumber(500) < 1 && healthCounter > 500)
        {
            addObject(new Health(), getWidth(), getRandomNumber(160, 760));
            healthCounter = 0;
        }
    }

    /**
     * Method used to get a random number between two integers
     */
    public int getRandomNumber(int start,int end)
    {
        int normal = Greenfoot.getRandomNumber(end-start+1);
        return normal+start;
    }

    /**
     * Plays background music when scenario is ran
     */
    public void started()
    {
        chillwave.setVolume(25);
        chillwave.play();
    }

    /**
     * Stops background music when scenario is stopped
     */
    public void stopped()
    {
        chillwave.stop();
    }

    public void act()
    {
        int timeElapsed = timer.getTimeElapsed();
        if(timeElapsed > 0)
        {
            addingAsteroid();
            addingSpeedPowerup();
            addingHealthPowerup(); 
        }
        scoreOverTime();
    }       
}
Lastly my score class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    private int totalScore = 0;
    
    public Score()
    {
        setImage(new GreenfootImage("SCORE: 0", 25, Color.WHITE, Color.BLACK));
    }
    
    public void addScore(int amount) 
    {
        totalScore += amount;
        setImage(new GreenfootImage("SCORE: " + totalScore, 20, Color.WHITE, Color.BLACK));
    }    
        
    public int returnTotalScore()
    {
        return totalScore;
    }
    
}
So how would I get around this?
danpost danpost

2020/4/21

#
DaRafster wrote...
I have a return method in the world-class Galaxy. This return method returns the object score which holds information of the total score. I would like to access the method through the game over world, but how would I do that?
You wouldn't do it by creating a new unused Score object !! Easiest way is to add a parameter to the GameOver constructor by changing line 13 of GameOver class to:
public GameOver(Score score)
and then changing line 19 to:
theScore = score;
DaRafster DaRafster

2020/4/22

#
danpost wrote...
DaRafster wrote...
I have a return method in the world-class Galaxy. This return method returns the object score which holds information of the total score. I would like to access the method through the game over world, but how would I do that?
You wouldn't do it by creating a new unused Score object !! Easiest way is to add a parameter to the GameOver constructor by changing line 13 of GameOver class to:
public GameOver(Score score)
and then changing line 19 to:
theScore = score;
I'm getting what you mean, and I have implemented it. However when it sets to the new world, it says the final score is "null" when I draw the image on the background. Here is what I did in the gameover world:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends World
{
    GreenfootImage over = new GreenfootImage("Game Over Pop Up.png");
    private Score theScore;
    public GameOver(Score score)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 900, 1); 
        prepare();
        
        theScore = score;
    }
    
    public void prepare()
    {
        Picture gameOver = new Picture(over, 1, 1);
        addObject(gameOver, getWidth()/2, 350);
        
        TryAgain tryAgain = new TryAgain();
        addObject(tryAgain, getWidth()/2, getHeight()/2 + 30);
        
        Exit exit = new Exit();
        addObject(exit, getWidth()/2, getHeight()/2 + 125);
        
        GreenfootImage finalScore = new GreenfootImage("FINAL SCORE: " + theScore, 20, Color.WHITE, new Color(0,0,0,0));
        getBackground().drawImage(finalScore, 300, 300);
        
    }
    
}
when I set a new game over world in spacecraft class, I called the return score method from the galaxy class to obtain the score. Here is what I did in the spacecraft class:
/**
     * Method that decides what the spacecraft does when it's health is below zero
     */
    public void noHealth()
    {
        if(health <= 0)
        {          
            gameover.setVolume(35);
            gameover.play();
            
            Galaxy galaxyWorld = (Galaxy) getWorld();
            Score score = galaxyWorld.getScore();
            GameOver over = new GameOver(score);
            Greenfoot.setWorld(over);
        }
    }
However it still returns null, why is this so?
danpost danpost

2020/4/22

#
Switch lines 17 and 19.
DaRafster DaRafster

2020/4/22

#
danpost wrote...
Switch lines 17 and 19.
Thank you, this prepare method at the bottom has become a habit for me. I really should bring it up to the top!
You need to login to post a reply.