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

2019/8/26

how to save counter (score) on the other world

joejoe joejoe

2019/8/26

#
i have a counter or score in my "background" world but i want it also to save in my "game over" world so that it tells the score the code for the zombie:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class zombie extends Actor
{
    public int speed = 1;
    public zombie()
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/6;
        int myNewWidth = (int)myImage.getWidth()/6;
        myImage.scale(myNewWidth, myNewHeight);

    }
    
    /**
     * Act - do whatever the zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        chaseSurvivor();
        die();
        
    }    
    
    public void chaseSurvivor()
    {
        turnTowards (player.playerX, player.playerY);
        move(speed);
        eat();
    }
    
    public void die()
    {
        Actor bullet = getOneIntersectingObject(Bullet.class);
        
        if(bullet!=null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(bullet);
            Background background = (Background)myWorld;
            Counter counter = background.getCounter();
            counter.addScore();
            myWorld.removeObject(this); 
            
            
        }
        
       
    }
    
    public void eat()
    { 
        Actor player;
        player = getOneObjectAtOffset(0, 0, player.class);
        if (player != null)
        { 
         World myWorld = getWorld();
         GameOver gameOver = new GameOver();
         Greenfoot.setWorld(gameOver);
         myWorld.removeObject(player);
         
        }
        
    } 
    
    
    
}
code for backgorund:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

   
    private int spawnTimer;
   
 
    private void checkForSpawning() // call from act method
   {
    spawnTimer = (spawnTimer+1)%50; // repeat every 10 seconds (about)
     if (spawnTimer == 0) // at each timer reset
    {
        addObject(new zombieSpawner (), 400,400 );
        addObject(new zombieSpawnerSide (), 400,400 );
    }
   }  
   
    Counter counter = new Counter();
    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    public Counter getCounter()
    {
        return counter;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(counter, 67, 12);
        zombie zombie = new zombie();
        addObject(zombie,750,194);
        removeObject(zombie);
        zombieSpawner zombieSpawner = new zombieSpawner();
        addObject(zombieSpawner,797,595);

        player player = new player();
        addObject(player,437,331);
        player.setLocation(388,546);
        zombieSpawnerSide zombieSpawnerSide = new zombieSpawnerSide();
        addObject(zombieSpawnerSide,796,3);
    }
}
and the code for the game over:
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
{

    private boolean enterDown;
    Counter counter = new Counter();
    /**
     * Constructor for objects of class GameOver.
     * 
     */
    public GameOver()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    public Counter getCounter()
    {
        return counter;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(counter, 403, 383);
        mainmenu mainmenu = new mainmenu();
        addObject(mainmenu,404,427);
        mainmenu.setLocation(425,434);
        GameOverText gameOverText = new GameOverText();
        addObject(gameOverText,406,242);
        mainmenu.setLocation(513,437);
        mainmenu.setLocation(515,436);
        mainmenu.setLocation(515,436);
        mainmenu.setLocation(390,428);
        mainmenu.setLocation(395,365);
        mainmenu.setLocation(402,365);
        mainmenu.setLocation(423,368);
        mainmenu.setLocation(405,350);
        mainmenu.setLocation(410,461);
    }

    public void act()
    {
     if (!enterDown && (Greenfoot.isKeyDown("q")))
         {
         enterDown = true;
         Greenfoot.setWorld(new title());
         }
     if (enterDown && !(Greenfoot.isKeyDown("q")))
     {  
        enterDown = false;
     }
     
    }
}
joejoe joejoe

2019/8/26

#
i placed the code of the zombie instead of the bullet because if the zombie hits the player, then it would go to "game Over" world
joejoe joejoe

2019/8/26

#
just for clariffication
danpost danpost

2019/8/26

#
joejoe wrote...
i have a counter or score in my "background" world but i want it also to save in my "game over" world so that it tells the score << Code Omitted >>
One way is just by adding the counter into the new GameOver world:
gameOver.addObject(((Background)getWorld()).getCounter(), 400, 60);
joejoe joejoe

2019/8/26

#
where shall i specifically put it? could you like type where shall i put it
joejoe joejoe

2019/8/26

#
i am very very new to this programming
joejoe joejoe

2019/8/26

#
ok thanks i figured it all out by myself thank you so much glad there are some people who has efforts in replying to us new people thanks again :)
You need to login to post a reply.