Java飞机大战源代码

时间:2023-03-09 22:50:48
Java飞机大战源代码

刚学不久java,做了一个飞机大战的小小小小游戏,现在把这个思路总结以及代码分享出来。大佬别吐槽(emmmmmm .....
开发环境:jdk1.7 
开发工具:eclipese

PlanelJPanel.java

 package project02;

 import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Cursor;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Image;
 import java.awt.Shape;
 import java.awt.Stroke;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.math.MathContext;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;

 import javax.imageio.ImageIO;
 import javax.swing.JPanel;

 import sun.invoke.util.BytecodeName;

 public class PlanelJPanel extends JPanel implements Runnable,MouseListener,MouseMotionListener{
     //定义一个图片对象
     int zdx,zdy;
     static Image startImg;
     int cout=0;
     int score=0;
     //创建一个放飞机的数组
     static BufferedImage p[]=new BufferedImage[2];
     static BufferedImage game[]=new BufferedImage[2];
     //定义图片的坐标
     int x=0,y=0;
     //飞机坐标
     int px=100,py=100;
     int pc=0;
     //定义开始游戏的开关 ,当ck等于true表示没有开始游戏;
     boolean ck=true;
     //定义子弹图片
     static Image bImg;
     //定义奖励子弹
     static Image award2;
     static Image blast;
     int awardx=100;
     int awardy=100;
     //定义及奖励的出现的时间;
     int AwardTime=0;
     //定义敌人飞机图片数组
     static BufferedImage Dpanel[]=new BufferedImage[5];
     //定义敌人飞机坐标
     int Djx,Djy=0;
     List<Dpanel> Djs=new ArrayList<Dpanel>();//把敌人加到集合
     List<Bullet> bullets=new ArrayList<Bullet>();//把子弹加到集合
     //定义游戏暂停开关
     boolean suspend=false;
     boolean gz=false;
     boolean tt=false;
     boolean gameOver=false;//游戏结束按钮
     //定义线程
     Thread thread;
     //静态代码块
     static {
         //加载图片
         try {
             startImg=ImageIO.read(new File("images/GameInterface/interface_1.png"));
             p[0]=ImageIO.read(new File("images/1.png"));
             p[1]=ImageIO.read(new File("images/2.png"));
             bImg=ImageIO.read(new File("images/bullet/bullet_1.png"));
             Dpanel[0]=ImageIO.read(new File("images/LittlePlane/plane2.png"));
         } catch (IOException e) {
             // TODO 自动生成的 catch 块
             e.printStackTrace();
         }
     }
     //构造方法
     public PlanelJPanel() {
         addMouseMotionListener(this);
         addMouseListener(this);
         thread=new Thread(this);
     }
     //画布
     public void paint(Graphics g) {
         super.paint(g);
         g.drawImage(startImg, x, y, null);
         if(gz==true&&ck==false&&gameOver==false) {
             Graphics2D g2=(Graphics2D)g;
             g2.setFont(new Font("宋体", Font.BOLD, 50));
             g2.setColor(Color.red);
             g2.drawString("游戏暂停", 100, 200);
         }
         if(gameOver==true&&ck==false) {
             Graphics2D g2=(Graphics2D)g;
             g2.setFont(new Font("宋体", Font.BOLD, 50));
             g2.setColor(Color.red);
             g2.drawString("游戏结束!", 100, 200);
         }
         if(ck==false) {
             pc=pc==0?1:0;
             g.drawImage(p[pc], px, py, null);
             Graphics2D g2=(Graphics2D)g;
             g2.setFont(new Font("宋体", Font.BOLD, 20));
             g2.setColor(Color.red);
             g2.drawString("游戏得分"+score, 20,20 );
         }
         //画子弹的方法
         for (int i = 0; i < bullets.size(); i++) {
             Bullet bullet=bullets.get(i);
             bullet.drawBullet(g);
             //System.out.println(Djs.size());
         }
         //画敌人飞机的方法
         for (int i = 0; i < Djs.size(); i++) {
             Dpanel dpanel=Djs.get(i);
             dpanel.drawDpanel(g);

             //    repaint();
         }
     }
     //线程需要执行的方法
     public void run() {
         synchronized (this) {
             while (true) {
                 cout++;
                 //AwardTime++;
                 awardy++;
                 if(gz) {
                     try {
                         wait();
                     } catch (InterruptedException e) {
                         // TODO 自动生成的 catch 块
                         e.printStackTrace();
                     }
                 }
                 if(gameOver) {
                     thread.stop();
                 }
                 //创建敌人飞机,添加到集合
                 int Djx=(int)(Math.random()*300+50);
                 Dpanel dpanel0=new Dpanel(Dpanel[0], Djx, 0);
                 int Djx1=(int)(Math.random()*300+50);
                 Dpanel dpanel1=new Dpanel(Dpanel[1], Djx1, 0);
                 for (int i = 0; i < Djs.size(); i++) {
                     Dpanel panel=Djs.get(i);
                     panel.DpanelMove();
                 }
                 if(cout%50==0) {
                     Djs.add(dpanel0);
                 }
                 if(cout%20==0) {
                     Djy+=50;
                 }
                 //创建子弹,并且添加到集合里
                 if(cout%20==0) {
                     zdx=px+p[pc].getWidth()/2-bImg.getWidth(null)/2;
                     zdy= py-p[pc].getHeight()/2+bImg.getHeight(null)/2;
                     Bullet bullet0=new Bullet(zdx,zdy, bImg, 0);
                     Bullet bullet1=new Bullet(zdx,zdy, bImg, 1);
                     Bullet bullet2=new Bullet(zdx, zdy, bImg, 2);
                     bullets.add(bullet0);
                     bullets.add(bullet1);
                     bullets.add(bullet2);
                     if(zdy<=0||zdx<=0||zdx>=400) {
                         bullets.remove(bullet0);
                         bullets.remove(bullet1);
                         bullets.remove(bullet2);
                     }
                 }
                 //飞机的移动
                 for (int i = 0; i <Djs.size(); i++) {
                     Dpanel dpanel=Djs.get(i);
                 }
                 //子弹的移动
                 for (int i = 0; i <bullets.size(); i++) {
                     Bullet bullet=bullets.get(i);
                     if(bullet.direction==0) {
                         bullet.moveBullet1();
                     }
                     if(bullet.direction==1) {
                         bullet.moveBullet2();
                     }
                     if(bullet.direction==2) {
                         bullet.moveBullet3();
                     }
                 }
                 //冒泡遍历子弹和敌人
                 for (int i = 0; i < bullets.size(); i++) {
                     Bullet b1=bullets.get(i);
                     for (int j = 0; j < Djs.size(); j++) {
                         Dpanel b2=Djs.get(j);
                         Crash crash=new Crash();
                         boolean f=crash.isCollsion(b1, b2);
                         if(f){
                             score+=5;
                             bullets.remove(i);
                             Djs.remove(j);
                         }
                     }
                 }
                 //敌人和我方飞机的碰撞

                 for (int j = 0; j < Djs.size(); j++) {
                     Dpanel b2=Djs.get(j);
                     Crash crash=new Crash();
                     boolean f1=crash.isCollsion1(px,py,60,80, b2);
                     if(f1){
                         gameOver=true;
                     }
                 }
                 y++;
                 if(y==0) {
                     y=-5400;
                 }
                 try {
                     Thread.sleep(10);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 repaint();
             }
         }
         //定义俩个方法,一个是暂停的方法,一个唤醒的方法。
     }
     public synchronized void regame() {
         notify();

     }
     public void mouseDragged(MouseEvent e) {

     }
     public void mouseMoved(MouseEvent e) {
         //当鼠标移动到开始游戏的区域  变动鼠标
         if(gameOver==false){
             if(ck &&e.getX()>=130&&e.getX()<261&&e.getY()>391&&e.getY()<=430) {
                 setCursor(new Cursor(Cursor.HAND_CURSOR));
             }else if(ck==false){
                 setCursor(new Cursor(Cursor.HAND_CURSOR));
             }else {
                 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
             }
             px=e.getX()-p[pc].getWidth()/2;
             py=e.getY()-p[pc].getHeight()/2;
             if(ck==false&&px<=0) {
                 px=0;
             }
             if(ck==false&&py<=0) {
                 py=0;
             }
             if(ck==false&&px+p[pc].getWidth()>=this.getWidth()) {
                 px=this.getWidth()-p[pc].getWidth();
             }
             if(ck==false&&py+p[pc].getHeight()>=this.getHeight()) {
                 py=this.getHeight()-p[pc].getHeight();
             }
         }

     }

     @Override
     public void mouseClicked(MouseEvent e) {
         //鼠标点击开始游戏要进入游戏
         if(ck && e.getModifiers()==e.BUTTON1_MASK&&e.getX()>=130&&e.getX()<261&&e.getY()>391&&e.getY()<=430) {
             ck=false;
             try {
                 startImg=ImageIO.read(new File("images/background/background_2.png"));

             } catch (IOException e1) {
                 // TODO 自动生成的 catch 块
                 e1.printStackTrace();
             }
             y=-5400;
             //    repaint();
             thread.start();
         }
     }

     @Override
     public void mousePressed(MouseEvent e) {
         // TODO 自动生成的方法存根

     }

     @Override
     public void mouseReleased(MouseEvent e) {
         // TODO 自动生成的方法存根

     }

     @Override
     public void mouseEntered(MouseEvent e) {
         // 进来重新开始线程
         regame();
         gz=false;
     }

     @Override
     public void mouseExited(MouseEvent e) {
         // 出去窗口暂停线程
         gz=true;

     }

 }

PlanelJFrame.java

 package project02;

 import java.awt.Image;

 import javax.swing.JFrame;

 public class PlanelJFrame extends JFrame{

     public PlanelJFrame() {
         //设置窗体标题
         this.setTitle("吃饺子的喵");
         //设置窗体的大小
         this.setSize(400, 600);
         //设置窗体居中
         this.setLocationRelativeTo(null);
         this.setResizable(false);
         //this.setResizable(false);
         //关联关闭按钮
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         //把画布类进行封装成对象
         PlanelJPanel jpanel=new PlanelJPanel();
         this.add(jpanel);

         //让窗体显示
         this.setVisible(true);
     }

     public static void main(String[] args) {
         PlanelJFrame frame=new PlanelJFrame();

     }
 }

Crash.java

 package project02;

 public class Crash {
     public boolean isCollsion(Bullet bullet,Dpanel dpanel){ //敌机和子弹的碰撞
         //敌人 50*50   子弹--10*46
         int x1=bullet.bx;
         int x2=dpanel.Dx;
         int y1=bullet.by;
         int y2=dpanel.Dy;
         int w1=10;
         int h1=46;
         int w2=50;
         int h2=50;
         if(x1>=x2&&x1>=x2+w2){
             return false;
         }else if(x1<=x2&&x1+w1<=x2){
             return false;
         }else if(y1>=y2&&y1>=y2+h2){
             return false;
         }else if(y1<=y2&&y1+h1<=y2){
             return false;
         }

         return true;
     }
     public boolean isCollsion1(int x1,int y1,int w1,int h1,Dpanel dpanel){ //敌机和我方飞机的碰撞

         int x2=dpanel.Dx;
         int y2=dpanel.Dy;
         int w2=50;
         int h2=50;
         if(x1>=x2&&x1>=x2+w2){
             return false;
         }else if(x1<=x2&&x1+w1<=x2){
             return false;
         }else if(y1>=y2&&y1>=y2+h2){
             return false;
         }else if(y1<=y2&&y1+h1<=y2){
             return false;
         }

         return true;
     }

 }

Dpanel.java

 package project02;

 import java.awt.Graphics;
 import java.awt.Image;

 public class Dpanel {
     Image Dpanel;//敌人飞机图片
     int Dx;//飞机X坐标
     int Dy;//飞机Y坐标
     boolean exist=true;//飞机是否存在
     public Dpanel(Image dpanel, int dx, int dy) {
         super();
         this.Dpanel = dpanel;
         this.Dx = dx;
         this.Dy = dy;

     }
     public void drawDpanel(Graphics g) {
         g.drawImage(Dpanel, Dx, Dy, null);
     }
     public void DpanelMove() {
         Dy+=5;
         if(Dy>=600) {
             exist=false;
         }
     }
 }

Bullet.java

 package project02;
 /*
  * 子弹类中主要写子弹的属性和方法
  */

 import java.awt.Graphics;
 import java.awt.Image;

 public class Bullet{
     public int bx,by;//子弹坐标
     Image bImg;//子弹图片
     int direction;//子弹方向
     boolean exist=true;
     int bsend=10;//速度
     public Bullet(int bx, int by, Image bImg, int direction) {
         super();
         this.bx = bx;
         this.by = by;
         this.bImg = bImg;
         this.direction = direction;

     }
     //画子弹的方法
     public void drawBullet(Graphics g) {
         g.drawImage(bImg, bx, by, null);
     }
     public void moveBullet1() {
         by-=bsend;
         if(by<=0) {
             exist=false;
         }
     }
     public void moveBullet2() {
         by-=bsend;
         bx-=bsend/3;
         if(by<=0) {
             exist=false;
         }
     }
     public void moveBullet3() {
         by-=bsend;
         bx+=bsend/3;
         if(by<=0) {
             exist=false;
         }
     }

 }

运行效果:

Java飞机大战源代码

由于有以下没有仔细的算 大概的就写了是图片像素之间的碰撞 效果略差:

Java飞机大战源代码