这个飞机大战的游戏我采用的开发模式是工厂模式,首先有一个工厂类,专门负责产生我们所需要的对象,然后有一个实体包,里面对应的是项目中所需要的自定义类的实体文件,UI包里面是一个主控制类,负责我们整个游戏的业务逻辑控制以及界面的绘制,config包里面主要是我们所需要的一些工具类以及常量类
代码如下:
config包:三个枚举类BulletType,dir,Gamestate,分别是子弹类型,方向控制,游戏状态。Conts为常量类,SoundUtils声音工具类,
BulletType:

package com.game.config;public enum BulletType {Normal,Boom,Normal1,Normal2;
}

Conts:

package com.game.config;import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Random;import javax.imageio.ImageIO;public class Conts {public static int EMMAX=250;
public static int PLAYER_BULLTE_MAX=20;
public static int PLAYER_BULLTE_X_MAX=50;
public static final Random RAN = new Random();
public static final int GAME_WEIDTH=420;
public static final int GAME_HEIGHT=650;
public static final int HP_Height=10;
public static HashMap<String,BufferedImage> IMAGES=new HashMap<String,BufferedImage>();
public static HashMap<String,File> AUDIOS=new HashMap<String,File>();//静态代码块
static{try {URI uri=Conts.class.getResource("/sucai").toURI();File f=new File(uri);File[] fs=f.listFiles();for (File ft : fs) {if(ft.getName().toLowerCase().endsWith(".jpg")||ft.getName().toLowerCase().endsWith(".png")||ft.getName().toLowerCase().endsWith(".gif")){IMAGES.put(ft.getName(), ImageIO.read(ft));}else if(ft.getName().toLowerCase().endsWith(".wav")){AUDIOS.put(ft.getName(), ft);}}} catch (URISyntaxException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

DIR:

package com.game.config;public enum Dir {Down,Left,Right;
}

GameState:

package com.game.config;public enum GameState {Gameing,Pause,Over,BeforGame,Pass
}

SoundUtils

package com.game.config;import java.io.File;
import java.io.IOException;import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;public class SoundUtils {   public static void Play(final File  f,final boolean loop){new Thread(){@Overridepublic void run() {SoundUtils.playMusic(loop,f );}}.start();}public static void playMusic(boolean loop,File file) {byte[] audioData = new byte[1024];AudioInputStream ais = null;        SourceDataLine line = null;try {ais = AudioSystem.getAudioInputStream(file);} catch (Exception e) {e.printStackTrace();}if (ais != null) {AudioFormat baseFormat = ais.getFormat();DataLine.Info info = new DataLine.Info(SourceDataLine.class,baseFormat);try {line = (SourceDataLine) AudioSystem.getLine(info);line.open(baseFormat);} catch (LineUnavailableException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (line == null) {return;}line.start();int inByte = 0;while (inByte != -1) {if(true){try {inByte = ais.read(audioData, 0, 1024);} catch (IOException ioe) {ioe.printStackTrace();}try {if (inByte > 0) {line.write(audioData, 0, inByte);}} catch (Exception e) {e.printStackTrace();}}}line.drain();line.stop();line.close();if (loop) {playMusic(loop,file);}}}

factory包里面是一个工厂类,负责产生对象

package com.game.factory;import com.game.config.BulletType;
import com.game.config.Conts;
import com.game.config.Dir;
import com.game.model.Boss;
import com.game.model.Bullet;
import com.game.model.EmPlane;
import com.game.model.Plane;
import com.game.model.Prop;public class Factory {public static Plane getPlane(int pindex){Plane p = new Plane();p.setImage("player"+pindex+".png");p.setShadow("shadow"+pindex+".png");if(pindex==0){p.setId(pindex);p.setDefense(0.9);p.setMaxindex(2);p.setSpeedx(30);p.setSpeedy(30);p.setMaxHp(1000);   p.setAttact(120);}else if(pindex==1){p.setId(pindex);p.setDefense(0.8);p.setAttact(100);p.setMaxindex(1);p.setSpeedx(30);p.setSpeedy(20);p.setMaxHp(2000);   }else if(pindex==2){p.setId(pindex);p.setDefense(0.7);p.setAttact(80);p.setMaxindex(1);p.setSpeedx(20);p.setSpeedy(20);p.setMaxHp(3000);   }else if(pindex==3){p.setId(pindex);p.setDefense(0.6);p.setAttact(60);p.setMaxindex(1);p.setSpeedx(10);p.setSpeedy(20);p.setMaxHp(4000);   }p.setHp(p.getMaxHp());p.setBoss(-1);p.setHeight(Conts.IMAGES.get(p.getImage()).getHeight());p.setWidth(Conts.IMAGES.get(p.getImage()).getWidth()/p.getMaxindex());  p.setX((Conts.GAME_WEIDTH-p.getWidth())/2);p.setY(Conts.GAME_HEIGHT-p.getHeight()-50);return p;}public static void getBullet(Plane p,BulletType bt){if(p.getId()==0){if(bt==BulletType.Normal){Bullet b = new Bullet();b.setImage("bullet2.png");b.setAttact(p.getAttact());b.setBt(BulletType.Normal);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(40);if(p.getBlist().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist().add(b);}else{for (int i = 0; i < p.getBlist().size(); i++) {if(p.getBlist().get(i)==null){p.getBlist().set(i, b);break;}}}   }else if(bt==BulletType.Normal1){Bullet b = new Bullet();b.setImage("bullet03.png");b.setAttact(50);b.setBt(BulletType.Normal1);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(10);b.setSpeedx(2);if(p.getBlist1().size()<Conts.PLAYER_BULLTE_X_MAX){p.getBlist1().add(b);}else{for (int i = 0; i < p.getBlist1().size(); i++) {if(p.getBlist1().get(i)==null){p.getBlist1().set(i, b);break;}}}}else{Bullet b = new Bullet();b.setImage("zhadan1.png");b.setAttact(500);b.setBt(BulletType.Boom);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(40);if(p.getBlist().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist().add(b);}else{for (int i = 0; i < p.getBlist().size(); i++) {if(p.getBlist().get(i)==null){p.getBlist().set(i, b);break;}}}}}else if(p.getId()==1){if(bt==BulletType.Normal){Bullet b = new Bullet();b.setImage("m1.png");b.setAttact(p.getAttact());b.setBt(BulletType.Normal);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(60);if(p.getBlist().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist().add(b);}else{for (int i = 0; i < p.getBlist().size(); i++) {if(p.getBlist().get(i)==null){p.getBlist().set(i, b);break;}}}   }else if(bt==BulletType.Normal1){Bullet b = new Bullet();int id =Conts.RAN.nextInt(100);if(id<50)b.setId(0);else b.setId(1);b.setImage("m9.png");b.setAttact(50);b.setBt(BulletType.Normal1);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(10);b.setSpeedx(2);if(p.getBlist1().size()<Conts.PLAYER_BULLTE_X_MAX){p.getBlist1().add(b);}else{for (int i = 0; i < p.getBlist1().size(); i++) {if(p.getBlist1().get(i)==null){p.getBlist1().set(i, b);break;}}}}else{Bullet b = new Bullet();b.setImage("m10.png");b.setAttact(500);b.setBt(BulletType.Boom);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(25);if(p.getBlist().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist().add(b);}else{for (int i = 0; i < p.getBlist().size(); i++) {if(p.getBlist().get(i)==null){p.getBlist().set(i, b);break;}}}}}else if(p.getId()==2){if(bt==BulletType.Normal){Bullet b = new Bullet();b.setImage("m8.png");b.setAttact(p.getAttact());b.setBt(BulletType.Normal);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(60);if(p.getBlist().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist().add(b);}else{for (int i = 0; i < p.getBlist().size(); i++) {if(p.getBlist().get(i)==null){p.getBlist().set(i, b);break;}}}   } elseif(bt==BulletType.Normal1){Bullet b = new Bullet();int id =Conts.RAN.nextInt(100);if(id<50)b.setId(0);else b.setId(1);b.setImage("m7.png");b.setAttact(40);b.setBt(BulletType.Normal1);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(10);b.setSpeedx(5);if(p.getBlist1().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist1().add(b);}else{for (int i = 0; i < p.getBlist1().size(); i++) {if(p.getBlist1().get(i)==null){p.getBlist1().set(i, b);break;}}}   }else{Bullet b = new Bullet();b.setImage("m11.png");b.setAttact(300);b.setBt(BulletType.Boom);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(15);if(p.getBlist().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist().add(b);}else{for (int i = 0; i < p.getBlist().size(); i++) {if(p.getBlist().get(i)==null){p.getBlist().set(i, b);break;}}}}}else if(p.getId()==3){if(bt==BulletType.Normal){Bullet b = new Bullet();b.setImage("m12.png");b.setAttact(p.getAttact());b.setBt(BulletType.Normal);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(40);if(p.getBlist().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist().add(b);}else{for (int i = 0; i < p.getBlist().size(); i++) {if(p.getBlist().get(i)==null){p.getBlist().set(i, b);break;}}}   }else if(bt==BulletType.Normal1){Bullet b = new Bullet();b.setImage("m13.png");b.setAttact(30);b.setBt(BulletType.Normal1);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(10);b.setSpeedx(2);if(p.getBlist1().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist1().add(b);}else{for (int i = 0; i < p.getBlist1().size(); i++) {if(p.getBlist1().get(i)==null){p.getBlist1().set(i, b);break;}}}}else{Bullet b = new Bullet();b.setImage("m111.png");b.setAttact(250);b.setBt(BulletType.Boom);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(p.getY());b.setX(p.getX()+(p.getWidth()-b.getWidth())/2);b.setSpeedy(10);if(p.getBlist().size()<Conts.PLAYER_BULLTE_MAX){p.getBlist().add(b);}else{for (int i = 0; i < p.getBlist().size(); i++) {if(p.getBlist().get(i)==null){p.getBlist().set(i, b);break;}}}}}       }public static void getEmBullet(EmPlane ep){if(ep.getDir()==Dir.Down){if(Conts.RAN.nextInt(100)<5){Bullet b = new Bullet();b.setImage("bullet00.png");b.setAttact(50);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(ep.getY());b.setX(ep.getX()+(ep.getWidth()-b.getWidth())/2);b.setSpeedy(20);ep.getBlist().add(b);}}else if(ep.getDir()==Dir.Right){if(Conts.RAN.nextInt(100)<10){Bullet b = new Bullet();b.setImage("bullet3.png");b.setAttact(80);b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(ep.getY());b.setX(ep.getX()+(ep.getWidth()-b.getWidth())/2);b.setSpeedy(15);ep.getBlist().add(b);}}else if(ep.getBoss()==1){Bullet b = new Bullet();if(Conts.RAN.nextInt(100)<10){          b.setImage("bossb.png");b.setAttact(500);}else if(Conts.RAN.nextInt(100)<40){            b.setImage("m9.png");b.setAttact(20);}if(b.getImage()!=null){b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(ep.getY()+ep.getHeight()-10);b.setX(ep.getX()+(ep.getWidth()-b.getWidth())/2);b.setSpeedy(40);ep.getBlist().add(b);}}/*** 需要修改* */else if(ep.getBoss()==2){Bullet b = new Bullet();if(Conts.RAN.nextInt(100)<10){          b.setImage("bossb.png");b.setAttact(500);}else if(Conts.RAN.nextInt(100)<40){            b.setImage("m9.png");b.setAttact(20);}if(b.getImage()!=null){b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(ep.getY()+ep.getHeight()-10);b.setX(ep.getX()+(ep.getWidth()-b.getWidth())/2);b.setSpeedy(40);ep.getBlist().add(b);}}else if(ep.getBoss()==3){Bullet b = new Bullet();if(Conts.RAN.nextInt(100)<10){          b.setImage("bossb.png");b.setAttact(500);}else if(Conts.RAN.nextInt(100)<40){            b.setImage("m9.png");b.setAttact(20);}if(b.getImage()!=null){b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(ep.getY()+ep.getHeight()-10);b.setX(ep.getX()+(ep.getWidth()-b.getWidth())/2);b.setSpeedy(40);ep.getBlist().add(b);}}else if(ep.getBoss()==4){Bullet b = new Bullet();if(Conts.RAN.nextInt(100)<10){          b.setImage("bossb.png");b.setAttact(500);}else if(Conts.RAN.nextInt(100)<40){            b.setImage("m9.png");b.setAttact(20);}if(b.getImage()!=null){b.setHeight(Conts.IMAGES.get(b.getImage()).getHeight());b.setWidth(Conts.IMAGES.get(b.getImage()).getWidth());  b.setY(ep.getY()+ep.getHeight()-10);b.setX(ep.getX()+(ep.getWidth()-b.getWidth())/2);b.setSpeedy(40);ep.getBlist().add(b);}}}public static Prop getProp(int index){Prop p = new Prop();if(index==0){p.setImage("hp.png");}else if(index==1){p.setImage("att.png");}else if(index==2){p.setImage("alldeath.png");}else if(index==3){p.setImage("protect.png");}return p;}public static Boss getBoss(int level){Boss boss = new Boss();boss.setBoss(level);boss.setImage("boss"+level+".png"); boss.setHeight(Conts.IMAGES.get(boss.getImage()).getHeight());boss.setWidth(Conts.IMAGES.get(boss.getImage()).getWidth());boss.setDir(Dir.Left);boss.setMaxHp(10000*level);boss.setHp(10000*level);boss.setX((Conts.GAME_WEIDTH-boss.getWidth())/2);boss.setY(0);boss.setSpeedx(5);return boss;}public static EmPlane getEmPlane(){EmPlane ep = new EmPlane();if(Conts.RAN.nextInt(100)<80){if(Conts.RAN.nextInt(100)<5)ep.setProp(Conts.RAN.nextInt(4) );elseep.setProp(7 );ep.setImage("a2-1.png");ep.setDir(Dir.Down);ep.setSpeedy(10);ep.setBoss(0);ep.setMaxHp(200);ep.setHp(ep.getMaxHp());ep.setHeight(Conts.IMAGES.get(ep.getImage()).getHeight());ep.setWidth(Conts.IMAGES.get(ep.getImage()).getWidth());    ep.setX(Conts.RAN.nextInt(Conts.GAME_WEIDTH-ep.getWidth()));ep.setY(-ep.getHeight());}else if(Conts.RAN.nextInt(100)<15){if(Conts.RAN.nextInt(100)<8)ep.setProp(Conts.RAN.nextInt(4) );elseep.setProp(7);ep.setBoss(0);ep.setDir(Dir.Right);ep.setImage("r.png");ep.setMaxHp(400);ep.setHp(ep.getMaxHp());ep.setSpeedx(6);ep.setHeight(Conts.IMAGES.get(ep.getImage()).getHeight());ep.setWidth(Conts.IMAGES.get(ep.getImage()).getWidth());    ep.setX(-ep.getWidth());ep.setY(Conts.RAN.nextInt(Conts.GAME_WEIDTH/2-ep.getHeight()));}else if(Conts.RAN.nextInt(100)<20){if(Conts.RAN.nextInt(100)<5)ep.setProp(Conts.RAN.nextInt(4) );elseep.setProp(7 );ep.setBoss(0);ep.setDir(Dir.Left);ep.setImage("l.png");ep.setSpeedx(45);ep.setMaxHp(200);ep.setHp(ep.getMaxHp());ep.setHeight(Conts.IMAGES.get(ep.getImage()).getHeight());ep.setWidth(Conts.IMAGES.get(ep.getImage()).getWidth());    ep.setX(Conts.GAME_WEIDTH);ep.setY(Conts.RAN.nextInt(Conts.GAME_WEIDTH/2-ep.getHeight()));}return ep;}
}

model包里面有一个GameModel类,该类是其他实体的父类,
其子类有Plane:玩家飞机,Boss:boss,EmPlane:敌机,
Bullet:子弹类,Prop:道具类。
GameModel:

package com.game.model;public class GameModel {private int x,y,index,width,height,maxindex,speedx,speedy,attact,id,prop;private boolean isDeathing;private double defense;private String image;private String shadow;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 getIndex() {return index;}public void setIndex(int index) {this.index = index;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getMaxindex() {return maxindex;}public void setMaxindex(int maxindex) {this.maxindex = maxindex;}public int getSpeedx() {return speedx;}public void setSpeedx(int speedx) {this.speedx = speedx;}public int getSpeedy() {return speedy;}public void setSpeedy(int speedy) {this.speedy = speedy;}public String getImage() {return this.image;}public void setImage(String image) {this.image = image;}public String getShadow() {return shadow;}public void setShadow(String shadow) {this.shadow = shadow;}public int getAttact() {return attact;}public void setAttact(int attact) {if(attact<=200)this.attact = attact;else this.attact = 200;}public double getDefense() {return defense;}public void setDefense(double defense) {this.defense = defense;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getProp() {return prop;}public void setProp(int prop) {this.prop = prop;}public boolean isDeathing() {return isDeathing;}public void setDeathing(boolean isDeathing) {this.isDeathing = isDeathing;}
}

Plane:

package com.game.model;import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;import com.game.config.Conts;
public class Plane extends GameModel{private int hp;private int maxHp,boss;private boolean isDeathing;private ArrayList<Bullet> blist = new ArrayList<Bullet>();private ArrayList<Bullet> blist1 = new ArrayList<Bullet>();//  private HashMap<String, ArrayList<Bullet>> mapblist = new HashMap<String, ArrayList<Bullet>>();public Rectangle getHurtRec(){return new Rectangle((getX()+getWidth()/4),(getHeight()/4+getY()),getWidth()/2,getHeight()/2);}public void moveLeft(){int x1=getX();x1-=getSpeedx();if(x1<0)x1=0;setX(x1);}public void moveRight(){int x1=getX();x1+=getSpeedx();if(x1>Conts.GAME_WEIDTH-getWidth())x1=Conts.GAME_WEIDTH-getWidth();setX(x1);}public void moveUp(){int y1=getY();y1-=getSpeedy();if(y1<0)y1=0;setY(y1);}public void moveDown(){int y1=getY();y1+=getSpeedy();if(y1>Conts.GAME_HEIGHT-getHeight())y1=Conts.GAME_HEIGHT-getHeight();setY(y1);}public void bs(){int index = getIndex();index++;if(index>=getMaxindex())setIndex(0);elsesetIndex(index);}public ArrayList<Bullet> getBlist() {return blist;}public void setBlist(ArrayList<Bullet> blist) {this.blist = blist;}public boolean isDeathing() {return isDeathing;}public void setDeathing(boolean isDeathing) {this.isDeathing = isDeathing;}public int getHp() {return hp;}public void setHp(int hp) {this.hp = hp;}public int getMaxHp() {return maxHp;}public void setMaxHp(int maxHp) {this.maxHp = maxHp;}public ArrayList<Bullet> getBlist1() {return blist1;}public void setBlist1(ArrayList<Bullet> blist1) {this.blist1 = blist1;}public int getBoss() {return boss;}public void setBoss(int boss) {this.boss = boss;}
}

EmPlane:

package com.game.model;import com.game.config.Conts;
import com.game.config.Dir;public class EmPlane extends Plane {private Dir dir;public Dir getDir() {return dir;}public void setDir(Dir dir) {this.dir = dir;}public boolean emMoveLeft(){int xx=this.getX();xx-=this.getSpeedx();if(xx<=-this.getWidth()){return false;}else {setX(xx);return true;}}public boolean emMoveRight(){int xx=this.getX();xx+=this.getSpeedx();if(xx>=Conts.GAME_WEIDTH+this.getWidth()){return false;}else {setX(xx);return true;}}public boolean emMoveDown(){int yy=this.getY();yy+=this.getSpeedy();if(yy>=Conts.GAME_HEIGHT+this.getHeight()){return false;}else {setY(yy);return true;}}}

Bullet:

package com.game.model;import java.awt.Rectangle;import com.game.config.BulletType;
import com.game.config.Conts;public class Bullet extends GameModel {private int attact;private BulletType bt;public boolean Move(){int yy=this.getY();yy-=this.getSpeedy();if(yy<=-this.getHeight()){return false;}else {setY(yy);return true;}}public boolean MoveXL(){int yy=this.getY();int xx=this.getX();xx-=this.getSpeedx();yy-=this.getSpeedy();if(yy<=-this.getHeight()||xx<=-this.getWidth()){return false;}else {setY(yy);setX(xx);return true;}}public boolean MoveXR(){int yy=this.getY();int xx=this.getX();xx+=this.getSpeedx();yy-=this.getSpeedy();if(yy<=-this.getHeight()||xx>=this.getWidth()+Conts.GAME_WEIDTH){return false;}else {setY(yy);setX(xx);return true;}}public boolean moveDown(){int yy=this.getY();yy+=this.getSpeedy();if(yy>=this.getHeight()+Conts.GAME_HEIGHT){return false;}else {setY(yy);return true;}}public Rectangle getHurtRec(){return new Rectangle(getX(),getY(),getWidth(),getHeight());}public int getAttact() {return attact;}public void setAttact(int attact) {this.attact = attact;}public BulletType getBt() {return bt;}public void setBt(BulletType bt) {this.bt = bt;}
}

Boss:

package com.game.model;public class Boss extends EmPlane {public void bossMoveLeft(){int xx=getX();xx-=getSpeedx();setX(xx);}public void bossMoveRight(){int xx=getX();xx+=getSpeedx();setX(xx);}}

Prop:

package com.game.model;import java.awt.Rectangle;public class Prop extends GameModel {public Prop(int x,int y,int w,int h,int p,String img){setX(x);setY(y);setWidth(w);setHeight(h);setProp(p);setImage(img);}public Prop(){}public Rectangle getHurtRec(){return new Rectangle(getX(),getY(),getWidth(),getHeight());}
}

最后是ui包,里面一个UI类,是整个业务逻辑的核心,控制该游戏的逻辑以及界面的绘制
UI:

package com.game.ui;import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;import com.game.config.BulletType;
import com.game.config.Conts;
import com.game.config.Dir;
import com.game.config.GameState;
import com.game.config.SoundUtils;
import com.game.factory.Factory;
import com.game.model.Boss;
import com.game.model.EmPlane;
import com.game.model.Plane;
import com.game.model.Prop;/*** 打飞机游戏* * @author Sebrina* */
public class Ui extends JFrame implements Runnable {private Plane p;private Prop prop;private int source;private EmPlane ep;private File file = new File("./src/sucai/saves.sav");private BufferedReader br;private BufferedWriter bw;private HashMap<String, String> sourcemap = new HashMap<String, String>();private FileReader fr;private FileWriter fw;private String play = "play0.png";private String restart = "restart0.png";private String exit = "exit0.png";private GameState gs = GameState.BeforGame;private int delay = 10, BullteDelay = 0, MaxBoom = 3, ccount = 0,emcount = 0;private int lc = 1, Alc = 0, level = 1, bgspeed = 5, pcount = 0,cindex = 0;private int[] flag = { 0, 0, 0, 0, 0, 0, 0, 0 };private boolean ML, MR, MU, MD, isBossing;private MyPaint hb;private List<EmPlane> emlist = new ArrayList<EmPlane>();private List<Prop> proplist = new ArrayList<Prop>();private Boss boss;public Ui() {setTitle("Plane");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// setLocationRelativeTo(null);setLocation(500, 20);setSize(Conts.GAME_WEIDTH, Conts.GAME_HEIGHT);setUndecorated(true);// p = Factory.getPlane(cindex);Container c = getContentPane();hb = new MyPaint();c.setFocusable(true);c.addKeyListener(new MyKey());c.addMouseListener(new MyMouse());c.addMouseMotionListener(new MyMouse());c.add(hb);setVisible(true);}public static void main(String[] args) {Ui u = new Ui();Thread t = new Thread(u);SoundUtils.Play(Conts.AUDIOS.get("bgm.wav"), true);if (u.gs == GameState.Pause)t.stop();elset.start();}class MyKey extends KeyAdapter {@Overridepublic void keyPressed(KeyEvent e) {switch (e.getKeyCode()) {case KeyEvent.VK_W:MU = true;break;case KeyEvent.VK_S:MD = true;break;case KeyEvent.VK_A:ML = true;break;case KeyEvent.VK_D:MR = true;break;case KeyEvent.VK_P:if (gs != GameState.BeforGame) {pcount++;System.out.println("pcount::::"+pcount);pcount = pcount % 2;if (pcount == 1)gs = GameState.Pause;else if (pcount == 0)gs = GameState.Gameing;}break;case KeyEvent.VK_SPACE:if (MaxBoom >= 1) {Factory.getBullet(p, BulletType.Boom);SoundUtils.Play(Conts.AUDIOS.get("big.wav"), false);MaxBoom--;}break;}}@Overridepublic void keyReleased(KeyEvent e) {switch (e.getKeyCode()) {case KeyEvent.VK_W:MU = false;break;case KeyEvent.VK_S:MD = false;break;case KeyEvent.VK_A:ML = false;break;case KeyEvent.VK_D:MR = false;break;}}}class MyMouse extends MouseAdapter {@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);System.out.println("click");if (gs == GameState.Pause) {if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2+ 20+ Conts.IMAGES.get(exit).getHeight(), Conts.IMAGES.get(exit).getWidth(), Conts.IMAGES.get(exit).getHeight()).contains(e.getPoint())) {{System.out.println("exitsuccess");SoundUtils.Play(Conts.AUDIOS.get("click.wav"), false);if (source >= Integer.valueOf(sourcemap.get("player"+ p.getId()))) {int choice = 0;choice = JOptionPane.showConfirmDialog(null,"您已超越最高积分,是否需要保存?", "退出游戏",JOptionPane.YES_NO_OPTION);if (choice == 1)System.exit(0);else if (choice == 0) {if (p != null) {int s;s = Integer.valueOf(sourcemap.get("player"+ p.getId()));if (source >= s)sourcemap.put("player" + p.getId(),String.valueOf(source));System.out.println("pid=::" + p.getId());try {fw = new FileWriter(file);System.out.println("filename:::"+ file.getName());bw = new BufferedWriter(fw);for (int i = 0; i <= 3; i++) {bw.write("player" + i);bw.newLine();bw.write(sourcemap.get("player" + i));bw.newLine();}// fw.close();bw.close();System.exit(0);} catch (IOException e1) {e1.printStackTrace();}}}} elseSystem.exit(0);}}}if (gs == GameState.BeforGame) {if (getChooseId(e) >= 0) {cindex = getChooseId(e);SoundUtils.Play(Conts.AUDIOS.get("click.wav"), false);p = Factory.getPlane(cindex);ccount++;}if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(play).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(play).getHeight()) / 2, Conts.IMAGES.get(play).getWidth(), Conts.IMAGES.get(play).getHeight()).contains(e.getPoint())&& cindex >= 0) {{SoundUtils.Play(Conts.AUDIOS.get("click.wav"), false);System.out.println("clicksuccess");gs = GameState.Gameing;}}if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2+ 20+ Conts.IMAGES.get(exit).getHeight(), Conts.IMAGES.get(exit).getWidth(), Conts.IMAGES.get(exit).getHeight()).contains(e.getPoint())) {{System.out.println("exitsuccess");SoundUtils.Play(Conts.AUDIOS.get("click.wav"), false);System.exit(0);}}} else if (gs == GameState.Over) {if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(restart).getWidth()) / 2,(Conts.GAME_HEIGHT - Conts.IMAGES.get(restart).getHeight()) / 2 + 50, Conts.IMAGES.get(restart).getWidth(), Conts.IMAGES.get(restart).getHeight()).contains(e.getPoint())) {SoundUtils.Play(Conts.AUDIOS.get("click.wav"), false);gs = GameState.BeforGame;}if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2+ 10+ 50+ Conts.IMAGES.get(exit).getHeight(), Conts.IMAGES.get(exit).getWidth(), Conts.IMAGES.get(exit).getHeight()).contains(e.getPoint())) {SoundUtils.Play(Conts.AUDIOS.get("click.wav"), false);System.exit(0);}} else if (gs == GameState.Pass) {if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2+ 10+ 50+ Conts.IMAGES.get(exit).getHeight(), Conts.IMAGES.get(exit).getWidth(), Conts.IMAGES.get(exit).getHeight()).contains(e.getPoint())) {SoundUtils.Play(Conts.AUDIOS.get("click.wav"), false);System.exit(0);}}repaint();}@Overridepublic void mouseMoved(MouseEvent e) {System.out.println(1);if (gs == GameState.Pause) {if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2+ 10+ Conts.IMAGES.get(exit).getHeight(), Conts.IMAGES.get(exit).getWidth(), Conts.IMAGES.get(exit).getHeight()).contains(e.getPoint())) {if (flag[1] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[1] = 1;exit = "exit1.png";System.out.println("success");} else {exit = "exit0.png";flag[1] = 0;}}if (gs == GameState.BeforGame) {getChooseId(e);if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(play).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(play).getHeight()) / 2, Conts.IMAGES.get(play).getWidth(), Conts.IMAGES.get(play).getHeight()).contains(e.getPoint())) {play = "play1.png";if (flag[0] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[0] = 1;System.out.println("success");} else {play = "play0.png";flag[0] = 0;}if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2+ 10+ Conts.IMAGES.get(exit).getHeight(), Conts.IMAGES.get(exit).getWidth(), Conts.IMAGES.get(exit).getHeight()).contains(e.getPoint())) {if (flag[1] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[1] = 1;exit = "exit1.png";System.out.println("success");} else {exit = "exit0.png";flag[1] = 0;}} else if (gs == GameState.Over) {if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(restart).getWidth()) / 2,(Conts.GAME_HEIGHT - Conts.IMAGES.get(restart).getHeight()) / 2 + 50, Conts.IMAGES.get(restart).getWidth(), Conts.IMAGES.get(restart).getHeight()).contains(e.getPoint())) {if (flag[2] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[2] = 1;restart = "restart1.png";System.out.println("success");} else {restart = "restart0.png";flag[2] = 0;}if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2+ 10+ 50+ Conts.IMAGES.get(exit).getHeight(), Conts.IMAGES.get(exit).getWidth(), Conts.IMAGES.get(exit).getHeight()).contains(e.getPoint())) {if (flag[3] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[3] = 1;exit = "exit1.png";System.out.println("success");} else {exit = "exit0.png";flag[3] = 0;}} else if (gs == GameState.Pass) {if (new Rectangle((Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2+ 10+ 50+ Conts.IMAGES.get(exit).getHeight(), Conts.IMAGES.get(exit).getWidth(), Conts.IMAGES.get(exit).getHeight()).contains(e.getPoint())) {if (flag[3] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[3] = 1;exit = "exit1.png";System.out.println("success");} else {exit = "exit0.png";flag[3] = 0;}}repaint();}}public int getChooseId(MouseEvent e) {if (gs == GameState.BeforGame && ccount % 2 == 0) {if (new Rectangle(10, 470, 100, 130).contains(e.getPoint())) {if (flag[4] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[4] = 1;cindex = 0;} else if (new Rectangle(110, 470, 100, 130).contains(e.getPoint())) {if (flag[5] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[5] = 1;cindex = 1;} else if (new Rectangle(210, 470, 100, 130).contains(e.getPoint())) {if (flag[6] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[6] = 1;cindex = 2;} else if (new Rectangle(310, 470, 100, 130).contains(e.getPoint())) {if (flag[7] == 0)SoundUtils.Play(Conts.AUDIOS.get("choose.wav"), false);flag[7] = 1;cindex = 3;} else {flag[4] = 0;flag[5] = 0;flag[6] = 0;flag[7] = 0;cindex = -5;}}return cindex;}class MyPaint extends JPanel {@Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g);if (gs == GameState.Pass) {drawPass(g);}if (gs != GameState.BeforGame) {if (level <= 4)DrawBackGround(g);drawEmPlane(g);drawBoss(g);drawProp(g);drawPlane(g);if (!p.isDeathing()) {drawBullet(g);drawBossBullet(g);drawEmBullet(g);}if (p.isDeathing()) {// 最高记录存档if (p != null) {int s;s = Integer.valueOf(sourcemap.get("player" + p.getId()));if (source >= s)sourcemap.put("player" + p.getId(),String.valueOf(source));System.out.println("pid=::" + p.getId());try {fw = new FileWriter(file);System.out.println("filename:::" + file.getName());bw = new BufferedWriter(fw);for (int i = 0; i <= 3; i++) {bw.write("player" + i);bw.newLine();bw.write(sourcemap.get("player" + i));bw.newLine();}bw.close();} catch (IOException e) {e.printStackTrace();}}DrawOver(g);}drawTips(g);}if (gs == GameState.BeforGame) {drawStart(g);try {fr = new FileReader(file);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}br = new BufferedReader(fr);for (int i = 0; i <= 3; i++) {try {String key = br.readLine();String value = br.readLine();System.out.println(key + "==" + value);sourcemap.put(key, value);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}proplist.clear();lc = 1;isBossing = false;boss = null;emlist.clear();source = 0;Alc = 0;level = 1;emcount = 0;MaxBoom = 3;}if (gs == GameState.Pause) {drawPause(g);}}private void drawPass(Graphics g) {g.drawImage(Conts.IMAGES.get("bg4_5.jpg"), 0, 0, this);g.drawImage(Conts.IMAGES.get("pass.png"),(Conts.GAME_WEIDTH - Conts.IMAGES.get("pass.png").getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get("pass.png").getHeight()) / 2, this);g.drawImage(Conts.IMAGES.get(exit),(Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2,(Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2 + Conts.IMAGES.get(exit).getHeight() + 10 + 50,this);}private void drawProp(Graphics g) {for (int i = 0; i < proplist.size(); i++) {Prop prop1 = proplist.get(i);if (prop1 != null) {g.drawImage(Conts.IMAGES.get(prop1.getImage()),prop1.getX(), prop1.getY(), prop1.getWidth(),prop1.getHeight(), this);System.out.println("index::::::::::::" + prop1.getIndex());g.setFont(new Font("微软雅黑", Font.BOLD, 20));g.setColor(Color.RED);if (prop1.getProp() == 0)g.drawString("血量加满", prop1.getX() + 45, prop1.getY());else if (prop1.getProp() == 1)g.drawString("炸弹+1", prop1.getX() + 45, prop1.getY());else if (prop1.getProp() == 2)g.drawString("消灭所有敌机", prop1.getX() + 45, prop1.getY());else if (prop1.getProp() == 3)g.drawString("攻击力+50", prop1.getX() + 45, prop1.getY());}}}private void DrawOver(Graphics g) {g.drawImage(Conts.IMAGES.get(p.getImage()).getSubimage(p.getIndex() * p.getWidth(), 0, p.getWidth(),p.getHeight()), p.getX(), p.getY(),p.getWidth() * 2, p.getHeight() * 2, this);g.drawImage(Conts.IMAGES.get("fail.png"),(Conts.GAME_WEIDTH - Conts.IMAGES.get("fail.png").getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get("fail.png").getHeight()) / 2 - 100, this);g.drawImage(Conts.IMAGES.get(restart),(Conts.GAME_WEIDTH - Conts.IMAGES.get(restart).getWidth()) / 2,(Conts.GAME_HEIGHT - Conts.IMAGES.get(restart).getHeight()) / 2 + 50,this);g.drawImage(Conts.IMAGES.get(exit),(Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2,(Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2 + Conts.IMAGES.get(exit).getHeight() + 10 + 50,this);}private void drawPause(Graphics g) {g.drawImage(Conts.IMAGES.get("pause.png"),(Conts.GAME_WEIDTH - Conts.IMAGES.get("pause.png").getWidth()) / 2, (Conts.GAME_HEIGHT - Conts.IMAGES.get("pause.png").getHeight()) / 2, this);g.drawImage(Conts.IMAGES.get(exit),(Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2,(Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2 + Conts.IMAGES.get(exit).getHeight() + 10, this);}private void drawStart(Graphics g) {g.drawImage(Conts.IMAGES.get("startbg.jpg"), 0, 0,Conts.GAME_WEIDTH, Conts.GAME_HEIGHT, this);g.drawImage(Conts.IMAGES.get(play),(Conts.GAME_WEIDTH - Conts.IMAGES.get(play).getWidth()) / 2,(Conts.GAME_HEIGHT - Conts.IMAGES.get(play).getHeight()) / 2,this);g.drawImage(Conts.IMAGES.get(exit),(Conts.GAME_WEIDTH - Conts.IMAGES.get(exit).getWidth()) / 2,(Conts.GAME_HEIGHT - Conts.IMAGES.get(exit).getHeight())/ 2 + Conts.IMAGES.get(exit).getHeight() + 10, this);g.drawImage(Conts.IMAGES.get("player0.png").getSubimage(0, 0,Conts.IMAGES.get("player0.png").getWidth() / 2,Conts.IMAGES.get("player0.png").getHeight()), 15,480, this);g.drawImage(Conts.IMAGES.get("pbg.png"), 10 + (cindex * 100), 470,this);g.setFont(new Font("宋体", Font.BOLD, 15));g.setColor(Color.red);g.drawString("生命值:" + 1000, 10, 615);g.drawString("攻击力:" + 120, 10, 630);g.drawString("防御值:" + 10, 10, 645);for (int i = 1; i <= 3; i++) {g.drawImage(Conts.IMAGES.get("player" + i + ".png"),18 + i * 100, 500, this);g.drawImage(Conts.IMAGES.get("pbg.png"), 10 + (cindex * 100),470, this);g.setFont(new Font("宋体", Font.BOLD, 15));g.setColor(Color.red);g.drawString("生命值:" + (1000 + i * 1000), 18 + i * 100, 615);g.drawString("攻击力:" + (120 - i * 20), 18 + i * 100, 630);g.drawString("防御值:" + (10 + i * 10), 18 + i * 100, 645);}}private void drawBossBullet(Graphics g) {if (boss != null)for (int i = 0; i < boss.getBlist().size(); i++) {if (boss.getBlist().get(i) != null) {g.drawImage(Conts.IMAGES.get(boss.getBlist().get(i).getImage()).getSubimage(0,0,boss.getBlist().get(i).getWidth(),boss.getBlist().get(i).getHeight()), boss.getBlist().get(i).getX(), boss.getBlist().get(i).getY(), this);}}}private void drawBoss(Graphics g) {if (isBossing == true && boss != null) {emlist.clear();if (!boss.isDeathing()) {g.drawImage(Conts.IMAGES.get(boss.getImage()), boss.getX(),boss.getY(), null);g.drawRect(boss.getX(), boss.getY() + boss.getHeight(),boss.getWidth(), Conts.HP_Height);g.setColor(Color.red);g.fillRect(boss.getX(), boss.getY() + boss.getHeight(),(int) (boss.getWidth() * (boss.getHp() * 1.0 / boss.getMaxHp())), Conts.HP_Height);} else {g.drawImage(Conts.IMAGES.get(boss.getImage()).getSubimage(boss.getIndex() * boss.getWidth(), 0,boss.getWidth(), boss.getHeight()),boss.getX(), boss.getY(), this);}}}private void drawTips(Graphics g) {g.setFont(new Font("微软雅黑", Font.BOLD, 20));g.drawString("炸弹剩余数量: " + MaxBoom, 50, 50);g.drawString("里程: " + Alc, 50, 80);g.setFont(new Font("微软雅黑", Font.BOLD, 14));g.drawString("摧毁敌机" + emcount + "架", 300, 30);g.drawString("当前攻击力:" + p.getAttact(), 260, 600);g.drawString("当前关卡:" + level, 260, 580);int s;System.out.println("qqqqqqqqqq"+ sourcemap.get("player" + p.getId()));s = Integer.valueOf(sourcemap.get("player" + p.getId()));if (source >= s)s = source;int hp = p.getHp();if (hp <= 0)hp = 0;g.drawString("当前HP:" + hp, 20, 620);g.drawString("当前积分:" + source, 260, 560);g.drawString("当前飞机最高记录:" + s, 260, 540);}private void drawEmBullet(Graphics g) {for (int j = 0; j < emlist.size(); j++) {ep = emlist.get(j);if (ep != null)for (int i = 0; i < ep.getBlist().size(); i++) {if (ep.getBlist().get(i) != null) {g.drawImage(Conts.IMAGES.get(ep.getBlist().get(i).getImage()), ep.getBlist().get(i).getX(), ep.getBlist().get(i).getY(), this);}}}}private void drawBullet(Graphics g) {for (int i = 0; i < p.getBlist().size(); i++) {if (p.getBlist().get(i) != null) {int py;if (cindex == 0)py = 20;elsepy = -20;g.drawImage(Conts.IMAGES.get(p.getBlist().get(i).getImage()), p.getBlist().get(i).getX(), p.getBlist().get(i).getY()+ py, this);}}for (int i = 0; i < p.getBlist1().size(); i++) {if (p.getBlist1().get(i) != null) {if (cindex > 0 && cindex < 3) {if (p.getBlist1().get(i).getId() == 0)g.drawImage(Conts.IMAGES.get(p.getBlist1().get(i).getImage()), p.getBlist1().get(i).getX() - 30, p.getBlist1().get(i).getY() + 40, this);elseg.drawImage(Conts.IMAGES.get(p.getBlist1().get(i).getImage()), p.getBlist1().get(i).getX() + 30, p.getBlist1().get(i).getY() + 40, this);} else if (cindex == 0 || cindex == 3) {int py;if (cindex == 0)py = 80;elsepy = -40;g.drawImage(Conts.IMAGES.get(p.getBlist1().get(i).getImage()), p.getBlist1().get(i).getX() - 30, p.getBlist1().get(i).getY()+ py, this);g.drawImage(Conts.IMAGES.get(p.getBlist1().get(i).getImage()), p.getBlist1().get(i).getX() + 30, p.getBlist1().get(i).getY()+ py, this);}}}}private void drawEmPlane(Graphics g) {for (int i = 0; i < emlist.size(); i++) {EmPlane ep = emlist.get(i);if (ep != null) {if (ep.isDeathing()) {System.out.println(ep.getX() + "----" + ep.getY());try {g.drawImage(Conts.IMAGES.get(ep.getImage()).getSubimage(ep.getIndex()* ep.getWidth(), 0,ep.getWidth(),ep.getHeight()), ep.getX(),ep.getY(), this);} catch (java.awt.image.RasterFormatException e) {}} else {g.drawImage(Conts.IMAGES.get(ep.getImage()), ep.getX(),ep.getY(), this);g.drawRect(ep.getX(), ep.getY() + ep.getHeight(),ep.getWidth(), Conts.HP_Height);g.setColor(Color.red);g.fillRect(ep.getX(), ep.getY() + ep.getHeight(),(int) (ep.getWidth() * (ep.getHp() * 1.0 / ep.getMaxHp())), Conts.HP_Height);}}}}private void drawPlane(Graphics g) {System.out.println(cindex + "    飞机编号:::" + p.getImage());// if(p.isDeathing()==false&&gs!=GameState.Over)g.drawImage(Conts.IMAGES.get(p.getImage()).getSubimage(p.getIndex() * p.getWidth(), 0, p.getWidth(),p.getHeight()), p.getX(), p.getY(), this);g.drawRect(0, Conts.GAME_HEIGHT - Conts.HP_Height,Conts.GAME_WEIDTH, Conts.HP_Height);g.setColor(Color.red);g.fillRect(0,Conts.GAME_HEIGHT - Conts.HP_Height,(int) (Conts.GAME_WEIDTH * (p.getHp() * 1.0 / p.getMaxHp())),Conts.HP_Height);if (!p.isDeathing())g.drawImage(Conts.IMAGES.get(p.getShadow()), p.getX() + 20,p.getY() + 20, this);}private void DrawBackGround(Graphics g) {int upindex = lc / Conts.GAME_HEIGHT + 2;int downindex = lc / Conts.GAME_HEIGHT + 1;if (upindex >= 6)upindex = 1;g.drawImage(Conts.IMAGES.get("bg" + level + "_" + downindex + ".jpg"),0, lc % Conts.GAME_HEIGHT, this);g.drawImage(Conts.IMAGES.get("bg" + level + "_" + upindex + ".jpg").getSubimage(0,Conts.GAME_HEIGHT - lc % Conts.GAME_HEIGHT,Conts.GAME_WEIDTH, lc % Conts.GAME_HEIGHT),0, 0, this);}}@Overridepublic void run() {// TODO Auto-generated method stubwhile (true) {if (emlist.size() == Conts.EMMAX)emlist.clear();// p.bs();if (gs == GameState.Gameing) {if (p != null && p.isDeathing()) {int index = p.getIndex();index++;if (index < p.getMaxindex()) {p.setIndex(index);} else {gs = GameState.Over;SoundUtils.Play(Conts.AUDIOS.get("over.wav"), false);}}if (level == 5) {gs = GameState.Pass;SoundUtils.Play(Conts.AUDIOS.get("pass.wav"), false);}System.out.println("move");if (gs != GameState.Pass) {SoundUtils.Play(Conts.AUDIOS.get("bullet.wav"), false);crateEmPlane();System.out.println("敌机数目:::" + emlist.size());crateBoss();crateBullet();bossBulletMove();emPlaneMove();bossMove();propEat();playerMove();bulletMove();bullet1Move();emBulletMove();moveLc();}}try {Thread.sleep(100);delay++;BullteDelay++;// EmBullteDelay++;} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}hb.repaint();}}private void propEat() {for (int i = 0; i < proplist.size(); i++) {Prop prop = proplist.get(i);System.out.println("check!!!!!!!!!");if (prop != null && p.getHurtRec().intersects(prop.getHurtRec())) {System.out.println("碰撞!!!!!!!");SoundUtils.Play(Conts.AUDIOS.get("prop.wav"), false);checkProp(prop);    proplist.set(i, null);}}}private void bossMove() {System.out.println(boss);if (isBossing == true && boss != null) {if (boss.isDeathing()) {int index = boss.getIndex();index++;if (index < boss.getMaxindex()) {boss.setIndex(index);} else {setBossDeath();}} else {if (boss.getDir() == Dir.Left) {boss.bossMoveLeft();if (!boss.isDeathing()&& boss.getHurtRec().intersects(p.getHurtRec())) {int hp = boss.getHp();hp -= 2000;boss.setHp(hp);setPlaneDeath(p);}if (boss.getX() <= 0)boss.setDir(Dir.Right);}if (boss.getDir() == Dir.Right) {boss.bossMoveRight();if (!boss.isDeathing()&& boss.getHurtRec().intersects(p.getHurtRec())) {int hp = boss.getHp();hp -= 2000;boss.setHp(hp);setPlaneDeath(p);}if (boss.getX() >= Conts.GAME_WEIDTH - boss.getWidth())boss.setDir(Dir.Left);}Factory.getEmBullet(boss);}}}private void crateBoss() {System.out.println(level);if (isBossing == true && boss == null) {boss = Factory.getBoss(level);}}private void moveLc() {lc += bgspeed;if (isBossing == false) {Alc += 10;}if (lc >= 4 * Conts.GAME_HEIGHT - 20) {bgspeed = 0;isBossing = true;}}private void bossBulletMove() {if (boss != null) {for (int i = 0; i < boss.getBlist().size(); i++) {if (boss.getBlist().get(i) != null) {if (boss.getBlist().get(i).moveDown() == false) {boss.getBlist().set(i, null);} else {if (p != null)if (p.isDeathing() == false&& boss.getBlist().get(i).getHurtRec().intersects(p.getHurtRec())) {int hp = p.getHp();hp -= boss.getBlist().get(i).getAttact()* p.getDefense();if (hp <= 1)setPlaneDeath(p);elsep.setHp(hp);boss.getBlist().set(i, null);}}}}}}private void emBulletMove() {for (int j = 0; j < emlist.size(); j++) {ep = emlist.get(j);if (ep != null) {for (int i = 0; i < ep.getBlist().size(); i++) {if (ep.getBlist().get(i) != null) {if (ep.getBlist().get(i).moveDown() == false) {ep.getBlist().set(i, null);} else {if (p != null)if (p.isDeathing() == false&& ep.getBlist().get(i).getHurtRec().intersects(p.getHurtRec())) {int hp = p.getHp();hp -= ep.getBlist().get(i).getAttact()* p.getDefense();if (hp <= 1)setPlaneDeath(p);elsep.setHp(hp);ep.getBlist().set(i, null);}}}}}}}private void bulletMove() {for (int i = 0; i < p.getBlist().size(); i++) {if (p.getBlist().get(i) != null) {boolean move = false;move = p.getBlist().get(i).Move();if (move == false) {p.getBlist().set(i, null);} else {// if (isBossing == false) {for (int j = 0; j < emlist.size(); j++) {ep = emlist.get(j);if (ep != null) {if (p.getBlist().get(i) != null)if (ep.isDeathing() == false&& p.getBlist().get(i).getHurtRec().intersects(emlist.get(j).getHurtRec())) {int hp = ep.getHp();hp -= p.getBlist().get(i).getAttact();if (hp <= 0) {if (ep.getDir() == Dir.Left) {source += 50;} else if (ep.getDir() == Dir.Right) {source += 30;} else if (ep.getDir() == Dir.Down) {source += 10;}setEmDeath(ep);emcount++;} elseep.setHp(hp);if (p.getBlist().get(i).getBt() == BulletType.Normal)p.getBlist().set(i, null);}}}// } else{if (boss != null) {if (p.getBlist().get(i) != null)if (boss.isDeathing() == false&& p.getBlist().get(i).getHurtRec().intersects(boss.getHurtRec())) {int hp = boss.getHp();hp -= p.getBlist().get(i).getAttact();if (hp <= 0) {source += 100 * level;setEmDeath(boss);emcount++;}elseboss.setHp(hp);if (p.getBlist().get(i).getBt() == BulletType.Normal)p.getBlist().set(i, null);}}}}}}}private void setBossDeath() {isBossing = false;level++;lc = 1;bgspeed = 5;int k = 0;for (int i = 0; i < proplist.size(); i++) {if (proplist.get(i) == null && k < 3) {int r = Conts.RAN.nextInt(3);proplist.set(i, new Prop(boss.getX() + Conts.RAN.nextInt(50)+ 50, boss.getY() + Conts.RAN.nextInt(80) + 80, 40, 40,r, Factory.getProp(r).getImage()));}}boss = null;emlist.clear();}private void bullet1Move() {for (int i = 0; i < p.getBlist1().size(); i++) {boolean d = false;if (p.getBlist1().get(i) != null) {if (cindex == 0 || cindex == 3) {if (cindex == 0)p.getBlist1().get(i).setSpeedy(60);else {p.getBlist1().get(i).setSpeedy(20);}d = p.getBlist1().get(i).Move();}if (cindex == 1) {if (p.getBlist1().get(i).getId() == 0)d = p.getBlist1().get(i).MoveXL();elsed = p.getBlist1().get(i).MoveXR();} else if (cindex == 2) {if (p.getBlist1().get(i).getId() == 0)d = p.getBlist1().get(i).MoveXR();elsed = p.getBlist1().get(i).MoveXL();}if (d == false) {p.getBlist1().set(i, null);} else {// if (isBossing == false) {for (int j = 0; j < emlist.size(); j++) {ep = emlist.get(j);if (ep != null) {if (p.getBlist1().get(i) != null)if (ep.isDeathing() == false&& p.getBlist1().get(i).getHurtRec().intersects(emlist.get(j).getHurtRec())) {int hp = ep.getHp();hp -= p.getBlist1().get(i).getAttact();if (hp <= 0) {if (ep.getDir() == Dir.Left) {source += 50;} else if (ep.getDir() == Dir.Right) {source += 30;} else if (ep.getDir() == Dir.Down) {source += 10;}emcount++;setEmDeath(ep);} elseep.setHp(hp);if (p.getBlist1().get(i).getBt() == BulletType.Normal1)p.getBlist1().set(i, null);}}}// } else{if (boss != null) {if (p.getBlist1().get(i) != null)if (boss.isDeathing() == false&& p.getBlist1().get(i).getHurtRec().intersects(boss.getHurtRec())) {int hp = boss.getHp();hp -= p.getBlist1().get(i).getAttact();if (hp <= 0) {source += 100 * level;setEmDeath(boss);emcount++;}elseboss.setHp(hp);if (p.getBlist1().get(i).getBt() == BulletType.Normal1)p.getBlist1().set(i, null);}}}}}}}private void setPlaneDeath(Plane p2) {// gs=GameState.Over;p2.setDeathing(true);SoundUtils.Play(Conts.AUDIOS.get("boom.wav"), false);// p.getBlist().set(i, null);p2.setImage("boom.png");p2.setIndex(0);p2.setMaxindex(7);p2.setWidth(Conts.IMAGES.get(p2.getImage()).getWidth()/ p2.getMaxindex());p2.setHeight(Conts.IMAGES.get(p2.getImage()).getHeight());}private void setEmDeath(EmPlane ep2) {ep2.setDeathing(true);SoundUtils.Play(Conts.AUDIOS.get("boom.wav"), false);// p.getBlist().set(i, null);if (ep2 == boss)ep2.setImage("boom01.png");else {ep2.setImage("boom.png");if (ep2.getProp() >= 0 && ep2.getProp() < 4) {prop = Factory.getProp(ep2.getProp());if (ep2.getX() < 40)prop.setX(Conts.RAN.nextInt(300) + 80);else if (ep2.getX() > 400)prop.setX(Conts.RAN.nextInt(300) + 80);elseprop.setX(ep2.getX());if (ep2.getY() < 40)prop.setY(Conts.RAN.nextInt(500) + 100);else if (ep2.getY() > 620)prop.setY(Conts.RAN.nextInt(500) + 100);elseprop.setY(ep2.getY());prop.setWidth(40);prop.setHeight(40);prop.setProp(ep2.getProp());if (proplist.size() < 5) {proplist.add(prop);} else {for (int i = 0; i < proplist.size(); i++) {if (proplist.get(i) == null) {proplist.set(i, prop);break;}}}}}ep2.setIndex(0);ep2.setMaxindex(7);ep2.setWidth(Conts.IMAGES.get(ep2.getImage()).getWidth()/ ep2.getMaxindex());ep2.setHeight(Conts.IMAGES.get(ep2.getImage()).getHeight());}private void crateBullet() {if (BullteDelay > 1) {Factory.getBullet(p, BulletType.Normal1);if (p.getId() != 3)Factory.getBullet(p, BulletType.Normal);else if (p.getId() == 3 && p.getHp() <= 2000)Factory.getBullet(p, BulletType.Normal);BullteDelay = 0;}}private void emPlaneMove() {for (int i = 0; i < emlist.size(); i++) {EmPlane ep = emlist.get(i);if (ep != null) {if (ep.isDeathing()) {int index = ep.getIndex();index++;if (index < ep.getMaxindex()) {ep.setIndex(index);} elseemlist.set(i, null);} else {if (ep.getDir() == Dir.Down) {if (ep.emMoveDown() == false) {emlist.set(i, null);} else if (!ep.isDeathing()&& ep.getHurtRec().intersects(p.getHurtRec())) {setEmDeath(ep);int hp = p.getHp();hp -= 500;p.setHp(hp);}elseFactory.getEmBullet(ep);} else if (ep.getDir() == Dir.Left) {if (ep.emMoveLeft() == false)emlist.set(i, null);else if (!ep.isDeathing()&& ep.getHurtRec().intersects(p.getHurtRec())) {setEmDeath(ep);int hp = p.getHp();hp -= 500;p.setHp(hp);}// else// Factory.getEmBullet(ep);// 子弹未添加}else {if (ep.emMoveRight() == false)emlist.set(i, null);else if (!ep.isDeathing()&& ep.getHurtRec().intersects(p.getHurtRec())) {setEmDeath(ep);int hp = p.getHp();hp -= 500;p.setHp(hp);}elseFactory.getEmBullet(ep);// 子弹未添加}}}}}private void checkProp(Prop p2) {if (p2.getProp() == 0) {p.setHp(p.getMaxHp());} else if (p2.getProp() == 1) {MaxBoom++;} else if (p2.getProp() == 2) {for (int i = 0; i < emlist.size(); i++) {if (emlist.get(i) != null)setEmDeath(emlist.get(i));}} else if (p2.getProp() == 3) {if (p.getAttact() <= 200)p.setAttact(p.getAttact() + 50);}}private void crateEmPlane() {if (isBossing == false) {if (emlist.size() < Conts.EMMAX) {if (delay >= 5) {System.out.println("new");emlist.add(Factory.getEmPlane());// Collections.shuffle(emlist);delay = 0;}} else {for (int i = 0; i < emlist.size(); i++) {if (emlist.get(i) == null) {System.out.println("new");emlist.set(i, Factory.getEmPlane());// Collections.shuffle(emlist);// break;}}}}}private void playerMove() {if (p != null && !p.isDeathing()) {if (ML)p.moveLeft();if (MR)p.moveRight();if (MU)p.moveUp();if (MD)p.moveDown();   }}}

java飞机大战游戏相关推荐

  1. JAVA飞机大战游戏附源码

    JAVA飞机大战附源码 java飞机大战 效果图如下 java飞机大战 话不多说 直接上源码 窗体部分 package UI; //自定义窗体 //创建类 继承JFrame //创建构造方法,设置窗体 ...

  2. java飞机大战编程_[源码和文档分享]Java飞机大战游戏设计与实现

    1 概述 本次Java课程设计是做一个飞机大战的游戏,应用Swing编程,完成一个界面简洁流畅.游戏方式简单,玩起来易于上手的桌面游戏.该飞机大战项目运用的主要技术即是Swing编程中的一些窗口类库. ...

  3. 飞机大战java代码_[源码和文档分享]Java飞机大战游戏设计与实现

    1 概述 1.1 项目简介 本次Java课程设计是做一个飞机大战的游戏,应用Swing编程,完成一个界面简洁流畅.游戏方式简单,玩起来易于上手的桌面游戏.该飞机大战项目运用的主要技术即是Swing编程 ...

  4. Java飞机大战游戏(需求分析+代码+图片)

    shoot射击游戏项目一 1.需求分析: 运行项目,看结果: A:小敌机,大敌机,蜜蜂,--------敌人 英雄机发射子弹 B: 子弹击中小敌机----玩家得分 1分 子弹击中大敌机----玩家得分 ...

  5. 基于Java的飞机大战游戏的设计与实现论文

    源码下载 http://www.byamd.xyz/hui-zong-1/ 摘 要 现如今,随着智能手机的兴起与普及,加上4G(the 4th Generation mobile communicat ...

  6. 基于Java的飞机大战游戏的设计与实现(含源文件)

    欢迎添加微信互相交流学习哦! 项目源码:https://gitee.com/oklongmm/biye 基于Java的飞机大战游戏的设计与实现 摘   要 现如今,随着智能手机的兴起与普及,加上4G( ...

  7. 以java语言为工具的粗糙的飞机大战游戏的开发与实现

    以java语言为工具的粗糙的飞机大战游戏的开发与实现 第一步,导入外部图片的素材库 第二步,构建窗体和面板 第三步,在GamePanel上面构建背景图画 第四步,构建英雄机和敌人的机器,支援机器之类( ...

  8. 用Java编写飞机大战游戏

    飞机大战(Plane War)是一款非常受欢迎的小游戏,它通过增加玩家的难度和挑战性,促使玩家不断提高自己的操作能力和反应速度,并在升级过程中逐步拓展游戏世界的规模和内容.本文将介绍如何使用Java编 ...

  9. 毕业设计 基于Java的飞机大战游戏的设计与实现

    文章目录 前言 一.项目设计 1. 模块设计 功能需求 游戏状态控制功能模块 游戏难度的调整模块 游戏界面绘画功能模块 玩家游戏控制功能模块 2. 实现效果 二.部分源码 项目源码 前言 今天学长向大 ...

  10. Java Swing 飞机大战游戏

    呀~好久没有更新文章啦~~这次我们用Java的Swing图形界面框架写一个飞机大战小游戏 飞机大战游戏分为几个部分: com.mr // 主目录 main Constants // 常量 Start ...

最新文章

  1. 计算机网络基础常考面试题总结
  2. H5拍照、预览、压缩、上传采坑记录
  3. 168. Excel Sheet Column Title
  4. NOV. 2nd, 减肥第三天
  5. 编译rocksdb源码导致的部署失败
  6. Leetcode--442. 数组中重复的数据
  7. reactor模型_Reactor模式以及Netty中的应用
  8. Aruba7010 默认密码_钟祥人注意!手机这个密码必须设,否则危险!
  9. 8条体面的程序员偷懒技巧
  10. StringUtils 中 isEmpty 和 isBlank 的区别
  11. office 高效办公智慧树_精品网课office,让工作学习更轻松
  12. bom sap 替代项目_SAP BOM替代物料讲解
  13. 电脑声音无法找到输出设备咋整啊?
  14. word文档 文字变网址 解决办法
  15. python stack unstack_Python之数据重塑
  16. 网上值机软件测试,值机员考试(B)题
  17. 华为正式上线鸿蒙,华为Mate30、鸿蒙OS资料汇总,方舟编译器正式上线
  18. xp系统计算机怎么连接到网络打印机,WinXP电脑如何添加网络打印机?WinXP添加网络打印机的步骤...
  19. HTML登录注册页面简单实现
  20. 索辰科技在科创板过会:拟募资金额有所下调,上半年亏损4000万元

热门文章

  1. android 4.3版本应用使用内网,Android Studio 在内网的使用方法(离线使用gradle)
  2. php 判断3g,php 3g{WAp}网站编写注意事项
  3. leetcode·动态规划
  4. java加解密算法概述
  5. MySQL(6)-----数据类型
  6. Python 02 编写代码
  7. C++调用C#编写的DLL【转】
  8. Spring Boot 集成RabbitMQ
  9. [C++]类的继承与派生
  10. 函数:MySQL中取整函数round和floor