-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
349 lines (301 loc) · 7.79 KB
/
main.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
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
var canvas = document.querySelector("canvas");
var ctx = canvas.getContext("2d");
var width = (canvas.width = window.innerWidth);
var height = (canvas.height = window.innerHeight);
var MOVE_MIN_WIDTH = width * 0.1;
var MOVE_MIN_HEIGHT = height * 0.1;
var MOVE_MAX_WIDTH = width * 0.9;
var MOVE_MAX_HEIGHT = height * 0.9;
var ANIMATION = null;
// function to generate random number
function random(min, max) {
var num = Math.floor(Math.random() * (max - min)) + min;
return num;
}
//系统变量
function GameParams() {
this.time = 10;
this.chapter = 1;
this.ballCount = 10;
this.start = false;
}
function Shape(x, y, velX, velY, exists) {
this.x = x;
this.y = y;
this.velX = velX;
this.velY = velY;
this.exists = exists;
}
//定义控制器
function EvilCircle(x, y, exists) {
Shape.call(this, x, y, exists);
this.color = "white";
this.size = 10;
this.velX = 10;
this.velY = 10;
this.hp = 50; // 生命值
}
EvilCircle.prototype.draw = function () {
var img = document.querySelector("#avatar");
// ctx.beginPath();
ctx.strokeStyle = this.color;
ctx.drawImage(img, this.x - 10, this.y - 10, 20, 20);
// ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
// ctx.stroke();
};
EvilCircle.prototype.checkBounds = function () {
if (this.x + this.size >= MOVE_MAX_WIDTH) {
this.x -= this.size;
}
if (this.x - this.size <= MOVE_MIN_WIDTH) {
this.x += this.size;
}
if (this.y + this.size >= MOVE_MAX_HEIGHT) {
this.y -= this.size;
}
if (this.y - this.size <= MOVE_MIN_HEIGHT) {
this.y += this.size;
}
};
EvilCircle.prototype.setControls = function () {
var _this = this;
var _keyPressed = {};
window.onkeyup = function (e) {
_keyPressed[e.keyCode] = false;
if (e.keyCode === 16) {
_this.velX = 10;
_this.velY = 10;
}
};
window.onkeydown = function (e) {
_keyPressed[e.keyCode] = true;
if (e.keyCode === 16) {
_this.velX = 15;
_this.velY = 15;
}
};
setInterval(function () {
for (var key in _keyPressed) {
if (_keyPressed[key]) {
switch (parseInt(key)) {
case 65:
_this.x -= _this.velX;
break;
case 68:
_this.x += _this.velX;
break;
case 87:
_this.y -= _this.velY;
break;
case 83:
_this.y += _this.velY;
break;
default:
break;
}
}
}
}, 50);
};
//碰撞判断
EvilCircle.prototype.collisionDetect = function () {
for (var j = 0; j < balls.length; j++) {
var dx = this.x - balls[j].x;
var dy = this.y - balls[j].y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.size + balls[j].size) {
this.hp--; //扣分
this.color =
"rgb(" +
random(0, 255) +
"," +
random(0, 255) +
"," +
random(0, 255) +
")";
showMainHP(this.hp);
}
}
};
// define Ball constructor
function Ball(x, y, velX, velY, exists, color, size) {
Shape.call(this, x, y, velX, velY, exists); //继承调用
// this.x = x;
// this.y = y;
// this.velX = velX;
// this.velY = velY;
this.color = color;
this.size = size;
}
// define ball draw method
Ball.prototype.draw = function () {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.fill();
};
// define ball update method
Ball.prototype.update = function () {
if (this.x + this.size >= width) {
this.velX = -this.velX;
}
if (this.x - this.size <= 0) {
this.velX = -this.velX;
}
if (this.y + this.size >= height) {
this.velY = -this.velY;
}
if (this.y - this.size <= 0) {
this.velY = -this.velY;
}
this.x += this.velX;
this.y += this.velY;
};
// define ball collision detection
Ball.prototype.collisionDetect = function () {
for (var j = 0; j < balls.length; j++) {
if (!(this === balls[j])) {
var dx = this.x - balls[j].x;
var dy = this.y - balls[j].y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.size + balls[j].size) {
balls[j].color = this.color =
"rgb(" +
random(0, 255) +
"," +
random(0, 255) +
"," +
random(0, 255) +
")";
}
}
}
};
// define array to store balls
var balls = [];
// define loop that keeps drawing the scene constantly
function loop() {
ctx.fillStyle = "rgba(0,0,0,0.25)";
ctx.fillRect(0, 0, width, height);
ctx.lineWidth = 5;
ctx.strokeRect(width * 0.1, height * 0.1, width * 0.8, height * 0.8); //绘制矩形边框
while (balls.length < gameParams.ballCount) {
var size = random(10, 20);
var ball = new Ball(
// ball position always drawn at least one ball width
// away from the adge of the canvas, to avoid drawing errors
random(0 + size, width - size),
random(0 + size, height - size),
random(-5, 5),
random(-5, 5),
true,
"rgb(" +
random(0, 255) +
"," +
random(0, 255) +
"," +
random(0, 255) +
")",
size
);
balls.push(ball);
}
for (var i = 0; i < balls.length; i++) {
if (balls[i].exists) {
balls[i].draw();
if (gameParams.start) {
balls[i].update();
balls[i].collisionDetect();
}
}
}
evilCircle.draw();
evilCircle.checkBounds();
evilCircle.collisionDetect();
ANIMATION = requestAnimationFrame(loop);
}
function updateHtml() {
document.getElementById("ballCount").innerHTML = gameParams.ballCount;
document.getElementById("chapter").innerHTML = gameParams.chapter;
}
function showMainHP(val) {
document.getElementById("mainHP").innerHTML = val;
if (val <= 0) {
document.getElementById("result").innerHTML = "GAME OVER";
window.cancelAnimationFrame(ANIMATION);
clearInterval(timer); //停止定时器
var historyChapter = localStorage.getItem("gameParams.chapter") || 0;
if (gameParams.chapter > historyChapter) {
localStorage.setItem("gameParams.chapter", gameParams.chapter);
document.getElementById("best-chapter").innerHTML = gameParams.chapter;
}
}
}
//增加球速度
function promoteVel() {
for (var itm of balls) {
console.log(itm);
itm.velX < 0
? (itm.velX -= random(1, 9) / 10)
: (itm.velX += random(1, 9) / 10);
itm.velY < 0
? (itm.velY -= random(1, 9) / 10)
: (itm.velY += random(1, 9) / 10);
}
}
function successAction() {
clearInterval(timer); //停止定时器
gameParams.start = false;
//延时一秒增加
if (gameParams.ballCount < 20) {
gameParams.ballCount += 2;
} else if (gameParams.ballCount < 30) {
promoteVel(); //提升速度
gameParams.ballCount += 1;
} else {
promoteVel(); //提升速度
}
gameParams.time = 10;
gameParams.chapter++;
updateHtml();
setTimeout(() => {
//延时3秒启动
gameParams.start = true;
timer = setInterval(() => {
document.getElementById("timeout").innerHTML = gameParams.time;
if (gameParams.time <= 0) {
successAction();
}
gameParams.time--;
}, 1000);
}, 3000);
}
alert("开始游戏?");
// 历史最高分
document.getElementById("best-chapter").innerHTML =
localStorage.getItem("gameParams.chapter") || 0;
//定时器
var timer = setInterval(() => {
gameParams.time--;
document.getElementById("timeout").innerHTML = gameParams.time;
if (gameParams.time <= 0) {
successAction();
}
}, 1000);
//初始化系统变量
var gameParams = new GameParams();
//初始化目标
var evilCircle = new EvilCircle(
random(MOVE_MIN_WIDTH, MOVE_MAX_WIDTH),
random(MOVE_MIN_HEIGHT, MOVE_MAX_HEIGHT),
true
);
evilCircle.setControls();
window.onload = function () {
updateHtml();
showMainHP(evilCircle.hp);
setTimeout(() => {
gameParams.start = true;
}, 1000);
loop();
};