I have been trying for a couple hours to add a life when the heart is pressed on and removed, not when it reaches the end of the world. Thank you very much.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class Life extends Animals { boolean remove = false ; //creates boolean variable of remove public void act(){ setLocation(getX(), getY() - 5 ); //sets x and speed the heart crosses if (getY() == 0 ) { remove= true ; FrogHitter(); //removes heart when hit } } public int getCount(){ if (remove != true ){ return 0 ; } return 0 ; //heart has no score value, only increases lives } public void playHitSound(){ Greenfoot.playSound( "pop.wav" ); //plays hezrt sound when hit } } |
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 | public class FrogWorld extends World { Counter counter = new Counter( "Score: " ); //creates string to display you cumulative score Counter FrogLife = new Counter( "Frogs Remaining: " ); //Creats counter displaying the amont of frogs left GreenfootSound backgroundMusic = new GreenfootSound( "Greenfoot Song.mp3" ); //sets sound as background music public FrogWorld(){ super ( 600 , 600 , 1 ); //creats screen createWorldObjects(); // calls the method createWorldObjects backgroundMusic.playLoop(); //plays the background music when game is being played } public void act() { scores(); //calls the method of scores } public void countHit(FrogsHit hit){ counter.add(hit.getCount()); //counts the hits of animaks } public void lose(){ if (FrogLife.getValue()== 1 ){ // if you have no lives left addObject( new ScoreLose(counter.getValue()), getWidth()/ 2 , getHeight()/ 2 ); //displays a scoreboard from the ScoreLose class Greenfoot.playSound( "Lose.wav" ); //plays a clip that says "you lose" backgroundMusic.stop(); //ends music when the game ends Greenfoot.stop(); // and stops the greenfoot game } else { //if you have more than a life left FrogLife.subtract( 1 ); //it takes away one life from } } public void win(){ if (FrogLife.getValue()>= 1 && FrogLife.getValue() < 10 ){ // if you have more than 1 life and less than 10 FrogLife.add( 1 ); //you are given an extra life when heart is hit } } private void createWorldObjects(){ addObject( new FrogHunter(), 300 , 300 ); //calls frog hunter and starts baseball bat in middle of scree addObject(counter, 90 , 560 ); //shows the counter of score at places it on screen addObject(FrogLife, 170 , 540 ); // shows "frogs left: " on screen and places in bottom corner FrogLife.add( 5 ); //adds 5 lives to begin the game } public void scores(){ int score = counter.getValue(); // creates variable score which is the value of the counter int num = Greenfoot.getRandomNumber( 100 ); // sets random number between 1-100 int num1 = Greenfoot.getRandomNumber( 100 ); // sets random number between 1-100 int num2 = Greenfoot.getRandomNumber( 200 ); // sets random number between 1-200 int num3 = Greenfoot.getRandomNumber( 750 ); // sets random number between 1-750 if (score > 0 && score <= 250 ){ //whenever score is below or at 250 at above 1 if (num > 1 && num < 6 ){ addObject( new SwimmingFrog(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a frog whenever the random number is between 1-6 } } if (score > 250 && score <= 500 ){ //whenever score is between 250-500 if (num > 1 && num < 7 ){ addObject( new SwimmingFrog(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a frog whenever the random number is between 1-7 } if (num2 == 1 || num2 == 2 ){ addObject( new Fish(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a fish when the random number is equal to 1 or 2 } } if (score > 500 && score <= 2000 ){ //whenever score is between 500 and 2000 if (num > 1 && num < 8 ){ addObject( new SwimmingFrog(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a frog whenever the random number is between 1-8 } if (num2 == 1 || num2 == 2 ){ addObject( new Fish(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a fish when the random number is equal to 1 or 2 } if (num2 == 3 || num2 == 4 ){ addObject( new PolarBear(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a polar bear when the random number is equal to 3 or 4 } } if (score >= 1 && score < 3000 ){ //between 0 and 3000 or anytime game is being played if (num3 == 375 ){ addObject( new Fly(), 600 , Greenfoot.getRandomNumber( 599 )); // adds a fly whenever random number 3 equals 1 } } if (score > 2000 ){ //whenever score is above 2000 if (num > 1 && num < 9 ){ addObject( new SwimmingFrog(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a frog whenever the random number is between 1-9 } if (num2 == 1 || num2 == 2 ){ addObject( new Fish(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a fish when the random number is equal to 1 or 2 } if (num2 == 3 || num2 == 4 ){ addObject( new PolarBear(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a polar bear when the random number is equal to 3 or 4 } } if (score == 499 || score == 999 || score == 1499 ||score == 1999 || score == 2499 ){ addObject( new Life(), Greenfoot.getRandomNumber( 599 ), 600 ); //adds a new heart (for new life) every 500 points } if ((FrogLife.getValue()>= 1 && score >= 3000 )){ //if you have a life and a score above 3000 addObject( new ScoreWin(counter.getValue()), getWidth()/ 2 , getHeight()/ 2 ); //displays score board from the score win class Greenfoot.playSound( "Win.wav" ); //plays a winnner sound backgroundMusic.stop(); //ends music when the game ends Greenfoot.stop(); //stops the game } } } |