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

2021/1/20

Count through multiple worlds?

jenhasnofriends jenhasnofriends

2021/1/20

#
I need the count to reset when I click play again.
public class FishWorld extends World
{
    /**
     * Constructor for objects of class FlappyWorld.
     * 
     */
    Counter counter = new Counter();
    int count = 0;
    private boolean gameStart = false;
    GreenfootSound pointSound = new GreenfootSound("sfx_point.wav");
    public FishWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1, false); 
        prepare();
    }
    public Counter getCounter()
    {
        return counter;
    }
    public void act()
    {
        count++;
        addSeaweed();
        addScore();
        youWin();
        while(!gameStart)
        {
            if(Greenfoot.isKeyDown("enter"))
            {
                gameStart = true;
            }
        }
    }
    public void youWin()
    {
        if(counter.score == 20)
        {
            Greenfoot.setWorld(new YouWinScreen());
        }
    }
    public void addSeaweed()
    {
        if(count % 100 == 0)
        {
            int randomNum = Greenfoot.getRandomNumber(6);
            addObject(new BottomSeaweed(), getWidth()-1, 400 + 50 * randomNum);
            addObject(new TopSeaweed(), getWidth()-1, -200 + 50 * randomNum);
        }
    }
    public void addScore()
    {
        if(timeToScore())
        {
            counter.addScore(1);
        }
    }
    public boolean timeToScore()
    {
        if(count == 240)
        {
            pointSound.play();
            return true;
        }
        if (count > 301 && (count + 60) % 100 == 0)
        {
            pointSound.play();
            return true;
        }
        else
        {
            return false;
        }
    }
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        setPaintOrder(Counter.class, Fish.class);
        Ground ground = new Ground();
        addObject(ground,165,546);
        Ground ground2 = new Ground();
        addObject(ground2,500,546);
        Ground ground3 = new Ground();
        addObject(ground3,746,555);
        Ground ground4 = new Ground();
        addObject(ground4,716,546);
        Fish fish = new Fish();
        addObject(fish,117,131);
        Enter enter = new Enter();
        addObject(enter,400,250);
        addObject(counter,90,30);
    }
}
public class Counter extends AllObjects
{
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static int score;
    public Counter()
    {
        setImage(new GreenfootImage("Score: " + score, 50, Color.WHITE, Color.BLACK));
    }
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + score, 50, Color.WHITE, Color.BLACK));
    } 
    public void addScore(int scoreUp)
    {
        score =+ score + scoreUp;
    }
}
public class GameOverScreen extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class GameOverScreen.
     * 
     */
    public GameOverScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 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()
    {
        TitleButton titleButton = new TitleButton();
        addObject(titleButton,198,225);
        PlayAgainButton playAgainButton = new PlayAgainButton();
        addObject(playAgainButton,198,375);
        ExitButtonL exitButtonL = new ExitButtonL();
        addObject(exitButtonL,198,525);
        Counter counter = new Counter();
        addObject(counter,580,210);
    }
}
danpost danpost

2021/1/20

#
And the PlayAgainButton codes?
jenhasnofriends jenhasnofriends

2021/1/20

#
public class PlayAgainButton extends AllObjects
{
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            FishWorld gameWorld = new FishWorld();
            Greenfoot.setWorld(gameWorld);
        }
    }    
}
public class ExitButtonL extends AllObjects
{
    /**
     * Act - do whatever the LExitButtom wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mousePressed(this))
        {
            System.exit(0);
        }
    }    
}
danpost danpost

2021/1/20

#
jenhasnofriends wrote...
<< Code Omitted >>
I had to see what you were doing to start a new game. Inssert after "super" call:
Counter.score = 0;
jenhasnofriends jenhasnofriends

2021/1/20

#
Is there anyway to not have the last score be there before 0?
danpost danpost

2021/1/20

#
jenhasnofriends wrote...
Is there anyway to not have the last score be there before 0?
In FishWorld above, change line 7 to:
Counter counter;
and before line 94, insert this:
counter = new Counter();
jenhasnofriends jenhasnofriends

2021/1/20

#
Thank you!
You need to login to post a reply.