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

2016/6/16

Getting the score value from my world called MyWorld to my Game Over World

shivzzz123 shivzzz123

2016/6/16

#
// imports needed tools
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;

public class MyWorld extends World
{
   
    // declaring instance variables
    private int xS;
    private int yS;
    private int timer = 0; 
    private int timerTwo = 0;
    // declared my Spaceship object
    private Spaceship mainCharacter; 
    // calls the actor Score, HealthBar and Counter
    // gives it a name and creates a new object
    Score score = new Score();
    HealthBar healthBar = new HealthBar();
    Counter counter = new Counter();
    // MyWorld's constructor.
    // a method that creates the instance of my class.
    public MyWorld()
    {    
        // creates my map in deminensons below.
        super(800, 600, 1);
        // intialized my Spaceship object 
        mainCharacter = new Spaceship ();
        // adds my main character, score counter and my healthbar at coridinates below 
        addObject (mainCharacter, 400, 570);
        addObject(score, 60, 40);
        addObject(healthBar, 40, 350);
        addObject(counter, 63, 60);

    }
    // creates a method called act
    public void act()
    {
        // add one to the timerTwo that has as value of 0 
        timerTwo++;
        // use the method called Asteroids, HealthPowerUp and SpeedBoost
        Asteroids();
        HealthPowerUp();
        SpeedBoost();
        // if the timer is greater than zero it subtracts the timer
        if (timer>0)timer--;
        // if the timer is equal to zero and the space button is pressed then it will call the 
        // shoot method and it will set the timer to 40
        // this allows a delay in bullets when your shooting with the space button with 40 acts
        // if the timer is equal to zero and the space button is pressed then
        if (timer==0 &&Greenfoot.isKeyDown("space"))
        {
            // call the shoot method
            shoot();
            // timer set to 40 acts
            timer = 40;
        }
        // if the u letter on the keyboard is pressed then 
        if(Greenfoot.isKeyDown("u"))
        {
            // it will change the world to LevelTwo
            Greenfoot.setWorld (new LevelTwo());
        }
        // if the i letter on the keyboard is pressed then 
        if(Greenfoot.isKeyDown("i"))
        {
            // it will change the world to LevelThree
            Greenfoot.setWorld (new LevelThree());
        }
        // if the o letter on the keyboard is pressed then 
        if(Greenfoot.isKeyDown("o"))
        {
            // it will change the world to LevelFour
            Greenfoot.setWorld (new LevelFour());
        }
        // if the p button is pressed then
        if (Greenfoot.isKeyDown("P"))

        {
            // it stops greenfoot
            Greenfoot.stop();
            // it will change the world to Pause
            Greenfoot.setWorld (new Pause());
        }
        // if the timerTwo is equal to 3000 acts then 
        if (timerTwo == 3000 )
        {
            // it will change the world to LevelTwoStartScreen
            Greenfoot.setWorld (new LevelTwoStartScreen());

        }

    } 

    public HealthBar getHealthBar()
    {
        return healthBar;

    }

    public HealthBar getHealthPowerUp()
    {
        return healthBar;

    }
   

    public Score getScore()
    {

        return score;

    }
    // Created a method called shoot
    public void shoot()
    {

        // declared my Bullet object, called it b and intialized it.
        Bullets b = new Bullets();
        // adds object b with the x coridinate of where my mainCharacter object is and a the y coridnate of where my main character object is
        // subtracted by the height of the image divided by two.
        addObject(b, mainCharacter.getX(), mainCharacter.getY()-((mainCharacter.getImage().getHeight())/2));
        // my main character is rotated so it faces upwards
        // it gets the rotation of my main character and assigns the same rotation to my bullet object called b, so it also shoots upwards
        b.setRotation (mainCharacter.getRotation());
        // plays the shooting sound effect everytime the method is called
        Greenfoot.playSound("shootingsoundeffect.wav");

    }
    // creates a method called SpeedBoost
    public void SpeedBoost()
    {
        // if the random number generated by greenfoot from 1 to 3999 is less then 5 then
        if (Greenfoot.getRandomNumber(4000) < 5)
        {
            // add a new speedBoost object with a random y cordinate of 1 to 599 and a x coridnate of 40.
            addObject (new SpeedBoost(),40, Greenfoot.getRandomNumber(600));
        }
    }
    // creates a method called HealthPowerUp
    public void HealthPowerUp ()

    {
        // if the random number generated by greenfoot from 1 to 3999 is less then 1 then
        if (Greenfoot.getRandomNumber(4000) < 1)
        {
            // add a new HealthPowerUp object with a random x cordinate of 1 to 799 and a y cordinate of 40.
            addObject (new HealthPowerUp(), Greenfoot.getRandomNumber(800), 40);
        }
    }
    // creates a method called Asteroids
    public void Asteroids()
    {
        // if the random number generated by greenfoot from 1 to 799 is less then 5 then 
        if (Greenfoot.getRandomNumber(800) < 5)
        {
            // add a new Asteroid object with a random x coridnate of 1 to 799 and a y coridnate of 40.
            addObject (new Asteroids(), Greenfoot.getRandomNumber(800), 40);
        }
     
    }

}
SPower SPower

2016/6/16

#
Because you want to pass it into a different world, you could put it in the constructor of the game over world:
public GameOverWorld(int score)
{
    // stuff
}
You need to login to post a reply.