[Java] 简单的飞机游戏 →→→→→进入此内容的聊天室

来自 , 2021-01-03, 写在 Java, 查看 132 次.
URL http://www.code666.cn/view/4462bf0d
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Point;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13.  
  14. import javax.imageio.ImageIO;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JPanel;
  18.  
  19. public class PlaneFrame extends JFrame{
  20.         PlanePanle jp;
  21.         //子弹队列
  22.         static List<Shell> list = new ArrayList<Shell>();
  23.         //boss队列
  24.         static List<Boss> bossList = new ArrayList<Boss>();
  25.         //敌方战机队列
  26.         static List<EnemyPlane> enemyList = new ArrayList<>();
  27.         //存放boss子弹对象
  28.         static ArrayList<Shell> bossBulletList=new ArrayList<Shell>();
  29.         //存放敌机子弹
  30.         static ArrayList<Shell> enemyShell = new ArrayList<Shell>();
  31.         static int enemyPlaneCount=0;//敌方的数目
  32.         static int enemyPlaneNumber=5;   //设置敌人最大飞机数目
  33.         static int bossCount;//boss的数量
  34.     static int bossNumber=1;//boss的最大数量
  35.         public static String path=System.getProperty("user.dir")+"\\Resouce";
  36.         public static HashMap<String,BufferedImage> maps = new HashMap<>();
  37.         public void start() {
  38.                 File[] file = new File(path).listFiles();
  39.                 for(int i=0;i<file.length;i++) {
  40.                         try {
  41.                                 maps.put(file[i].getName(), ImageIO.read(file[i]));
  42.                         } catch (IOException e) {
  43.                                 e.printStackTrace();
  44.                         }
  45.                 }
  46.         }
  47.         public PlaneFrame() {
  48.                 //设置窗口大小
  49.                 this.setSize(640,700);
  50.                 //设置窗口名称
  51.                 this.setTitle("信工16级2班黄通作品--飞机游戏");
  52.                 //居中
  53.                 this.setLocationRelativeTo(null);
  54.                 //设置不可改变大小
  55.                 this.setResizable(false);
  56.                 //关闭程序,释放资源
  57.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.                 //添加一个适配器
  59.                 this.addKeyListener(new MyKeyListener());
  60.                 //开始加载图片
  61.                 start();
  62.                 //添加面板
  63.                 jp = new PlanePanle();
  64.                 add(jp);
  65.                 //设置窗口可见
  66.                 setVisible(true);      
  67.         }
  68.         //添加一个适配器
  69.         class MyKeyListener extends KeyAdapter{
  70.                 public void keyPressed(KeyEvent e) {
  71.                         jp.keyPressed(e);
  72.                 }
  73.         }
  74.         public static void main(String[] args) {
  75.                 new PlaneFrame();
  76.         }
  77. }
  78.  
  79. import java.awt.Color;
  80. import java.awt.Font;
  81. import java.awt.Graphics;
  82. import java.awt.Point;
  83. import java.awt.event.KeyEvent;
  84. import java.awt.image.BufferedImage;
  85.  
  86. import javax.swing.JLabel;
  87. import javax.swing.JOptionPane;
  88. import javax.swing.JPanel;
  89. class PlanePanle extends JPanel{
  90.                 int key;
  91.                 JLabel bossXue;//boss血量
  92.                 JLabel myPlaneXue;//我方飞机血量
  93.                 //初始化背景坐标
  94.                 Point bgPoint = new Point(0,-2180);
  95.                 //初始化我方飞机
  96.                 MyPlane myPlane = new MyPlane(280,500);
  97.                 //初始化一个boss
  98.                 //Boss boss = new Boss();
  99.                 //初始化一个敌方飞机的坐标
  100.                 Point enemyPlane = new Point(280,10);
  101.                 //面板构造方法,启动线程
  102.                 public PlanePanle() {
  103.                         this.setLayout(null);
  104.                         bossXue = new JLabel("boss血量:"+Boss.life);
  105.                         bossXue.setForeground(Color.BLACK);
  106.                         bossXue.setBounds(300,650,100,30);
  107.                         add(bossXue);
  108.                         new Thread(new BgThread()).start();
  109.                 }
  110.                 public void paint(Graphics g) {
  111.                         //调用父类paint方法
  112.                         super.paint(g);
  113.                         //创建一个画板
  114.                         BufferedImage image = new BufferedImage(640,700,BufferedImage.TYPE_INT_RGB);
  115.                         //创建一个画笔
  116.                         Graphics gs = image.createGraphics();
  117.                         //画背景两张背景,循环连接播放
  118.                         gs.drawImage(PlaneFrame.maps.get("background2a.bmp"),bgPoint.x,bgPoint.y,this);
  119.                         gs.drawImage(PlaneFrame.maps.get("background2a.bmp"),bgPoint.x,bgPoint.y-2880,this);
  120.                         //画我方飞机
  121.                         myPlane.drawMyPlane(gs);
  122.                
  123.                                 //画敌方飞机
  124.                         for(int i = 0;i<PlaneFrame.enemyList.size();i++) {
  125.                                 PlaneFrame.enemyList.get(i).drawEnemyPlane(gs);
  126.                         }
  127.                         //画boss
  128.                         for(int i = 0;i<PlaneFrame.bossList.size();i++) {
  129.                                 PlaneFrame.bossList.get(i).drawBoss(gs);
  130.                         }
  131.                         //子弹
  132.                         for(int i = 0;i<PlaneFrame.list.size();i++) {
  133.                                 PlaneFrame.list.get(i).drawShell(gs);
  134.                         }
  135.                         //gs.setFont(new Font("宋体",Font.BOLD,20));
  136.                         gs.drawString("Boss的生命值:"+Integer.toString(Boss.life),400,20);
  137.                         gs.drawString("我方飞机血量:"+Integer.toString(MyPlane.life),400,60);
  138.                         gs.drawString("操作方法:方向键控飞机移动,空格键发子弹",200,650);
  139.                         //将画板添加到JPanel
  140.                         g.drawImage(image,0,0,this);
  141.                 }
  142.                 class BgThread implements Runnable{
  143.                         @Override
  144.                         public void run() {
  145.                                 while(true) {
  146.                                         //我方飞机死亡,游戏结束,提示窗口
  147.                                         if(MyPlane.life<=0) {
  148.                                                 JOptionPane.showMessageDialog(null,"飞机死亡,游戏结束");
  149.                                                 System.exit(0);
  150.                                         }
  151.                                         if(Boss.life<=0) {
  152.                                                 JOptionPane.showMessageDialog(null,"恭喜,杀死BOSS,游戏胜利!!游戏到此结束");
  153.                                                 System.exit(0);
  154.                                         }
  155.                                         //如果敌机小于2 ,boss的生命为整数而且大于5,重新生成小飞机
  156.                                         if(PlaneFrame.enemyPlaneNumber<=2&&Boss.life%10==0&&Boss.life<100) {
  157.                                                 //boss生命小于30,生成10个飞机
  158.                                                 if(Boss.life<30) {
  159.                                                         PlaneFrame.enemyPlaneNumber=10;
  160.                                                 }
  161.                                                 else
  162.                                                         PlaneFrame.enemyPlaneNumber=5;
  163.                                                 PlaneFrame.enemyPlaneCount=0;
  164.                                         }
  165.                                        
  166.                                         //创建敌机和boss
  167.                                         creatEnemy();
  168.                                         creatBoss();
  169.                                         //如果到到最下面,回到初始位置
  170.                                         if(bgPoint.y==700) {
  171.                                                 bgPoint.y=-2180;
  172.                                         }
  173.                                         bgPoint.y+=1;
  174.                                         //让子弹飞
  175.                                         for(int i = 0;i<PlaneFrame.list.size();i++) {
  176.                                                 PlaneFrame.list.get(i).y-=10;
  177.                                                 //子弹越界,删除该子弹对象
  178.                                                 if(PlaneFrame.list.get(i).y<0)
  179.                                                         PlaneFrame.list.remove(i);
  180.                                         }
  181.                                         //线程休眠
  182.                                         try {
  183.                                                 Thread.sleep(30);
  184.                                         } catch (InterruptedException e) {
  185.                                                 e.printStackTrace();
  186.                                         }
  187.                                         //重新绘图,更新
  188.                                         repaint();
  189.                                 }
  190.                         }
  191.                        
  192.                 }
  193.                 //键按下去
  194.                 public void keyPressed(KeyEvent e) {
  195.                         //如果按了空格键
  196.                         if(e.getKeyCode()==KeyEvent.VK_SPACE) {
  197.                                 fire();
  198.                         }
  199.                         //如果按了上键
  200.                         if(e.getKeyCode()==KeyEvent.VK_UP) {
  201.                                 myPlane.up=true;
  202.                                 myPlane.down=false;
  203.                                 myPlane.right=false;
  204.                                 myPlane.left=false;
  205.                         }
  206.                         //如果按了下键
  207.                         if(e.getKeyCode()==KeyEvent.VK_DOWN) {
  208.                                 myPlane.down=true;
  209.                                 myPlane.up=false;
  210.                                 myPlane.right=false;
  211.                                 myPlane.left=false;
  212.                         }
  213.                         //如果按了左键
  214.                         if(e.getKeyCode()==KeyEvent.VK_LEFT) {
  215.                                 myPlane.left=true;
  216.                                 myPlane.down=false;
  217.                                 myPlane.up=false;
  218.                                 myPlane.right=false;
  219.                                
  220.                         }
  221.                         //如果按了右键
  222.                         if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
  223.                                 myPlane.right=true;
  224.                                 myPlane.left=false;
  225.                                 myPlane.down=false;
  226.                                 myPlane.up=false;
  227.                         }
  228.                         repaint();
  229.                 }
  230.                 //子弹发射方法
  231.                 public void fire() {
  232.                         PlaneFrame.list.add(new Shell(myPlane.x+30,myPlane.y-80));
  233.                 }
  234.                 //创建敌方战机
  235.                 public void creatEnemy() {
  236.                         if(PlaneFrame.enemyPlaneCount<PlaneFrame.enemyPlaneNumber) {
  237.                                 PlaneFrame.enemyList.add(new EnemyPlane(myPlane));
  238.                                 PlaneFrame.enemyPlaneCount++;
  239.                         }
  240.                 }
  241.                 public void creatBoss() {
  242.                         if(PlaneFrame.bossCount<PlaneFrame.bossNumber) {
  243.                                 PlaneFrame.bossList.add(new Boss(myPlane));
  244.                                 PlaneFrame.bossCount++;
  245.                         }
  246.                 }
  247. }
  248.  
  249. import java.awt.Graphics;
  250. import java.util.ArrayList;
  251. import java.util.Random;
  252.  
  253. import org.omg.CORBA.portable.Delegate;
  254.  
  255. public class Boss {
  256.         Random random = new Random();
  257.         static int life=100;//boss的生命
  258.         int x=random.nextInt(500)-20;  //初始出现boss的x坐标
  259.     int y=random.nextInt(100)+10;   //初始出现boss的y坐标
  260.     int x1,x2;//boss水平移动增量
  261.     int y1,y2;//boss竖直的增量
  262.     int time;//移动一次需要的时间
  263.     static int shootTime;//boss发射子弹时间间隔
  264.     MyPlane myPlane;//传入我方飞机对象
  265.     public Boss(MyPlane myPlane) {
  266.         setMyPlane(myPlane);
  267.     }
  268.     public void setMyPlane(MyPlane myPlane) {
  269.         this.myPlane = myPlane;
  270.     }
  271.     ArrayList<Shell> bossBulletList=new ArrayList<Shell>();//存放boss子弹对象
  272.     boolean start = true;//判断游戏开始没,游戏开始产生一个boss
  273.     //画boss
  274.     public void drawBoss(Graphics g) {
  275.         timeX();
  276.         move();
  277.         if(Boss.life>0) {
  278.                         g.drawImage(PlaneFrame.maps.get("boss.png"),x,y,null);
  279.         }
  280.         else {
  281.                 PlaneFrame.bossList.remove(this);
  282.         }
  283.         bossShot(g);//boss发子弹
  284.     }
  285.     //boss发子弹实现
  286.     public void bossShot(Graphics g) {
  287.         shootInterval(x,y);//发弹间隔时间
  288.         ifBossShootMyPlane(g);//判断boss子弹是否射中我方战机
  289.         }
  290.     //是否击中我方飞机
  291.         public void ifBossShootMyPlane(Graphics g) {
  292.                 for(int i=0;i<bossBulletList.size();i++)
  293.                 {  
  294.                         if(bossBulletList.get(i).y>700)    //如果子弹出界
  295.                         { bossBulletList.remove(bossBulletList.get(i)); }                        //删除子弹对象
  296.                        
  297.                         else
  298.                         {
  299.                                 bossBulletList.get(i).drawBossBullet(g);       //画出子弹
  300.                                 int x1=bossBulletList.get(i).x;
  301.                                 int y1=bossBulletList.get(i).y;
  302.                                 if(x1-myPlane.x>-25&&x1-myPlane.x<60&&myPlane.y-y1<60&&y1-myPlane.y<0) {
  303.                                         System.out.println("子弹:"+"("+x1+","+y1+")");
  304.                                         System.out.println("飞机:"+"("+myPlane.x+","+myPlane.y+")");
  305.                                         int ex=myPlane.x;
  306.                                         int ey=myPlane.y;
  307.                                         MyPlane.life-=2;
  308.                                         bossBulletList.remove(bossBulletList.get(i));
  309.                                         Explode(ex,ey,g);
  310.                                 }
  311.                                
  312.                         }
  313.                 }
  314.         }
  315.         public void Explode (int x,int y,Graphics g) {
  316.                 for(int i=0;i<33;i++) {
  317.                         g.drawImage(PlaneFrame.maps.get((i+1)+".png"), x, y,null);
  318.                 }
  319.         }
  320.         public void shootInterval(int x, int y) {
  321.                 if(shootTime<=0)       //每隔一定时间发射子弹
  322.             {                                                                                        
  323.                 shootTime=time;
  324.                 Shell s=new Shell(x+90,y+145);
  325.                 bossBulletList.add(s);
  326.             }
  327.                  shootTime--;
  328.         }
  329.         public void timeX() {
  330.         if(time<=0) {
  331.                 time=random.nextInt(40)+20; //水平定向移动持续时间
  332.                 x1=random.nextInt(4);        //产生向右的移动增量
  333.                         x2=-random.nextInt(4);
  334.                         y1=random.nextInt(4);        //产生向下的移动增量
  335.                         y2=-random.nextInt(4);       //产生向上的移动增量
  336.                         while(y1+y2==0)              //让正负增量之和不为0 ,为0战机就不动了,不为0就动
  337.                                 {y2=-random.nextInt(4);} //让正负增量之和不为0 ,为0战机就不动了,不为0就动
  338.         }
  339.         time--;
  340.     }
  341.     public void move() {
  342.        
  343.                 x=x+x1+x2;
  344.                 y=y+y1+y2;
  345.                 //防止敌方飞机出界
  346.                 if(x<0){
  347.                         x=0;
  348.                         time=0;
  349.                 }  
  350.                         if(x>540){
  351.                                 x=540;
  352.                                 time=0;
  353.                         }
  354.                         //保证boss在上方
  355.                         if(y>150) {
  356.                                 y=150;
  357.                         }
  358.                         if(y<0) {
  359.                                 y=0;
  360.                         }
  361.         }
  362.    
  363. }
  364.  
  365. import java.awt.Graphics;
  366. import java.util.ArrayList;
  367. import java.util.Random;
  368.  
  369. public class EnemyPlane {
  370.         Random random = new Random();
  371.         int x=random.nextInt(500)-20;  //初始出现x坐标
  372.     int y=random.nextInt(300)+10;   //初始出现y坐标
  373.     int life=1;//敌方飞机生命
  374.     int x1,x2;//飞机左右移动增量
  375.     int timeX;//水平移动的时间
  376.     int shootTime;
  377.     MyPlane myPlane;
  378.     ArrayList<Shell> enemyShell = new ArrayList<Shell>();
  379.     public EnemyPlane(MyPlane myPlane) {
  380.         setMyPlane(myPlane);
  381.     }
  382.     public void setMyPlane(MyPlane myPlane) {
  383.         this.myPlane = myPlane;
  384.     }
  385.     public void drawEnemyPlane(Graphics g) {
  386.         timeX();
  387.         move();
  388.         if(this.life>0) {
  389.                 g.drawImage(PlaneFrame.maps.get("enemy8.png"), x, y+2, null);
  390.         }else {
  391.                 PlaneFrame.enemyList.remove(this);
  392.         }
  393.         enemyShot(g);//敌机发子弹
  394.     }
  395.     public void enemyShot(Graphics g) {
  396.         shootInterval(x,y,110);//发弹间隔时间
  397.         ifBossShootMyPlane(g);//判断boss子弹是否射中我方战机
  398.     }
  399.     private void ifBossShootMyPlane(Graphics g) {
  400.         for(int i=0;i<enemyShell.size();i++)
  401.                 {  
  402.                         if(enemyShell.get(i).y>700)    //如果子弹出界
  403.                         { enemyShell.remove(enemyShell.get(i)); }                        //删除子弹对象
  404.                        
  405.                         else
  406.                         {
  407.                                 enemyShell.get(i).drawEnemyBullet(g);       //画出子弹
  408.                                 int x1=enemyShell.get(i).x;
  409.                                 int y1=enemyShell.get(i).y;
  410.                                 if(x1-myPlane.x>-10&&x1-myPlane.x<70&&myPlane.y-y1<20&&y1-myPlane.y<0) {
  411.                                         System.out.println("子弹:"+"("+x1+","+y1+")");
  412.                                         System.out.println("飞机:"+"("+myPlane.x+","+myPlane.y+")");
  413.                                         int ex=myPlane.x;
  414.                                         int ey=myPlane.y;
  415.                                         MyPlane.life--;
  416.                                         enemyShell.remove(enemyShell.get(i));
  417.                                         Explode(ex,ey,g);
  418.                                 }
  419.                                
  420.                         }
  421.                 }
  422.         }
  423.     public void Explode (int x,int y,Graphics g) {
  424.                 for(int i=0;i<33;i++) {
  425.                         g.drawImage(PlaneFrame.maps.get((i+1)+".png"), x, y,null);
  426.                 }
  427.         }
  428.         private void shootInterval(int x, int y, int i) {
  429.                 if(shootTime<=0)       //每隔一定时间发射子弹
  430.             {                                                                                        
  431.                 shootTime=i;
  432.                 Shell s=new Shell(x+30,y+50);
  433.                 enemyShell.add(s);
  434.             }
  435.                  shootTime--;
  436.         }
  437.         public void timeX() {
  438.         if(timeX<=0) {
  439.                 timeX=random.nextInt(40)+20; //水平定向移动持续时间
  440.                 x1=random.nextInt(4);        //产生向右的移动增量
  441.                         x2=-random.nextInt(4);
  442.         }
  443.         timeX--;
  444.     }
  445.     public void move() {
  446.         x=x+x1+x2;
  447.         //y=y+random.nextInt(2);
  448.         //防止敌方飞机出界
  449.         if(x<0){
  450.                 x=0;
  451.                 timeX=0;
  452.         }  
  453.                 if(x>540){
  454.                         x=540;
  455.                         timeX=0;
  456.                 }
  457.     }
  458. }
  459.  
  460. import java.awt.Graphics;
  461. import java.awt.event.KeyEvent;
  462.  
  463. import javax.swing.JOptionPane;
  464.  
  465. public class MyPlane {
  466.         static int life=5;//我方战机的生命
  467.         //我方飞机的坐标
  468.         int x,y;
  469.         //飞机速度
  470.         int speed=4;
  471.         //飞机移动方向
  472.         boolean up;
  473.         boolean down;
  474.         boolean right;
  475.         boolean left;
  476.        
  477.         public MyPlane(int x,int y) {
  478.                 this.x=x;
  479.                 this.y=y;
  480.         }
  481.         public void drawMyPlane(Graphics g) {
  482.                 if(up) {
  483.                         y-=speed;
  484.                         if(y<=0)y=0;
  485.                 }
  486.                 if(down) {
  487.                         y+=speed;
  488.                         if(y>620)y=620;
  489.                 }
  490.                 if(left) {
  491.                         x-=speed;
  492.                         if(x<=0)x=0;
  493.                 }
  494.                 if(right) {
  495.                         x+=speed;
  496.                         if(x>540)x=540;
  497.                 }
  498.                 //画我方飞机
  499.                 if(life>0) {
  500.                         g.drawImage(PlaneFrame.maps.get("plane.png"),x,y,null);
  501.                 }
  502.                 }
  503. }
  504.  

回复 "简单的飞机游戏"

这儿你可以回复上面这条便签

captcha