import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Ship extends Actor
{
// True or false statement for touching other actors
boolean touchingAlien = false;
boolean touchingFinalBoss = false;
boolean touchingBossLaser = false;
private ScoreBoard score;
public Ship(ScoreBoard score)
{
this.score = score;
}
public void act()
{
movement();
Boundry();
PowerUpMovement();
hitAlien();
hitFinalBoss();
}
public void movement() {
// User input for navigating ship
if (Greenfoot.isKeyDown("up")) {
setLocation(getX(), getY() - 5);
}
if (Greenfoot.isKeyDown("down")) {
setLocation(getX(), getY() + 5);
}
if (Greenfoot.isKeyDown("right")) {
setLocation(getX() + 5, getY());
}
if (Greenfoot.isKeyDown("left")) {
setLocation(getX() - 5, getY());
}
// User input for shooting lasers (works when key is lifted)
if ("space".equals(Greenfoot.getKey())) {
shoot();
}
}
public void PowerUpMovement() {
World testShop = getWorld();
TestShop testshop = (TestShop)testShop;
if (getWorld() != null) {
if (testshop.OptionSpeed() == true) {
if (Greenfoot.isKeyDown("up")) {
setLocation(getX(), getY() - 5);
}
if (Greenfoot.isKeyDown("down")) {
setLocation(getX(), getY() + 5);
}
if (Greenfoot.isKeyDown("right")) {
setLocation(getX() + 5, getY());
}
if (Greenfoot.isKeyDown("left")) {
setLocation(getX() - 5, getY());
}
// User input for shooting lasers (works when key is lifted)
if ("space".equals(Greenfoot.getKey())) {
shoot();
}
}
}
}
public void shoot() {
getWorld().addObject(new Laser(score), getX(), getY());
}
public void hitAlien() {
// Define the actor as the ship touching an alien
Actor alien = getOneIntersectingObject(Alien.class);
if (alien != null) {
World myWorld = getWorld();
MyWorld myworld = (MyWorld)myWorld;
HealthBar healthbar = myworld.getHealthBar();
if (touchingAlien == false) {
healthbar.loseHealth();
touchingAlien = true;
if (healthbar.health <= 0) {
// Game over since health has hit 0
myWorld.removeObject(this);
Greenfoot.setWorld(new GameOver());
}
}
else {
// Continue game
touchingAlien = false;
}
}
}
public void hitFinalBoss() {
if (getWorld() != null) {
Actor finalboss = getOneIntersectingObject(FinalBoss.class);
Actor bosslaser = getOneIntersectingObject(BossLaser.class);
if (finalboss != null || bosslaser != null) {
World finalWorld = getWorld();
FinalWorld finalworld = (FinalWorld)finalWorld;
HealthBar healthbar = finalworld.getHealthBar();
if (touchingFinalBoss == false || touchingBossLaser == false) {
healthbar.loseHealth();
touchingFinalBoss = true;
touchingBossLaser = true;
if (healthbar.health <= 0) {
// Game over since health has hit 0
finalWorld.removeObject(this);
Greenfoot.setWorld(new GameOver());
}
}
else {
// Continue game
touchingFinalBoss = false;
touchingBossLaser = false;
}
}
}
}
public void Boundry() {
if (getX() > 980) {
setLocation(980, getY());
}
if (getX() < 0) {
setLocation(0, getY());
}
if (getY() > 575) {
setLocation(getX(), 575);
}
if (getY() < 0) {
setLocation(getX(), 0);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class TestShop extends World
{
public TestShop()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(980, 575, 1);
prepare();
OptionHealth();
OptionSpeed();
}
public void act()
{
}
public boolean OptionHealth()
{
if ("a".equals(Greenfoot.getKey())) {
return true;
}
return false;
}
public boolean OptionSpeed()
{
if ("s".equals(Greenfoot.getKey())) {
return true;
}
return false;
}
public void prepare ()
{
}
}

