Hondrok wrote...
the health_bar needs to change its image acording to the armour of the Mario actorimport greenfoot.*; public class Mario extends Actor { private int speed = 7; private int vSpeed = 0; private int acceleration = 2; private int jumpStrength = 25; public int armour = 3; public void act() { if (! getWorld().getObjects(GameOver.class).isEmpty()) { return; } checkKeys(); attack(); checkFall(); hitGoomba(); hitCastle_l1(); hitCastle_l2(); checkArmour(); } private void checkKeys() { if(Greenfoot.isKeyDown("left")) { moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } if(Greenfoot.isKeyDown("up")) { jump(); } } public void checkFall() { if(onGround() && vSpeed != -jumpStrength) { vSpeed=0; if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) { setImage("Goku_idle.png"); } } else { setImage("Goku_jump.png"); fall(); } } public boolean onGround() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class); return under != null; } public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void jump() { if (onGround()) { vSpeed = - jumpStrength; } } public void hitGoomba() { Actor goomba = getOneIntersectingObject(Goomba.class); if(goomba != null) { armour = armour - 1; getWorld().removeObject(goomba); } } public void hitCastle_l1() { Actor castle = getOneIntersectingObject(Castle.class); if(castle != null) { World world = new Level_2(); // reference to new world so you can work with it Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world mario2.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional) Greenfoot.setWorld(world); // set new level active } } public void hitCastle_l2() { Actor castle = getOneIntersectingObject(Castle_l2.class); if(castle != null) { World world = new Final_level(); // reference to new world so you can work with it Mario mario3 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world mario3.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional) Greenfoot.setWorld(world); // set new level active } } public void moveRight() { setLocation ( getX() + speed, getY()); setImage("Goku_move_right.png"); } public void moveLeft() { setLocation ( getX() - speed, getY()); setImage("Goku_move_left.png"); } public void attack() { if("space".equals(Greenfoot.getKey())) { getWorld().addObject(new Sphere_right(), getX() + 5, getY()); setImage("Goku_attack.png"); } } public void checkArmour() { Actor sphere = getOneIntersectingObject(Sphere_left.class); if(sphere != null) { armour = armour - 1; ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); getWorld().removeObject(sphere); } if(armour == 0) { getImage().setTransparency(0); World myWorld = getWorld(); GameOver gameover = new GameOver(); myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); Greenfoot.stop(); } } }
import greenfoot.*; public class Mario extends Actor { private int speed = 7; private int vSpeed = 0; private int acceleration = 2; private int jumpStrength = 25; public int armour = 3; public void act() { if (! getWorld().getObjects(GameOver.class).isEmpty()) { return; } checkKeys(); attack(); checkFall(); hitGoomba(); hitCastle_l1(); hitCastle_l2(); checkArmour(); } private void checkKeys() { if(Greenfoot.isKeyDown("left")) { moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } if(Greenfoot.isKeyDown("up")) { jump(); } } public void checkFall() { if(onGround() && vSpeed != -jumpStrength) { vSpeed=0; if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) { setImage("Goku_idle.png"); } } else { setImage("Goku_jump.png"); fall(); } } public boolean onGround() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class); return under != null; } public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void jump() { if (onGround()) { vSpeed = - jumpStrength; } } public void hitGoomba() { Actor goomba = getOneIntersectingObject(Goomba.class); if(goomba != null) { armour = armour - 1; getWorld().removeObject(goomba); ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); } } public void hitCastle_l1() { Actor castle = getOneIntersectingObject(Castle.class); if(castle != null) { World world = new Level_2(); // reference to new world so you can work with it Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world mario2.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional) Greenfoot.setWorld(world); // set new level active } } public void hitCastle_l2() { Actor castle = getOneIntersectingObject(Castle_l2.class); if(castle != null) { World world = new Final_level(); // reference to new world so you can work with it Mario mario3 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world mario3.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional) Greenfoot.setWorld(world); // set new level active } } public void moveRight() { setLocation ( getX() + speed, getY()); setImage("Goku_move_right.png"); } public void moveLeft() { setLocation ( getX() - speed, getY()); setImage("Goku_move_left.png"); } public void attack() { if("space".equals(Greenfoot.getKey())) { getWorld().addObject(new Sphere_right(), getX() + 5, getY()); setImage("Goku_attack.png"); } } public void checkArmour() { Actor sphere = getOneIntersectingObject(Sphere_left.class); if(sphere != null) { armour = armour - 1; ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); getWorld().removeObject(sphere); } if(armour == 0) { getImage().setTransparency(0); World myWorld = getWorld(); GameOver gameover = new GameOver(); myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); Greenfoot.stop(); } } }
import greenfoot.*; public class Health_Bar extends Actor { public void act() { } public void updateImage(int armour) { setImage("Health_bar_"+armour+".png"); } }
import greenfoot.*; public class Mario extends Actor { private int speed = 7; private int vSpeed = 0; private int acceleration = 2; private int jumpStrength = 25; public int armour = 3; public void act() { if (! getWorld().getObjects(GameOver.class).isEmpty()) { return; } checkKeys(); attack(); checkFall(); hitGoomba(); hitCastle_l1(); hitCastle_l2(); checkArmour(); } private void checkKeys() { if(Greenfoot.isKeyDown("left")) { moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } if(Greenfoot.isKeyDown("up")) { jump(); } } public void checkFall() { if(onGround() && vSpeed != -jumpStrength) { vSpeed=0; if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) { setImage("Goku_idle.png"); } } else { setImage("Goku_jump.png"); fall(); } } public boolean onGround() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class); return under != null; } public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void jump() { if (onGround()) { vSpeed = - jumpStrength; } } public void hitGoomba() { Actor goomba = getOneIntersectingObject(Goomba.class); if(goomba != null) { armour = armour - 1; getWorld().removeObject(goomba); ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); } } public void hitCastle_l1() { Actor castle = getOneIntersectingObject(Castle.class); if(castle != null) { World world = new Level_2(); // reference to new world so you can work with it Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world mario2.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional) Greenfoot.setWorld(world); // set new level active } } public void hitCastle_l2() { Actor castle = getOneIntersectingObject(Castle_l2.class); if(castle != null) { World world = new Final_level(); // reference to new world so you can work with it Mario mario3 = (Mario)world.getObjects(Mario.class).get(0); // reference to mario in new world mario3.armour = this.armour; // set value of armor of new mario to that of the one in current world ( 'this.' is optional) Greenfoot.setWorld(world); // set new level active } } public void moveRight() { setLocation ( getX() + speed, getY()); setImage("Goku_move_right.png"); } public void moveLeft() { setLocation ( getX() - speed, getY()); setImage("Goku_move_left.png"); } public void attack() { if("space".equals(Greenfoot.getKey())) { getWorld().addObject(new Sphere_right(), getX() + 5, getY()); setImage("Goku_attack.png"); } } public void checkArmour() { Actor sphere = getOneIntersectingObject(Sphere_left.class); if(sphere != null) { armour = armour - 1; ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); getWorld().removeObject(sphere); } if(armour == 0) { getImage().setTransparency(0); World myWorld = getWorld(); GameOver gameover = new GameOver(); myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); Greenfoot.stop(); } } }
((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
import greenfoot.*; public class Mario extends Actor { private int speed = 7; private int vSpeed = 0; private int acceleration = 2; private int jumpStrength = 25; public int armour = 3; public void act() { if (! getWorld().getObjects(GameOver.class).isEmpty()) { return; } checkKeys(); attack(); checkFall(); hitGoomba(); hitCastle_l1(); hitCastle_l2(); checkArmour(); } private void checkKeys() { if(Greenfoot.isKeyDown("left")) { moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } if(Greenfoot.isKeyDown("up")) { jump(); } } public void checkFall() { if(onGround() && vSpeed != -jumpStrength) { vSpeed=0; if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) { setImage("Goku_idle.png"); } } else { setImage("Goku_jump.png"); fall(); } } public boolean onGround() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class); return under != null; } public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void jump() { if (onGround()) { vSpeed = - jumpStrength; } } public void hitGoomba() { Actor goomba = getOneIntersectingObject(Goomba.class); if(goomba != null) { armour = armour - 1; getWorld().removeObject(goomba); ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); } } public void hitCastle_l1() { Actor castle = getOneIntersectingObject(Castle.class); if(castle != null) { World world = new Level_2(); Mario mario2 = (Mario)world.getObjects(Mario.class).get(0); mario2.armour = this.armour; ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); Greenfoot.setWorld(world); } } public void hitCastle_l2() { Actor castle = getOneIntersectingObject(Castle_l2.class); if(castle != null) { World world = new Final_level(); Mario mario3 = (Mario)world.getObjects(Mario.class).get(0); mario3.armour = this.armour; ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); Greenfoot.setWorld(world); } } public void moveRight() { setLocation ( getX() + speed, getY()); setImage("Goku_move_right.png"); } public void moveLeft() { setLocation ( getX() - speed, getY()); setImage("Goku_move_left.png"); } public void attack() { if("space".equals(Greenfoot.getKey())) { getWorld().addObject(new Sphere_right(), getX() + 5, getY()); } } public void checkArmour() { Actor sphere = getOneIntersectingObject(Sphere_left.class); if(sphere != null) { armour = armour - 1; ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour); getWorld().removeObject(sphere); } if(armour == 0) { getImage().setTransparency(0); World world = new Game_Over_Lose(); Greenfoot.setWorld(world); } } }