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

2021/1/20

How to make a high score?

jenhasnofriends jenhasnofriends

2021/1/20

#
I want to make the score as well as the highest score of all time a user has (not a leaderboard).
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); 
        Counter.score = 0;
        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 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);
    }
}
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 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...
I want to make the score as well as the highest score of all time a user has (not a leaderboard).
Create a HighScore class. Make use of the UserInfo class and the storage provided by it.
jenhasnofriends jenhasnofriends

2021/1/20

#
I looked online and saw this for the UserInfo class
if (UserInfo.isStorageAvailable()) {
         UserInfo myInfo = UserInfo.getMyInfo();
         if (newScore > myInfo.getScore()) {
             myInfo.setScore(newScore);
             myInfo.store();  // write back to server
         }
     }
But I am very new at coding and need help on what to do.
danpost danpost

2021/1/20

#
jenhasnofriends wrote...
I looked online and saw this for the UserInfo class << Code Omitted >> But I am very new at coding and need help on what to do.
You will use something quite similar. I suggest making use of (by overriding) the addedToWorld method so you can easily remove the actor if storage is not available or the user is not logged in. In current, or some recent, versions of greenfoot, myInfo should be checked for a null value before proceeding with line 3.
jenhasnofriends jenhasnofriends

2021/1/20

#
I tried this but I'm not sure what to do
public class HighScore extends Actor
{
    /**
     * Act - do whatever the HighScore wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public HighScore()
    {
        if (UserInfo.isStorageAvailable()) 
        {
            UserInfo myInfo = UserInfo.getMyInfo();
            if (count > myInfo.getScore()) 
            {
                myInfo.setScore(count);
                myInfo.store();  // write back to server
            }
        }
    }
    public void act() 
    {
        // Add your action code here.
    }    
}
danpost danpost

2021/1/20

#
Change it to:
public class HighScore extends Actor
{
    protected void addedToWorld(World world)
    {
        if (UserInfo.isStorageAvailable())
        {
            UserInfo myInfo = UserInfo.getMyInfo();
            if (myInfo != null)
            {
                if (count > myInfo.getScore())
                {
                    myInfo.setScore(count);
                    myInfo.store();
                }
                String text = "High score: "+myInfo.getScore;
                setImage(new GreenfootImage(text, 50, Color.WHITE, Color.BLACK));
                return;
            }
        }
        world.removeObject(this);
    }
}
jenhasnofriends jenhasnofriends

2021/1/20

#
Lines 10, 12, 15 (cannot find symbol).
danpost danpost

2021/1/20

#
jenhasnofriends wrote...
Lines 10, 12, 15 (cannot find symbol).
Oops ... sorry. Before line 5, insert:
int count = Counter.score;
jenhasnofriends jenhasnofriends

2021/1/20

#
Line 15 still doesn't work (cannot find symbol).
danpost danpost

2021/1/20

#
jenhasnofriends wrote...
Line 15 still doesn't work (cannot find symbol).
Add parenthesis after getScore (like in line 10).
jenhasnofriends jenhasnofriends

2021/1/21

#
How do I call this method in the game over world?
danpost danpost

2021/1/21

#
jenhasnofriends wrote...
How do I call this method in the game over world?
You don't. Just create an instance and add it to the world. Greenfoot will call it.
jenhasnofriends jenhasnofriends

2021/1/21

#
Before I used
Counter counter = new Counter();
How do I create the instance?
addedToWorld(World world)?
danpost danpost

2021/1/21

#
jenhasnofriends wrote...
Before I used
Counter counter = new Counter();
How do I create the instance?
new Counter() creates a Counter instance. new HighScore() will create a HighScore instance. Use:
addObject(new HighScore(), ???, ???);
or
HighScore highScore = new HighScore();
addObject(highScore, ???, ???);
jenhasnofriends jenhasnofriends

2021/1/21

#
Thanks so much for all the help today man. Really helped me with my school project.
You need to login to post a reply.