import java.awt.*;
import java.awt.image.BufferedImage;
public abstract class FlyObject {
protected int x;
protected int y;
protected int height;
protected int width;
protected int speed;
protected BufferedImage image;
public void draw(Graphics g){
g.drawImage(image,x,y,null);
}
public abstract void move();
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
}
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Airplane extends FlyObject implements Enemy,Award{
private int speed = 10;
private int x;
private int y;
private int height;
private int width;
private boolean alive;
private BufferedImage image;
private static Random random = new Random(47);
public Airplane(BufferedImage image,int speed){
this(image,random.nextInt(FlyFrame.WIDTH),0,50,50,speed);
}
public Airplane(BufferedImage image,int x,int y,int height,int width,int speed){
this.image = image;
this.height = height;
this.width = width;
this.x = x;//敌机初始化横坐标是0-(窗口宽度-敌机图片宽度) 不会出界
this.y = y;//敌机初始化纵坐标是负的敌机高度
this.speed = speed;
this.alive = true;
}
public int getAward(){
return (int)Math.random()*3+1;
}
public int score(){
return 50;
}
public void clear(){
setHeight(0);
setWidth(0);
setX(0);
setY(0);
}
public void move() {
int direction = (int) (Math.random() * 10);
/*if (direction < 2) {
this.x = this.x - 50;
if (this.x < 0) {
this.x = 50;
}
}*/
if (direction > 4 && direction < 7) {
this.y = y + 10;
if (this.y > FlyFrame.HEIGHT) {
this.y = FlyFrame.HEIGHT - 10;
}
}
if (direction > 7) {
this.y = y + 10;
if (this.y > FlyFrame.HEIGHT) {
this.y = FlyFrame.HEIGHT - 10;
}
}
/*if (direction < 4 && direction > 2) {
this.x = this.x + 30;
if (this.x > FlyFrame.WIDTH - 30) {
this.x = FlyFrame.WIDTH;
}
}*/
}
static class AirplaneFactory{
public static Airplane createAirplane(BufferedImage image,int speed){
return new Airplane(image,speed);
}
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
@Override
public int getSpeed() {
return speed;
}
@Override
public void setSpeed(int speed) {
this.speed = speed;
}
@Override
public int getX() {
return x;
}
@Override
public void setX(int x) {
this.x = x;
}
@Override
public int getY() {
return y;
}
@Override
public void setY(int y) {
this.y = y;
}
@Override
public int getHeight() {
return height;
}
@Override
public void setHeight(int height) {
this.height = height;
}
@Override
public int getWidth() {
return width;
}
@Override
public void setWidth(int width) {
this.width = width;
}
@Override
public BufferedImage getImage() {
return image;
}
@Override
public void setImage(BufferedImage image) {
this.image = image;
}
public Rectangle getRectangle(){
return new Rectangle(x,y,width,height);
}
static class AirPlaneFactory{
public static Airplane createAirplane(BufferedImage image,int x,int y,int height,int width,int speed){
return new Airplane(image,x,y,height,width,speed);
}
}
}
import java.awt.*;
import java.awt.image.BufferedImage;
public class Bullet extends FlyObject{
private double degree;
private int x;
private int y;
private int height;
private int width;
private int speed;
private BufferedImage image;
private boolean alive;
public Bullet(BufferedImage image,int x, int y,int speed){
this(image,x,y,50,50,speed);
}
public Bullet(BufferedImage image,int x, int y,int height,int width,int speed){
this.x = x;//子弹横坐标随飞机
this.y = y;//子弹纵坐标随飞机
this.image = image;
this.height = height;
this.width = width;
this.speed = speed;
this.degree = Math.PI*Math.random();//随机角度
this.alive = true;
}
public void clear(){
setHeight(0);
setWidth(0);
setX(0);
setY(0);
}
public void move(){
//this.x += speed*Math.cos(degree);