import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Missile here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Missile extends Actor
{
/**int h = 0;**/
public void act()
{
setLocation(getX() + speed, getY());
checkBoundaries();
destroyHelicopter();
}
//we add a method "checkBoundaries()" that destroys bullets that are off screen.
public void checkBoundaries()
{
if(getX() > getWorld().getWidth() - 1) {
getWorld().removeObject(this);
}
else if(getX() < 1)
getWorld().removeObject(this);
if(getY() > getWorld().getHeight() - 1)
getWorld().removeObject(this);
else if(getY() < 1)
getWorld().removeObject(this);
}
//"destroyEnemies()" destroys enemies.
public void destroyHelicopter()
{
//"Enemy" can be any class that you want the bullet to destroy.
Actor helicopter = getOneIntersectingObject(Helicopter.class);
if(helicopter != null) {
getWorld().removeObject(helicopter);
Myworld myworld = (Myworld)getWorld();
myworld.addScore(20);
}
}
private int speed = 10;
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This is a white blood cell. This kind of cell has the job to catch bacteria and
* remove them from the blood.
*
* @author Michael Kölling
* @version 1.0
*/
public class Aircraft extends Actor
{
Boolean spaceDown = false;
/**
* Act: move up and down when cursor keys are pressed.
*/
public void act()
{
checkKeyPress();
checkCollision();
checkFire();
//leave the rest of your player's act method alone
}
//after the "act()" method, add a new method:
public void checkFire()
{
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
spaceDown = true;
getWorld().addObject(new Missile(), getX(), getY());
}
if (spaceDown && !Greenfoot.isKeyDown("space"))
{
spaceDown = false;
}
/*if(Greenfoot.isKeyDown("space")) {
getWorld().addObject(new Missile(), getX(), getY());
}*/
}
/**
* Check whether a keyboard key has been pressed and react if it has.
*/
private void checkKeyPress()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-8);
}
if (Greenfoot.getRandomNumber(100) < 99)
{
setLocation(getX(), getY()+3);
}
if (Greenfoot.getRandomNumber(100) < 99)
{
setLocation(getX(), getY());
}
if (Greenfoot.isKeyDown("left"))
{
}
}
/**
* Check whether we are touching a bacterium or virus. Remove bacteria.
* Game over if we hit a virus.
*/
private void checkCollision()
{
if (isTouching(Bacteria.class))
{
Greenfoot.playSound("slurp.wav");
removeTouching(Bacteria.class);
Myworld myworld = (Myworld)getWorld();
myworld.addScore(20);
}
if (isTouching(Helicopter.class))
{
removeTouching(Helicopter.class);
Myworld myworld = (Myworld)getWorld();
myworld.addScore(-20);
}
}
}
