In my game your supposed to dodge Thwomps and Terence from above. When hitting them you take damage. However i want when hitting Watermelon that you gain 5 health and that it is visible in the healthbar. Here is my code for my player character and healthbar. Im a student doing this for school and help is aprreciated.
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import greenfoot.*; public class Joker extends Actor { public int snelheid = 4 ; private int vSpeed = 0 ; private int acceleration = 1 ; private int state = 1 ; int sprinten = 1 ; int tellerAnimatie = 10 ; private Healthbar healthBar; private SimpleTimer immunityTimer = new SimpleTimer(); private GreenfootSound sterSound = new GreenfootSound( "Super Mario Bros - Star.mp3" ); private int thwompCooldown = 0 ; private final int THWOMP_COOLDOWN_DURATION = 70 ; public Joker(Healthbar healthBar) { this .healthBar = healthBar; } public void act() { checkKeys(); fall(); if (state == 2 && immunityTimer.millisElapsed() > 15000 ) { state = 1 ; } if (thwompCooldown > 0 ) { thwompCooldown--; } raakThwomp(); raakSuperStar(); } public void raakSuperStar() { if (isTouching(SuperStar. class )) { state = 2 ; immunityTimer.mark(); sterSound.play(); } } public void raakThwomp() { if (state == 1 && thwompCooldown == 0 && (isTouching(Thwomp. class ) || isTouching(SuperThwomp. class ))) { int damage = 4 ; if (isTouching(SuperThwomp. class )) { damage = 7 ; } if (isTouching(SuperThwomp. class )) { damage = 7 ; } if (isTouching(Terence. class )) { damage = 25 ; } if (healthBar != null ) { healthBar.loseHealth(damage); thwompCooldown = THWOMP_COOLDOWN_DURATION; Greenfoot.playSound( "Joker Persona 5 Royal Sound Effects HD.mp3" ); Actor thwomp = getOneIntersectingObject(Thwomp. class ); Actor superThwomp = getOneIntersectingObject(SuperThwomp. class ); Actor Terence = getOneIntersectingObject(Terence. class ); if (thwomp != null ) { getWorld().removeObject(thwomp); } if (superThwomp != null ) { getWorld().removeObject(superThwomp); } if (Terence != null ) { getWorld().removeObject(Terence); } } } } public void checkKeys() { if (Greenfoot.isKeyDown( "d" )) { if (sprinten<= 10 ) { if (tellerAnimatie == 10 ) { setImage( "jokerright" + sprinten + ".png" ); sprinten++; tellerAnimatie = 1 ; } } else { sprinten= 1 ; } tellerAnimatie++; setLocation(getX() + snelheid, getY()); } if (Greenfoot.isKeyDown( "a" )) { if (sprinten<= 10 ) { if (tellerAnimatie == 10 ) { setImage( "jokerleft" + sprinten + ".png" ); sprinten++; tellerAnimatie = 1 ; } } else { sprinten= 1 ; } tellerAnimatie++; setLocation(getX() - snelheid, getY()); } if (Greenfoot.isKeyDown( "w" )&& (onGrond() == true )) { jump(); } } public void jump() { if (getWorld().getClass() == Hell. class || getWorld().getClass() == Japan. class ) { vSpeed = - 20 ; } else { vSpeed = - 25 ; } fall(); setImage( "jokerjump6.png" ); } public boolean onGrond() { Actor under = getOneObjectAtOffset ( 0 , getImage().getHeight() / 2 , Grond. class ); return under != null ; } public void fall() { vSpeed = vSpeed + acceleration; setLocation(getX(), getY() + vSpeed); } } |
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 | /** * Write a description of class Healthbar here. * * @author (your name) * @version (a version number or a date) */ public class Healthbar extends Actor { int health = 40 ; int maxHealth = 40 ; int healthBarWidth = 120 ; int healthBarHeight = 20 ; int pixelsPerHealthPoint = healthBarWidth / health; public Healthbar() { update(); } public void act() { update(); } public void update() { GreenfootImage image = new GreenfootImage(healthBarWidth + 2 , healthBarHeight + 2 ); image.setColor(Color.WHITE); image.fillRect( 0 , 0 , healthBarWidth + 2 , healthBarHeight + 2 ); if (health <= 0 ) { Greenfoot.setWorld( new GameOverScherm()); } else { if (health < 10 ) { image.setColor(Color.RED); } else { image.setColor(Color.GREEN); } image.fillRect( 1 , 1 , health * pixelsPerHealthPoint, healthBarHeight); } setImage(image); } public void loseHealth( int amount) { health -= amount; if (health < 0 ) { health = 0 ; } } public void add( int amount) { health += amount; if (health > maxHealth) { health = maxHealth; } } } |