飞机大战源码

java版飞机大战代码

代码有点乱,需要整理一下

稍后会整理一份出来

啥都不说上代码

目前先整理到这里

后续直接上传写好的代码包

预留地址

Plane

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

packagecom;;
importjava.awt.Color;;
importjava.awt.Font;;
importjava.awt.Graphics;;
importjava.awt.Image;;
importjava.awt.Rectangle;
importjava.awt.event.Key;
importjava.io.IOExceptio;imppackage com;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Plane {
Image feijiImage = null;
int x = 300;
int y = 700;
int lifeCount=5;
public Plane() {
try {
feijiImage = ImageIO.read(Plane.class.getClassLoader()
.getResourceAsStream("images/feiji.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
// 画飞机图片
g.drawImage(feijiImage, x, y, null);
// 飞机移动
this.move();
// 血条
if(lifeCount>0){
g.setColor(Color.WHITE);
g.fillRect(20, 80, 100, 10);
g.setColor(Color.red);
g.fillRect(20, 80, (100/5)*lifeCount, 10);
g.setColor(Color.blue);
g.setFont(new Font("幼圆", Font.BOLD, 30));
g.drawString("Score:"+Play01.count, 20, 60);
}
}
//
public void move(){ if(isUP && !isDown && !isLeft && !isRight){ 上 y=y-5; }else if(!isUP && isDown && !isLeft && !isRight){

y=y+5;
}else if(!isUP && !isDown && isLeft && !isRight){

x=x-5;
}else if(!isUP && !isDown && !isLeft && isRight){

x=x+5;
}else if(isUP && !isDown && isLeft && !isRight){
左上
x=x-5;
y=y-5;
}else if(!isUP && isDown && isLeft && !isRight){
左下
x=x-5;
y=y+5;
}else if(isUP && !isDown && !isLeft && isRight){
右上
x=x+5;
y=y-5;
}else if(!isUP && isDown && !isLeft && isRight){
右下
x=x+5;
y=y+5;
} // // // // // // //
} boolean isUP = false; boolean isDown = false; boolean isLeft = false; boolean isRight = false; // 摁下 public void keyPressed(KeyEvent e) {
} if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) { isRight=true; } else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) { isLeft=true; } else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) { isUP=true; } else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) { isDown=true; }
// 放开
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
isRight=false;
} else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
isLeft=false;
} else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
isUP=false;
} else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) { isDown=false;
}
}
public Rectangle getRectangle(){
return new Rectangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null)); }
}

PlaneStatus类

1
2
3
4
5
package com;
public class PlaneStatus {
public static int roleNum = 1;
public static int playStatus = 0;// 0 游戏开始前 1 第一关 -1 游戏结束
}

Power类

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
package com;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Power {
Image PowerImage = null;
int x = 20;
int y = 0;
public Power() {
this.x=(int)(Math.random()*540)+20;
try {
PowerImage = ImageIO.read(Gift.class.getClassLoader()
.getResourceAsStream("images/power.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
// 画血瓶图片
g.drawImage(PowerImage, x, y, null);
// 血瓶移动
y++;
}
public Rectangle getRectangle(){
return new Rectangle(x,y,PowerImage.getWidth(null),PowerImage.getHeight(null)); }
}

play01类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.List;
import javax.imageio.ImageIO;
public class Play01 {
static int count=0;
Image bgImage = null;
// 战机 Plane plane = new Plane(); // 战机子弹 List<MyZiDan> mzds = new ArrayList<MyZiDan>(); // 敌机 List<Diji> dijis = new ArrayList<Diji>(); // 敌机子弹 //List<DijiZiDan> dijizidans = new ArrayList<DijiZiDan>(); // 血瓶 List<Gift> gifts = new ArrayList<Gift>(); List<Power> powers = new ArrayList<Power>(); public Play01() { try { bgImage = ImageIO.read(Play01.class.getClassLoader() .getResourceAsStream("images/bg_01.jpg")); } catch (IOException e) { e.printStackTrace(); } } int bgY1 = 0; int bgY2 = -600; int fireTime = 0; boolean flag=false; public void draw(Graphics g) { // 画背景图片 g.drawImage(bgImage, 0, bgY1, null); bgY1 += 5;


bgY1=0;}g.drawImage(bgIm;dijis.add(newDiji());}//;}//判断战机和血瓶相撞 for(inti=0;i;}}booleanisFire1=false; b;packagecom;;importjava.awt.Color;;importjava.awt.Font;;importjava.awt.Graphics;;


bgY1 = 0; } g.drawImage(bgImage, 0, bgY2, null); bgY2 += 5; if (bgY2 >= 0) { bgY2 = -600; } // 清理战机子弹 for (int i = 0; i < mzds.size(); i++) { MyZiDan myZidan = mzds.get(i); if (myZidan.x > 800) { mzds.remove(i); } } // 添加子弹 if (isFire1 == true && flag==false) { if (fireTime % 8 == 0) { mzds.add(new MyZiDan(plane.x + 25, plane.y + 0)); } fireTime++; } if (isFire1== true && flag==true) { if (fireTime % 4 == 0) { } mzds.add(new MyZiDan(plane.x +8, plane.y +0)); mzds.add(new MyZiDan(plane.x +52, plane.y +0)); } fireTime++; // 画战机子弹 for (int i = 0; i < mzds.size(); i++) { MyZiDan myZidan = mzds.get(i); myZidan.draw(g); } // 清理敌机 for (int i = 0; i < dijis.size(); i++) { Diji dj = dijis.get(i); if (dj.x < -100) { dijis.remove(i); } } // 抽奖:添加敌机
dijis.add(new Diji()); } // 抽奖:添加血瓶 if ((int) (Math.random() * 1000) == 5) { gifts.add(new Gift()); } for (int i = 0; i < gifts.size(); i++) { Gift gift = gifts.get(i); gift.draw(g); } // // 抽奖:添加power if ((int) (Math.random() * 1000) == 5) { powers.add(new Power()); } for (int i = 0; i < powers.size(); i++) { Power power = powers.get(i); power.draw(g); } // 画敌机 for (int i = 0; i < dijis.size(); i++) { Diji dj = dijis.get(i); dj.draw(g); } // 画飞机 plane.draw(g); // 判断战机相撞 for (int i = 0; i < dijis.size(); i++) { // 先得到每一个敌机 Diji dj = dijis.get(i); Rectangle r1 = dj.getRectangle(); Rectangle r2 = plane.getRectangle(); if (r1.intersects(r2)) { dijis.remove(i); plane.lifeCount = plane.lifeCount - 1; if(plane.lifeCount>0) flag=false; if(plane.lifeCount<1){ PlaneStatus.playStatus=-1; }
} // 判断战机和血瓶相撞 for (int i = 0; i < gifts.size(); i++) { } // 先得到每一个血瓶 Gift gift = gifts.get(i); Rectangle r1 = gift.getRectangle(); Rectangle r2 = plane.getRectangle(); if (r1.intersects(r2)) { gifts.remove(i); } if (plane.lifeCount <5) { } plane.lifeCount = plane.lifeCount + 1; //// 判断战机和Power相撞 for (int i = 0; i < powers.size(); i++) { // 先得到每一个power Power power = powers.get(i); Rectangle r1 = power.getRectangle(); Rectangle r2 = plane.getRectangle(); if (r1.intersects(r2)) { powers.remove(i); flag=true; } } // 判断敌机在中弹 for (int i = 0; i < mzds.size(); i++) { // 得到每个战机子弹 MyZiDan myzidan = mzds.get(i); Rectangle r1 = myzidan.getRectangle(); for (int j = 0; j < dijis.size(); j++) { // 每一个敌机 Diji diji = dijis.get(j); Rectangle r2 = diji.getRectangle(); if (r1.intersects(r2)) { mzds.remove(i); dijis.remove(j); count++; }
}
} } boolean isFire1 = false; boolean isFire2 = false; public void keyPressed(KeyEvent e) { plane.keyPressed(e); if (e.getKeyCode() == KeyEvent.VK_J) { isFire1 = true; } /*if (e.getKeyCode() == KeyEvent.VK_K) { isFire2 = true; }*/ } public void keyReleased(KeyEvent e) { plane.keyReleased(e); if (e.getKeyCode() == KeyEvent.VK_J) { isFire1 = false; fireTime = 0; } /*if (e.getKeyCode() == KeyEvent.VK_K) { isFire2 = false; fireTime = 0; }*/ if( e.getKeyCode()==KeyEvent.VK_P){ for(int i=0;i<dijis.size();i++) dijis.remove(i); } }

over类

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
package com;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
// 游戏结束
public class Over {
Image obg=null;
Image ng=null;
public Over() {
try {
obg
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/obg.jpg")); ng
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/ng.png")); } catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g){
g.drawImage(obg, 0, 0, null);
g.drawImage(ng, 120,100, null);
g.setColor(Color.black);
g.setFont(new Font("幼圆", Font.BOLD, 40));
g.drawString("游戏结束啦!",200 ,600 );
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}

MainFrame主类

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
package com;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image; = =
importjava.awt.event.Key;importjava.awt.event.Key;importjavax.swing.JFrame;publicclassMainFrameexte;Beforebefore=newBefore();Play01play01=newPlay01();Overover=newOver();;publicMain


import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
Before before =new Before();
Play01 play01 = new Play01();
Over over = new Over();
public MainFrame() {
//
//
//
//
//
//
//
设 置标题 this.setTitle(" 让子弹飞一会~"); 设置大小 this.setSize(600, 800); 定位居中 this.setLocationRelativeTo(null); 添加关闭操作 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 禁止重置大小z this.setResizable(false); 添加键盘监听器 KeyListener --> KeyAdapter this.addKeyListener(new KeyAdapter(){ @Override public void keyPressed(KeyEvent e) { if(PlaneStatus.playStatus == 0){ before.keyPressed(e); }else if(PlaneStatus.playStatus ==1){ play01.keyPressed(e); }else if(PlaneStatus.playStatus==-1){ over.keyPressed(e); } } @Override public void keyReleased(KeyEvent e) { if(PlaneStatus.playStatus == 0){ before.keyReleased(e); }else if(PlaneStatus.playStatus ==1){ play01.keyReleased(e); }else if(PlaneStatus.playStatus==-1){ over.keyReleased(e); } } });
//
//
//
启 动一个线程:每隔20 毫秒执行一次 new Thread(){ public void run(){ while(true){ MainFrame.this.repaint(); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); 显示 this.setVisible(true); } Image bufferImage =null; 这个方法也是20毫秒执行一次 public void paint(Graphics g){ if(bufferImage==null){ bufferImage = this.createImage(600, 800); } Graphics g4Image = bufferImage.getGraphics(); g4Image.setColor(Color.BLACK); g4Image.fillRect(0, 0, 600, 800); this.draw(g4Image); g.drawImage(bufferImage, 0, 0, null); } public void draw(Graphics g){ if(PlaneStatus.playStatus == 0){ before.draw(g); }else if(PlaneStatus.playStatus ==1){ play01.draw(g); }else if(PlaneStatus.playStatus==-1){ over.draw(g); } } public static void main(String[] args) {
new MainFrame();
}
}

MyZiDan

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
package com;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.IOException;
import javax.imageio.ImageIO;
public class MyZiDan {
Image zidanImage = null;
int x ;
int y ;
public MyZiDan(int x,int y) {
this.x=x;
this.y=y;
try {
zidanImage = ImageIO.read(MyZiDan.class.getClassLoader()
.getResourceAsStream("images/zidan.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
// 画我军子弹图片
g.drawImage(zidanImage, x, y, null);
// 我的飞机子弹移动速度
y-=20;
}
public Rectangle getRectangle(){
return new Rectangle(x,y,zidanImage.getWidth(null),zidanImage.getHeight(null)); }
}
package com;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Diji {
Image dijiImage = null;
int y = -20;
int x = 10;//20 ~ 760
int r;
public Diji() {
this.x=(int)(Math.random()*540)+40;
try {
r = (int)(Math.random()*3)+1;
dijiImage = ImageIO.read(Diji.class.getClassLoader()
.getResourceAsStream("images/diji_"+r+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
//
//
}
画 敌机图片 g.drawImage(dijiImage, x, y, null); 敌机移动 if(r==1){ y+=4; }else if(r==2){ y+=4; }else if(r==3){ y+=7; } } public Rectangle getRectangle(){ return new Rectangle(x,y,dijiImage.getWidth(null),dijiImage.getHeight(null)); }
package com;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Gift {
Image GiftImage = null;
int x = 20;
int y = 0;
public Gift() {
this.x=(int)(Math.random()*540)+20;
try {
GiftImage = ImageIO.read(Gift.class.getClassLoader()
.getResourceAsStream("images/blood.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
// 画血瓶图片
g.drawImage(GiftImage, x, y, null);
// 血瓶移动
y++;
}
public Rectangle getRectangle(){
return new Rectangle(x,y,GiftImage.getWidth(null),GiftImage.getHeight(null)); }
}
package com;
import java.awt.Graphics;
importjava.awt.Image;;importjava.awt.Rectangle;importjava.io.IOExceptio;importjavax.imageio.Imag;publicclassDijiZiDan{;ImagezidanImage=null;;intx;;inty;;intfangxiang=0;;publicDijiZ


import java.awt.Image;
import java.awt.Rectangle;
import java.io.IOException;
import javax.imageio.ImageIO;
public class DijiZiDan {
Image zidanImage = null;
int x ;
int y ;
int fangxiang =0;
public DijiZiDan(int x,int y) {
this.x=x;
this.y=y;
fangxiang = (int)(Math.random()*5);//0 ~ 7
try {
zidanImage = ImageIO.read(DijiZiDan.class.getClassLoader()
.getResourceAsStream("images/dijizidan.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void draw(Graphics g) {
//
//
//
//
//
//
画 敌机子弹图片 g.drawImage(zidanImage, x, y, null); 移动 this.move(); } public void move(){ if(fangxiang==0){ 下 y=y+2; }else if(fangxiang==1){ 左 x=x-2; }else if(fangxiang==2){ 右 x=x+2; }else if(fangxiang==3){ 左下 x=x-2; y=y+2; }else if(fangxiang==4){
// 右下
x=x+2;
y=y+2;
}
}
public Rectangle getRectangle(){
return new Rectangle(x,y,zidanImage.getWidth(null),zidanImage.getHeight(null)); }
}
package com;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
// 游戏开始之前
public class Before {
Image bg=null;
Image wfeiji=null;
Image kdiji1=null;
Image kdiji2=null;
Image kdiji3=null;
public Before() {
try {
bg
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/bg.jpg")); wfeiji
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/feiji_1.png")); kdiji1
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/kdiji_01.png")); kdiji2
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/kdiji_02.png")); kdiji3
ImageIO.read(Before.class.getClassLoader().getResourceAsStream("images/kdiji_03.png")); = = = = =
//
e.printStackTrace(); } } int time=0; public void draw(Graphics g){ if(PlaneStatus.roleNum==1){ } // 画妹妹 g.drawImage(bg, 0, 0, null); g.drawImage(wfeiji, 260, 600, null); g.drawImage(kdiji1, 200, 50, null); g.drawImage(kdiji1, 400, 50, null); g.drawImage(kdiji1, 150, 480, null); g.drawImage(kdiji1, 400, 480, null); g.drawImage(kdiji2, 100, 200, null); g.drawImage(kdiji2, 300, 100, null); g.drawImage(kdiji2, 480, 200, null); g.drawImage(kdiji3, 300, 300, null); g.drawImage(kdiji3, 70, 400, null); g.drawImage(kdiji3, 510, 400, null); 画回车符 time++; g.setColor(Color.YELLOW); g.setFont(new Font("幼圆", Font.BOLD, 30)); if(time<=10){ g.drawString("[Enter]>>", 50, 750); }else if(time<=20){ g.drawString("[Enter] >>", 50, 750); }else if(time<=30){ g.drawString("[Enter] >>", 50, 750); if(time==30){ time=0; } } } public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_ENTER){
} } if(keyCode ==KeyEvent.VK_LEFT ||keyCode==KeyEvent.VK_RIGHT ){ PlaneStatus.roleNum = 3-PlaneStatus.roleNum;//1~2 互换 } } public void keyReleased(KeyEvent e) { }

本文标题:飞机大战源码

文章作者:範範

发布时间:2019年06月04日 - 14:06

最后更新:2019年08月14日 - 11:08

原始链接:https://trhsy.github.io/aircraft_war.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------The End-------------
範範 wechat
欢迎关注微信公众号,孬蛋等着你!
坚持原创技术分享,您的支持将鼓励我继续创作!