hey, i wanted to do a thing, when a monster touches the player it should remove a heart from the top left as shown in the image. but the health stays the same, do u know how to fix it?
import greenfoot.*;
public class Player extends Actor {
public int health;
private int coins;
private int weaponLevel;
private int ammo = 5;
// obrázky
private GreenfootImage imageBackView;
private GreenfootImage imageRightBow;
private GreenfootImage imageLeftBow;
private GreenfootImage imageFrontBow;
private GreenfootImage imageFrontGunOne;
private GreenfootImage imageRightGunOne;
private GreenfootImage imageLeftGunOne;
private GreenfootImage imageFrontGunTwo;
private GreenfootImage imageRightGunTwo;
private GreenfootImage imageLeftGunTwo;
// obrázky
public Player(int difficulty) {
if (difficulty == 1) {
this.health = 5;
} else if (difficulty == 2) {
this.health = 3;
} else {
this.health = 1;
}
this.weaponLevel = 1;
this.coins = 20;
// obrázky
setImage(imageFrontBow);
imageBackView = new GreenfootImage("player-backview.png");
imageFrontBow = new GreenfootImage("player-bow-frontview.png");
imageLeftBow = new GreenfootImage("player-bow-leftview.png");
imageRightBow =new GreenfootImage("player-bow-rightview.png");
imageFrontGunOne = new GreenfootImage("player-gun-frontview.png");
imageRightGunOne = new GreenfootImage("player-gun-rightview.png");
imageLeftGunOne = new GreenfootImage("player-gun-leftview.png");
imageFrontGunTwo = new GreenfootImage("player-gun2-frontview.png");
imageRightGunTwo = new GreenfootImage("player-gun2-rightview.png");
imageLeftGunTwo = new GreenfootImage("player-gun2-leftview.png");
//obrázky
}
public void act() {
moveAround();
shoot();
checkCollision();
checkForUpgrade();
moveToMouse();
updateImageBasedOnRotation();
}
private void moveAround() {
if (Greenfoot.isKeyDown("w")) {
setLocation(getX(), getY() - 2);
}
if (Greenfoot.isKeyDown("s")) {
setLocation(getX(), getY() + 2);
}
if (Greenfoot.isKeyDown("a")) {
setLocation(getX() - 2, getY());
}
if (Greenfoot.isKeyDown("d")) {
setLocation(getX() + 2, getY());
}
}
private void moveToMouse() {
MouseInfo mouseinfo = Greenfoot.getMouseInfo();
if (mouseinfo != null ) {
int x = mouseinfo.getX();
int y = mouseinfo.getY();
turnTowards(x, y);
}
}
private void shoot() {
if (Greenfoot.mouseClicked(null)) {
if (munition() == true) {
Bullet bullet = new Bullet(getRotation(), weaponLevel);
getWorld().addObject(bullet, getX(), getY());
bullet.move(50);
bullet.setRotation(getRotation());
ammo--;
}
}
}
public boolean munition()
{
if(ammo > 0)
{
return true;
}
else
{
return false;
}
}
private void checkCollision() {
Actor monster = getOneIntersectingObject(Monster.class);
if (monster != null) {
loseHealth();
getWorld().removeObject(monster);
}
}
public void loseHealth() {
health--;
if (health <= 0) {
die();
}
}
private void die() {
Greenfoot.stop();
}
private void checkForUpgrade() {
if (Greenfoot.isKeyDown("u") && coins >= 10) { // zmáčknout u na up grade zbraně
weaponLevel++;
coins -= 10;
}
}
public void addCoins(int amount) {
coins += amount;
}
public int getHealth() {
return health;
}
private void updateImageBasedOnRotation() { // obrázky postavy (měnění podle úhlu pohledu)
int rotation = getRotation();
if (weaponLevel == 1)
{
if (rotation >=0 && rotation <= 50) {
setImage(imageRightBow);
} else if (rotation >=131 && rotation <=220) {
setImage(imageLeftBow);
} else if (rotation >=51 && rotation <= 130) {
setImage(imageFrontBow);
} else if (rotation >=221 && rotation <=310) {
setImage(imageBackView);
} else {
setImage(imageRightBow);
}
} else if (weaponLevel == 2) {
if (rotation >=0 && rotation <= 50) {
setImage(imageRightGunOne);
} else if (rotation >=131 && rotation <=220) {
setImage(imageLeftGunOne);
} else if (rotation >=51 && rotation <= 130) {
setImage(imageFrontGunOne);
} else if (rotation >=221 && rotation <=310) {
setImage(imageBackView);
} else {
setImage(imageRightGunOne);
}
} else if (weaponLevel == 3) {
if (rotation >=0 && rotation <= 50) {
setImage(imageRightGunTwo);
} else if (rotation >= 131 && rotation <=220) {
setImage(imageLeftGunTwo);
} else if (rotation >=51 && rotation <= 130) {
setImage(imageFrontGunTwo);
} else if (rotation >=221 && rotation <=310) {
setImage(imageBackView);
} else {
setImage(imageRightGunTwo);
}
}
}
}
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 * 5, 50);
GreenfootImage heart = new GreenfootImage("heart-removebg-preview.png");
heart.scale(40, 40);
for (int i = 0; i < health; i++) {
image.drawImage(heart, 5+i * 40, 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 static int killCounter = 0;
public Dungeon(int difficulty, int initialHealth) {
super(600, 600, 1);
player = new Player(difficulty);
addObject(player, getHeight()/2, getWidth()/2);
addObject(new Monster1(2, 2), 100, 200);
addObject(new Monster1(2, 2), 500, 500);
}
public Dungeon(int initialHealth) {
this(1, initialHealth);
healthBar = new HealthBar(initialHealth);
addObject(healthBar, 125, 30);
}
public void nextDungeon()
{
if (killCounter >= 4)
{
Greenfoot.setWorld(new Dungeon2());
}
}
public void act() {
nextDungeon();
}
public Player getPlayer() {
return player;
}
}
import greenfoot.*;
public class Monster extends Actor {
private int health = 5;
public void act() {
moveTowardsPlayer();
}
protected void moveTowardsPlayer() {
Actor player = getWorld().getObjects(Player.class).get(0);
turnTowards(player.getX(), player.getY());
move(1);
}
public void takeDamage(int damage) {
health -= damage;
if (health <= 0) {
getWorld().removeObject(this);
Dungeon.killCounter++;
}
}
}



