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

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

服务器之家 - 编程语言 - Java教程 - java实现简易飞机大战

java实现简易飞机大战

2022-12-06 15:10编程夜游神 Java教程

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

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

整体思路

1.创建游戏窗体,添加面板JPanel,重写JPanel中的paint方法,遍历所有飞机和子弹绘制,用定时器进行重绘,实现动画效果
2.添加敌机和发射子弹用的是多线程
3.碰撞检测采用的是矩形类Rectangle中的intersects方法

代码实现

用手机查看代码好像只显示62行

英雄战机类

?
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.cml.model;
 
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
 
import com.cml.util.ImageUtil;
 
public class Hero {
    private int x, y;// 坐标
    private int width, height;    //宽高
    private Image heroImage; // 图片
    private boolean isAlive = true;
    private ArrayList<Bullet> bullets = new ArrayList<>();
    public Hero() {
        this.x = 180;
        this.y = 600;
        this.heroImage = ImageUtil.hero;
        width = heroImage.getWidth(null);
        height = heroImage.getHeight(null);
        initBullets();
    }
    private void initBullets() {
        //用线程发射子弹
        new Thread() {
            @Override
            public void run() {
                while (isAlive) {
                    bullets.add(new Bullet(Hero.this));
                    try {
                        sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
 
    public Hero(int x, int y, Image heroImage) {
        super();
        this.x = x;
        this.y = y;
        this.heroImage = heroImage;
    }
 
    public int getX() {
        return x;
    }
 
    public void setX(int x) {
        this.x = x;
    }
 
    public int getY() {
        return y;
    }
 
    public void setY(int y) {
        this.y = y;
    }
 
    public Image getHeroImage() {
        return heroImage;
    }
 
    public void setHeroImage(Image heroImage) {
        this.heroImage = heroImage;
    }
 
    //绘制英雄战机
    public void paint(Graphics g) {
        if (!isAlive) {
            heroImage = ImageUtil.hero_destory;
        }
        g.drawImage(heroImage, x, y, null);
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            if (bullet.getY() < 0) {
                bullets.remove(bullet);
            }
            bullet.paint(g);
        }
    }
 
    public int getWidth() {
        return width;
    }
 
    public void setWidth(int width) {
        this.width = width;
    }
 
    public int getHeight() {
        return height;
    }
 
    public void setHeight(int height) {
        this.height = height;
    }
 
    //鼠标拖拽移动
    public void mouseDragged(MouseEvent e) {
        if (isAlive) {
            int x0 = e.getX();
            int y0 = e.getY();
            if (isInScopePanel(x0, y0)) {
                if (isInScopeImageWidth(x0) && isInScopeImageheigth(y0)) {
                    this.x = x0 - width / 2;
                    this.y = y0 - height / 2;
                }
            } else {
                if (isInScopeImageWidth(x0)) {
                    this.x = x0 - width / 2;
                }
                if (isInScopeImageheigth(y0)) {
                    this.y = y0 - height / 2;
                }
            }
 
        }
        
    }
 
    private boolean isInScopePanel(int x0, int y0) {
        if (x0 > 10 && x0 < 460 && y0 > 140 && y0 < 730) {
            return true;
        }
        return false;
    }
 
    private boolean isInScopeImageheigth(int y0) {
        if (y0 >= y && y0 <= y + height) {
            if (y0 > 140 && y0 < 730) {
                return true;
            }
        }
        return false;
    }
 
    private boolean isInScopeImageWidth(int x0) {
        if (x0 >= x && x0<= x + width) {
            if (x0 > 10 && x0 < 460) {
                return true;
            }
        }
        return false;
    }
    public ArrayList<Bullet> getBullets() {
        return bullets;
    }
    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }
    public boolean isAlive() {
        return isAlive;
    }
}

敌机类

?
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
package com.cml.model;
 
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
 
import com.cml.util.ImageUtil;
 
public class Enemy {
    private Random random = new Random();
 
    private int x, y;// 坐标
    private int width, height; // 宽高
    private boolean isAlive = true;
    private static final int SPEED = 4;
    private Image enemyImage; // 图片
 
    public Enemy() {
        RandomEnemyXY();
        enemyImage = ImageUtil.enemy;
        width = enemyImage.getWidth(null);
        height = enemyImage.getHeight(null);
    }
 
    private void RandomEnemyXY() {
        x = random.nextInt(430);
        y = 0;
    }
 
    public void paint(Graphics g) {
        if (!isAlive) {
            enemyImage = ImageUtil.bomb;
        }
        g.drawImage(enemyImage, x, y, null);
        move();
    }
 
    public boolean isAlive() {
        return isAlive;
    }
 
    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }
 
    private void move() {
        if (isAlive) {
            y += SPEED;
        }
        
    }
 
    public int getX() {
        return x;
    }
 
    public void setX(int x) {
        this.x = x;
    }
 
    public int getY() {
        return y;
    }
 
    public void setY(int y) {
        this.y = y;
    }
 
    public int getWidth() {
        return width;
    }
 
    public void setWidth(int width) {
        this.width = width;
    }
 
    public int getHeight() {
        return height;
    }
 
    public void setHeight(int height) {
        this.height = height;
    }
}

子弹类

?
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
package com.cml.model;
 
import java.awt.Graphics;
import java.awt.Image;
 
import com.cml.util.ImageUtil;
 
public class Bullet {
    private int x, y;// 坐标
    private int width, height; // 宽高
    private static final int SPEED = 10; // 速度
    private Image bulletImage; // 图片
 
    public Bullet(Hero hero) {
        bulletImage = ImageUtil.bullet;
        width = bulletImage.getWidth(null);
        height = bulletImage.getHeight(null);
        this.x = hero.getX() + hero.getWidth() / 2 - width / 2;
        this.y = hero.getY();
    }
 
    public int getX() {
        return x;
    }
 
    public void setX(int x) {
        this.x = x;
    }
 
    public int getY() {
        return y;
    }
 
    public void setY(int y) {
        this.y = y;
    }
 
    public int getWidth() {
        return width;
    }
 
    public void setWidth(int width) {
        this.width = width;
    }
 
    public int getHeight() {
        return height;
    }
 
    public void setHeight(int height) {
        this.height = height;
    }
 
    public void paint(Graphics g) {
        g.drawImage(bulletImage, x, y, null);
        move();
    }
 
    private void move() {
        y -= SPEED;
    }
}

图片工具类

?
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
package com.cml.util;
 
import java.awt.image.BufferedImage;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
public class ImageUtil {
    public static BufferedImage hero;
    public static BufferedImage enemy;
    public static BufferedImage bullet;
    public static BufferedImage bg;
    public static BufferedImage bomb;
    public static BufferedImage hero_destory;
    public static BufferedImage login;
    
    static {
        try {
            hero = ImageIO.read(ImageUtil.class.getResource("/img/hero.png"));
            enemy = ImageIO.read(ImageUtil.class.getResource("/img/enemy.png"));
            bullet = ImageIO.read(ImageUtil.class.getResource("/img/bullet.png"));
            bg = ImageIO.read(ImageUtil.class.getResource("/img/bg.png"));
            bomb = ImageIO.read(ImageUtil.class.getResource("/img/bomb.png"));
            hero_destory = ImageIO.read(ImageUtil.class.getResource("/img/hero_destory.png"));
//            login = ImageIO.read(new File("img/login.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

游戏窗体类

?
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.cml.frame;
 
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
 
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 
import com.cml.model.Bullet;
import com.cml.model.Enemy;
import com.cml.model.Hero;
import com.cml.util.ImageUtil;
import com.sun.java.swing.plaf.windows.resources.windows;
 
public class GameFrame extends JFrame {
 
    private JPanel gamePanel;
 
    private Hero hero;
    private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
    private ArrayList<Bullet> hero_bullet;
    private Timer timer;
 
    public GameFrame() {
        // 初始化游戏窗体
        initGameFrame();
        // 初始化英雄战机
        initHero();
        // 初始化游戏面板
        initGamePanel();
        // 初始化定时器
        initTimer();
        // 初始化敌军战机
        initEnemies();
    }
 
    private void initEnemies() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    enemies.add(new Enemy());
                }
 
            }
        }.start();
 
    }
 
    private void initTimer() {
        timer = new Timer();
        TimerTask task = new TimerTask() {
 
            @Override
            public void run() {
                gamePanel.repaint();
            }
        };
        timer.scheduleAtFixedRate(task, 0, 20);
    }
 
    private void initHero() {
        hero = new Hero();
        hero_bullet = hero.getBullets();
    }
 
    private void initGameFrame() {
        setTitle("打飞机");
        setSize(480, 800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
    }
 
    private void initGamePanel() {
        gamePanel = new JPanel() {
            private int score = 0;
            /**
             * 判断敌机与子弹相撞
             * @param enemy
             * @param bullet
             * @return
             */
            public boolean isHit(Enemy enemy, Bullet bullet) {
                Rectangle r1 = new Rectangle(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
                Rectangle r2 = new Rectangle(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight());
                return r1.intersects(r2);
            }
            
            /**
             * 判断英雄战机与敌机相撞
             * @param enemy
             * @param hero
             * @return
             */
            public boolean isHit(Enemy enemy, Hero hero) {
                Rectangle r1 = new Rectangle(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
                Rectangle r2 = new Rectangle(hero.getX() + hero.getWidth() / 3, hero.getY() + hero.getHeight() / 3,
                        hero.getWidth() / 3, hero.getHeight() / 3);
                return r1.intersects(r2);
            }
 
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.drawImage(ImageUtil.bg, 0, 0, 480, 800, null);
                for (int i = 0; i < enemies.size(); i++) {
                    Enemy enemy = enemies.get(i);
                    for (int j = 0; j < hero_bullet.size(); j++) {
                        Bullet bullet = hero_bullet.get(j);
                        if (isHit(enemy, bullet) && enemy.isAlive()) {
                            enemy.setAlive(false);
                            hero_bullet.remove(bullet);
                            new Thread() {
                                public void run() {
                                    score += 10;
                                    try {
                                        sleep(200);
                                    } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    enemies.remove(enemy);
                                };
                            }.start();
                            break;
                        }
                    }
                    if (isHit(enemy, hero)) {
                        timer.cancel();
                        hero.setAlive(false);
                        enemy.setAlive(false);
                        JOptionPane.showMessageDialog(this, "游戏结束,您的得分是:" + score);
                        System.exit(0);
                    }
                    if (enemy.getY() > 800) {
                        enemies.remove(enemy);
                    }
                    enemy.paint(g);
                }
                if (hero != null) {
                    hero.paint(g);
                }
                g.setFont(new Font("宋体", Font.BOLD, 24));
                g.drawString("得分:" + score, 350, 30);
 
            }
        };
        add(gamePanel);
        // 设置拖拽监听事件
        gamePanel.addMouseMotionListener(new MouseAdapter() {
 
            @Override
            public void mouseDragged(MouseEvent e) {
                if (hero != null) {
                    hero.mouseDragged(e);
                }
            }
        });
    }
 
}

启动游戏类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.cml.main;
 
import com.cml.frame.GameFrame;
 
public class Start {
 
    public static void main(String[] args) {
        /**
         * 初始化窗体
         */
        new GameFrame().setVisible(true);
    }
}

运行效果图

java实现简易飞机大战

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

原文链接:https://blog.csdn.net/weixin_48598155/article/details/106959333

延伸 · 阅读

精彩推荐