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

2022/11/19

How can I show final Score in another world?

Bob_Rafael Bob_Rafael

2022/11/19

#
I am programming a game and I want to show the final score of the game but in a different world, I am not sure how to do it..
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
{
    private static final String bgImageName = "space1.jpg";
    private static final int picHeight = (new GreenfootImage(bgImageName)).getHeight();
  
    private GreenfootImage bgImage, bgBase;
    private int scrollPosition = 0;
    
    HealthBar healthbar = new HealthBar();
    
    int enemyCount = 0;
    int enemy2Count = 0;
    int enemy3Count = 0;
    int healthCount = 0;
    int projectile2Spawn = 0;
    Counter counter = new Counter();
    GreenfootSound bgm = new GreenfootSound("bgm1.mp3");
    /**
     * 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);
        
        setBackground(bgImageName);
        bgImage = new GreenfootImage(getBackground());
        bgBase = new GreenfootImage(getHeight(), picHeight);

        prepare();
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    public Counter getCounter()
    {
        return counter;
    }
    
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
    
    public void act(){   
        paint(scrollPosition);
        
        if(Greenfoot.mouseClicked(this))
        {
            bgm.stop();
        }
        else if(!bgm.isPlaying())
        {
            bgm.setVolume(15);
            bgm.playLoop();
        }
        
        spawnEnemy1();
        spawnEnemy2();
        spawnEnemy3();
        
        spawnHealth();
        spawnProjectile2();
            
    }
    
    private void paint(int position)
    {
        int scrollAmt = 5; 
        GreenfootImage bg = new GreenfootImage(getBackground());
        getBackground().drawImage(bg, 0, scrollAmt); // scroll image down
        getBackground().drawImage(bg, 0, scrollAmt-getHeight());
    }
    
     public void spawnProjectile2()
    {
        projectile2Spawn++;
        if(projectile2Spawn>2000)
        {
            addObject(new Projectile2(), Greenfoot.getRandomNumber(600),0);
            projectile2Spawn = 0;
        }
        
        
    }
    
    public void spawnHealth()
    {
        healthCount++;
        if(healthCount>1000)
        {
            addObject(new HealthUp(), Greenfoot.getRandomNumber(600),0);
            healthCount = 0;
        }
    }
    
    
        
    public void spawnEnemy1(){
        enemyCount++;
        if(enemyCount>80)
        {
            addObject(new Enemy1(), Greenfoot.getRandomNumber(600),0);
            enemyCount = 0;
        }
        
    }
    
    public void spawnEnemy2(){
        if(counter.score >= 50)
        {
            enemy2Count++;
            if(enemy2Count>200)
            {
                addObject(new Enemy2(), Greenfoot.getRandomNumber(600),0);
                enemy2Count = 0;
                
            }
        }
        
    }
   
    public void spawnEnemy3(){
        if(counter.score >=100)
        {
            enemy3Count++;
            if(enemy3Count>350)
            {
                addObject(new Enemy3(), Greenfoot.getRandomNumber(600),0);
                enemy3Count =0;
            }
        }
        
    }
    //spawn enemy3
    
    private void prepare()
    {
        Player player = new Player();
        addObject(player,241,343);
        player.setLocation(295,372);

        addObject(counter,25,25);
        counter.setLocation(506,379);
        
        addObject(healthbar, 88, 378);
    }
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy1 extends Enemies
{
    /**
     * Act - do whatever the Enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int enemyHealth = 2;
    public Enemy1()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth()-2, image.getHeight()-2);
        setImage(image);
        setRotation(0);
    }
    public void act()
    {
        // Add your action code here.
        EnemyDirection();
        hitByProjectile();
    }
    
    public void EnemyDirection()
    {
        setLocation(getX(),getY()+1);
        if(getY()==70)
        {
            enemyFire();
        }
    }
    
    public void removeEnemy(){
        if(getY()==399){
            getWorld().removeObject(this);
        }
    }
    
    public void hitByProjectile()
    {
        Actor playerProjectile = getOneIntersectingObject(playerProjectile.class);
        Actor playerProjectile2 = getOneIntersectingObject(playerProjectile2.class);
        if(playerProjectile != null)
        {
            getWorld().removeObject(playerProjectile);
            enemyHealth--;
            
        }else if(playerProjectile2 != null)
        {
            getWorld().removeObject(playerProjectile2);
            enemyHealth = enemyHealth-2;
        }
        if(enemyHealth == 0)
        {
            getScoreCounter();
            getWorld().removeObject(this);
            
        }
        else if(getY()==399){
            World world  = getWorld();
            MyWorld myWorld = (MyWorld)world;
            HealthBar healthbar = myWorld.getHealthBar();
            healthbar.loseHealth();
            
            getWorld().removeObject(this);
        }
    }
    
    public void getScoreCounter()
    {
        World world = getWorld();
        MyWorld myWorld = (MyWorld)world;
        Counter counter =myWorld.getCounter();
        counter.addScore();
    }
    
    public void enemyFire()
    {
        getWorld().addObject(new enemyProjectile(),getX(),getY()+5);
    }
}


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 Counter extends Actor { int score = 0; String str = "Score: "; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Counter(){ setImage(new GreenfootImage(str+ score,30,Color.BLUE,null)); } public void act() { // Add your action code here. setImage(new GreenfootImage(str+ score,30,Color.BLUE,null)); } public void addScore() { score++; } }
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
{

    /**
     * Constructor for objects of class GameOver.
     * 
     */
    public GameOver()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        loseImg loseImg = new loseImg();
        addObject(loseImg,289,189);
    }
}
Spock47 Spock47

2022/11/20

#
You can pass the score directly to the constructor of GameOver:
// in MyWorld class when the game ends:
Greenfoot.setWorld(GameOver(counter));

// GameOver world (only lines 7 and 11 are changed from your version)
public class GameOver extends World
{
    public GameOver(final Counter score)
    {    
        super(600, 400, 1);
        prepare();
        addObject(score, 200, 100);
    }
 
    private void prepare()
    {
        loseImg loseImg = new loseImg();
        addObject(loseImg,289,189);
    }
}
Live long and prosper, Spock47
Bob_Rafael Bob_Rafael

2022/11/20

#
Nice, it works thank you
You need to login to post a reply.