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);
}
}
}
