my ship has the health
The redlaser need to remove the ship after three hits
import greenfoot.*;
import java.util.List;
/**
*/
public class Ship extends Actor
{
int speed = 5;
int shotCounter = 0;
private int delay = 0;
private final int MAX_DELAY = 13;
private int health = 3;
/**
* Method act: move spaceship
*/
public void act()
{
move();
if(delay <= 0) {
if( Greenfoot.isKeyDown("space") )
{
fire();
Greenfoot.playSound("laser.wav");
delay = MAX_DELAY;
}
}
else {
delay--;
}
}
/**
* Fire the laser
*/
private void fire()
{
Shot shot = new Shot();
getWorld().addObject(shot, getX(), getY());
getWorld().addObject(shot, getX(), getY());
shot.setRotation(getRotation());
}
/**
* Method move: checks for keystrokes and applies the changes, then moves the ship.
* I applied a bit of slowing to the ship's speed (so it would be drifting to a stop)
*/
private void move()
{
int dz = 0;
if (Greenfoot.isKeyDown("d")) dz++;
if (Greenfoot.isKeyDown("a")) dz--;
setRotation(getRotation() + dz * 5);
int ds = -1;
if (Greenfoot.isKeyDown("w")) ds += 2;
speed += ds;
if (speed < 0) speed = 0;
if (speed > 90) speed = 90;
if (speed >= 200) move(speed / 100);
if (Greenfoot.isKeyDown("s")) ds += 5;
speed += ds;
if (speed < 5) speed = 5;
if (speed > 200) speed = 200;
if (speed >= 20) move(speed / 10);
}
// private int health = 5;
public void setHealth(int points) {
health += points;
}
public int getHealth() {
return health;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Enemylaser here.
*
*/
public class Redlaser extends Mover
{
// private int life;
/**
* Act - do whatever the Enemylaser wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(20);
eat();
//life--;
//if (life == 0)
// {
// getWorld().removeObject(this);
//}
ifAtWorldEdge();
}
public void ifAtWorldEdge()
{
if (atWorldEdge())
{
getWorld().removeObject(this);
}
}
public void eat()
{
//Actor Ship;
//Ship = getOneObjectAtOffset(0, 0, Ship.class);
// if (Ship != null)
// {
// World World;
// World = getWorld();
// GameOver gameover = new GameOver();
// World.addObject(gameover, World.getWidth()/2, World.getHeight()/2);
// World.removeObject(Ship);
//}
// if (Ship != null)
// {
// World world;
// world = getWorld();
// world.removeObject(Ship);
// GameOver gameover = new GameOver();
// }
Actor BigRock;
BigRock = getOneObjectAtOffset(0, 0, BigRock.class);
if (BigRock != null)
{
World world;
world = getWorld();
world.removeObject(BigRock);
}
}
//in your bullet class you have to add this method:
public void hitEnemy() {
Ship ship = (Ship) getOneObjectAtOffset(0, 0, Ship.class);
if (ship != null) {
ship.setHealth(-1);//decrements the health of this enemy;
//if you want your bullet to be removed now you have to add the next line; otherwhise delete it.
getWorld().removeObject(this);
}
}
}
