刚学不久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飞机大战源代码的更多相关文章

  1. Java飞机大战MVC版

    PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...

  2. java飞机大战之子弹的自动生成

    import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...

  3. 飞机大战编写以及Java的面向对象总结

    面向对象课程完结即可编写一个简单的飞机大战程序.我觉得我需要总结一下 飞机大战中类的设计: 父类:FlyingObject(抽象类) 接口:Award .Enemy 子类:Hero.Bullet.Ai ...

  4. java版飞机大战 实战项目详细步骤.md

    [toc] 分析 飞机大战 首先对这个游戏分析,在屏幕上的物体都是飞行物,我们可以把建一个类,让其他飞行物继承这个类.游戏中应有英雄机(也就是自己控制的飞机).敌人.而敌人应该分为打死给分的飞机(就是 ...

  5. 用面向对象的编程方式实现飞机大战小游戏,java版

    概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...

  6. java之线程飞机大战制作

    import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...

  7. Java实现飞机大战游戏

    飞机大战详细文档 文末有源代码,以及本游戏使用的所有素材,将plane2文件复制在src文件下可以直接运行. 实现效果: 结构设计 角色设计 飞行对象类 FlyObject 战机类 我的飞机 MyPl ...

  8. Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现

    看到cocos2d-x推出了3.1版本号,真是每月一次新版本号,速度. 另一个好消息就是http://cn.cocos2d-x.org/上线了,祝贺!啥时候把我的视频和教程放上去呢?!! . 视频下载 ...

  9. cocos2dx 3.0 飞机大战

    因为课程须要.然后又水平有限.所以写了个飞机大战.加上不会画画.所以图片资源也是从微信apk解压出来的,设计思路參考的偶尔e网事. 闲话不说.先讲一下设计.大体上一共分为3个场景.场景以下是Layer ...

随机推荐

  1. vue项目中对axios的二次封装

    近来在使用vue重构公司m站时,使用了axios来进行数据的请求,由于项目的需要,对axios进行了二次封装,点击进入axios //引入axios import axios from 'axios' ...

  2. 阿里巴巴开源前端框架--Weex实践

    Weex是最近很火很NB的一个技术产品,因为本篇介绍的是怎样使用Weex的最佳实践,所以就不罗里吧嗦的夸它怎么怎么好了,感兴趣的可以访问Weex HomePage,或加入旺旺群:1330170019. ...

  3. WebService之CXF注解之三(Service接口实现类)

    ITeacherServiceImpl.java: /** * @Title:ITeacherServiceImpl.java * @Package:com.you.service.impl * @D ...

  4. Linux查看网络的联机状态

    Linux查看网络的联机状态 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ netstat -a^C unix 3 [ ] 流 已连接 14923 @/tmp ...

  5. Linux显示目前与过去登入系统的用户相关信息

    Linux显示目前与过去登入系统的用户相关信息 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ last youhaido pts/0 :0 Sat Jan 2 ...

  6. web开发中对缓存的使用

    很久没有发表随笔了,最近工作不是太忙,抽点时间 给大家谈谈缓存吧 ; 在我从事web开发的几年实践中  接触了缓存技术 也是比较多的,在最初的 项目当中 我们用到 hibernate 的 一二级缓存, ...

  7. 如何把Excel中的E+数值批量修改为文本格式?

    日常工作中,经常会出现这样的情况,当我们把一组数据导入EXCEL表中时,本想让数字在表中全部显示出来,但是表格中却以E+的方式显示,如果数据较少,我们可以用最笨的方法一个一个的点击单元格来实现目的,但 ...

  8. 自定义JS乘法运算误差解决!

    在实际开发中遇到这样一个乘法公式:数量*单价=总价 像这样的浮点数列子:200*8.2,JS算出的结果是: 像这种浮点数的乘法计算就会有误差,我们需要得到准确的值应该是:1640,与我们后台C#计算结 ...

  9. CentOS 7.0 启动多个MySQL实例(mysql-5.7.21)

    Linux系统:CentOS-7.0 MySQL版本:5.7.21 Linux系统下启动多个MySQL实例,目前知道有两种方法,一种是通过官方提供的mysqld_multi.server来实现,但是我 ...

  10. npm包管理器小节一下

    淘宝npm镜像cnpm设置 npm install -g cnpm --registry=https://registry.npm.taobao.org 更新npm的版本 npm install np ...