-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
706 lines (627 loc) · 19.3 KB
/
script.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
const tetrisCanvas = document.getElementById('tetris_screen')
// fix blur images
tetrisCanvas.width = 600;
tetrisCanvas.height = 1200;
tetrisCanvas.style.width = "300px";
tetrisCanvas.style.height = "600px";
const tetrisContext = tetrisCanvas.getContext('2d');
tetrisContext.scale(2, 2); // Scale the displayed grid two times to get good display
// Constants
const holdPieceCanvas = document.getElementById('hold_piece');
const holdPieceContext = holdPieceCanvas.getContext('2d');
holdPieceCanvas.width = holdPieceCanvas.height = 150;
holdPieceCanvas.style.width = holdPieceCanvas.style.height = '75px';
const nextPieceCanvas = document.getElementById('next_piece');
const nextPieceContext = nextPieceCanvas.getContext('2d');
nextPieceCanvas.width = nextPieceCanvas.height = 150;
nextPieceCanvas.style.width = nextPieceCanvas.style.height = '75px';
const SQUARE_SIZE = 20;
const CANVAS_WIDTH = (tetrisCanvas.width / 2) / SQUARE_SIZE;
const CANVAS_HEIGHT = (tetrisCanvas.height / 2) / SQUARE_SIZE;
const PIECES_NAME = ['z', 's', 'o', 'j', 'l', 'i', 't'];
const VACANT = '#111d5e';
// Game variables
let board = [];
let gameOver = false;
// Drop piece variables
let dropStart = Date.now();
let fallSpeed = 1000;
let currentSpeed = fallSpeed;
// Hold piece
let nextPiece = null;
let holdPiece = [];
let didChanged = false;
// Points
let currentPointsElement = document.getElementById('points');
let lastDifficultyCheckpoint = 3000;
let difficultyChange = 1;
function initBoard() {
/*
@desc - initialization of array of the game where the
piece will be 'locked' in place
*/
tetrisContext.clearRect(0, 0, tetrisCanvas.width, tetrisCanvas.height);
for (let row = 0; row < CANVAS_HEIGHT; row++) {
board[row] = []
for (let col = 0; col < CANVAS_WIDTH; col++) {
board[row][col] = VACANT;
}
}
// Reinitialize the hold piece
holdPiece = [];
holdPieceContext.clearRect(
0, 0,
(holdPieceCanvas.width),
(holdPieceCanvas.height));
// Reinitalize points
currentPointsElement.innerHTML = 0;
}
function checkRow() {
/*
@desc - check row if full, if full, clear the specific row
in the virtual board, redraw it in canvas and add
the points
*/
// Check if row is full
let multiplier = 0;
let newPoints = 0;
board.forEach((valueSet, index) => {
if (!valueSet.includes(VACANT)) {
multiplier += 1;
newPoints += 100;
board.splice(index, 1);
board.unshift(Array(CANVAS_WIDTH).fill(VACANT));
// Redraw the new board
board.forEach((colSet, col) => {
colSet.forEach((color, row) => {
tetrisContext.fillStyle = color;
tetrisContext.fillRect(
row * SQUARE_SIZE,
col * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE
)
})
})
}
});
currentPointsElement.innerHTML = parseInt(currentPointsElement.innerHTML) + (newPoints * multiplier);
currentPointsElement = document.getElementById('points');
if (currentPointsElement.innerHTML >= lastDifficultyCheckpoint * difficultyChange) {
fallSpeed -= 75;
difficultyChange += 1;
}
}
function switchPiece(localPiece=undefined) {
/*
@desc - hold current piece and exchange if there is present
*/
if (holdPiece[0]) {
// Reset holdPiece coords
holdPiece[0].offset.x = 6;
holdPiece[0].offset.y = -1;
holdPiece.push(localPiece);
piece.clearMatrix();
piece = holdPiece.shift();
} else {
holdPiece.push(localPiece);
piece.clearMatrix();
piece = nextPiece;
nextPiece = new Tetromino(PIECES_NAME[Math.floor(Math.random() * PIECES_NAME.length)]);
}
holdPieceContext.clearRect(
0, 0,
(holdPieceCanvas.width),
(holdPieceCanvas.height));
holdPiece[0].currentPiece.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
holdPieceContext.fillStyle = holdPiece[0].color;
holdPieceContext.fillRect(
(x * 40),
(y * 40),
40,
40)
};
});
});
didChanged = true;
}
class Tetromino {
/*
@desc - Tetromino class that represents a tetromino piece.
*/
TETROMINOES_MATRICES = {
z: [
[
[1, 1, 0],
[0, 1, 1],
[0, 0, 0]
],
[
[0, 0, 1],
[0, 1, 1],
[0, 1, 0]
],
[
[0, 0, 0],
[1, 1, 0],
[0, 1, 1]
],
[
[0, 1, 0],
[1, 1, 0],
[1, 0, 0]
]
],
s: [
[
[0, 1, 1],
[1, 1, 0],
[0, 0, 0]
],
[
[0, 1, 0],
[0, 1, 1],
[0, 0, 1]
],
[
[0, 0, 0],
[0, 1, 1],
[1, 1, 0]
],
[
[1, 0, 0],
[1, 1, 0],
[0, 1, 0]
]
],
j: [
[
[0, 1, 0],
[0, 1, 0],
[1, 1, 0]
],
[
[1, 0, 0],
[1, 1, 1],
[0, 0, 0]
],
[
[0, 1, 1],
[0, 1, 0],
[0, 1, 0]
],
[
[0, 0, 0],
[1, 1, 1],
[0, 0, 1]
]
],
t: [
[
[0, 0, 0],
[1, 1, 1],
[0, 1, 0]
],
[
[0, 1, 0],
[1, 1, 0],
[0, 1, 0]
],
[
[0, 1, 0],
[1, 1, 1],
[0, 0, 0]
],
[
[0, 1, 0],
[0, 1, 1],
[0, 1, 0]
]
],
l: [
[
[0, 1, 0],
[0, 1, 0],
[0, 1, 1]
],
[
[0, 0, 0],
[1, 1, 1],
[1, 0, 0]
],
[
[1, 1, 0],
[0, 1, 0],
[0, 1, 0]
],
[
[0, 0, 1],
[1, 1, 1],
[0, 0, 0]
]
],
i: [
[
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]
],
[
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0]
],
[
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 0]
],
[
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
],
o: [
[
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]
],
[
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]
]
]
}
TETROMINOES_COLOR = {
z: '#f9d5bb',
s: '#ffb091',
o: '#fa9a87',
j: '#f57c5d',
l: '#f66767',
i: '#d35656',
t: '#d4322d'
}
// Notifiers
locked = false;
isOnHold = false;
// The current place of the piece
offset = {
x: 6,
y: -1
}
constructor(piece, place=0) {
/*
@desc - Create a tetromino instance using the piece name and its initial state
@param string - piece name => name of the piece in TETRIMINOES_MATRICES
@param int - piece rotation => default place is 0 for initial state
*/
this.piece = piece;
this.place = place;
this.currentPiece = this.TETROMINOES_MATRICES[this.piece][this.place];
this.color = this.TETROMINOES_COLOR[this.piece];
}
// PIECE DRAW
drawMatrix() {
/*
@desc - draw the current tetrimino matrix in place using $offset
*/
this.currentPiece.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
tetrisContext.fillStyle = this.color;
tetrisContext.fillRect(
(x * SQUARE_SIZE) + this.offset.x * SQUARE_SIZE,
(y * SQUARE_SIZE) + this.offset.y * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE);
};
});
});
};
clearMatrix() {
/*
@desc - clear the current tetrimino matrix in place using $offset
*/
this.currentPiece.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
tetrisContext.clearRect(
(x * SQUARE_SIZE) + this.offset.x * SQUARE_SIZE,
(y * SQUARE_SIZE) + this.offset.y * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE);
};
});
});
}
// PIECE MOVEMENTS
moveLeft() {
/*
@desc - Move the piece one square to left if no collision is detected
*/
if (!this.collision(-1, 0)) {
this.clearMatrix();
this.offset.x -= 1
this.drawMatrix();
}
}
moveRight() {
/*
@desc - Move the piece one square to right if no collision is detected
*/
if (!this.collision(1, 0)) {
this.clearMatrix();
this.offset.x += 1;
this.drawMatrix();
}
}
invertMatrixClockwise() {
/*
@desc - invert matrix clockwise
*/
if (!this.piece.locked) {
let kick = 0;
let kickBottom = 0;
this.clearMatrix();
if (this.place >= this.TETROMINOES_MATRICES[this.piece].length - 1) {
this.place = 0;
} else {
this.place += 1;
}
this.currentPiece = this.TETROMINOES_MATRICES[this.piece][this.place];
// Right and Left Wall collision
if (this.collision(0, 0)) {
if (this.offset.x <= 0) {
kick = 1;
// Condition for the 'i' piece
if (this.collision(kick, 0)) {
kick += 1;
}
} else {
kick = -1;
// Condition for the 'i' piece
if (this.collision(kick, 0)) {
kick -= 1;
}
}
}
if (this.collision(0, 1)) {
kickBottom += 1;
}
this.offset.x += kick;
this.offset.y -= kickBottom;
this.drawMatrix();
}
}
invertMatrixCounter() {
/*
@desc - invert matrix counter clockwise
*/
if (!this.piece.locked) {
let kick = 0;
let kickBottom = 0;
this.clearMatrix();
if (this.place >= this.TETROMINOES_MATRICES[this.piece].length - 1) {
this.place = 0;
} else {
this.place += 1;
}
this.currentPiece = this.TETROMINOES_MATRICES[this.piece][this.place];
// Right and Left Wall collision
if (this.collision(0, 0)) {
if (this.offset.x <= 0) {
kick = 1;
// Condition for the 'i' piece
if (this.collision(kick, 0)) {
kick += 1;
}
} else {
kick = -1;
// Condition for the 'i' piece
if (this.collision(kick, 0)) {
kick -= 1;
}
}
}
if (this.collision(0, 1)) {
kickBottom += 1;
}
this.offset.x += kick;
this.offset.y -= kickBottom;
this.drawMatrix();
}
}
fallDown() {
/*
@desc - makes the piece fall down using delta (time intervals)
*/
this.drawMatrix();
if (this.collision(0, 1)) {
// lock the piece in place when collision returned true
// lock the piece in the abstract board
this.locked = true;
for (let i = 0; i < this.currentPiece.length; i++) {
for (let j = 0; j < this.currentPiece[i].length; j++) {
if (!this.currentPiece[i][j]) {
continue;
}
try {
board[this.offset.y + i][this.offset.x + j] = this.color;
} catch (exc) {
// TODO: fix this, make it say game over when a
// piece was locked at the outside top
gameOver = true;
}
}
}
} else {
let now = Date.now();
let delta = now - dropStart;
if (delta > currentSpeed) {
this.clearMatrix();
this.offset.y += 1;
this.drawMatrix();
dropStart = Date.now();
}
}
}
// PIECE COLLISION
collision(x, y) {
/*
@desc boolean - see if the new planned coordinates collides with something
@param int - x => planned next x offset
@param int - y => planned next y offset
*/
for (let i = 0; i < this.currentPiece.length; i++) {
for (let j = 0; j < this.currentPiece[i].length; j++) {
if (!this.currentPiece[i][j]) {
continue;
}
let newX = this.offset.x + j + x;
let newY = this.offset.y + i + y;
if (newX < 0 || newX >= CANVAS_WIDTH || newY >= CANVAS_HEIGHT
|| board[newY][newX] != VACANT) {
return true;
}
}
}
}
}
// START ANIMATION
window.addEventListener('load', start);
function start() {
initBoard();
gameOverDialog.style.display = 'none';
window.requestAnimationFrame(loop);
}
function loop() {
if (typeof(piece) == 'undefined') {
// Create first tetromino
nextPiece = new Tetromino(PIECES_NAME[Math.floor(Math.random() * PIECES_NAME.length)]);
piece = new Tetromino(PIECES_NAME[Math.floor(Math.random() * PIECES_NAME.length)]);
}
if (!gameOver) {
if (piece.locked) {
checkRow();
piece = nextPiece;
nextPiece = new Tetromino(PIECES_NAME[Math.floor(Math.random() * PIECES_NAME.length)]);
didChanged = false;
} else {
piece.fallDown();
}
// Show next piece
nextPieceContext.clearRect(
0, 0,
(nextPieceCanvas.width),
(nextPieceCanvas.height));
nextPiece.currentPiece.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
nextPieceContext.fillStyle = nextPiece.color;
nextPieceContext.fillRect(
(x * 40),
(y * 40),
40,
40)
};
});
});
window.requestAnimationFrame(loop);
} else {
// TODO: Implement better game over
window.requestAnimationFrame(over);
}
}
function over() {
gameOverDialog.style.display = 'flex';
}
// Game controls
window.addEventListener('keydown', (event) => {
switch (event.keyCode) {
// arrow right
case 39:
document.getElementById('moveRight_key').style.cssText = 'background: white; color: black';
event.preventDefault();
piece.moveRight();
break;
// arrow left
case 37:
document.getElementById('moveLeft_key').style.cssText = 'background: white; color: black';
event.preventDefault();
piece.moveLeft();
break;
// arrow up
case 38:
document.getElementById('rotateC_key').style.cssText = 'background: white; color: black';
event.preventDefault();
piece.invertMatrixClockwise();
break;
// arrow down
case 40:
document.getElementById('fall_key').style.cssText = 'background: white; color: black';
event.preventDefault();
currentSpeed = 10;
break;
// character 'C'
case 67:
document.getElementById('rotateCC_key').style.cssText = 'background: white; color: black';
event.preventDefault();
piece.invertMatrixCounter();
break;
// character 'V'
case 86:
document.getElementById('hold_key').style.cssText = 'background: white; color: black';
event.preventDefault();
if (!didChanged) {
switchPiece(piece);
}
break;
}
})
window.addEventListener('keyup', (event) => {
switch (event.keyCode) {
// arrow right
case 39:
document.getElementById('moveRight_key').style.cssText = 'background: transparent; color: white';
break;
// arrow left
case 37:
document.getElementById('moveLeft_key').style.cssText = 'background: transparent; color: white';
break;
// arrow up
case 38:
document.getElementById('rotateC_key').style.cssText = 'background: transparent; color: white';
break;
// arrow down
case 40:
document.getElementById('fall_key').style.cssText = 'background: transparent; color: white';
currentSpeed = fallSpeed;
break;
// character 'C'
case 67:
document.getElementById('rotateCC_key').style.cssText = 'background: transparent; color: white';
break;
// character 'V'
case 86:
document.getElementById('hold_key').style.cssText = 'background: transparent; color: white';
break;
}
})
//----- WEBPAGE ELEMENTS
let gameOverDialog = document.getElementById('game_over_box');
let dialogBtn = document.querySelector('.game_over_btn');
dialogBtn.addEventListener('click', (event) => {
gameOver = false;
window.requestAnimationFrame(start);
})