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

2026/5/27

enemy help!

An0n_N0mad14 An0n_N0mad14

2026/5/27

#
how would i code an enemy to move left but when it reaches the edge of the platform its on that it starts moving to the right and when it hits the edge that it turns left and repeats this process
An0n_N0mad14 An0n_N0mad14

2026/5/27

#
nvm guys, i got it. But how do i add gravity to my game so that my player can jump, and how do i add hitboxes to platforms
danpost danpost

2026/5/27

#
An0n_N0mad14 wrote...
how do i add gravity to my game so that my player can jump, and how do i add hitboxes to platforms
Gravity: add a constant downward velocity. Apply gravity every act step, moving player. Then check for ground and other objects. Adjust player position and velocity when ground or object is encountered. Hitboxes: no extra objects are needed as hitboxes. Just check for intersecting platforms. If one is found, use the opposite direction to that move to cause the collision. That direction will determine which dimensions (height or width, of player and platform) are used to determine where to place the player.
An0n_N0mad14 An0n_N0mad14

2026/5/28

#
how do i make a floating health bar that follows above my bee (proto type game)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HealthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HealthBar extends Actor
{   int health=5;
    int healthBarWidth=100;
    int healthBarHeight=20;
    int pixelsPerHealthPoint=healthBarWidth/health;
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public HealthBar()
    {
        update();
    }
    
    public void act()
    {
        update();
        Loser();
    }
    
    public void update()
    {
        setImage(new GreenfootImage(healthBarWidth+2,healthBarHeight+2));
        getImage().setColor(Color.BLACK);
        getImage().drawRect(0,0, healthBarWidth+1,healthBarHeight+1);
        getImage().setColor(Color.RED);
        getImage().fillRect(1,1,health*pixelsPerHealthPoint,healthBarHeight);
    }
    
    public void Loser()
    {
        if(health==0)
        {
            Greenfoot.setWorld(new LoserScreen());
            Greenfoot.stop();
        }
    }
}



/**
 * Write a description of class Bee here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bee extends Actor
{
    
    
    int health = 100; // Current health
    int maxHealth = 100;
    int healthBarWidth = 100; // Physical width in pixels
    int healthBarHeight = 15;
    int pixelsPerHealthPoint= healthBarWidth / maxHealth;
    boolean hitRock=false;
    /**
     * Act - do whatever the Earth wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        hitRocks();
        playerControls();
        
    }
    
    public void loseHealth(int amount) 
    {
        health -= amount;
        if (health < 0) health = 0;
    }
    
    public void hitRocks()
    {
        if(isTouching(Poison.class) && hitRock == false)
        {
            MyWorld myWorld= (MyWorld) getWorld();
            HealthBar healthbar=myWorld.getHealthBar();
            healthbar.health--;
            hitRock = true;
            
        }
        else if (!isTouching(Poison.class))
        {
            hitRock=false;
        }
    }
    

    
    public void playerControls()
    {
        //Moves Crab up and down
        if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("W"))
        {
            setLocation(getX(), getY() - 5);
        }
        if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("S"))
        {
            setLocation(getX(), getY() + 5);
        }
        
        //Moves Crab left and right
        if (Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("D"))
        {
            setLocation(getX() +5, getY());
        }
        if (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("A"))
        {
            setLocation(getX() -5, getY());
        }
    }
}




/**
 * Write a description of class Bee here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bee extends Actor
{
    
    
    int health = 100; // Current health
    int maxHealth = 100;
    int healthBarWidth = 100; // Physical width in pixels
    int healthBarHeight = 15;
    int pixelsPerHealthPoint= healthBarWidth / maxHealth;
    boolean hitRock=false;
    /**
     * Act - do whatever the Earth wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        hitRocks();
        playerControls();
        
    }
    
    public void loseHealth(int amount) 
    {
        health -= amount;
        if (health < 0) health = 0;
    }
    
    public void hitRocks()
    {
        if(isTouching(Poison.class) && hitRock == false)
        {
            MyWorld myWorld= (MyWorld) getWorld();
            HealthBar healthbar=myWorld.getHealthBar();
            healthbar.health--;
            hitRock = true;
            
        }
        else if (!isTouching(Poison.class))
        {
            hitRock=false;
        }
    }
    

    
    public void playerControls()
    {
        //Moves Crab up and down
        if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("W"))
        {
            setLocation(getX(), getY() - 5);
        }
        if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("S"))
        {
            setLocation(getX(), getY() + 5);
        }
        
        //Moves Crab left and right
        if (Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("D"))
        {
            setLocation(getX() +5, getY());
        }
        if (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("A"))
        {
            setLocation(getX() -5, getY());
        }
    }
}




/**
 * Write a description of class Poison here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Poison extends Actor
{
    private int timer = 0;
    /**
     * Act - do whatever the Poison wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Poison()
    {
        getImage().fillRect(0, 0, 1200, 10);
        GreenfootImage img=new GreenfootImage(1200,10);
        img.setColor(Color.RED);
        img.fill();
        setImage(img);
    }
    
    
    public void act() 
    {
        
        //Every second the poison rises
        timer ++; 
        if (timer >= 60) 
        {
            rise();
            timer = 0; 
        }
        
        
        
        
    }
    
    public void rise() 
    {
        
        //actually rises
        int currentX = getX();
        int currentY = getY();
        int upwardSpeed = 5; 
        setLocation(currentX, currentY - upwardSpeed);
        //so that is the poison rises, the image does so its not just a straight line going up the screen
        getImage().scale(getImage().getWidth()+0, getImage().getHeight()+10);
    }
    
      
    
}
danpost danpost

2026/5/28

#
An0n_N0mad14 wrote...
how do i make a floating health bar that follows above my bee (proto type game) << Code Omitted >>
The HealthBar class given does not seem to be compatible with a Bee object having a maximum of 100 health points (as opposed to 5 health points). Let the Bee class have its health field -- it belongs to each Bee object created; but, the health bar dimension fields and related fields in your Bee class belong to the HealthBar class ... only. One way to give each bee a health bar is by using codes like the following in the Bee class:
int health = 100; // or 5, or whatever
HealthBar healthbar; // each bee retains a reference to its health bar object

public Bee() {
    healthbar = new BeeHealthBar(100, 15, health); // dimensions (width and height) and maximum health value given as parameters
}

// add health bar to world when Bee object is added to the world
protected void addedToWorld(World world) {
    world.addObject(healthbar, getX(), getY()-getImage().getHeight()/2-5); // given positional offset is retained in health bar object
}

/**  the following is an extension of the HealthBar class, giving health bars
 **  the ability to move with the object it was created for
 */
protected class BeeHealthBar extends HealthBar
{
    int offsetX, offsetY; // positional offsets to Bee object it belongs to
    
    public BeeHealthBar(int width, int height, int parts) {
        healthBarWidth = width;
        healthBarHeight = height;
        pixelsPerHealthPoint = width/parts;
        width = width-(width%pixels); // removing excess pixels
    }
    
    // save positional offsets when added into world
    protected void addedToWorld(World world) {
        offsetX = getX()-Bee.this.getX();
        offsetY = getY()-Bee.this.getY();
    }
    
    public void act() {
        if (Bee.this.getWorld() == null) {
            getWorld().removeObject(this);
            return;
        }
        positionSelf();
        super.act();
    }
    
    protected void positionSelf() {
        setLocation(Bee.this.getX()+offsetX, Bee.this.getY()+offsetY);
    }
}
That is NOT a separate class. It is an INNER class -- one inside another (BeeHealthBar class inside the Bee class codes). Any object created from the inner class belongs to (or, is owned by) the outer class object that created it. So, what I did was have each Bee object create its own self-positioning health bar using your current HealthBar class. The Loser method could be moved to the BeeHealthBar class along with the call to it from the act method. The update method could also be called from the subclass so as to be able to remove the act method entirely from the HealthBar class (the super.act() line will then need to be removed from the act method in the subclass. The only thing I don't care for is updating the health bar every act step. It would be much better to only update it when the health value changes, and only then call the update method.
An0n_N0mad14 An0n_N0mad14

2026/5/29

#
but does this make the health bar float on top of the bee actor, even when the bee moves?
An0n_N0mad14 An0n_N0mad14

2026/5/29

#
and the 5 is there because thats how many hits it takes before it dies, so technically 20 health gets removed every time
An0n_N0mad14 An0n_N0mad14

2026/5/29

#
An0n_N0mad14 wrote...
but does this make the health bar float on top of the bee actor, even when the bee moves?
nvm thanks for the help, but the health bar doesnt work properly, if i hit the start button for the world the health bar gets completely drained and just doesnt work aftter that
danpost danpost

2026/5/29

#
An0n_N0mad14 wrote...
the health bar doesnt work properly, if i hit the start button for the world the health bar gets completely drained and just doesnt work aftter that
I think the hitbox is not adjusted when scaling an image. I believe it would need to be set again. Here is a slightly revised version of the Poison class:
import greenfoot.*;

public class Poison extends Actor
{
    private int timer = 0;
    private int growth = 2;
    
    protected void addedToWorld(World world) {
        updateImage();
    }
    
    private void updateImage() {
        GreenfootImage img = new GreenfootImage(getWorld().getWidth(), 4*growth);
        img.setColor(Color.RED);
        img.fill();
        setImage(img);
        setLocation(getWorld().getWidth()/2, getWorld().getHeight()-2*growth);
    }
    
    public void act() {
        timer ++;
        if (timer >= 60) {
            growth++;
            updateImage();
            timer = 0;
        }
    }
}
I wrote a test scenario to make sure things worked properly. If you wish to see the codes for the various classes, just let me know.
An0n_N0mad14 An0n_N0mad14

3 days ago

#
it didnt work either
An0n_N0mad14 An0n_N0mad14

3 days ago

#
thats fine, i appreciate u helping, but i think im gonna make it so that there are three hearts and if it hits an enemy that one heart gets removed
You need to login to post a reply.