基于Java制作一个好玩的打飞机游戏

 更新时间:2022年01月26日 10:54:39   作者:白大锅  
这篇文章主要介绍了基于Java制作的打飞机小游戏,这里整理了详细的代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

1.效果图

2.项目整体构造

3.主类代码展示

public class MyGameFrame  extends  Frame {
    
    Image   planeImg  = GameUtil.getImage("images/plane.png");
    Image   bg  = GameUtil.getImage("images/bg.jpg");
    
    Plane   plane = new Plane(planeImg,250,250);
    Shell[]   shells = new Shell[50];
    
    Explode   bao ;
    Date  startTime = new Date();
    Date  endTime;
    int period;   //游戏持续的时间
    
    //@Override
    public void paint(Graphics g) {        //自动被调用。  g相当于一只画笔
        Color   c =  g.getColor();
        g.drawImage(bg, 0, 0, null);
        
        plane.drawSelf(g);  //画飞机
        
        //画出所有的炮弹
        for(int i=0;i<shells.length;i++){
            shells[i].draw(g);
            
            //飞机和炮弹的碰撞检测!!!
            boolean  peng = shells[i].getRect().intersects(plane.getRect());
            if(peng){
                plane.live = false;
                if(bao ==null){
                    bao  = new Explode(plane.x, plane.y);
                    
                    endTime = new Date();
                    period = (int)((endTime.getTime()-startTime.getTime())/1000);
                }
                bao.draw(g);
            }
            
            //计时功能,给出提示
            if(!plane.live){
                g.setColor(Color.red);
                Font   f  =  new Font("宋体", Font.BOLD, 50);
                g.setFont(f);
                g.drawString("时间:"+period+"秒", (int)plane.x, (int)plane.y);
            }
            
        }
        
        g.setColor(c);
    }
    
    
    //帮助我们反复的重画窗口!
    class  PaintThread  extends  Thread  {
        //@Override
        public void run() {
            while(true){
                repaint();        //重画
                
                try {
                    Thread.sleep(40);       //1s=1000ms
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }        
            }
        }
        
    }
    
    //定义键盘监听的内部类
    class   KeyMonitor extends  KeyAdapter  {

        //@Override
        public void keyPressed(KeyEvent e) {
            plane.addDirection(e);
        }

        //@Override
        public void keyReleased(KeyEvent e) {
            plane.minusDirection(e);
        }
        
        
    }
    
    
    /**
     * 初始化窗口
     */
    public  void  launchFrame(){
        this.setTitle("飞机大战");
        this.setVisible(true);
        this.setSize(Constant.GAME_WIDTH    , Constant.GAME_HEIGHT);
        this.setLocation(300, 300);
        
        this.addWindowListener(new WindowAdapter() {
            //@Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        new PaintThread().start();    //启动重画窗口的线程
        addKeyListener(new KeyMonitor());   //给窗口增加键盘的监听
        
        
        //初始化50个炮弹
        for(int i=0;i<shells.length;i++){
            shells[i] = new Shell();
        }
        
    }

4.飞机类代码展示

public  void  drawSelf(Graphics  g){
        if(live){
                g.drawImage(img, (int)x,(int) y, null);
                
                if(left){
                    x -=speed;
                }
                if(right){
                    x += speed;
                }
                if(up){
                    y -=speed;    //y = y-speed;
                }
                if(down){
                    y += speed;
            }
        }else{
            
        }
        
        
        
    }
    
    public  Plane(Image  img, double x, double y){
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = 3;
        this.width = img.getWidth(null) ;
        this.height = img.getHeight(null);
        
    }
    
    //按下某个键,增加相应的方向
    public  void   addDirection(KeyEvent  e){
        switch (e.getKeyCode()) {
        case KeyEvent.VK_LEFT:
            left = true;
            break;
        case KeyEvent.VK_UP:
            up = true;
            break;
        case KeyEvent.VK_RIGHT:
            right = true;
            break;
        case KeyEvent.VK_DOWN:
            down = true;
            break;
        }
    }

5.炮弹类代码展示

public  Shell(){
        x = 200;
        y = 200;
        width=10;
        height = 10;
        speed = 3;
        degree = Math.random()*Math.PI*2;
    }
    
    public  void   draw(Graphics  g){
        Color   c =  g.getColor();
        g.setColor(Color.YELLOW);
        
        g.fillOval((int)x,(int) y, width, height);
        
        //炮弹沿着任意角度去飞
        x += speed*Math.cos(degree);
        y += speed*Math.sin(degree);
        
        
        if(x<0||x>Constant.GAME_WIDTH-width){
            degree  = Math.PI - degree;
        }
        
        if(y<30||y>Constant.GAME_HEIGHT-height){
            degree  = - degree;
        }

6.爆炸类代码展示

static {
        for (int i = 0; i < 16; i++) {
            imgs[i] = GameUtil.getImage("images/explode/e" + (i + 1) + ".gif");
            imgs[i].getWidth(null);
        }
    }

    int count;

    public void draw(Graphics g) {
        if (count <= 15) {
            g.drawImage(imgs[count], (int) x, (int) y, null);
            count++;
        }
    }

    public Explode(double x, double y) {
        this.x = x;
        this.y = y;
    }

到此这篇关于基于Java制作一个好玩的打飞机游戏的文章就介绍到这了,更多相关Java打飞机游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Java如何利用数字描述更多的信息

    详解Java如何利用数字描述更多的信息

    在数据库里面 ,通常我们会用数字的递进来描述状态等信息 , 但是如果想进行更复杂的操作 , 就有必要对二进制有一定理解了。本文就来趣味性的探讨一下 , 如何通过更少的空间描述更多的信息
    2022-09-09
  • SpringBoot拦截器Filter的使用方法详解

    SpringBoot拦截器Filter的使用方法详解

    这篇文章主要介绍了SpringBoot拦截器Filter的使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java简易登录注册小程序

    Java简易登录注册小程序

    这篇文章主要为大家详细介绍了Java图形界面开发,简易登录注册小程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Java快速掌握Vector类方法

    Java快速掌握Vector类方法

    Vector 类实现了一个动态数组。和 ArrayList 很相似,但是两者是不同的:Vector 是同步访问的;Vector 包含了许多传统的方法,这些方法不属于集合框架
    2022-03-03
  • 使用maven war包打包去除jar包瘦身

    使用maven war包打包去除jar包瘦身

    这篇文章主要介绍了使用maven war包打包去除jar包瘦身操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Spring及Mybatis整合占位符解析失败问题解决

    Spring及Mybatis整合占位符解析失败问题解决

    这篇文章主要介绍了Spring及Mybatis整合占位符解析失败问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • java数据结构排序算法之归并排序详解

    java数据结构排序算法之归并排序详解

    这篇文章主要介绍了java数据结构排序算法之归并排序,结合具体实例形式详细分析了归并排序的原理、实现技巧与相关注意事项,需要的朋友可以参考下
    2017-05-05
  • JDK动态代理,代理接口没有实现类,实现动态代理方式

    JDK动态代理,代理接口没有实现类,实现动态代理方式

    这篇文章主要介绍了JDK动态代理,代理接口没有实现类,实现动态代理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • SpringBoot应用的接口访问从HTTP改为HTTPS

    SpringBoot应用的接口访问从HTTP改为HTTPS

    本文主要介绍了SpringBoot应用的接口访问从HTTP改为HTTPS,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-03-03
  • 深入理解Java设计模式之外观模式

    深入理解Java设计模式之外观模式

    这篇文章主要介绍了JAVA设计模式之外观模式的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
    2021-11-11

最新评论