import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Graphics;
/** 飞行物 */
public abstract class FlyingObject {
protected int width;
protected int height;
protected int x;
protected int y;
public static final int LIFE = 0;
public static final int DEAD = 1;
public static final int REMOVE = 2;
protected int state = LIFE;
public abstract void step();
public abstract BufferedImage getImage();
public void paint(Graphics g){
g.drawImage(getImage(),x,y,null);
}
public boolean isLife(){
return state==LIFE;
}
public boolean isDead(){
return state==DEAD;
}
public boolean isRemove(){
return state==REMOVE;
}
public void goDead(){
state = DEAD;
}
public boolean hit(FlyingObject other){
int x1 = this.x-other.width;
int x2 = this.x+this.width;
int y1 = this.y-other.height;
int y2 = this.y+this.height;
int x = other.x;
int y = other.y;
return x>=x1 && x<=x2 && y>=y1 && y<=y2;
}
public abstract boolean outOfBounds();
}
public interface Award {
public int DOUBLE_FIRE = 0;
public int LIFE = 1;
public int getType();
}
```java得分接口
public interface Enemy {
public int getScore();
}
```java敌机源码
import java.awt.image.BufferedImage;
/** 小敌机 */
public class Airplane extends FlyingObject implements Enemy {
private int step;
/** 构造方法 */
public Airplane(){
width = 48;
height = 50;
x = (int)(Math.random()*(World.WIDTH-this.width));
y = -this.height;
step = 2; //移动速度
}
/** 小敌机走步step() */
public void step(){
y+=step;
}
int deadIndex = 1;
public BufferedImage getImage(){
if(isLife()){
return Images.airplanes[0];
}else if(isDead()){
BufferedImage img = Images.airplanes[deadIndex++];
if(deadIndex==Images.airplanes.length){
state = REMOVE;
}
return img;
}
return null;
}
public int getScore(){
return 1;
}
public boolean outOfBounds(){
return this.y>=World.HEIGHT;
}
}
大敌机源码
import java.awt.image.BufferedImage;
/** 大敌机 */
public class BigAirplane extends FlyingObject implements Enemy {
private int step;
/** 构造方法 */
public BigAirplane(){
width = 66;
height = 89;
x = (int)(Math.random()*(World.WIDTH-this.width));
y = -this.height;
step = 2;
}
/** 大敌机走步step() */
public void step(){
y+=step;
}
int deadIndex = 1;
public BufferedImage getImage(){
if(isLife()){
return Images.bigairplanes[0];
}else if(isDead()){
BufferedImage img = Images.bigairplanes[deadIndex++];
if(deadIndex==Images.bigairplanes.length){
state = REMOVE;
}
return img;
}
return null;
}
public int getScore(){
return 3;
}
public boolean outOfBounds(){
return this.y>=World.HEIGHT;
}
}
小蜜蜂源码
import java.awt.image.BufferedImage;
/** 小蜜蜂 */
public class Bee extends FlyingObject implements Award {
private int xStep; //x坐标走步
private int yStep; //y坐标走步
private int awardType; //奖励类型
/** 构造方法 */
public Bee(){
width = 60;
height = 51;
x = (int)(Math.random()*(World.WIDTH-this.width));
y = -this.height;
xStep = 1;
yStep = 2;
awardType = (int)(Math.random()*2);
}
/** 蜜蜂走步step() */
public void step(){
x+=xStep;
y+=yStep;
if(x>=World.WIDTH-this.width || x<=0){
xStep*=-1;
}
}
int deadIndex = 1;
public BufferedImage getImage(){
if(isLife()){
return Images.bees[0];
}else if(isDead()){
BufferedImage img = Images.bees[deadIndex++];
if(deadIndex==Images.bees.length){
state = REMOVE;
}
return img;
}
return null;
}
public int getType(){
return awardType;
}
public boolean outOfBounds(){
return this.y>=World.HEIGHT;
}
}
子弹源码
import java.awt.image.BufferedImage;
/** 子弹 */
public class Bullet extends FlyingObject {
private int step;
/** 构造方法 */
public Bullet(int x,int y){
width = 8;
height = 20;
this.x = x;
this.y = y;
step = 3;
}
/** 子弹走步step() */
public void step(){
y-=step;
}
public BufferedImage getImage(){
if(isLife()){
return Images.bullet;
}
if(isDead()){
state = REMOVE;
}
return null;
}
public boolean outOfBounds(){
return this.y<=-this.height;
}
}
英雄级源码
import java.awt.image.BufferedImage;
/** 英雄机 */
public class Hero extends FlyingObject {
private int life; //命
private int doubleFire; //火力值
/** 构造方法 */
public Hero(){
width = 97;
height = 139;
x = 140;
y = 400;
life = 3;
doubleFire = 0;
}
public void step(){
}
/** 英雄机随着鼠标动 x:鼠标的x坐标 y:鼠标的y坐标 */
public void moveTo(int x,int y){
this.x = x-this.width/2;
this.y = y-this.height/2;
}
private int index = 0;
public BufferedImage getImage(){
if(isLife()){
return Images.heros[index++%Images.heros.length];
}
return null;
}
public Bullet[] shoot(){
int xStep = this.width/4;
int yStep = 20;
if(doubleFire>0){
Bullet[] bs = new Bullet[2];
bs[0] = new Bullet(this.x+1*xStep,this.y-yStep);
bs[1] = new Bullet(this.x+3*xStep,this.y-yStep);
doubleFire-=2;
return bs;
}else{
Bullet[] bs = new Bullet[1];
bs[0] = new Bullet(this.x+2*xStep,this.y-yStep);
return bs;
}
}
public void addLife(){
life++;
}
public void addDoubleFire(){
doubleFire+=40;
}
public int getLife(){
return life;
}
public void subtractLife(){
life--;
}
public int getDoubleFire(){
return doubleFire;
}
public void clearDoubleFire(){
doubleFire = 0;
}
public boolean outOfBounds(){
return false;
}
}
初始化图片
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/** 图片工具类 */
public class Images {
public static BufferedImage sky;
public static BufferedImage bullet;
public static BufferedImage[] airplanes;
public static BufferedImage[] bigairplanes;
public static BufferedImage[] bees;
public static BufferedImage[] heros;
static{
sky = loadImage("background.png");
bullet = loadImage("bullet.png");
airplanes = new BufferedImage[5];
airplanes[0] = loadImage("airplane0.png");
bigairplanes = new BufferedImage[5];
bigairplanes[0] = loadImage("bigairplane0.png");
bees = new BufferedImage[5];
bees[0] = loadImage("bee0.png");
for(int i=1;i<airplanes.length;i++){
airplanes[i] = loadImage("bom"+i+".png");
bigairplanes[i] = loadImage("bom"+i+".png");
bees[i] = loadImage("bom"+i+".png");
}
heros = new BufferedImage[2];
for(int i=0;i<heros.length;i++){
heros[i] = loadImage("hero"+i+".png");
}
}
public static BufferedImage loadImage(String fileName){
try{
BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName));
return img;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}
}
}
天空源码
import java.awt.image.BufferedImage;
import java.awt.Graphics;
/** 天空 */
public class Sky extends FlyingObject {
private int step;
private int y1; //天空移动
/** 构造方法 */
public Sky(){
width = World.WIDTH;
height = World.HEIGHT;
x = 0;
y = 0;
step = 1;
y1 = -this.height;
}
/** 天空走步step() */
public void step(){
y+=step;
y1+=step;
if(y>=this.height){
y=-this.height;
}
if(y1>=this.height){
y1=-this.height;
}
}
public BufferedImage getImage(){
return Images.sky;
}
public void paint(Graphics g){
g.drawImage(getImage(),x,y,null);
g.drawImage(getImage(),x,y1,null);
}
public boolean outOfBounds(){
return false;
}
}
界面
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
/** 世界 */
public class World extends JPanel{
public static final int WIDTH = 400;
public static final int HEIGHT = 700;
private Sky sky = new Sky();
private Hero hero = new Hero();
private FlyingObject[] enemies = {}; //敌人数组
private Bullet[] bullets = {}; //子弹数组
public static final int START = 0;
public static final int RUNNING = 1;
public static final int PAUSE = 2;
public static final int GAME_OVER = 3;
private int state = START;
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
static{
start = Images.loadImage("start.png");
pause = Images.loadImage("pause.png");
gameover = Images.loadImage("gameover.png");
}
public FlyingObject nextOne(){
Random rand = new Random();
int type = rand.nextInt(20);
if(type < 5){
return new Bee();
}else if(type < 12){
return new Airplane();
}else{
return new BigAirplane();
}
}
int flyEnterIndex = 0;
public void enterAction(){
flyEnterIndex++;
if(flyEnterIndex%1==0){
FlyingObject obj = nextOne();
enemies = Arrays.copyOf(enemies,enemies.length+1);
enemies[enemies.length-1] = obj;
}
}
public void stepAction(){
sky.step();
for(int i=0;i<enemies.length;i++){
enemies[i].step();
}
for(int i=0;i<bullets.length;i++){
bullets[i].step();
}
}
int shootIndex = 0;
public void shootAction(){
shootIndex++;
if(shootIndex%3==0){
Bullet[] bs = hero.shoot();
bullets = Arrays.copyOf(bullets,bullets.length+bs.length);
System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
}
}
private int score = 0;
public void bangAction(){
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];
for(int j=0;j<enemies.length;j++){
FlyingObject f = enemies[j];
if(b.isLife() && f.isLife() && b.hit(f)){
b.goDead();
f.goDead();
if(f instanceof Enemy){
Enemy e = (Enemy)f;
score += e.getScore();
}
if(f instanceof Award){
Award a = (Award)f;
int type = a.getType();
switch(type){
case Award.DOUBLE_FIRE:
hero.addDoubleFire();
break;
case Award.LIFE:
hero.addLife();
break;
}
}
}
}
}
}
public void outOfBoundsAction(){
int index = 0;
FlyingObject[] enemyLives = new FlyingObject[enemies.length];
for(int i=0;i<enemies.length;i++){
FlyingObject e = enemies[i];
if(!e.outOfBounds() && !e.isRemove()){
enemyLives[index++] = e;
}
}
enemies = Arrays.copyOf(enemyLives,index);
index = 0;
Bullet[] bulletLives = new Bullet[bullets.length];
for(int i=0;i<bullets.length;i++){
Bullet b = bullets[i];
if(!b.outOfBounds() && !b.isRemove()){
bulletLives[index++] = b;
}
}
bullets = Arrays.copyOf(bulletLives,index);
}
public void hitAction(){
for(int i=0;i<enemies.length;i++){
FlyingObject f = enemies[i];
if(f.isLife() && hero.isLife() && hero.hit(f)){
f.goDead();
hero.subtractLife();
hero.clearDoubleFire();
}
}
}
public void checkGameOverAction(){
if(hero.getLife()<=0){
state = GAME_OVER;
}
}
int num = 1;
public void action(){ //启动执行
MouseAdapter l = new MouseAdapter(){
public void mouseMoved(MouseEvent e){
if(state==RUNNING){
int x = e.getX();
int y = e.getY();
hero.moveTo(x, y);
}
}
public void mouseClicked(MouseEvent e){
switch(state){
case START:
state = RUNNING;
break;
case GAME_OVER:
score = 0;
hero = new Hero();
enemies = new FlyingObject[0];
bullets = new Bullet[0];
state = START;
break;
}
}
public void mouseExited(MouseEvent e){
if(state==RUNNING){
state = PAUSE;
}
}
public void mouseEntered(MouseEvent e){
if(state==PAUSE){
state = RUNNING;
}
}
};
this.addMouseListener(l);
this.addMouseMotionListener(l);