import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class PlaneFrame extends JFrame{ PlanePanle jp; //子弹队列 static List list = new ArrayList(); //boss队列 static List bossList = new ArrayList(); //敌方战机队列 static List enemyList = new ArrayList<>(); //存放boss子弹对象 static ArrayList bossBulletList=new ArrayList(); //存放敌机子弹 static ArrayList enemyShell = new ArrayList(); static int enemyPlaneCount=0;//敌方的数目 static int enemyPlaneNumber=5; //设置敌人最大飞机数目 static int bossCount;//boss的数量 static int bossNumber=1;//boss的最大数量 public static String path=System.getProperty("user.dir")+"\\Resouce"; public static HashMap maps = new HashMap<>(); public void start() { File[] file = new File(path).listFiles(); for(int i=0;i bossBulletList=new ArrayList();//存放boss子弹对象 boolean start = true;//判断游戏开始没,游戏开始产生一个boss //画boss public void drawBoss(Graphics g) { timeX(); move(); if(Boss.life>0) { g.drawImage(PlaneFrame.maps.get("boss.png"),x,y,null); } else { PlaneFrame.bossList.remove(this); } bossShot(g);//boss发子弹 } //boss发子弹实现 public void bossShot(Graphics g) { shootInterval(x,y);//发弹间隔时间 ifBossShootMyPlane(g);//判断boss子弹是否射中我方战机 } //是否击中我方飞机 public void ifBossShootMyPlane(Graphics g) { for(int i=0;i700) //如果子弹出界 { bossBulletList.remove(bossBulletList.get(i)); } //删除子弹对象 else { bossBulletList.get(i).drawBossBullet(g); //画出子弹 int x1=bossBulletList.get(i).x; int y1=bossBulletList.get(i).y; if(x1-myPlane.x>-25&&x1-myPlane.x<60&&myPlane.y-y1<60&&y1-myPlane.y<0) { System.out.println("子弹:"+"("+x1+","+y1+")"); System.out.println("飞机:"+"("+myPlane.x+","+myPlane.y+")"); int ex=myPlane.x; int ey=myPlane.y; MyPlane.life-=2; bossBulletList.remove(bossBulletList.get(i)); Explode(ex,ey,g); } } } } public void Explode (int x,int y,Graphics g) { for(int i=0;i<33;i++) { g.drawImage(PlaneFrame.maps.get((i+1)+".png"), x, y,null); } } public void shootInterval(int x, int y) { if(shootTime<=0) //每隔一定时间发射子弹 { shootTime=time; Shell s=new Shell(x+90,y+145); bossBulletList.add(s); } shootTime--; } public void timeX() { if(time<=0) { time=random.nextInt(40)+20; //水平定向移动持续时间 x1=random.nextInt(4); //产生向右的移动增量 x2=-random.nextInt(4); y1=random.nextInt(4); //产生向下的移动增量 y2=-random.nextInt(4); //产生向上的移动增量 while(y1+y2==0) //让正负增量之和不为0 ,为0战机就不动了,不为0就动 {y2=-random.nextInt(4);} //让正负增量之和不为0 ,为0战机就不动了,不为0就动 } time--; } public void move() { x=x+x1+x2; y=y+y1+y2; //防止敌方飞机出界 if(x<0){ x=0; time=0; } if(x>540){ x=540; time=0; } //保证boss在上方 if(y>150) { y=150; } if(y<0) { y=0; } } } import java.awt.Graphics; import java.util.ArrayList; import java.util.Random; public class EnemyPlane { Random random = new Random(); int x=random.nextInt(500)-20; //初始出现x坐标 int y=random.nextInt(300)+10; //初始出现y坐标 int life=1;//敌方飞机生命 int x1,x2;//飞机左右移动增量 int timeX;//水平移动的时间 int shootTime; MyPlane myPlane; ArrayList enemyShell = new ArrayList(); public EnemyPlane(MyPlane myPlane) { setMyPlane(myPlane); } public void setMyPlane(MyPlane myPlane) { this.myPlane = myPlane; } public void drawEnemyPlane(Graphics g) { timeX(); move(); if(this.life>0) { g.drawImage(PlaneFrame.maps.get("enemy8.png"), x, y+2, null); }else { PlaneFrame.enemyList.remove(this); } enemyShot(g);//敌机发子弹 } public void enemyShot(Graphics g) { shootInterval(x,y,110);//发弹间隔时间 ifBossShootMyPlane(g);//判断boss子弹是否射中我方战机 } private void ifBossShootMyPlane(Graphics g) { for(int i=0;i700) //如果子弹出界 { enemyShell.remove(enemyShell.get(i)); } //删除子弹对象 else { enemyShell.get(i).drawEnemyBullet(g); //画出子弹 int x1=enemyShell.get(i).x; int y1=enemyShell.get(i).y; if(x1-myPlane.x>-10&&x1-myPlane.x<70&&myPlane.y-y1<20&&y1-myPlane.y<0) { System.out.println("子弹:"+"("+x1+","+y1+")"); System.out.println("飞机:"+"("+myPlane.x+","+myPlane.y+")"); int ex=myPlane.x; int ey=myPlane.y; MyPlane.life--; enemyShell.remove(enemyShell.get(i)); Explode(ex,ey,g); } } } } public void Explode (int x,int y,Graphics g) { for(int i=0;i<33;i++) { g.drawImage(PlaneFrame.maps.get((i+1)+".png"), x, y,null); } } private void shootInterval(int x, int y, int i) { if(shootTime<=0) //每隔一定时间发射子弹 { shootTime=i; Shell s=new Shell(x+30,y+50); enemyShell.add(s); } shootTime--; } public void timeX() { if(timeX<=0) { timeX=random.nextInt(40)+20; //水平定向移动持续时间 x1=random.nextInt(4); //产生向右的移动增量 x2=-random.nextInt(4); } timeX--; } public void move() { x=x+x1+x2; //y=y+random.nextInt(2); //防止敌方飞机出界 if(x<0){ x=0; timeX=0; } if(x>540){ x=540; timeX=0; } } } import java.awt.Graphics; import java.awt.event.KeyEvent; import javax.swing.JOptionPane; public class MyPlane { static int life=5;//我方战机的生命 //我方飞机的坐标 int x,y; //飞机速度 int speed=4; //飞机移动方向 boolean up; boolean down; boolean right; boolean left; public MyPlane(int x,int y) { this.x=x; this.y=y; } public void drawMyPlane(Graphics g) { if(up) { y-=speed; if(y<=0)y=0; } if(down) { y+=speed; if(y>620)y=620; } if(left) { x-=speed; if(x<=0)x=0; } if(right) { x+=speed; if(x>540)x=540; } //画我方飞机 if(life>0) { g.drawImage(PlaneFrame.maps.get("plane.png"),x,y,null); } } }