-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.html
executable file
·440 lines (414 loc) · 10.9 KB
/
plane.html
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>打飞机</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
html ,body{
width: 100%;
height: 100%;
overflow: hidden;
}
#loading{
font-size: 30px;
text-align: center;
padding-top: 250px;
display: none;
}
#loading p{
margin-bottom: 20px;
}
#score{
position: absolute;
left: 0;
font-size: 30px;
}
#lose{
position: absolute;
left: 15%;
top: 20%;
width: 70%;
height: 150px;
border: 1px solid gray;
text-align: center;
font-size: 25px;
border-radius: 5%;
background-color: beige;
display: none;
}
#lose dt{
margin: 10px auto;
/*border: 1px solid darkorange;*/
}
#lose dd{
margin-bottom: 10px;
border-bottom: 2px solid greenyellow;
height: 35px;
}
</style>
</head>
<body>
<div id="loading">
<p>游戏加载中 ... ...</p>
<img src="img/loading.gif"/>
</div>
<div id="audioList">
<audio id="bulletsound" src="audio/bullet.mp3"></audio>
<audio id="enemy1sound" src="audio/enemy1_down.mp3"></audio>
<audio id="enemy2sound" src="audio/enemy2_down.mp3"></audio>
<audio id="enemy3sound" src="audio/enemy3_down.mp3"></audio>
<audio id="enemy2_out" src="audio/enemy2_out.mp3"></audio>
<audio id="game_music" src="audio/game_music.mp3"></audio>
<audio id="game_over" src="audio/game_over.mp3"></audio>
</div>
<div id="score">
分数:<span id="scores">
0
</span>
</div>
<canvas id="myCanvas" width="" height=""></canvas>
<!--游戏结束界面-->
<dl id="lose">
<dt>游戏结束</dt>
<dd>您的分数为:<span id="scoreend">0</span></dd>
<dd><a href="###" id="again"></a></dd>
</dl>
</body>
<script type="text/javascript">
var loading = document.getElementById("loading");
var bulletsound = document.getElementById("bulletsound");
var enemy1sound = document.getElementById("enemy1sound");
var enemy2sound = document.getElementById("enemy2sound");
var enemy3sound = document.getElementById("enemy3sound");
var gameover = document.getElementById("game_over");
var gamemusic = document.getElementById("game_music");
var scores = document.getElementById("scores");
var scoreend = document.getElementById("scoreend");
var again = document.getElementById("again");
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
//set canvas's width and height
canvas.width = document.documentElement.clientWidth;
canvas.height = document.documentElement.clientHeight;
//store picture
var pic_load = [
'img/background.png',
'img/bullet1.png',
'img/bullet2.png',
'img/enemy1.png',
'img/enemy2.png',
'img/enemy3.png',
'img/herofly.png',
'img/prop.png'
];
var n=0;
for (var i=0;i<pic_load.length;i++) {
var loadingImage = new Image();
loadingImage.src = pic_load[i];
loadingImage.onload = function () {
n++;
if (n/pic_load.length>=0.9) {
gamemusic.play();
loading.style.display = "none";
}
}
}
//backgound-img roll
var bgimg = new Image();
bgimg.src = "img/background.png";
var backImg = {
x:0,
y:0,
w:canvas.width,
h:canvas.height,
draw:function(){
//count the max column
var maxX = Math.ceil(canvas.width/320);
var maxY = Math.ceil(canvas.height/560);
for (var i=0;i<maxX;i++) {
for (var j=-maxY;j<maxY;j++) {
context.drawImage(bgimg,i*320,j*568+this.y,320,568);
}
}
},
move:function () {
if (this.y>=this.h) {
this.y = 0;
}
this.y++;
// context.drawImage(bgimg,this.x,this.y,this.w,this.h);
// context.drawImage(bgimg,this.x,this.y-this.h,this.w,this.h);
}
}
//kuangjia
function animate () {
context.clearRect(0,0,canvas.width,canvas.height);
main();
// backImg.draw();
window.requestAnimationFrame(animate);
}
var heroImage = new Image();
heroImage.src = "img/herofly.png";
var hero = {
x:canvas.width/2 - 33,
y:canvas.height-82-30,
w:66,
h:82,
rx:0,//hero die scroll picture
draw:function () {
context.drawImage(heroImage,this.rx,0,this.w,this.h,this.x,this.y,this.w,this.h);
}
}
//windwo hero move
canvas.onmousedown = function (e) {
var e = e||event;
var dx = e.clientX;
var dy = e.clientY;
if (dx>hero.x&&dx<hero.x+hero.w&&dy>hero.y&&dy<hero.y+hero.h) {
canvas.onmousemove = function () {
var e = e||window.event;
hero.x = e.clientX-33;
hero.y = e.clientY-41;
}
}
}
canvas.onmouseup = function () {
canvas.onmousemove = null;
}
//pc hero move .because has many finger so uses touches[0]
canvas.addEventListener("touchstart",function (e) {
var e = e||window.event;
var x = e.touches[0].clientX;
var y = e.touches[0].clientY;
//judge touched plane
if (x>hero.x&&x<hero.x+hero.w&&y>hero.y&&y<hero.y+hero.h) {
canvas.addEventListener("touchmove",function (e) {
var e = e||window.event;
var newx = e.touches[0].clientX-33;
var newy = e.touches[0].clientY-41;
hero.x = newx;
hero.y = newy;
},false);
}
},false)
//creat bullet
var bullet1 = new Image();
bullet1.src = "img/bullet1.png";
var bullet2 = new Image();
bullet2.src ="img/bullet2.png";
function Bullet (x,y,w,h,imgs,hurt) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.imgs = imgs;
this.hurt = hurt;
}
//bullet-move and creat array to store bullet
var frameNum = 0;
var bullets = [];
var bullet = null;
Bullet.prototype.move = function () {
frameNum ++;
if (frameNum % 1 == 0) {
bullet = new Bullet(this.x,this.y,this.w,this.h,this.imgs,this.hurt);
bullets.push(bullet);
frameNum = 0;
}
for (var i=0;i<bullets.length;i++) {
bullets[i].y -=5;
context.drawImage(bullets[i].imgs,bullets[i].x,bullets[i].y);
if (bullets[i].y<0) {
bullets.splice(i,1);
i--;
}
}
}
var bulletbol = false;
var btbol = false;
var bombol = false;
var propbol = false;//judge have prop
var prop;
var proImg = new Image();
proImg.src = "img/prop.png";
function Prop (x,y,w,h,rx,speed) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.rx = rx*this.w;
this.speed = speed;
}
Prop.prototype.move = function () {
this.y += this.speed;
context.drawImage(proImg,this.rx,0,this.w,this.h,this.x,this.y,this.w,this.h);
}
//creat different enemy plane
var enemyArr = [];
//creat enemy plane
var enemy1 = new Image();
enemy1.src = "img/enemy1.png";
var enemy2 = new Image();
enemy2.src = "img/enemy3.png";
var enemy3 = new Image();
enemy3.src = "img/enemy2.png";
function Enemies (x,y,w,h,imgs,hp,speed,frames,score) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.imgs = imgs;
this.hp = hp;
this.speed = speed;
this.frames = frames;
this.score = score;
this.rx = 0;//level x
this.die = false;
this.boom = false;
}
Enemies.prototype.move = function () {
if (!this.boom) {//if not die go on move
this.y += this.speed;
}else{
this.frames --;
this.rx += this.w;
if (this.frames <=1) {
//delete
this.die = true;
}
}
context.drawImage(this.imgs,this.rx,0,this.w,this.h,this.x,this.y,this.w,this.h);
}
//main function
function main () {
backImg.draw();
backImg.move();
hero.draw();
var propSpeed = randomNum(2,5);
var enemiesPro = randomNum(1,600);
var enemy1x = randomNum(0,canvas.width - 38);
var enemy2x = randomNum(0,canvas.width - 46);
var enemy3x = randomNum(0,canvas.width - 110)
if (enemiesPro<100) {
if (enemiesPro<50) {
enemyArr.push(new Enemies(enemy1x,-34,40,34,enemy1,1,propSpeed,5,100));
}else if (enemiesPro<80) {
enemyArr.push(new Enemies(enemy2x,-64,46,64,enemy2,5,propSpeed,6,500));
}else{
enemyArr.push(new Enemies(enemy3x,-164,110,164,enemy3,10,propSpeed,10,1000));
}
}
//begin move plane
for (var i=0;i<enemyArr.length;i++) {
console.log(enemyArr[i]);
if (enemyArr[i].y>canvas.height||enemyArr[i].die) {
enemyArr.splice(i,1);
i--;
}
// enemyArr[i].move();
else{
enemyArr[i].move();
}
}
for (var i=0;i<enemyArr.length;i++) {
for (var j=0;j<bullets.length;j++) {
if (crash(enemyArr[i],bullets[j])) {
enemyArr[i].hp -= bullets[j].hurt;
bullets.splice(j,1);
j--;
if (enemyArr[i].hp <=0) {
//to know whether plane dieed
enemyArr[i].boom = true;
}
}
}
}
//creat bullet begin
if (bulletbol) {//double
bullet = new Bullet(hero.x+33-24,hero.y-14,48,14,bullet2,4);
} else if(btbol){//single
bullet = new Bullet(hero.x+33-3,hero.y-14,6,14,bullet1,1);
// hz=5;
} else if(bombol){//bomb
for (var i=0;i<enemyArr.length;i++) {
enemyArr[i].boom = true;
}
}else{//normal
console.log("1");
bullet = new Bullet(hero.x+33-3,hero.y-14,6,14,bullet1,1);
}
bullet.move();
//creat porp
var propNum = randomNum(1,300);
var propX = randomNum(0,canvas.width-39);
var propRx =null;// = randomNum(0,2);//which prop is
var propSpeed = randomNum(2,5);//speed is
var propS = randomNum(1,100);//different prop
propRx = (function () {
var Rx;
if (propS<5) {
Rx=0;
}else if (propS>10&&propS<20) {
Rx = 1;
}else if (propS>20&&propS<40) {
Rx = 2;
}
return Rx;
})();
if (propNum<100&&propbol == false&&propRx!=null) {
prop = new Prop(propX,-68,39,68,propRx,propSpeed);
propbol = true;
}
if (propbol) {
prop.move();
if (prop.y>canvas.height) {
propbol= false;
}
if (crash(hero,prop)) {
propbol = false;
if (propRx ==0) {
// bombol = true;
// bulletbol = false;
// btbol = false;
}else if (propRx == 1) {//double
bombol = false;
bulletbol = true;
btbol = false;
}else if (propRx == 2) {
bombol = false;
bulletbol = false;
btbol = true;
}
}
}
}
//Create collision function
function crash (obj1,obj2) {
var l1 = obj1.x;
var r1 = l1+obj1.w;
var t1 = obj1.y;
var b1 = t1+obj1.h;
var l2 = obj2.x;
var r2 = l2+obj2.w;
var t2 = obj2.y;
var b2 = t2+obj2.h;
if (l1<r2&&l2<r1&&t1<b2&&b1>t2) {
// if(l1<r2 && r1>l2 && t1<b2 && b1>t2){
return true;
} else{
return false;
}
}
function randomNum (m,n) {
return parseInt(Math.random()*(n-m+1)+m);
}
window.onload = function () {
animate();
}
</script>
</html>