Java-Plane-Game

游戏规则

  • S键为战机大招:子弹短时间增加到释放3排子弹
  • 通过鼠标的移动来避开敌方飞机的冲撞
  • 每击毁一架小飞机获得10点分数
  • 当达到1000分时,会出现Boss机,打败即可赢得胜利

目录结构

  • Bullet
  • Data
  • Enemy
  • Fire
  • FlyObject
  • GameFrame
  • GameLogic
  • GamePanel
  • Listener
  • MainStart
  • MenuLogic
  • MenuPanel
  • MyPlane
  • Wboss

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
27
28
29
30
31
32
33
34
35
public class Bullet extends FlyObject{

int dir;

public Bullet(int x1,int y1,int dir){

img = Data.getBullet();

w = img.getWidth();
h = img.getHeight();

x = x1;
y = y1;

this.dir = dir;

}

public void move(){

if(dir==0){
y-=10;
}

if(dir==1){
x-=1;
y-=10;
}

if(dir==2){
x+=1;
y-=10;
}
}
}

Data

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
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

//数据集类
public class Data extends FlyObject{


public static BufferedImage getImgURL(String url){

try {
return ImageIO.read(Data.class.getResource(url));
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

private static BufferedImage bee = getImgURL("/static/img/bee0.png");
private static BufferedImage myPlane = getImgURL("/static/img/hero1.png");
private static BufferedImage enemy = getImgURL("/static/img/airplane0.png");
private static BufferedImage boss = getImgURL("/static/img/bigplane0.png");
private static BufferedImage background = getImgURL("static/img/background.png");
private static BufferedImage start = getImgURL("/static/img/start.png");
private static BufferedImage over = getImgURL("/static/img/gameover.png");
private static BufferedImage pause = getImgURL("/static/img/pause.png");
private static BufferedImage bullet = getImgURL("/static/img/bullet.png");

public static BufferedImage getBee() {

return bee;
}

public static BufferedImage getMyPlane() {
return myPlane;
}

public static BufferedImage getEnemy() {
return enemy;
}

public static BufferedImage getBoss() {
return boss;
}

public static BufferedImage getBackground() {
return background;
}

public static BufferedImage getStart() {
return start;
}

public static BufferedImage getOver() {
return over;
}

public static BufferedImage getPause() {
return pause;
}

public static BufferedImage getBullet() {
return bullet;
}
}

Enemy

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
import java.util.Random;

public class Enemy extends FlyObject{

//设置敌机飞行速度
int speed;
//
int blood;
Random random = new Random();

public Enemy(){

img = Data.getEnemy();
w=img.getWidth();
h=img.getHeight();
x=random.nextInt(Data.getBackground().getWidth()-w);
speed=5;
blood=2;
}

public void move(){
y+=speed;
}

public boolean isShot(Bullet b){
Boolean is = x <= b.x+b.w &&x>b.x-w&&y<=b.y+b.h&&y>b.y-h;
return is;
}

public boolean isShot(MyPlane m){
Boolean is = x <= m.x+m.w &&x>m.x-w&&y<=m.y+m.h&&y>m.y-h;
return is;
}
}

Fire

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
public class Fire extends FlyObject{
//子弹当前移动方向,0为左上角飞,1垂直飞,2右上角飞
int dir;

//构造方法,初始化子弹
public Fire(int hx, int hy, int dir){
//获取子弹的图片
img = Data.getBullet();
w = img.getWidth();
h = img.getHeight();
//根据构造函数传进来的参数设置子弹的位置以及子弹的方向
x = hx;
y = hy;
this.dir=dir;
}

//子弹的移动方法
public void move() {

if(dir==0){
x -= 1;
y += 10;
}
else if(dir == 1){
x += 1;
y += 10;
}
else if(dir == 2){
x -= 2;
y += 10;
}
else if(dir == 3){
x += 2;
y += 10;
}
else if(dir == 4) {
x -= 3;
y += 10;
}
else if(dir == 5){
x += 3;
y += 10;
}
else if(dir == 6) {
y += 10;
}
}

public boolean isShot(MyPlane m){
Boolean is = x <= m.x+m.w &&x>m.x-w&&y<=m.y+m.h&&y>m.y-h;
return is;
}
}

FlyObject

1
2
3
4
5
6
7
8
9
import java.awt.image.BufferedImage;

public class FlyObject {
BufferedImage img;
int x;
int y;
int w;
int h;
}

GameFrame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import javax.swing.*;
import java.awt.*;

public class GameFrame extends JFrame {


public GameFrame() {

setBounds(400, 100, Data.getBackground().getWidth(), Data.getBackground().getHeight());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setTitle("Java飞机大战");
setBackground(Color.BLACK);
}
}

GameLogic

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.awt.image.BufferedImage;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class GameLogic {

//构造本机
MyPlane myPlane = new MyPlane();
//Boss
Wboss boss;
//敌机集合
List<Enemy> enemies = new ArrayList<>();
//弹药集合
List<Bullet> bullets = new ArrayList<>();
//Boss弹药集合
List<Fire> fires = new ArrayList<>();
//定义分数
int score;
//设置游戏开关
Boolean gameover=false;
//设置火力
int power = 1;

//创建本机子弹
int index_MB;
protected void inputMyBullet(){
index_MB++;
if(index_MB>=10){
if(power==1){
Bullet bullet0 = new Bullet(myPlane.x+45,myPlane.y,0);
bullets.add(bullet0);
}else if(power==2){
Bullet bullet0 = new Bullet(myPlane.x+15,myPlane.y,0);
bullets.add(bullet0);
Bullet bullet1 = new Bullet(myPlane.x+75,myPlane.y,0);
bullets.add(bullet1);
}else if(power==3){
Bullet bullet0 = new Bullet(myPlane.x+15,myPlane.y,0);
bullets.add(bullet0);
Bullet bullet1 = new Bullet(myPlane.x+45,myPlane.y,0);
bullets.add(bullet1);
Bullet bullet2 = new Bullet(myPlane.x+75,myPlane.y,0);
bullets.add(bullet2);
}
index_MB=0;
}
}
//本机子弹移动
protected void MyBulletMove(){
for (int i = 0; i < bullets.size(); i++) {
//获取每一颗子弹位置
Bullet bullet = bullets.get(i);
//依次移动每一颗子弹
bullet.move();
}
}
//创建敌机
int index_E;
protected void inputEnemy(){
index_E++;
//创建敌机
if(index_E>=20){
//创建敌机
Enemy enemy = new Enemy();
//加入集合
enemies.add(enemy);
//重置计数器
index_E = 0;
}
}
//敌机移动
protected void enemyMove(){
//遍历敌机集合,依次移动
for (int i = 0; i < enemies.size(); i++) {
//获取集合中敌机
Enemy enemy = enemies.get(i);
//调用敌机类中的移动方法
enemy.move();
}
}
//创建Boss子弹
int index_BB;
protected void inputBossBullet(){
index_BB++;
if (boss!=null){
if(index_BB>=40){
for (int i = 0; i < 7; i++) {
Fire fire = new Fire(boss.x+30,boss.y+70,i);
fires.add(fire);
}
index_BB = 0;
}
}

}
//Boss子弹飞行
protected void BossBulletMove(){
for (int i = 0; i < fires.size(); i++) {
Fire fire = fires.get(i);
fire.move();
}
}
//命中碰撞逻辑
protected void aim(){
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
for (int j = 0; j < enemies.size(); j++) {
Enemy enemy = enemies.get(j);
if(enemy.isShot(bullet)){
enemy.blood--;
bullets.remove(bullet);
if(enemy.blood==0){
score += 10;
enemies.remove(enemy);
}
}
}
}
if(boss!=null){
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
if(boss.isShot(bullet)){
boss.blood--;
bullets.remove(bullet);
if(boss.blood==0){
boss = null;
score+=1000;
break;
}
}
}
}

}
//受击碰撞逻辑
protected void hurt(){
for (int i = 0; i < enemies.size(); i++) {
Enemy enemy = enemies.get(i);
if(enemy.isShot(myPlane)){
enemies.remove(enemy);
myPlane.blood--;
score+=10;
if(myPlane.blood==0){
gameover=true;
}
}
}
for (int i = 0; i < fires.size(); i++) {
Fire fire = fires.get(i);
if(fire.isShot(myPlane)){
myPlane.blood--;
fires.remove(fire);
if(myPlane.blood==0){
gameover=true;
}
}
}
}

//本机大招逻辑
int index_Big;
public void superBang(){
power = 3;
isBang = true;
index_Big++;
if(index_Big>=200){
power = 1;
index_Big = 0;
isBang = false;
}

}
//
//
//
//

Boolean isBang = false;
public void run(){
//创建线程
new Thread(){
public void run(){
//无线循环创建
while (true){
//判断如果游戏没有失败,则执行以下操作
if(!gameover){

//敌机进场
inputEnemy();
//敌机移动
enemyMove();

//本机子弹进场
inputMyBullet();
//本机子弹移动
MyBulletMove();
//执行大招
if(isBang){
superBang();
}

if(score == 100){
//Boss进场
// 构造Boss
boss = new Wboss();
}
if(score >= 100){
//Boss子弹进场
inputBossBullet();
//Boss子弹移动
BossBulletMove();
}

//判断子弹是否击中敌机
aim();
//检测敌机是否撞到游戏机
hurt();
}
//每执行一次,线程休眠一会儿
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();//让线程开始运行
}
}

GamePanel

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
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;


//游戏面板
public class GamePanel extends JPanel {

GameLogic gameLogic = new GameLogic();

//绘制背景
//绘制敌机
//绘制生命值
//绘制击败敌机数
//绘制Boss
//绘制死亡画面
//绘制胜利画面
//绘制菜单页
//绘制选项页
//绘制
//

public GamePanel(GameFrame frame){
//构造对象时执行主逻辑
gameLogic.run();
//加入鼠标监听
//加入键盘监听
frame.addKeyListener(new Listener().getKeyAdapter(gameLogic));
addMouseListener(new Listener().getMouseAdapter(gameLogic));
addMouseMotionListener(new Listener().getMouseAdapter(gameLogic));
}

//画图方法
@Override
public void paint(Graphics g) {
//调用父类中的一些渲染方法
super.paint(g);
//画背景
g.drawImage(Data.getBackground(),0,0,null);
//画敌机
for (int i = 0; i < gameLogic.enemies.size(); i++) {
Enemy enemy = gameLogic.enemies.get(i);
g.drawImage(enemy.img,enemy.x,enemy.y,null);
}
//画子弹
for (int i = 0; i < gameLogic.bullets.size(); i++) {
Bullet bullet = gameLogic.bullets.get(i);
g.drawImage(bullet.img,bullet.x,bullet.y,bullet.w,bullet.h,null);

}
//画分数
g.setColor(Color.white);
//设置字体型号,设置加粗,设置字体大小
g.setFont(new Font("\u6977\u4F53",Font.BOLD,30));
//显示分数
g.drawString("分数:"+gameLogic.score,10,30);
//画游戏机
g.drawImage(gameLogic.myPlane.img,gameLogic.myPlane.x,gameLogic.myPlane.y,null);
//画游戏机血量
for (int i = 0; i < gameLogic.myPlane.blood; i++) {
g.drawImage(gameLogic.myPlane.img,380+i*35,5,30,30,null);
}
if(gameLogic.boss!=null){
g.drawImage(gameLogic.boss.img,gameLogic.boss.x,gameLogic.boss.y,null);
}

if(gameLogic.boss!=null && gameLogic.score>=100){

for (int i = 0; i < gameLogic.fires.size(); i++) {
Fire fire = gameLogic.fires.get(i);
g.drawImage(fire.img,fire.x,fire.y,fire.w,fire.h,null);

}
}
//画游戏结束
if(gameLogic.gameover){

g.drawImage(Data.getOver(),0,0,Data.getBackground().getWidth(),Data.getBackground().getHeight(),null);
//设置字体颜色为红色
//g.setColor(Color.red);
//设置字体型号,设置加粗,设置字体大小
//g.setFont(new Font("楷体",Font.BOLD,35));
//显示字体
//g.drawString("GAMEOVER",170,300);
//设置字体颜色为绿色
//g.setColor(Color.green);
//设置字体型号,设置加粗,设置字体大小
//g.setFont(new Font("楷体",Font.BOLD,29));
//显示字体
g.drawString("点击屏幕任意位置重新开始",50,450);
}
//重新绘制界面
repaint();
}
}

Listener

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
import java.awt.event.*;

//适配器与监听器集
public class Listener {

//鼠标适配器
public MouseAdapter getMouseAdapter(GameLogic gameLogic) {
MouseAdapter adapter = new MouseAdapter() {
//点击鼠标时会执行的代码
@Override
public void mouseClicked(MouseEvent e) {
//游戏结束时候,点击屏幕时重新开始游戏
if (gameLogic.gameover) {
//重新初始化主机
gameLogic.myPlane = new MyPlane();
//重置游戏开关
gameLogic.gameover = false;
//分数清0
gameLogic.score = 0;
//清空敌机集合
gameLogic.enemies.clear();
//随机背景图
//重新绘制
}
}

//确定需要监听的事件,此处监听鼠标移动事件
@Override
public void mouseMoved(MouseEvent e) {
//让游戏机的横纵坐标等于鼠标的移动坐标
//获取鼠标的横纵坐标
int x = e.getX();
int y = e.getY();
//传递坐标
if (!gameLogic.gameover) {
//使鼠标坐标位于图片中央
gameLogic.myPlane.move(x - 114 / 2, y - 93 / 2);
}
//重新绘制界面
}
};
//将适配器加入到监听器中
return adapter;
}

public KeyAdapter getKeyAdapter(GameLogic gameLogic){
KeyAdapter adapter = new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {

}

//当键盘被按下触发
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
//上键
if(keyCode == KeyEvent.VK_UP){
gameLogic.myPlane.y-=10;
}
//下键
else if(keyCode == KeyEvent.VK_DOWN){
gameLogic.myPlane.y+=10;
}
//左键
else if(keyCode == KeyEvent.VK_LEFT){
gameLogic.myPlane.x-=10;
}
//右键
else if(keyCode == KeyEvent.VK_RIGHT){
gameLogic.myPlane.x+=10;
}
//Key == S
else if(keyCode == KeyEvent.VK_S){
gameLogic.superBang();
}
}

@Override
public void keyReleased(KeyEvent e) {

}
};
return adapter;
}

}
//键盘监听
//
//
//
//
//

MainStart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MainStart {

public static void main(String[] args) {

GameFrame frame = new GameFrame();

GamePanel panel = new GamePanel(frame);

frame.add(panel);

frame.setVisible(true);

}
}

MyPlane

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MyPlane extends FlyObject{

int blood;

public MyPlane(){

img = Data.getMyPlane();

x=500;
y=200;

w=img.getWidth();
h=img.getHeight();

blood = 10;

}

public void move(int x1,int y1){
x = x1;
y = y1;
}
}

Wboss

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Wboss extends FlyObject{

int blood;

public Wboss(){

img = Data.getBoss();

x=220;
y=0;

w=img.getWidth();
h=img.getHeight();

blood = 10;
}

public boolean isShot(Bullet b) {
Boolean is = x <= b.x+b.w &&x>b.x-w&&y<=b.y+b.h&&y>b.y-h;
return is;
}
}