-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
566 lines (388 loc) · 9.76 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//随机函数 a,b为整数 [a,b]
function random(a, b) {
return a + Math.floor(Math.random() * (b - a + 1));
}
WIDTH_NUM = 13;
HEIGH_NUM = 20;
function Direction() {
}
Direction.UP = 0;
Direction.RIGHT = 1;
Direction.DOWN = 2;
Direction.LEFT = 3;
//0:地板,啥都不是。 1: 岩石,固定不动了。 2: 形状中的grid
function Category() {
}
Category.GROUND = 0;
Category.SOLID = 1;
Category.MOVE = 2;
//类别2 L1型
//类别3 L2型
//类别4 Z1型
//类别5 Z2型
//类别6 O型
//类别7 T型
function Type() {
}
Type.I = 0
Type.L1 = 1;
Type.L2 = 2;
Type.Z1 = 3;
Type.Z2 = 4;
Type.O = 5;
Type.T = 6;
Type.ALL = [];
Type.ALL[Type.I] = [0x4444, 0x0f00, 0x4444, 0x0f00,
[0, WIDTH_NUM - 2, HEIGH_NUM - 4, -1],
[-1, WIDTH_NUM - 4, HEIGH_NUM - 2, 0],
[0, WIDTH_NUM - 2, HEIGH_NUM - 4, -1],
[-1, WIDTH_NUM - 4, HEIGH_NUM - 2, 0]
];
Type.ALL[Type.L1] = [0x4460, 0x0740, 0x0622, 0x02e0,
[0, WIDTH_NUM - 3, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 4, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 4, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 3, 0]
];
Type.ALL[Type.L2] = [0x2260, 0x0470, 0x0644, 0x0e20,
[0, WIDTH_NUM - 3, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 4, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 4, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 3, 0]
];
Type.ALL[Type.Z1] = [0x0630, 0x0264, 0x0630, 0x0264,
[-1, WIDTH_NUM - 4, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 4, -1],
[-1, WIDTH_NUM - 4, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 4, -1]
];
Type.ALL[Type.Z2] = [0x0360, 0x0462, 0x0360, 0x0462,
[-1, WIDTH_NUM - 4, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 4, -1],
[-1, WIDTH_NUM - 4, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 4, -1]
];
Type.ALL[Type.O] = [0x0660, 0x0660, 0x0660, 0x0660,
[-1, WIDTH_NUM - 3, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 3, -1]
];
Type.ALL[Type.T] = [0x04e0, 0x4640, 0x0720, 0x0262,
[-1, WIDTH_NUM - 3, HEIGH_NUM - 3, 0],
[0, WIDTH_NUM - 3, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 4, HEIGH_NUM - 3, -1],
[-1, WIDTH_NUM - 3, HEIGH_NUM - 4, -1]
];
function Color() {
}
Color.ALL = ["", "orange", "olive", "cornflowerblue", "cyan", "red"];
//形状类
function Shape(game) {
this.game = game;
this.x = 0;
this.y = 0;
//类别。7种类别
this.type = Type.I;
//方向。4中方向 上右下左
this.direction = Direction.UP;
}
Shape.prototype.newShape = function (x, y, direction) {
var shape = new Shape(this.game);
shape.x = x;
shape.y = y;
shape.type = this.type;
shape.direction = direction;
return shape;
}
//判断当前的shape是合法的吗? 不超过边界,不和障碍物重叠。
Shape.prototype.isValid = function () {
//首先判断是否越界。
var edge = Type.ALL[this.type][4 + this.direction];
if (this.y < edge[0]) {
return false;
} else if (this.x > edge[1]) {
return false;
} else if (this.y > edge[2]) {
return false;
} else if (this.x < edge[3]) {
return false;
}
//考虑是否和现有的地板冲突。
var grids = this.getGrids();
for (var i = 0; i < grids.length; i++) {
var grid = grids[i];
if (grid.category == Category.SOLID) {
return false;
}
}
return true;
}
//往下面掉一格 能够继续掉落返回true,反之 返回 false.
Shape.prototype.dropOneStep = function () {
var nextShape = this.newShape(this.x, this.y + 1, this.direction);
if (nextShape.isValid()) {
this.setCategory(Category.GROUND);
this.y++;
this.setCategory(Category.MOVE);
this.game.refreshStage();
return true;
} else {
this.setCategory(Category.SOLID);
this.game.refreshStage();
return false;
}
}
//往左移动一格
Shape.prototype.moveLeftOneStep = function () {
var nextShape = this.newShape(this.x - 1, this.y, this.direction);
if (nextShape.isValid()) {
this.setCategory(Category.GROUND);
this.x--;
this.setCategory(Category.MOVE);
this.game.refreshStage();
return true;
} else {
return false;
}
}
//上:改变方向
Shape.prototype.changeDirection = function () {
var newDirection = this.direction;
newDirection++;
if (newDirection > Direction.LEFT) {
newDirection = Direction.UP;
}
var nextShape = this.newShape(this.x, this.y, newDirection);
if (nextShape.isValid()) {
this.setCategory(Category.GROUND);
this.direction = newDirection;
this.setCategory(Category.MOVE);
this.game.refreshStage();
return true;
} else {
return false;
}
}
//往右移动一格
Shape.prototype.moveRightOneStep = function () {
var nextShape = this.newShape(this.x + 1, this.y, this.direction);
if (nextShape.isValid()) {
this.setCategory(Category.GROUND);
this.x++;
this.setCategory(Category.MOVE);
this.game.refreshStage();
return true;
} else {
return false;
}
}
//获取到当前shape所占用的grids. 四个grid.
Shape.prototype.getGrids = function () {
var hex = Type.ALL[this.type][this.direction];
// 0100 0100 0100 0100
var binary = hex.toString(2);
var whiteLen = 16 - binary.length;
for (var n = 0; n < whiteLen; n++) {
binary = "0" + binary;
}
var grids = [];
for (var y0 = 0; y0 < 4; y0++) {
for (var x0 = 0; x0 < 4; x0++) {
var index = y0 * 4 + x0;
var bit = binary.charAt(index);
var x = this.x + x0;
var y = this.y + y0;
if (bit == '1') {
grids.push(this.game.grids[x][y]);
}
}
}
return grids;
}
//将当前的形状全部置为一种category
Shape.prototype.setCategory = function (cate) {
var grids = this.getGrids();
for (var i = 0; i < grids.length; i++) {
var grid = grids[i];
grid.category = cate;
}
}
//类别2 L1型
//类别3 L2型
//类别4 Z1型
//类别5 Z2型
//类别6 O型
//类别7 T型
Shape.prototype.refresh = function () {
}
//小方块类,构成舞台的最基本元素。
function Grid() {
this.x = 0;
this.y = 0;
this.unit = 30;
//0:地板,啥都不是。 1: 岩石,固定不动了。 2: 形状中的grid
this.category = Category.GROUND;
this.$dom = $("<span></span>");
}
Grid.prototype.refresh = function () {
this.$dom.removeAttr("class");
var color = Color.ALL[0];
if (this.category == Category.SOLID) {
color = Color.ALL[1];
} else if (this.category == Category.MOVE) {
color = Color.ALL[2];
}
this.$dom.addClass(color);
}
//游戏类,总控全局的。
function Game() {
this.$stage = $(".stage");
this.grids = [];
this.width = WIDTH_NUM;
this.height = HEIGH_NUM;
//当前正在掉落的shape
this.shape = null;
//得分
this.score = 0;
//掉落的时间 0表示最快。
this.interval = 200;
}
//在dom中更新得分
Game.prototype.updateScore = function () {
$(".score").html(this.score);
}
//打印出当前的grids,为了方便我们调试。
Game.prototype.printGrids = function () {
console.log("------print grids------")
for (var y = 0; y < HEIGH_NUM; y++) {
var line = "";
for (var x = 0; x < WIDTH_NUM; x++) {
var grid = this.grids[x][y];
line += grid.category;
}
console.log(line);
}
console.log("------finish print grids------")
}
//将自身属性 grids 填充完毕
Game.prototype.fillGrids = function () {
for (var x = 0; x < WIDTH_NUM; x++) {
var xArr = [];
for (var y = 0; y < HEIGH_NUM; y++) {
var grid = new Grid();
grid.x = x;
grid.y = y;
xArr.push(grid);
//将 grids 附着到 $stage上去 准备舞台
grid.$dom.css({"left": (x * grid.unit) + "px", "top": (y * grid.unit) + "px"});
this.$stage.append(grid.$dom);
}
this.grids.push(xArr)
}
}
//刷新画布上的颜色
Game.prototype.refreshStage = function () {
for (var x = 0; x < WIDTH_NUM; x++) {
for (var y = 0; y < HEIGH_NUM; y++) {
var grid = this.grids[x][y];
grid.refresh();
}
}
}
//监听键盘点击事件
Game.prototype.listenEvent = function () {
var that = this;
$("body").keydown(function (e) {
//上
if (e.keyCode == 38) {
that.shape.changeDirection();
}
//右
else if (e.keyCode == 39) {
that.shape.moveRightOneStep();
}
//下
else if (e.keyCode == 40) {
that.interval = 0;
}
//左
else if (e.keyCode == 37) {
that.shape.moveLeftOneStep();
}
})
}
//消除第y这一行,如果能消除返回true,不能消除返回false.
Game.prototype.eliminateOneLine = function (y) {
//判断能不能消除。
for (var x = 0; x <= WIDTH_NUM - 1; x++) {
var grid = this.grids[x][y];
if (grid.category != Category.SOLID) {
return false;
}
}
//这一行表示能够消除。
for (var y0 = y; y0 >= 0; y0--) {
for (var x0 = 0; x0 <= WIDTH_NUM - 1; x0++) {
if (y0 == 0) {
this.grids[x0][y0].category = Category.GROUND;
} else {
this.grids[x0][y0].category = this.grids[x0][y0 - 1].category;
}
}
}
return true;
}
Game.prototype.eliminateLines = function () {
var lines = 0;
for (var y = HEIGH_NUM - 1; y >= 0; y--) {
console.log("开始消除y = " + y);
while (this.eliminateOneLine(y)) {
lines++;
}
}
if (lines == 1) {
this.score += 100;
} else if (lines == 2) {
this.score += 300;
} else if (lines == 3) {
this.score += 500;
} else if (lines == 4) {
this.score += 1000;
}
this.updateScore();
//速度设置为正常
this.interval = 200;
}
Game.prototype.init = function () {
var that = this;
this.listenEvent();
this.fillGrids();
this.refreshStage();
this.shape = new Shape(this);
this.shape.x = 5;
this.shape.y = 0;
this.shape.direction = random(0, 3);
this.shape.type = random(0, 6);
var temp = 0;
var intervalHandler = setInterval(function () {
if (temp > that.interval) {
temp = 0;
var canDrop = that.shape.dropOneStep();
if (!canDrop) {
//开始消除。
that.eliminateLines();
that.shape.x = 5;
that.shape.y = 0;
that.shape.direction = random(0, 3);
that.shape.type = random(0, 6);
}
} else {
temp += 10;
}
}, 10);
}
$(function () {
var game = new Game();
game.init();
});