服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java实现雷霆战机

java实现雷霆战机

2023-02-23 13:57qq_1741337072 Java教程

这篇文章主要为大家详细介绍了java实现雷霆战机,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

GameFame.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
    }
}

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

原文链接:https://blog.csdn.net/qq_54393060/article/details/122849462

延伸 · 阅读

精彩推荐
  • Java教程容器环境的JVM内存设置实践记录

    容器环境的JVM内存设置实践记录

    Docker和K8S的兴起,很多服务已经运行在容器环境,对于java程序,JVM设置是一个重要的环节,这里总结下我们项目里的最佳实践,对容器环境的JVM内存相关知...

    jqpeng9432022-10-21
  • Java教程java实现分页显示效果

    java实现分页显示效果

    这篇文章主要为大家详细介绍了java实现页显示效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    魔灵儿6602021-05-28
  • Java教程java实现支付宝退款功能

    java实现支付宝退款功能

    这篇文章主要为大家详细 介绍了java实现支付宝退款功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    王啸tr19127342021-06-02
  • Java教程如何从官网下载Hibernate jar包的方法示例

    如何从官网下载Hibernate jar包的方法示例

    这篇文章主要介绍了如何从官网下载Hibernate jar包的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    wanglongfei7602021-07-28
  • Java教程Java实现邮件发送功能

    Java实现邮件发送功能

    这篇文章主要为大家详细介绍了Java实现邮件发送功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Marvellous丶10002021-11-13
  • Java教程Java多条件判断场景中规则执行器的设计

    Java多条件判断场景中规则执行器的设计

    近日在公司领到一个小需求,需要对之前已有的试用用户申请规则进行拓展。本文去掉if 判断,试试用一个规则执行器来替代它,感兴趣的可以了解一下...

    老郑_10552021-09-15
  • Java教程java基于反射得到对象属性值的方法

    java基于反射得到对象属性值的方法

    这篇文章主要介绍了java基于反射得到对象属性值的方法,结合实例形式分析了java基于反射获取对象属性值的相关实现方法与操作技巧,需要的朋友可以参考下...

    ITshu5232020-09-02
  • Java教程spring mvc+localResizeIMG实现HTML5端图片压缩上传

    spring mvc+localResizeIMG实现HTML5端图片压缩上传

    这篇文章主要为大家详细介绍了使用spring mvc+localResizeIMG实现HTML5端图片压缩上传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    Mr_Smile20143012020-09-15