-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitPlane.js
300 lines (208 loc) · 7.99 KB
/
hitPlane.js
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 获取开始界面和主界面
var mainDiv = document.getElementById('mainDiv');
var startDiv =document.getElementById('startDiv');
// 获取显示分数界面
var scoreDiv = document.getElementById('scorediv');
var scoreLabel = document.getElementById('label');
var enddiv = document.getElementById('enddiv');
var planscore = document.getElementById('planscore');
var tid; //定时器
var backgroundPositionY = 0;//背景移动的距离
var time1 = 0;
var time2 = 0;
var enemys = []; //敌方飞机的数组
var bullets = [];//子弹数组
var scores = 0;//初始化的分数
function jixu(){
location.reload(true);
}
function begin(){
startDiv.style.display = "none"; //隐藏开始界面
mainDiv.style.display = "block"; //显示主界面
// 定时器的创建
tid = setInterval(start,30);
}
// 定时器调用的函数
function start(){
// 1 背景移动
mainDiv.style.backgroundPositionY = backgroundPositionY+'px';
backgroundPositionY+=2;
// 2 创建敌方飞机
time1++;
if (time1==20) { //0.6s
// 创建飞机
time2++;
if (time2%5 == 0) { //3s
// 创建中型飞机
var midEnemy = new Enemy(25,286,'image/enemy3_fly_1.png',3,"image/中飞机爆炸.gif",46,60,360,6,5000);
enemys.push(midEnemy);
if (time2 == 20) { //12s
// 创建大型飞机
enemys.push(new Enemy(57,216,'image/enemy2_fly_1.png',2,"image/大飞机爆炸.gif",110,114,540,12,30000));
time2 ==0;
}
}else{
// 创建小型小飞机
enemys.push(new Enemy(19,286,'image/enemy1_fly_1.png',4,"image/小飞机爆炸.gif",34,24,360,1,1000));
}
time1 =0;
}
// 移动敌方飞机
for (var i = 0; i < enemys.length; i++) {
var enemy = enemys[i];
enemy.planeMove();
//飞机超出界面
if (enemys[i].imageNode.offsetTop>568) {
// 在主界面删除对应节点
mainDiv.removeChild(enemys[i].imageNode);
enemys.splice(i,1);//从飞机数组中移除对应元素
}
// 飞机判定死亡
if (enemys[i].isdie == true) {
// 需要经过一段时间后,再清除敌机
enemys[i].planedietimes+=20; //
if (enemys[i].planedietimes>=enemys[i].planedietime) {
mainDiv.removeChild(enemys[i].imageNode);
enemys.splice(i,1);//从飞机数组中移除对应元素
}
}
}
// 3 创建子弹
if (time1%5 ==0) { //.15s
// 创建子弹
// 30 :我方飞机宽度的一半
var bullet = new Bullet(ourPlane.imageNode.offsetLeft+30,
ourPlane.imageNode.offsetTop-10,"image/bullet1.png");
bullets.push(bullet);
}
// 移动子弹
for (var i = 0; i < bullets.length; i++) {
bullets[i].bulletMove();
}
// 4 碰撞判断
for (var k = 0;k<bullets.length;k++){
// k = 0, b1 b2 b3
for (var j = 0;j<enemys.length;j++){
// 子弹和飞机的碰撞判断
if((bullets[k].bulletImageNode.offsetLeft+6>enemys[j].imageNode.offsetLeft)&&(bullets[k].bulletImageNode.offsetLeft<enemys[j].imageNode.offsetLeft+enemys[j].planeWidth)){
if(bullets[k].bulletImageNode.offsetTop<=enemys[j].imageNode.offsetTop+enemys[j].planeHeight&&bullets[k].bulletImageNode.offsetTop+14>=enemys[j].imageNode.offsetTop){
//敌方飞机的血量减去子弹的攻击力
enemys[j].planehp = enemys[j].planehp-1;
//当敌机的血量为0时,敌机才爆炸
if (enemys[j].planehp == 0) {
// 0 敌方飞机的图片改成爆炸的图片
enemys[j].imageNode.src = enemys[j].boomSrc;
//1 删除敌方飞机
enemys[j].isdie = true;
// 计分
scores = scores+enemys[j].planescore;
scoreLabel.innerHTML = scores;
}
//2 删除子弹
mainDiv.removeChild(bullets[k].bulletImageNode);
bullets.splice(k,1);
}
}
// 判断本方飞机的碰撞(敌方飞机和我方飞机的)
if (enemys[j].isdie == false) {
if (enemys[j].imageNode.offsetLeft+enemys[j].planeWidth>=ourPlane.imageNode.offsetLeft&&
enemys[j].imageNode.offsetLeft<=ourPlane.imageNode.offsetLeft+ourPlane.planeWidth) {
if (enemys[j].imageNode.offsetTop+enemys[j].planeHeight>=ourPlane.imageNode.offsetTop+40&&
enemys[j].imageNode.offsetTop<=ourPlane.imageNode.offsetTop-20+ourPlane.planeHeight) {
// 碰撞本飞机
ourPlane.imageNode.src = "image/本方飞机爆炸.gif";
clearInterval(tid); //停止定时器
mainDiv.removeEventListener("mousemove",move,true);
// 结束界面,并且统计分数
enddiv.style.display = "block";
planescore.innerHTML = scores;
}
}
}
}
}
}
// ------------------构造函数----------------
// (1) 飞机的构造函数
function Plane(x,y,imageSrc,speed,boomSrc,width,height,dietime,hp,score){ //speed:下落的速度
// hp:飞机的血量 score:打爆飞机的得分
this.x = x;
this.y = y;
this.imageSrc = imageSrc;
this.speed = speed;
this.boomSrc = boomSrc;
this.planeWidth = width;
this.planeHeight = height;
this.isdie = false;
this.planedietime = dietime;//该飞机的死亡持续时间
this.planedietimes = 0;//累计时间
this.planehp = hp;
this.planescore = score;
this.imageNode = null;
this.init = function(){ //创建节点
this.imageNode = document.createElement('img');
this.imageNode.style.position = "absolute";
this.imageNode.style.top = this.y+'px';
this.imageNode.style.left =this.x+'px';
this.imageNode.src =imageSrc;
mainDiv.appendChild(this.imageNode); //将节点添加到mainDiv
}
this.init();
// 飞机移动的方法
this.planeMove = function(){
// this.imageNode.style.top--->XXX px
// this.imageNode.offsetTop ===>XXX
this.imageNode.style.top = this.imageNode.offsetTop+this.speed+'px';
}
}
//(2) 构造自己的飞机 函数
function OurPlane(x,y){
// f.call(o,a,b); 对象o调用函数f,函数f的实参是a,b
Plane.call(this,x,y,"image/我的飞机.gif",0,"image/本方飞机爆炸.gif",66,80,660,1,0);
}
// (3)构造敌方飞机的函数
function Enemy(a,b,imageSrc,speed,boomSrc,width,height,dietime,hp,score){ //a b:x轴方向的取值范围
Plane.call(this,random(a,b),100,imageSrc,speed,boomSrc,width,height,dietime,hp,score);
}
// 返回min~max之间的一个随机数
function random(min,max){
return Math.floor((min+Math.random()*(max-min)));
}
// 返回一个100~300的随机数
// 100+随机数*(300-100);
// 子弹的构造函数
function Bullet(x,y,imageSrc){ //speed:下落的速度
// bulletImageNode
this.x = x;
this.y = y;
this.bulletImageSrc = imageSrc;
this.bulletImageNode = null;
this.init = function(){ //创建节点
this.bulletImageNode = document.createElement('img');
this.bulletImageNode.style.position = "absolute";
this.bulletImageNode.style.top = this.y+'px';
this.bulletImageNode.style.left =this.x+'px';
this.bulletImageNode.src =imageSrc;
mainDiv.appendChild(this.bulletImageNode); //将节点添加到mainDiv
}
this.init();
// 子弹移动的方法
this.bulletMove = function(){
// this.imageNode.style.top--->XXX px
// this.imageNode.offsetTop ===>XXX
this.bulletImageNode.style.top = this.bulletImageNode.offsetTop-20+'px';
}
}
var move = function(){
//飞机随着鼠标的坐标而移动
var oevent =window.event||arguments[0];//兼容(IE)
var selfPlanX =oevent.clientX-300;
var selfPlanY =oevent.clientY;
ourPlane.imageNode.style.left = selfPlanX - 33+'px';
ourPlane.imageNode.style.top= selfPlanY -40 +'px';
}
// 给主界面添加鼠标事件
mainDiv.addEventListener("mousemove",move,true);
// ------------------对象----------------
// 创建自己的飞机对象
var ourPlane =new OurPlane(120,485);