import greenfoot.*;
/**
* Write a description of class Airplane here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Airplane extends AnimBase
{
GreenfootSound Bullet = new GreenfootSound ("Bullet.wav");
GreenfootSound Grenade = new GreenfootSound ("Grenade.wav");
GreenfootSound GameOver = new GreenfootSound ("GameOver.wav");
GreenfootSound backgroundMusic = new GreenfootSound("Background.wav");
int bulletStep=0;
protected int Score = 0;
protected int Point = 0;
/**
* Act - do whatever the Airplane wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void stopped ()
{
Point = 0;
Score = 0;
}
public void act()
{
move (1);
Actor Ok = getOneIntersectingObject(Ok.class);
if(Ok!= null && Greenfoot.isKeyDown("Left"))
{
move (+10);
}
bombDetect();
{
catchKey();
}
stopped();
}
private void catchKey()
{
catchKeyMove();
catchKeyShoot();
}
private void catchKeyMove()
{
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-5, getY());
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+5, getY());
}
if (Greenfoot.isKeyDown("Up"))
{
setLocation(getX(), getY()-3);
}
if (Greenfoot.isKeyDown("Down"))
{
setLocation(getX(), getY()+3);
}
}
private void catchKeyShoot()
{
if(Greenfoot.isKeyDown("space"))
{
Bullet.play ();
bulletStep++;
if(bulletStep==10)
{
Bullets b = new Bullets();
int posX = getX()+Math.floorDiv(getImage().getWidth(),2)+Math.floorDiv(b.getImage().getWidth(),2);
getWorld().addObject(b, posX, getY());
bulletStep=0;
}
}
}
public void bombDetect()
{
GameOver gameover = new GameOver();
if (isTouching ( Bomb.class))
{
Grenade.play ();
Greenfoot.setWorld(gameover);
backgroundMusic.stop ();
GameOver.play ();
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bullets here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullets extends AnimBase
{
/**
* Act - do whatever the Bullets wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (isTouching (Robot.class))
{
removeTouching (Robot.class);
}
detectCollision ();
if(getY()<getWorld().getWidth()-getImage().getWidth()/2)
{
move (10);
}
else
getWorld().removeObject(this);
}
public void detectCollision()
{
if (getY()>5000)
{
getWorld().removeObject(this);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bomb here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bomb extends AnimBase
{
/**
* Act - do whatever the Bomb wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(getY()<getWorld().getWidth()-getImage().getWidth()/2)
move (-2);
else
getWorld().removeObject(this);
}
public void RemoveBomb ()
{
if (isTouching(Ok.class))
{
Score = 0;
getWorld().removeObject(this);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Robot here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Robot extends AnimBase
{
/**
* Act - do whatever the Robot wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
protected int Score = 0;
public void act()
{
animCounter = 0;
move (20);
turn(Greenfoot.getRandomNumber(20));
{
animCounter++;
animMove();
detectCollision();
}
}
private void animMove()
{
if(animCounter<10)
{
setLocation(getX(), getY()-2);
turn(Greenfoot.getRandomNumber(10));
//Greenfoot.delay(5);
}
else if(animCounter<30)
{
setLocation(getX(), getY()+2);
turn(Greenfoot.getRandomNumber(10));
//Greenfoot.delay(5);
}
else if(animCounter<40)
{
setLocation(getX(), getY()-2);
turn(Greenfoot.getRandomNumber(10));
//Greenfoot.delay(5);
}
else
{
animCounter=-1;
}
}
private void detectCollision()
{
if(isTouching(Bullets.class))
{
removeTouching(Bullets.class);
updatePoint();
checkLast();
getWorld().removeObject(this);
}
}
private void updatePoint()
{
point++;
getWorld().showText("Score :"+point,60, 10);
}
YouWin youwin = new YouWin();
private void checkLast()
{
if(point==30)
{
getWorld().showText("Finish Score "+point,getWorld().getWidth()/2, getWorld().getHeight()/2);
removeBullet();
removeHero();
Greenfoot.stop();
Greenfoot.setWorld (youwin);
}
}
public void removeBullet()
{
if(getWorld().getObjects(Bullets.class)!=null)
for(Bullets b : getWorld().getObjects(Bullets.class))
{
getWorld().removeObject(b);
}
}
public void removeHero()
{
if(getWorld().getObjects(Airplane.class)!=null)
getWorld().removeObject(getWorld().getObjects(Airplane.class).get(0));
}
}
