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:
and for my world:
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.
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--;
}
}
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;
}
}
}
