Hey, i am trying to make a game, on the main menu screen you select your difficulty, e.g easy, you will have 5 lives and they will show up as hearts in the top left corner of the game, but when i pick medium or hard they will be moved a bit to the right, does anyone know why is that?
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import greenfoot.*; public class HealthBar extends Actor { private int health; public HealthBar( int initialHealth) { this .health = initialHealth; update(); } public void update() { GreenfootImage image = new GreenfootImage( 50 * health, 50 ); image.clear(); for ( int i = 0 ; i < health; i++) { GreenfootImage heart = new GreenfootImage( "heart.png" ); heart.scale( 40 , 40 ); image.drawImage(heart, i * 45 , 5 ); } setImage(image); } public void loseHealth() { if (health > 0 ) { health--; update(); } } public boolean isAlive() { return health > 0 ; } } import greenfoot.*; public class Dungeon extends World { private Player player; private HealthBar healthBar; public Dungeon( int difficulty, int initialHealth) { super ( 600 , 400 , 1 ); player = new Player(difficulty); addObject(player, 300 , 200 ); populateMonsters(); } public Dungeon( int initialHealth) { super ( 800 , 600 , 1 ); healthBar = new HealthBar(initialHealth); addObject(healthBar, 125 , 30 ); } private void populateMonsters() { for ( int i = 0 ; i < 10 ; i++) { addObject( new Monster(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); } addObject( new Boss(), 500 , 300 ); } public void act() { if (getObjects(Monster. class ).isEmpty()) { } } public Player getPlayer() { return player; } } import greenfoot.*; public class Button extends Actor { private String text; private int difficulty; public Button(String text, int difficulty) { this .text = text; this .difficulty = difficulty; updateImage(); } private void updateImage() { GreenfootImage image = new GreenfootImage(text + " - Click to start" , 24 , Color.WHITE, Color.BLACK); setImage(image); } public void act() { if (Greenfoot.mouseClicked( this )) { int initialHealth = (difficulty == 1 ) ? 5 : ((difficulty == 2 ) ? 3 : 1 ); Greenfoot.setWorld( new Dungeon(initialHealth)); } } } import greenfoot.*; public class MainMenu extends World { public MainMenu() { super ( 600 , 400 , 1 ); prepare(); } private void prepare() { addObject( new Button( "Easy" , 1 ), 300 , 150 ); addObject( new Button( "Medium" , 2 ), 300 , 200 ); addObject( new Button( "Hard" , 3 ), 300 , 250 ); } } |