I added hearts in my world to represent the number of lives Mario has. Every time mario dies, a heart is subtracted from the game. But I am not sure why my heart isn't being removed when Mario is no longer alive.
Here is the life code
This is the World parent class:
World Child class
And thats mario
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Life here. * * @author (your name) * @version (a version number or a date) */ public class Life extends Actor { public void act() { setLook(); } public void setLook() { GreenfootImage life = new GreenfootImage(getImage()); setImage(life); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The parent class of worlds * * @soumya.__.khanna (Soumya Khanna) * @I'llLookItUp (Jun 17th, 2020) */ public class Worlds extends World { public int width = getWidth(); //Width of the world --Accessed in children classes public SwimmingMario mario = new SwimmingMario(); //Number of hearts based on the life of Mari public Life life0; public Life life1; public Life life2; public Life life3; public Life life4; public Score score; public void act() { checkLife(mario.getLives()); } public Worlds() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 2200 , 576 , 1 , false ); } public void lifeCounter( int x, int y) { life0 = new Life(); life1 = new Life(); life2 = new Life(); life3 = new Life(); life4 = new Life(); addObject(life0, x, y); addObject(life1, x + 30 , y); addObject(life2, x + 60 , y); addObject(life3, x + 90 , y); addObject(life4, x + 120 , y); } public void checkLife( int numLives) { //Make lifeCounter represent Mario's lives if (mario.getLives() == 4 ) { removeObject(life4); } else if (mario.getLives() == 3 ) { removeObject(life3); } else if (mario.getLives() == 2 ) { removeObject(life2); } else if (mario.getLives() == 1 ) { removeObject(life1); } else if (mario.getLives() == 0 ) { removeObject(life0); } } public int setLives() { return mario.getLives(); } public int getLives() { return mario.LifeLine; } public Score getScore() { return score; //Information from scoreboard } } |
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 | public void act() { super .act(); } public WaterWorld() { //Placing all actors in the world prepare(); } public void prepare() { //Adds the floor blocks addFloor(); //First row of blocks topBlocks(getWidth() - 2248 ,getHeight()- 500 ); //Left chunck of blocks topBlocks(getWidth() - 200 ,getHeight()- 500 ); //Right blocks middleBlocks(getWidth()/ 2 ,getHeight()- 500 ); // Main chunk of blocks in the middle of the world //Smaller chuncks of blocks on each side sideBlocks(getWidth()/ 3 - 500 ,getHeight()- 310 ); //Left Side sideBlocks(getWidth()/ 3 + 1300 ,getHeight()- 310 ); //Right Side //Main hero addObject( new SwimmingMario(), getWidth()/ 8 , getHeight()/ 2 ); //Adding Cheep Cheeps addEnimies(); //Placing the hearts that represent Mario's life lifeCounter(getWidth()/ 30 , getHeight() - 50 ); lifeCounter(getWidth() - 150 , getHeight() - 50 ); } |
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 | private int frame; private int actCounter; private int skipRate; private int floor = 448 ; //doubles to allow decimals private double gravity, gravityIncrement; //Booleans private boolean groundLevel; private boolean changeAnimation; private boolean movingRight; private boolean movingLeft; private boolean damageTaken = false ; //Hero has not taken any damage private int damageTakenTimer = 0 ; public SwimmingMario() { this .LifeLine = 5 ; frame = 0 ; //initial frame skipRate = 75 ; //speed of animaation actCounter = 0 ; //initial act animation = RIGHT; //Animation when staying the way it is speed = 3 ; //How fast it moves gravity = 0 ; //Don't sink gravityIncrement = . 1 ; //Velocity } public void act() { attack(); triggerReleased = true ; //Trigger is released updateAmimation(); actCounter++; marioAnimator(); //blockMovement(); whatIsGround(); marioMover(); floatingMario(); jumpingJumping(); checkCeiling(); checkContact(); } public void checkContact() { if (isTouching(Enemies. class ) && damageTaken == false ) { Enemies tempEnemy = (Enemies)getOneIntersectingObject(Enemies. class ); decreaseLives(); getWorld().removeObject( this ); //rip(); damageTaken = true ; damageTakenTimer = 50 ; //Greenfoot.delay(150); //setLocation(getWorld().getWidth()/9, getWorld().getHeight()-floatHeight); } if (damageTaken == true ) { //Prevent dying continuously when colliding damageTakenTimer--; if (damageTakenTimer <= 0 ) { damageTaken = false ; } } } public void decreaseLives() { this .LifeLine -= 1 ; } public int getLives() { return this .LifeLine; } |