-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
210 lines (199 loc) · 10.6 KB
/
game.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
window.onload=function(){
var container = document.getElementById("container");
var startGame = document.getElementById("startGame");
startGame.onclick = function(){
this.style.display = "none";
beeGame.init("container");
}
var beeGame;
beeGame = {
enemy: {e1: {name: "e1", style: "e1", blood: 1, speed: 2,score:1},
e2: {name: "e2", style: "e2", blood: 2, speed: 3,score:2},
e3: {name: "e3", style: "e3", blood: 3, speed: 5,score:3}},
levelInfo: [
{times:3000,speed: 1, count: 10, enemy: eval("[" + new Array(11).join("'e2',") + new Array(10).join("'e1',") + "'e1'" + "]")},
{times:2000,speed: 2, count: 10, enemy: eval("[" + new Array(21).join("'e2',") + new Array(40).join("'e1',") + "'e1'" + "]")},
{times:1500,speed: 3, count: 10, enemy: eval("[" + new Array(31).join("'e2',") + new Array(30).join("'e1',") + "'e1'" + "]")}
],
level: 0,
lis: [],
arr:[],
score:0,
init: function (parentId) {
this.parentDocument = document.getElementById(parentId);
this.createEnemy();
this.createPlane();
},
createEnemy: function () {
var ul = document.createElement("ul");
ul.id = "bee";
for (var i = 0; i < this.levelInfo[this.level].enemy.length; i++) {
var li = document.createElement("li");
li.className = this.levelInfo[this.level].enemy[i];
li.blood = this.enemy[this.levelInfo[this.level].enemy[i]].blood;
li.score = this.enemy[this.levelInfo[this.level].enemy[i]].score;
li.speed = this.enemy[this.levelInfo[this.level].enemy[i]].speed;
ul.appendChild(li);
}
this.parentDocument.appendChild(ul);
ul.style.width = (li.offsetWidth + 2) * this.levelInfo[this.level].count + 'px';
ul.style.left = (this.parentDocument.offsetWidth - ul.offsetWidth) / 2 + 'px';
this.lis=ul.getElementsByTagName("li");
for(var i = 0 ; i < this.lis.length;i++){
this.arr.push([this.lis[i].offsetLeft,this.lis[i].offsetTop]);
}
for(var i = 0 ; i < this.lis.length;i++){
this.lis[i].style.position = 'absolute';
this.lis[i].style.left = this.arr[i][0] + 'px';
this.lis[i].style.top = this.arr[i][1] + 'px';
}
this.moveBee(ul);
var This = this;
this.ul = ul;
this.ul.timer2 = setInterval(function(){
This.oneMove();
},this.levelInfo[this.level].times);
},
moveBee: function (ul) {
var This = this;
ul.timer = setInterval(function () {
ul.style.left = This.levelInfo[This.level].speed + ul.offsetLeft + 'px';
if (ul.offsetLeft + ul.offsetWidth > This.parentDocument.offsetWidth || ul.offsetLeft <= 0) {
ul.style.top = ul.offsetTop + This.lis[0].offsetHeight + 'px';
This.levelInfo[This.level].speed = 0 - This.levelInfo[This.level].speed;
}
}, 30);
},
oneMove:function(){
var index = Math.floor(Math.random()*this.lis.length)
var nowBee = this.lis[index];
var This = this;
this.lis[index].timer = setInterval(function(){
var a = (This.plane.offsetLeft + This.plane.offsetWidth/2) - (nowBee.offsetLeft + nowBee.offsetWidth/2 + nowBee.parentNode.offsetLeft);
var b = (This.plane.offsetTop + This.plane.offsetHeight/2) - (nowBee.offsetTop + nowBee.offsetHeight/2 + nowBee.parentNode.offsetTop);
var c = Math.sqrt(a*a + b*b);
var speedX = nowBee.speed * a/c;
var speedY = nowBee.speed * b/c;
nowBee.style.left = nowBee.offsetLeft + speedX + 'px';
nowBee.style.top = nowBee.offsetTop + speedY + 'px';
if((nowBee.offsetTop + nowBee.offsetHeight + nowBee.parentNode.offsetTop)>=(This.parentDocument.offsetHeight - This.plane.offsetHeight)){
This.gameOver();
}
},30);
},
createPlane: function () {
var This = this;
this.plane = document.createElement("div");
this.plane.id = 'plane';
this.parentDocument.appendChild(this.plane);
this.plane.style.left = (this.parentDocument.offsetWidth - this.plane.offsetWidth) / 2 + 'px';
this.plane.style.top = this.parentDocument.offsetHeight - this.plane.offsetHeight + 'px';
this.bindPlane();
},
bindPlane:function(){
var This = this;
document.onkeydown = function (e) {
if (!This.plane.timer) {
if (e.keyCode == '37') {
This.plane.timer = setInterval(function () {
if (This.plane.offsetLeft >= 10) {
This.plane.style.left = This.plane.offsetLeft - 10 + 'px';
}
}, 30);
} else if (e.keyCode == '39') {
This.plane.timer = setInterval(function () {
if (This.plane.offsetLeft + This.plane.offsetWidth <= This.parentDocument.offsetWidth - 10) {
This.plane.style.left = This.plane.offsetLeft + 10 + 'px';
}
}, 30);
}
}
}
document.onkeyup = function (e) {
clearInterval(This.plane.timer);
This.plane.timer = null;
if (e.keyCode == '32') {
This.createBuulet();
}
}
},
createBuulet: function () {
var This = this;
var bullet = document.createElement("div");
bullet.className = "bullet";
this.parentDocument.appendChild(bullet);
bullet.style.top = this.plane.offsetTop - bullet.offsetHeight + 'px';
bullet.style.left = this.plane.offsetLeft + this.plane.offsetWidth / 2 + 'px';
bullet.name="bullet";
This.runBullet(bullet);
},
runBullet:function(bullet){
var This = this;
bullet.timer = setInterval(function () {
bullet.style.top = bullet.offsetTop - 10 + 'px';
if (bullet.offsetTop <= 0) {
clearInterval(bullet.timer);
This.parentDocument.removeChild(bullet);
} else {
for (var i = 0; i < This.lis.length; i++) {
var bee = This.lis[i];
if (This.checkCrash(bullet, bee)) {
bee.blood--;
if(bee.blood ==0){
clearInterval(bee.timer);
This.ul.removeChild(bee);
This.score += bee.score;
document.getElementsByTagName("span")[0].innerHTML = This.score;
}
clearInterval(bullet.timer);
This.parentDocument.removeChild(bullet);
if(This.lis.length == 0){
This.nextLevel();
}
}
}
}
}, 30);
},
checkCrash: function (bullet, bee) {
var l1 = bullet.offsetLeft;
var r1 = bullet.offsetLeft + bullet.offsetWidth;
var t1 = bullet.offsetTop;
var b1 = bullet.offsetTop + bullet.offsetHeight;
var l2 = bee.offsetLeft + bee.parentNode.offsetLeft;
var r2 = bee.offsetLeft + bee.offsetWidth + bee.parentNode.offsetLeft;
var t2 = bee.offsetTop + bee.parentNode.offsetTop;
var b2 = bee.offsetTop + bee.offsetHeight + bee.parentNode.offsetTop;
if (r1 < l2 || l1>r2 || t1 > b2 || b1 < t2) {
return false;
} else {
return true;
}
},
gameOver:function(){
clearInterval(this.ul.timer2);
clearInterval(this.ul.timer);
for(var i = 0 ; i < this.lis.length ;i ++){
var li = this.lis[i];
clearInterval(li.timer);
}
var bullets = document.getElementsByClassName("bullet");
for(var i = 0 ;i< bullets.length ; i ++){
clearInterval((bullets[i].timer));
}
document.getElementById("gameOver").style.display="block";
document.onkeydown = function(){
};
document.onkeyup = function(){
}
// window.location.reload();
},
nextLevel:function(){
this.level ++;
this.createEnemy();
}
};
}
function restart(){
window.location.reload();
}