技术源于分享,所以今天抽空把自己之前用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
}
}
|
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;
/**
* 英雄机:是飞行物
*/
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
|