写在前面
技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习。java确实不适合写桌面应用,这里只是通过这个游戏让大家理解oop面向对象编程的过程,纯属娱乐。代码写的很简单,也很容易理解,并且注释写的很清楚了,还有问题,自己私下去补课学习。
效果如下
完整代码
敌飞机
?
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
|
import java.util.random;
敌飞机: 是飞行物,也是敌人
public class airplane extends flyingobject implements enemy {
private int speed = 3 ; //移动步骤
/** 初始化数据 */
public airplane(){
this .image = shootgame.airplane;
width = image.getwidth();
height = image.getheight();
y = -height;
random rand = new random();
x = rand.nextint(shootgame.width - width);
}
/** 获取分数 */
@override
public int getscore() {
return 5 ;
}
/** //越界处理 */
@override
public boolean outofbounds() {
return y>shootgame.height;
}
/** 移动 */
@override
public void step() {
y += speed;
}
}
|
分数奖励
?
1
2
3
4
5
6
7
8
9
|
/**
* 奖励
*/
public interface award {
int double_fire = 0 ; //双倍火力
int life = 1 ; //1条命
/** 获得奖励类型(上面的0或1) */
int gettype();
}
|
蜜蜂
?
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
|
import java.util.random;
/** 蜜蜂 */
public class bee extends flyingobject implements award{
private int xspeed = 1 ; //x坐标移动速度
private int yspeed = 2 ; //y坐标移动速度
private int awardtype; //奖励类型
/** 初始化数据 */
public bee(){
this .image = shootgame.bee;
width = image.getwidth();
height = image.getheight();
y = -height;
random rand = new random();
x = rand.nextint(shootgame.width - width);
awardtype = rand.nextint( 2 ); //初始化时给奖励
}
/** 获得奖励类型 */
public int gettype(){
return awardtype;
}
/** 越界处理 */
@override
public boolean outofbounds() {
return y>shootgame.height;
}
/** 移动,可斜着飞 */
@override
public void step() {
x += xspeed;
y += yspeed;
if (x > shootgame.width-width){
xspeed = - 1 ;
}
if (x < 0 ){
xspeed = 1 ;
}
}
}
|
子弹类:是飞行物体
?
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
|
/**
* 子弹类:是飞行物
*/
public class bullet extends flyingobject {
private int speed = 3 ; //移动的速度
/** 初始化数据 */
public bullet( int x, int y){
this .x = x;
this .y = y;
this .image = shootgame.bullet;
}
/** 移动 */
@override
public void step(){
y-=speed;
}
/** 越界处理 */
@override
public boolean outofbounds() {
return y<-height;
}
}
|
敌人的分数
?
1
2
3
4
5
6
7
|
/**
* 敌人,可以有分数
*/
public interface enemy {
/** 敌人的分数 */
int getscore();
}
|
飞行物(敌机,蜜蜂,子弹,英雄机)
?
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
|
import java.awt.image.bufferedimage;
/**
* 飞行物(敌机,蜜蜂,子弹,英雄机)
*/
public abstract class flyingobject {
protected int x; //x坐标
protected int y; //y坐标
protected int width; //宽
protected int height; //高
protected bufferedimage image; //图片
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 bufferedimage getimage() {
return image;
}
public void setimage(bufferedimage image) {
this .image = image;
}
/**
* 检查是否出界
* @return true 出界与否
*/
public abstract boolean outofbounds();
/**
* 飞行物移动一步
*/
public abstract void step();
/**
* 检查当前飞行物体是否被子弹(x,y)击(shoot)中
* @param bullet 子弹对象
* @return true表示被击中了
*/
public boolean shootby(bullet bullet){
int x = bullet.x; //子弹横坐标
int y = bullet.y; //子弹纵坐标
return this .x<x && x< this .x+width && this .y<y && y< this .y+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
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
|
import java.awt.image.bufferedimage;
/**
* 英雄机:是飞行物 java学习交流qq群:589809992 我们一起学java!
*/
public class hero extends flyingobject{
private bufferedimage[] images = {}; //英雄机图片
private int index = 0 ; //英雄机图片切换索引
private int doublefire; //双倍火力
private int life; //命
/** 初始化数据 */
public hero(){
life = 3 ; //初始3条命
doublefire = 0 ; //初始火力为0
images = new bufferedimage[]{shootgame.hero0, shootgame.hero1}; //英雄机图片数组
image = shootgame.hero0; //初始为hero0图片
width = image.getwidth();
height = image.getheight();
x = 150 ;
y = 400 ;
}
/** 获取双倍火力 */
public int isdoublefire() {
return doublefire;
}
/** 设置双倍火力 */
public void setdoublefire( int doublefire) {
this .doublefire = doublefire;
}
/** 增加火力 */
public void adddoublefire(){
doublefire = 40 ;
}
/** 增命 */
public void addlife(){ //增命
life++;
}
/** 减命 */
public void subtractlife(){ //减命
life--;
}
/** 获取命 */
public int getlife(){
return life;
}
/** 当前物体移动了一下,相对距离,x,y鼠标位置 */
public void moveto( int x, int y){
this .x = x - width/ 2 ;
this .y = y - height/ 2 ;
}
/** 越界处理 */
@override
public boolean outofbounds() {
return false ;
}
/** 发射子弹 */
public bullet[] shoot(){
int xstep = width/ 4 ; //4半
int ystep = 20 ; //步
if (doublefire> 0 ){ //双倍火力
bullet[] bullets = new bullet[ 2 ];
bullets[ 0 ] = new bullet(x+xstep,y-ystep); //y-ystep(子弹距飞机的位置)
bullets[ 1 ] = new bullet(x+ 3 *xstep,y-ystep);
return bullets;
} else { //单倍火力
bullet[] bullets = new bullet[ 1 ];
bullets[ 0 ] = new bullet(x+ 2 *xstep,y-ystep);
return bullets;
}
}
/** 移动 */
@override
public void step() {
if (images.length> 0 ){
image = images[index++/ 10 %images.length]; //切换图片hero0,hero1
}
}
/** 碰撞算法 */
public boolean hit(flyingobject other){
int x1 = other.x - this .width/ 2 ; //x坐标最小距离
int x2 = other.x + this .width/ 2 + other.width; //x坐标最大距离
int y1 = other.y - this .height/ 2 ; //y坐标最小距离
int y2 = other.y + this .height/ 2 + other.height; //y坐标最大距离
int herox = this .x + this .width/ 2 ; //英雄机x坐标中心点距离
int heroy = this .y + this .height/ 2 ; //英雄机y坐标中心点距离
return herox>x1 && herox<x2 && heroy>y1 && heroy<y2; //区间范围内为撞上了
}
}
|
游戏启动主类
?
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
|