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

2017/12/4

Space Invaders Project

Toaster Toaster

2017/12/4

#
Hi, I'm a freshman and new to coding, and I have a project due Friday, and I could use some help. I'm making a space invaders game, but a couple of things don't work, like the health bar. I also want to know how I can make my aliens move from one side, down, and move to the other side. Here's what I have so far:
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class Healthbar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Healthbar extends Actor
{
    int health = 4;
    int healthBarWidth = 80;
    int healthBarHeight = 15;
    int pixelsPerHealthPoint = (int)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();
    }  
    public void update()
    {
        setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(Color.RED);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
    }
    
    public void loseHealth()
    {
        health--;
    }
}
and for my world:
import greenfoot.*;
import java.util.List;
/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
    private static final int WIDTH = 900;
    private static final int HEIGHT = 850;
    private static final int ROW_LENGTH = 500;
    private static final int FIRING_INTERVAL = 10;
    private int actCounter = 0;
    Healthbar healthBar = new Healthbar();
    Counter counter = new Counter();
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(WIDTH, HEIGHT, 1); 
        GreenfootImage gBackgroundImage = new GreenfootImage(WIDTH, HEIGHT);
        prepare();
    }
     public Counter getCounter()
     {
         return counter;
     }
   
   
    

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
      addObject(counter, 800, 40);
      addObject(healthBar, 700, 40);
      Hunter hunter = new Hunter();
      addObject(hunter, WIDTH/2, 800);
        for(int i=0; i<8; i++) {
        addObject(new Enemy4(), 50+i*770/10, 100);
      }
        for(int i=0; i<8; i++) {
        addObject(new Enemy3(), 50+i*770/10, 180);
      }
        for(int i=0; i<8; i++) {
        addObject(new Enemy2(), 50+i*770/10, 255);
      }
        for(int i=0; i<8; i++) {
        addObject(new Enemy1(), 50+i*770/10, 330);
      }
      
}
public void act()
{
    actCounter++;
    if (actCounter > FIRING_INTERVAL)
    {
        List enemy4 = getObjects(Enemy4.class);
        if (!enemy4.isEmpty())
        {
           int number_of_enemy4 = enemy4.size(); 
           int i = Greenfoot.getRandomNumber(number_of_enemy4);
           Enemy4 randomEnemy4 = (Enemy4)enemy4.get(i);
           randomEnemy4.shoot();
        }
        actCounter = 0;
    }
}
}
The problem with my health bar is that it wants me to declare the class, but I don't want to. If someone can help, it will be greatly appreciated. Thank you.
danpost danpost

2017/12/4

#
Toaster wrote...
The problem with my health bar is that it wants me to declare the class.
I am not sure what you mean by this. Please show exactly what error output you are getting.
Toaster Toaster

2017/12/5

#
Sorry, in the health code where it says public Health(); every time I try to compile it, it says I need to declare it, so I put void in front of it but I don't want to do that. I'll put my code up, but I don't know anymore than that.
Toaster Toaster

2017/12/5

#
 public void HealthBar()
    {
        update();
    }
danpost danpost

2017/12/5

#
Toaster wrote...
 public void HealthBar()
    {
        update();
    }
You will need to show the entire class code so that it can be ascertained what exactly is going on.
Toaster Toaster

2017/12/5

#
But when I do this:
public HealthBar()
    {
        update();
    }
is says this: invalid method declaration; return type required
danpost danpost

2017/12/5

#
danpost wrote...
You will need to show the entire class code so that it can be ascertained what exactly is going on.
Nevermind. Change 'public HealthBar()' to 'public Healthbar()', using the actual name of the class.
Toaster Toaster

2017/12/5

#
Ok I'll try it. Thanks.
Toaster Toaster

2017/12/5

#
It worked. Thanks! I know i'm asking a lot, but could you tell me or guide me on how to get my aliens to move from one side, down, then move to the other? I'll try to upload my game if you need to know what I'm talking about.
You need to login to post a reply.