I want to make the score as well as the highest score of all time a user has (not a leaderboard).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 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 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 | public class PlayAgainButton extends AllObjects { public void act() { if (Greenfoot.mouseClicked( this )) { FishWorld gameWorld = new FishWorld(); Greenfoot.setWorld(gameWorld); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 ); } } } |