-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
487 lines (436 loc) · 13.7 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
const blockType = new Map([
['O', {
name: 'O',
color: "skyblue",
shape: [[[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]]
}],
['S', {
name: 'S',
color: "gray",
shape: [
[[0, 0, 0, 0], [0, 0, 1, 1], [0, 1, 1, 0], [0, 0, 0, 0]],
[[0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 1], [0, 0, 0, 0]]
]
}],
['Z', {
name: 'Z',
color: "purple",
shape: [
[[0, 0, 0, 0], [0, 1, 1, 0], [0, 0, 1, 1], [0, 0, 0, 0]],
[[0, 0, 0, 1], [0, 0, 1, 1], [0, 0, 1, 0], [0, 0, 0, 0]]
]
}],
['I', {
name: 'I',
color: "darkred",
shape: [
[[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]]
]
}],
['T', {
name: 'T',
color: "#FFD300",
shape: [
[[0, 0, 0, 0], [0, 1, 1, 1], [0, 0, 1, 0], [0, 0, 0, 0]],
[[0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 1, 0], [0, 0, 0, 0]],
[[0, 0, 1, 0], [0, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 1, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
]
}],
['L', {
name: 'L',
color: "green",
shape: [
[[0, 0, 0, 0], [0, 1, 1, 1], [0, 1, 0, 0], [0, 0, 0, 0]],
[[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 1], [0, 0, 0, 0]],
[[0, 0, 0, 1], [0, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
[[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
]
}],
['J', {
name: 'J',
color: "blue",
shape: [
[[0, 0, 0, 0], [0, 1, 1, 1], [0, 0, 0, 1], [0, 0, 0, 0]],
[[0, 0, 1, 1], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]],
[[0, 1, 0, 0], [0, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
[[0, 0, 1, 0], [0, 0, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]
]
}]
]);
var blockTypeArray = Array.from(blockType.keys());
var nowBlock; // Block
var nextBlockType; // blockType
var keepBlockType; // blockType
var canvas = document.getElementById("game");
var canvasNextBlock = document.getElementById("nextBlock");
const SMALL_BLOCK_NUM = 4;
const SMALL_BLOCK_SIZE = 20;
const BIG_BLOCK_SIZE = SMALL_BLOCK_SIZE * SMALL_BLOCK_NUM;
const GAME_SCREEN_WIDTH_NUM = 10;
const GAME_SCREEN_HEIGHT_NUM = 20;
const GAME_SCREEN_WIDTH = GAME_SCREEN_WIDTH_NUM * SMALL_BLOCK_SIZE;
const GAME_SCREEN_HEIGHT = GAME_SCREEN_HEIGHT_NUM * SMALL_BLOCK_SIZE;
const GAME_SCREEN_LEFT = canvas.clientLeft;
const GAME_SCREEN_RIGHT = canvas.clientLeft + GAME_SCREEN_WIDTH;
const GAME_SCREEN_TOP = canvas.clientTop;
const GAME_SCREEN_BOTTOM = canvas.clientTop + GAME_SCREEN_HEIGHT;
const WAIT_NEXTBLOCK_TIME = 1000;
const NOW_DELETE = -2;
const BEGIN_X = GAME_SCREEN_LEFT + GAME_SCREEN_WIDTH_NUM / 2 - 4 / 2;
const BEGIN_Y = GAME_SCREEN_TOP;
const SPEED = 1000;
const NONE_DUPLICATED = 0;
const LEFT_DUPLICATED = 1;
const RIGHT_DUPLICATED = 2;
const EITHER_DUPLICATED = 3;
const BLOCK_GAP = 1;
const LINE_COLOR = "black";
var gameScreenArray = new Array(GAME_SCREEN_HEIGHT_NUM)
.fill(-1)
.map(row => new Array(GAME_SCREEN_WIDTH_NUM).fill(-1));
if (canvas.getContext) {
var ctx = canvas.getContext("2d");
canvas.width = GAME_SCREEN_WIDTH + BLOCK_GAP;
canvas.height = GAME_SCREEN_HEIGHT + BLOCK_GAP;
var ctxNextBlock = canvasNextBlock.getContext("2d");
canvasNextBlock.width = BIG_BLOCK_SIZE;
canvasNextBlock.height = BIG_BLOCK_SIZE;
} else {
console.log("browser not supported canvas");
}
$(window).load(function() {
console.log("load");
setNextRandomBlockType();
drawWhiteLineOnBackground();
drawNewBlock();
});
setInterval(function() {
nowBlock.drawDown(nowBlock.x, nowBlock.y + 1);
}, SPEED);
var drawWhiteLineOnBackground = function() {
ctx.fillStyle = LINE_COLOR;
for (var i = 0; i <= GAME_SCREEN_WIDTH_NUM; i++) {
ctx.fillRect(
i * SMALL_BLOCK_SIZE,
0,
BLOCK_GAP,
GAME_SCREEN_HEIGHT
);
}
for (var i = 0; i <= GAME_SCREEN_HEIGHT_NUM; i++) {
ctx.fillRect(
0,
i * SMALL_BLOCK_SIZE,
GAME_SCREEN_WIDTH,
BLOCK_GAP
);
}
}
var drawNewBlock = function() {
nowBlock = new Block(nextBlockType, BEGIN_X, BEGIN_Y);
if (nowBlock.isBottom(BEGIN_X, BEGIN_Y)) {
gameEnd();
}
nowBlock.drawDown(BEGIN_X, BEGIN_Y);
setNextRandomBlockType();
drawNextBlock();
};
var drawNextBlock = function() {
var nextBlock = nextBlockType;
var x = 0;
var y = 0;
for (var i = 0; i < SMALL_BLOCK_NUM; i++) {
for (var j = 0; j < SMALL_BLOCK_NUM; j++) {
if (nextBlock.shape[0][i][j] == 1) {
ctxNextBlock.fillStyle = nextBlock.color;
} else {
ctxNextBlock.fillStyle = "white";
}
ctxNextBlock.fillRect(
(x + j) * SMALL_BLOCK_SIZE + BLOCK_GAP,
(y + i) * SMALL_BLOCK_SIZE + BLOCK_GAP,
SMALL_BLOCK_SIZE - BLOCK_GAP,
SMALL_BLOCK_SIZE - BLOCK_GAP
);
}
}
};
// sy ~ se row에서 지워질 수 있는 행 체크 & 지우기
// TODO test
var checkRowsAndErase = function(sy, ey) {
var isEraseAnything = false;
for (var i = sy; i <= ey; i++) {
if (canEraseRow(i)) {
eraseRow(i);
isEraseAnything = true;
}
}
if (isEraseAnything) {
rearrange();
}
};
var canEraseRow = function(row) {
for (var j = 0; j < GAME_SCREEN_WIDTH_NUM; j++) {
if (gameScreenArray[row][j] == -1) {
return false;
}
}
return true;
};
var eraseRow = function(row) {
for (var j = 0; j < GAME_SCREEN_WIDTH_NUM; j++) {
eraseOneBlock(row, j);
gameScreenArray[row][j] = NOW_DELETE;
}
};
var eraseOneBlock = function(x, y) {
ctx.clearRect(
x * SMALL_BLOCK_SIZE + BLOCK_GAP,
y * SMALL_BLOCK_SIZE + BLOCK_GAP,
SMALL_BLOCK_SIZE - BLOCK_GAP,
SMALL_BLOCK_SIZE - BLOCK_GAP
);
};
var drawOneBlock = function(x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(
x * SMALL_BLOCK_SIZE + BLOCK_GAP,
y * SMALL_BLOCK_SIZE + BLOCK_GAP,
SMALL_BLOCK_SIZE - BLOCK_GAP,
SMALL_BLOCK_SIZE - BLOCK_GAP
);
};
var rearrange = function() {
for (var j = 0; j < GAME_SCREEN_WIDTH_NUM; j++) {
// from bottom
var deleteBlockNum = 0;
for (var i = GAME_SCREEN_HEIGHT_NUM - 1; i >= 0; i--) {
if (gameScreenArray[i][j] == -1) continue;
if (gameScreenArray[i][j] == NOW_DELETE) {
gameScreenArray[i][j] = -1;
eraseOneBlock(j, i);
deleteBlockNum++;
} else {
var key = gameScreenArray[i][j];
eraseOneBlock(j, i);
gameScreenArray[i][j] = -1;
drawOneBlock(j, i + deleteBlockNum, getBlockTypeByKey(key).color);
gameScreenArray[i + deleteBlockNum][j] = key;
}
}
}
};
var drawBelow = function(block) {
console.log("below");
var x = block.x;
var y = block.y;
block.eraseBeforeBlock();
while (!block.isBottom(x, y + 1)) {
y = y + 1;
}
block.drawBlock(x, y);
setBlockInGameScreen(block);
checkRowsAndErase(
y,
Math.min(GAME_SCREEN_HEIGHT_NUM - 1, y + SMALL_BLOCK_NUM - 1)
);
drawNewBlock();
};
var gameEnd = function() {
alert("game over!");
};
var setBlockInGameScreen = function(block) {
for (var i = 0; i < SMALL_BLOCK_NUM; i++) {
for (var j = 0; j < SMALL_BLOCK_NUM; j++) {
if (block.shape[j][i] == 1) {
gameScreenArray[block.y + j][block.x + i] = block.type.name;
}
}
}
};
const keepOrLoadBlock = function() {
// #1
if(nowBlock.isLoaded) return false;
nowBlock.eraseBeforeBlock();
const willKeepType = nowBlock.type;
// #2
if(keepBlockType) {
nowBlock = new Block(keepBlockType, BEGIN_X, BEGIN_Y);
nowBlock.isLoaded = true;
}
else { // #3
nowBlock = new Block(nextBlockType, BEGIN_X, BEGIN_Y);
setNextRandomBlockType();
}
nowBlock.drawDown(BEGIN_X, BEGIN_Y);
keepBlockType = willKeepType;
if (nowBlock.isBottom(BEGIN_X, BEGIN_Y)) {
gameEnd();
}
drawNextBlock();
return true;
}
let getBlockTypeByKey = function(blockKey) {
return blockType.get(blockKey);
}
let getBlockTypeByIndex = function(blockIndex) {
return getBlockTypeByKey(blockTypeArray[blockIndex]);
}
let getRandomBlockType = function() {
return getBlockTypeByIndex(Math.floor(Math.random() * blockType.size));
}
let setNextRandomBlockType = function() {
nextBlockType = getRandomBlockType();
}
/**
*
* @param {*} blockType blockType
* @param {*} x block의 x 좌표
* @param {*} y block의 y 좌표
*/
function Block(blockType, x, y) {
this.type = blockType;
this.shapeIndex = 0;
this.shape = this.type.shape[this.shapeIndex];
this.x = x;
this.y = y;
this.isLoaded = false;
this.drawLeftOrRight = function(nx, ny) {
if (
this.isDuplicatedBlockOrOutOfGameScreen(nx, ny) != NONE_DUPLICATED
) {
console.log("duplicated!");
} else {
this.eraseBeforeBlock();
this.drawBlock(nx, ny);
}
};
this.drawDown = function(nx, ny) {
if (this.isBottom(nx, ny)) {
setBlockInGameScreen(this);
//한 줄 지울 수 있는지 체크
checkRowsAndErase(
this.y,
Math.min(
GAME_SCREEN_HEIGHT_NUM - 1,
this.y + SMALL_BLOCK_NUM - 1
)
);
setTimeout(drawNewBlock(), WAIT_NEXTBLOCK_TIME);
} else {
this.eraseBeforeBlock();
this.drawBlock(nx, ny);
}
};
this.eraseBeforeBlock = function() {
for (var i = 0; i < SMALL_BLOCK_NUM; i++) {
for (var j = 0; j < SMALL_BLOCK_NUM; j++) {
if (this.shape[i][j] == 1) {
eraseOneBlock(this.x + j, this.y + i);
}
}
}
};
this.drawBlock = function(x, y) {
// Change colors just before drawing to avoid affecting the previous or next block color.
ctx.fillStyle = this.type.color;
for (var i = 0; i < SMALL_BLOCK_NUM; i++) {
for (var j = 0; j < SMALL_BLOCK_NUM; j++) {
if (this.shape[i][j] == 1) {
drawOneBlock(x + j, y + i, this.type);
}
}
}
this.x = x;
this.y = y;
};
this.isDuplicatedBlockOrOutOfGameScreen = function(x, y) {
for (var i = 0; i < SMALL_BLOCK_NUM; i++) {
for (var j = 0; j < SMALL_BLOCK_NUM; j++) {
var nx = x + i;
var ny = y + j;
if (this.shape[j][i] == 0) continue;
// out of game screen
if (nx < 0) {
return LEFT_DUPLICATED;
}
if (GAME_SCREEN_WIDTH_NUM < nx) {
return RIGHT_DUPLICATED;
}
// duplicated another block
if (gameScreenArray[ny][nx] != -1) {
return EITHER_DUPLICATED;
}
}
}
return NONE_DUPLICATED;
};
this.isBottom = function(x, y) {
for (var i = 0; i < SMALL_BLOCK_NUM; i++) {
for (var j = 0; j < SMALL_BLOCK_NUM; j++) {
var nx = x + i;
var ny = y + j;
if (this.shape[j][i] == 0) continue;
if (GAME_SCREEN_HEIGHT_NUM <= ny) return true;
if (gameScreenArray[ny][nx] != -1) return true;
}
}
return false;
};
this.rotation = function() {
this.eraseBeforeBlock();
this.shapeIndex = (this.shapeIndex + 1) % this.type.shape.length;
this.shape = this.type.shape[this.shapeIndex];
var checkDuplicated = this.isDuplicatedBlockOrOutOfGameScreen(
this.x,
this.y
);
if (checkDuplicated != NONE_DUPLICATED) {
var moveIndex = 0;
if (
checkDuplicated == LEFT_DUPLICATED ||
checkDuplicated == EITHER_DUPLICATED
) {
for (var i = 1; i < SMALL_BLOCK_NUM; i++) {
if (
this.isDuplicatedBlockOrOutOfGameScreen(
this.x + i,
this.y
) == NONE_DUPLICATED
) {
moveIndex = i;
break;
}
}
}
if (
checkDuplicated == RIGHT_DUPLICATED ||
checkDuplicated == EITHER_DUPLICATED
) {
for (var i = 1; i < SMALL_BLOCK_NUM; i++) {
if (
this.isDuplicatedBlockOrOutOfGameScreen(
this.x - i,
this.y
) == NONE_DUPLICATED
) {
moveIndex = -i;
break;
}
}
}
// 움직여도 안되는 경우
if (moveIndex == 0) {
this.shapeIndex =
(this.shapeIndex + this.type.shape.length - 1) %
this.type.shape.length;
this.shape = this.type.shape[this.shapeIndex];
} else {
this.x += moveIndex;
}
}
this.drawBlock(this.x, this.y);
};
}