java实现雷霆战机

 更新时间:2022年06月10日 14:36:43   作者:qq_1741337072  
这篇文章主要为大家详细介绍了java实现雷霆战机,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现雷霆战机的具体代码,供大家参考,具体内容如下

GameFame.java

package cn. tx;
 
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;
 
 class GameFrame extends JFrame {
 
    HeroPlane heroPlane;
 
    //定义子弹的集合
    Vector<Bullet> bullets = new Vector<>();
    //敌机集合
     Vector<EnemyPlane> enemys = new Vector<>();
 
     GameFrame  frame;
 
    public GameFrame () {
        frame = this;
        //创建英雄机
        heroPlane =new HeroPlane();
        heroPlane.start();
        //设置窗体的宽高
        this.setSize(450, 730);
        //标题
        this.setTitle("雷霆战机");
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        //窗口可见
        this.setVisible(true);
 
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    repaint();
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
 
        //产生敌机的线程
        new Thread(new Runnable() {
            //创建随机数的对象
            Random r = new Random();
 
            @Override
            public void run() {
                while (true){
                    //启动敌机
                    EnemyPlane enemyPlane = new EnemyPlane(r.nextInt(500), 0, frame);
                    enemyPlane.start();
                    //添加敌机的时候,让x轴随机
                    enemys.add(enemyPlane);
                    try{
                        Thread.sleep(500);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
 
            }
        }).start();
 
    }
 
 
//        *在窗口上画,内容,paint这 个画笔的方法在窗口初始化的时候会默认的执行
//        @param g
 
        public void paint (Graphics g) {
            //System.out.println("绘制画板");
            //两背景
            BufferedImage image = (BufferedImage) this.createImage(this.getSize().width, this.getSize().height);
            //高效缓存的画笔
            Graphics bi = image.getGraphics();
            //画背景
            bi.drawImage(new ImageIcon("img/MAP02_01.png").getImage(),0,0,null);
            //画战斗机
            bi.drawImage(heroPlane.img, heroPlane.x,heroPlane.y, heroPlane.width,heroPlane.heigth,null);
            //飞机发射炮弹
            for (int i = 0; i < bullets.size(); i++) {
                System.out.println(bullets);
                Bullet bullet = bullets.get(i);
                if(bullet.y > 0)
                    bi.drawImage(bullet.image, bullet.x,bullet.y -= bullet.speed, bullet.width,bullet.height, null);
                else
                    bullets.remove(bullet);
            }
            //画敌机
            for (int i = 0; i < enemys.size(); i++) {
                System.out.println(enemys);
                EnemyPlane ep = enemys.get(i);
                if(ep.y < 730 )
                    bi.drawImage(ep.img, ep.x,ep.y += ep.speed, ep.width,ep.heigth,null);
                else
                    enemys.remove(ep);
            }
 
 
            //生效
            g.drawImage(image,0,0,null);
        }
        public static void main (String[]args){
            GameFrame frame = new GameFrame();
            Player player = new Player(frame);
            frame.addKeyListener(player);
        }
    }

HeroPlane

package cn.tx;
import javax.swing.*;
import java.awt.*;
 
public class HeroPlane extends Thread{
    //英雄机在画板上的位置
    int x=200, y=600;
 
    int width = 50, heigth = 50;
    //飞机的速度
    int speed = 10;
 
    Image img = new ImageIcon("img/10011.png").getImage();
 
    //定义方向键的标志
    boolean up,down,left,right;
 
    public HeroPlane() {
    }
 
    public HeroPlane(int x, int y, int width, int heigth, Image img) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = heigth;
    }
 
    @Override
    public void run() {
        while (true){
            if (up){
                y -= speed;
            }
            if (down){
                y += speed;
            }
            if (left){
                x -= speed;
            }
            if (right){
                x += speed;
            }
 
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Player

package cn.tx;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//定义一个玩家
public class Player extends KeyAdapter {
    GameFrame frame;
    HeroPlane heroPlane;
    public Player(GameFrame frame) {
       this.frame=frame;
    }
 
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //38、40、37、39
        switch (keyCode){
            case 38:
                frame.heroPlane.up = true;
                break;
            case 40:
                frame. heroPlane.down = true;
                break;
            case 37:
                frame. heroPlane.left = true;
                break;
            case 39:
                frame. heroPlane.right = true;
                break;
            case 66:
                addBullut();
                break;
        }
 
    }
 
    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //38、40、37、39
        switch (keyCode){
            case 38:
                frame.heroPlane.up = false;
                break;
            case 40:
                frame. heroPlane.down = false;
                break;
            case 37:
                frame.  heroPlane.left = false;
                break;
            case 39:
                frame. heroPlane.right = false;
                break;
        }
    }
    public void addBullut(){
        frame.bullets.add(new Bullet( frame.heroPlane.x+5,  frame.heroPlane.y - 20));
    }
}

EnemyPlane

package cn.tx;
 
import javax.swing.*;
import java.awt.*;
 
 
public class EnemyPlane extends Thread {
    public GameFrame gf;
    //子弹的坐标,大小速度
    public int x, y;
    public int width = 50;
    public int heigth = 50;
    public int speed = 2;
    public Image img = new ImageIcon("img/10021.png").getImage();
 
    public EnemyPlane(int x, int y, GameFrame gf) {
        super();
        this.x = x;
        this.y = y;
        this.gf = gf;
    }
 
    public EnemyPlane(int x, int y, int width, int heigth, GameFrame gf) {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = heigth;
        this.gf = gf;
    }
 
    //玛丽飞翔的逻辑;移动的逻辑都在这里。
    public void run() {
        while (true) {
            //向左走
            if (hit()) {
                System.out.println("hit................");
                this.speed = 0;
                this.img = new ImageIcon("img/300350.png").getImage();
                    try {
                        this.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    gf.enemys.remove(this);
                    break;
                }
                if (this.y >= 760) {
                    break;
                }
                try {
                    this.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }
 
 
    //检测碰撞
    public boolean hit() {
        // swing在水中,人家已经提供了
        Rectangle myrect = new Rectangle(this.x, this.y, this.width, this.heigth);
        Rectangle rect = null;
        for (int i = 0; 1 < gf.bullets.size(); i++) {
            Bullet bullet = gf.bullets.get(i);
            System.out.println("test hit");
            rect = new Rectangle(bullet.x, bullet.y - 1, bullet.width, bullet.height);
            //碰撞检测
            if (myrect.intersects(rect)) {
                return true;
            }
        }
        return false;
    }
 
    @Override
    public String toString() {
        return "EnemyPlane{" +
                "x=" + x +
                ", y=" + y +
                ", width=" + width +
                ", height=" + heigth +
                '}';
    }
}

Bullet

package cn.tx;
import javax.swing.*;
import java.awt.*;
 
public class Bullet {
    //在面板上的坐标
    int x, y;
    int width= 50,height = 50;
 
    //定义飞机默认速度
    int speed = 5;
 
    Image image = new ImageIcon("img/30022.png").getImage();
 
    public Bullet(int x, int y) {
        this.x = x;
        this.y = y;
    }
 
    public Bullet(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java Handler同步屏障浅析讲解

    Java Handler同步屏障浅析讲解

    同步屏障机制是什么?Handler发送的消息分为普通消息、屏障消息、异步消息,一旦Looper在处理消息时遇到屏障消息,那么就不再处理普通的消息,而仅仅处理异步的消息。不再使用屏障后,需要撤销屏障,不然就再也执行不到普通消息了
    2022-08-08
  • Java中LinkedHashSet的底层机制详解

    Java中LinkedHashSet的底层机制详解

    这篇文章主要介绍了Java中LinkedHashSet的底层机制解读,   LinkedHashSet是具有可预知迭代顺序的Set接口的哈希表和链接列表实现,此实现与HashSet的不同之处在于,后者维护着一个运行于所有条目的双重链接列表,需要的朋友可以参考下
    2023-09-09
  • java贪心算法初学感悟图解及示例分享

    java贪心算法初学感悟图解及示例分享

    这篇文章主要为大家介绍了本人在初学java贪心算法的感悟,并通过图解及示例代码的方式分享给大家,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-11-11
  • SpringBoot返回html界面的使用示例

    SpringBoot返回html界面的使用示例

    本文主要介绍了SpringBoot返回html界面的使用示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11
  • Spring七大组件是哪些以及作用

    Spring七大组件是哪些以及作用

    这篇文章主要介绍了Spring七大组件是哪些以及作用,帮助刚接触spring的朋友更快理解,如果有说的不对的地方还请指正,需要的朋友可以参考下
    2023-03-03
  • 详解设计模式在Spring中的应用(9种)

    详解设计模式在Spring中的应用(9种)

    这篇文章主要介绍了详解设计模式在Spring中的应用(9种),详细的介绍了这9种模式在项目中的应用,具有一定的参考价值,感兴趣的可以了解一下
    2019-04-04
  • Java DirectByteBuffer堆外内存回收详解

    Java DirectByteBuffer堆外内存回收详解

    这篇文章主要为大家详细介绍了Java中发DirectByteBuffer堆外内存回收,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-10-10
  • SpringBoot@Profile注解和Spring EL(多环境注入)

    SpringBoot@Profile注解和Spring EL(多环境注入)

    为了方便, Spring还提供了 Profile机制, 使我们可以很方便地实现各个环境之间的切换,在使用DI来依赖注入的时候,能够根据@profile标明的环境,将注入符合当前运行环境的相应的bean,本文通过示例代码介绍SpringBoot@Profile注解和Spring EL,需要的朋友可以参考下
    2024-02-02
  • SpringBoot项目部署到服务器的两种方式

    SpringBoot项目部署到服务器的两种方式

    目前,前后端分离的架构已成主流,而使用SpringBoot构建Web应用是非常快速的,项目发布到服务器上的时候,只需要打成一个jar包,然后通过命令 : java -jar jar包名称即可启动服务了,本文介绍了SpringBoot项目部署到服务器的两种方式,需要的朋友可以参考下
    2024-10-10
  • 浅谈Java类的加载,链接及初始化

    浅谈Java类的加载,链接及初始化

    今天给大家带来的是关于Java的相关知识,文章围绕着Java类的加载,链接及初始化展开,文中有非常详细的解释及代码示例,需要的朋友可以参考下
    2021-06-06

最新评论