This site requires JavaScript, please enable it in your browser!
Greenfoot back
daviddd
daviddd wrote ...

2024/5/9

My hearts aren´t dissapearing

daviddd daviddd

2024/5/9

#
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++;
        }
    }
}


Super_Hippo Super_Hippo

2024/5/10

#
In line 113, you call the loseHealth method. That’s the one in line 118, not the one in 211. The one in 211 is never called from anywhere.
danpost danpost

2024/5/11

#
Super_Hippo wrote...
In line 113, you call the loseHealth method. That’s the one in line 118, not the one in 211. The one in 211 is never called from anywhere.
... and it needs to be called on the HealthBar object that is currently in the world. Easiest fix is to add to Dungeon class:
public void loseHealth()
{
    healthBar.loseHealth();
}
Then add to actor class:
((Dungeon)getWorld()).loseHealth();
daviddd. daviddd.

2024/5/11

#
Then add to actor class:
((Dungeon)getWorld()).loseHealth();
where do i add this? Thanks
hamotikha hamotikha

2024/5/11

#
danpost danpost

2024/5/12

#
daviddd. wrote...
Then add to actor class: << Code Omitted >> where do i add this?
How about after line 114.
daviddd. daviddd.

2024/5/12

#
thanks, i got another problem though, when i start the game like this:
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(1,1), 100, 200);
        addObject(new Monster1(1,1), 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;
    }

    public void loseHealth()
    {
        healthBar.loseHealth();
    }
}
it goes alright but when i start the game like this:
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 Monster(1,1), 100, 200);
        addObject(new Monster(1,1), 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;
    }

    public void loseHealth()
    {
        healthBar.loseHealth();
    }
}
it lags, player doesnt appear and greenfoot crashes, i will add code of a few actors
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 = 0;

        // 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);
            ((Dungeon)getWorld()).loseHealth();
        }
    }
    
    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);
            }
        }
    }
}


[code]import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Monster1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Monster1 extends Actor
{
    int speed;
    int lives;
    /**
     * Act - do whatever the Monster1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        followPlayer();
        move(speed);
        shot();
        health();
    }
    public Monster1(int speed, int lives)
    {
        this.speed = speed; // Nastaví rychlost pohybu
        this.lives = lives; // Nastaví počet životů
    }
    private void followPlayer()
    {
        int distance = 1000;
        Actor closest = null;
         
        if(!getObjectsInRange(distance, Player.class).isEmpty())
        {
            for (Object obj: getObjectsInRange(distance, Player.class))
            {
                Actor player = (Actor) obj;
                int playerDistance = (int) Math.hypot(player.getX() - getX(), player.getY() - getY());
                if (closest == null || playerDistance< distance)
                {
                    closest = player;
                    distance = playerDistance;
                }
            }
            turnTowards(closest.getX(),closest.getY());
        }  
    }
    
    public void shot()
    {
        Bullet intersectingBullet = (Bullet) getOneIntersectingObject(Bullet.class);
        if (intersectingBullet != null)
        {
            lives--;
            removeTouching(Bullet.class);
        }
    }
    
    public void health()
    {
        if(lives == 0)
        {
            getWorld().removeObject(this);
            Dungeon.killCounter++;
        }
    }
}
import greenfoot.*;

public class Monster extends Actor {
    int speed;
    int lives;

    public void act()
    {
        followPlayer();
        move(speed);
        shot();
        health();
    }
    
    public Monster(int speed, int lives)
    {
        this.speed = speed; // Nastaví rychlost pohybu
        this.lives = lives; // Nastaví počet životů
    }
    private void followPlayer()
    {
        int distance = 1000;
        Actor closest = null;
         
        if(!getObjectsInRange(distance, Player.class).isEmpty())
        {
            for (Object obj: getObjectsInRange(distance, Player.class))
            {
                Actor player = (Actor) obj;
                int playerDistance = (int) Math.hypot(player.getX() - getX(), player.getY() - getY());
                if (closest == null || playerDistance< distance)
                {
                    closest = player;
                    distance = playerDistance;
                }
            }
            turnTowards(closest.getX(),closest.getY());
        }  
    }
    public void shot()
    {
        Bullet intersectingBullet = (Bullet) getOneIntersectingObject(Bullet.class);
        if (intersectingBullet != null)
        {
            lives--;
            removeTouching(Bullet.class);
        }
    }
    public void health()
    {
        if(lives == 0)
        {
            getWorld().removeObject(this);
            Dungeon.killCounter++;
        }
    }
}
daviddd. daviddd.

2024/5/12

#
nevermind, i fixed it!
daviddd. daviddd.

2024/5/12

#
nevermind, i didnt
danpost danpost

2024/5/13

#
daviddd. wrote...
when i start the game like this: << Code Omitted >> it goes alright but when i start the game like this: << Code Omitted >> it lags, player doesnt appear and greenfoot crashes, i will add code of a few actors << Code Omitted >>
Line 36 in your Player class code should be at the end of "obrázky" section -- not at the beginning where the GreenfootImage variable still has a null value.
daviddd. daviddd.

2024/5/13

#
thanks, another problem, i want to generate a new world "dungeon2" but it says i need to put something in the brackets in line 27, how do i add the current lives there? thanks
import greenfoot.*;

public class Dungeon extends AllDungeons {
    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 Monster(2,1), 100, 200);
        addObject(new Monster1(1,2), 550, 500);
        addObject(new Monster(2,1), 500, 150);
        addObject(new Monster1(1,2), 100, 550);
    }

    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;
    }

}
daviddd. daviddd.

2024/5/13

#
also, when i pick the option medium or hard i still get 5 lives, i have 3, 1 heart(s) on the screen but i dont die after running out of them? sorry if im a bother
danpost danpost

2024/5/16

#
daviddd. wrote...
also, when i pick the option medium or hard i still get 5 lives, i have 3, 1 heart(s) on the screen but i dont die after running out of them? sorry if im a bother
You have two fields that are to hold same value (well, supposed to -- at least). They are (1) the health field that the Player instance retains and (2) the health field that the HealthBar instance retains. To keep things simple, best is to limit yourself to one field per value. So, remove the one the player retains and move all health related code from the Dungeon class to the Player class. Also, add a method to the HealthBar class to "get" the value from a HealthBar instance. You can have a new Player instance create its own health bar and add it into any world that the Player instance is added into (via the addedToWorld(World) method). Then, instead of passing the health value from world to world, just pass the Player object (which retains the HealthBar object). One thing that lets you know that the HealthBar instance should be retained in a field in the Player class is this -- the player has a health state (or, the health belongs to the player); therefore, it (stuff related to player's health) belongs in the Player class as instance members (non-static fields and methods).
You need to login to post a reply.