-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTetris.js
648 lines (596 loc) · 21 KB
/
Tetris.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
var NCOL= prompt ("Digite a largura do tabuleiro");//Quantidade de colunas da matriL base
var NROW= prompt ("Digite a altura do tabuleiro");//Quantidade de linhas da matriL base
var players = [];//Criando diversos jogadores com o construtor Pessoa
const height_pixel = 500/NROW;//Tamanho dos blocos da matriL base
const width_pixel = 250/NCOL;
const EMPTY_SQ = "#EEEEEE";
var canvas = document.getElementById('Matriz');//Pegar a matriL principal pelo ID
canvas.width = 250;
canvas.height = 500;
var mainBlocks = canvas.getContext("2d");//Efeito 2d
var gameSpeed = 1000;
var interval = setInterval(tickMovimentation, gameSpeed);
var timePlayed = setInterval(gameTime, 1000);
var main = []; //MatriL de base
var gameState = 0;
var mainPiece;
var holdedPiece;
var nextPiece;
var rowscount = 0;
var points=0;
var controlSpeed = 0;
var level = 1;
var paused = 0;
var pieceCode = (Math.floor(Math.random()*6)+1);
var seconds=0;
var checkHoldedPiece = false;
var gameOver = new Audio('audios/smb_gameover (online-audio-converter.com).mp3');
var up = new Audio('audios/smb_1-up (online-audio-converter.com).mp3');
var levelUp = new Audio ('audios/smb_warning (online-audio-converter.com).mp3');
var continueMusic = new Audio('audios/y2mate.com - sonic_the_hedgehog_ost_green_hill_zone_G-i8HYi1QH0.mp3');
var activeInstruction = false;
var activeJogo = false;
//Final
document.getElementById("button2").disabled = true;
//variaveis do hold piece
var holdedPiece;
var checkHolded=false;
//variaveis do hold piece
var holdedPiece;
var checkHolded=false;
//Criando a Matriz base
//L[0] L[1] = posição girada 90 > L[2] posição girada 180 > L[3] posição girada 270 >
const L = [ [ [0,0,1],[1,1,1],[0,0,0]],[ [1,0,0],[1,0,0],[1,1,0]],[ [1,1,1],[1,0,0],[0,0,0]],[ [0,1,1],[0,0,1],[0,0,1]]]; //L normal
const M = [ [ [1,1,0],[1,1,0],[0,0,0]],[ [1,1,0],[1,1,0],[0,0,0]],[ [1,1,0],[1,1,0],[0,0,0]],[ [1,1,0],[1,1,0],[0,0,0]]]; //quadrado
const N = [ [ [1,0,0],[1,1,1],[0,0,0]],[ [1,1,0],[1,0,0],[1,0,0]],[ [1,1,1],[0,0,1],[0,0,0]],[ [0,0,1],[0,0,1],[0,1,1]]]; //L invertido
const O = [ [ [0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,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],[0,0,0,0],[1,1,1,1],[0,0,0,0]]]; // |
const Y = [ [ [0,1,0],[1,1,1],[0,0,0]],[ [0,1,0],[0,1,1],[0,1,0]],[ [0,0,0],[1,1,1],[0,1,0]],[ [0,1,0],[1,1,0],[0,1,0]]]; // _|_
const U = [ [ [1,0,1],[1,1,1],[0,0,0]],[ [0,1,1],[0,1,0],[0,1,1]],[ [0,0,0],[1,1,1],[1,0,1]],[ [1,1,0],[0,1,0],[1,1,0]]]; //U
for (row = 0 ;row < NROW ; row++){ //Gera linhas
main[row]= [];
for(col = 0; col < NCOL ;col++){//Gera colunas
main[row][col] = EMPTY_SQ;
}
}
class Piece{
constructor(Tetramino,color) //construtor da peça
{
this.Tetramino=Tetramino;
this.TetraminoN=0;//Mostra a posicao inicial do bloco (no caso L[0])
this.GoTetramino = this.Tetramino[this.TetraminoN]; //Vá bloco L = bloco [posicao0]
this.color=color;
if(Tetramino == O)
{
this.row=NROW-4;//posicao inicial do bloco O
this.col = Math.floor((NCOL / 2) - 1);
}
else {
this.row = NROW - 3;//posição inicial dos outros blocos
this.col = Math.floor((NCOL / 2) - 1);//posicao inicial acima da matriL principal (Para cair dps)
}
}
}
let checkGameOver = () => {
if(checkColision(0, 0, mainPiece.GoTetramino)){
printData();
gameState = 1;
clearInterval(interval);
document.getElementById("button2").disabled = false;
document.getElementById("button2").style.cursor = "pointer";
return true;
}
else{
return false;
}
};
function createMatrix(row, col, color) {
mainBlocks.fillStyle = EMPTY_SQ ;
mainBlocks.fillRect(row*width_pixel, col*height_pixel, width_pixel, height_pixel);
mainBlocks.strokeStyle = 'black';
mainBlocks.strokeRect(row*width_pixel, col*height_pixel, width_pixel, height_pixel);
}
function showMatrix() {
for (row = 0 ;row < NROW ; row++){
for(col = 0; col < NCOL ;col++){
createMatrix(col, row, main[col][row]);
}
}
}
showMatrix();
function generatePiece(random){ //função para gerar peça aleatoria
switch(random){
case 1:
return new Piece(L,"blue");
break;
case 2:
return new Piece(M,"red");
break;
case 3:
return new Piece(N,"green");
break;
case 4:
return new Piece(O,"Gold");
break;
case 5:
return new Piece(Y,"DeepPink");
break;
case 6:
return new Piece(U,"purple");
break;
}
}
function deletePiece(){
for (row = 0; (row+mainPiece.row) < (mainPiece.row + mainPiece.GoTetramino.length) ; row++) { //conta o tamanho (3x3) ou (4x4)
for (col = 0; (col + mainPiece.col) < (mainPiece.col + mainPiece.GoTetramino.length) ; col++) {
if(mainPiece.GoTetramino[row][col] == 1){
mainBlocks.fillStyle = EMPTY_SQ ; //Define a cor do bloco gerado
mainBlocks.fillRect((mainPiece.col+col)*width_pixel, (row+mainPiece.row)*height_pixel, width_pixel, height_pixel);//Linha*tamDoBloco,Coluna*TamDoBloco, TamDoBloco,TamDoBloco
mainBlocks.strokeRect((mainPiece.col+col)*width_pixel, (row+mainPiece.row)*height_pixel, width_pixel, height_pixel);
}
}
}
}
function deleteHoldedPiece(next){
var hold = document.getElementById('hold-canvas');
hold.width = 150;
hold.height = 100;
var holdedBlocks = hold.getContext("2d");
for (let row = 0; row < next.GoTetramino.length ; row++) { //conta o tamanho (3x3) ou (4x4)
for (let col = 0; col < next.GoTetramino.length ; col++) {
if(next.GoTetramino[row][col] == 1){
holdedBlocks.fillStyle = EMPTY_SQ; //Define a cor do bloco gerado
holdedBlocks.strokeStyle = EMPTY_SQ;
holdedBlocks.fillRect(col*20, row*20, 20, 20);//Linha*tamDoBloco,Coluna*TamDoBloco, TamDoBloco,TamDoBloco
holdedBlocks.strokeRect(col*20, row*20, 20, 20);
}
}
}
}
function drawPiece(){
for (row = 0; (row+mainPiece.row) < (mainPiece.row+ mainPiece.GoTetramino.length) ; row++) { //conta o tamanho (3x3) ou (4x4)
for (col = 0; (col + mainPiece.col) < (mainPiece.col + mainPiece.GoTetramino.length) ; col++) {
if(mainPiece.GoTetramino[row][col] == 1){
mainBlocks.fillStyle = mainPiece.color ; //Define a cor do bloco gerado
mainBlocks.fillRect((mainPiece.col+col)*width_pixel, (row+mainPiece.row)*height_pixel, width_pixel, height_pixel);//Linha*tamDoBloco,Coluna*TamDoBloco, TamDoBloco,TamDoBloco
mainBlocks.strokeRect((mainPiece.col+col)*width_pixel, (row+mainPiece.row)*height_pixel, width_pixel, height_pixel);
}
}
}
}
function drawNextPiece(next){
var nextCanvas = document.getElementById('next-canvas');
nextCanvas.width = 150;
nextCanvas.height = 100;
var nextBlocks = nextCanvas.getContext("2d");
for (let row = 0; row < next.GoTetramino.length ; row++) { //conta o tamanho (3x3) ou (4x4)
//(coluna+ColunaInicial) < (ColunaInicial+TamanhoDaPeca)
for (let col = 0; col < next.GoTetramino.length ; col++) {
if(next.GoTetramino[row][col] == 1){
nextBlocks.fillStyle = next.color ; //Define a cor do bloco gerado
nextBlocks.fillRect(col*20, row*20, 20, 20);//Linha*tamDoBloco,Coluna*TamDoBloco, TamDoBloco,TamDoBloco
nextBlocks.strokeRect(col*20, row*20, 20, 20);
}
}
}
}
mainPiece = generatePiece(pieceCode);
pieceCode = (Math.floor(Math.random()*6)+1);
nextPiece = generatePiece(pieceCode);
drawNextPiece(nextPiece);
drawPiece(mainPiece);
function startGame(){
continueMusic.play()
for (row = 0 ;row < NROW ; row++){ //Gera linhas
main[row]= [];
for(col = 0; col < NCOL ;col++){//Gera colunas
main[row][col] = EMPTY_SQ;
}
}
showMatrix();
document.getElementById("button2").disabled = true;
document.getElementById("button2").style.cursor = "not-allowed";
mainPiece = generatePiece(pieceCode);
pieceCode = (Math.floor(Math.random()*6)+1);
nextPiece = generatePiece(pieceCode);
if(checkHoldedPiece == true){
deleteHoldedPiece(holdedPiece);
}
drawNextPiece(nextPiece);
drawPiece(mainPiece);
rowscount = 0;
display = "Eliminated rows: " + rowscount.toString();
document.getElementById("rows").innerHTML = display;
points=0;
display = "Points: " + points.toString();
document.getElementById("points").innerHTML = display;
controlSpeed = 0;
level = 1;
display = "Level: " + level.toString();
document.getElementById("level").innerHTML = display;
paused = 0;
pieceCode = (Math.floor(Math.random()*6)+1);
seconds=0;
gameState = 0;
gameSpeed = 1000;
interval = setInterval(tickMovimentation, gameSpeed);
}
function drawHoldedPiece(next){
var hold = document.getElementById('hold-canvas');
hold.width = 150;
hold.height = 100;
var holdedBlocks = hold.getContext("2d");
for (let row = 0; row < next.GoTetramino.length ; row++) { //conta o tamanho (3x3) ou (4x4)
//(coluna+ColunaInicial) < (ColunaInicial+TamanhoDaPeca)
for (let col = 0; col < next.GoTetramino.length ; col++) {
if(next.GoTetramino[row][col] == 1){
holdedBlocks.fillStyle = next.color ; //Define a cor do bloco gerado
holdedBlocks.fillRect(col*20, row*20, 20, 20);//Linha*tamDoBloco,Coluna*TamDoBloco, TamDoBloco,TamDoBloco
holdedBlocks.strokeRect(col*20, row*20, 20, 20);
}
}
}
}
function tickMovimentation() { //Função para a movimentação constante da peça
if(paused == 1){
continueMusic.pause();
return false;
}
else{
continueMusic.play();
if(checkColision(-1, 0, mainPiece.GoTetramino)){
drawPieceOnBoard();
mainPiece = nextPiece;
nextPiece = generatePiece((Math.floor(Math.random()*6)+1));
if(checkGameOver()){
return false;
}
else{
drawPiece(mainPiece);
drawNextPiece(nextPiece);
}
}
else{
deletePiece(); //apagar peça antes de mover
mainPiece.row--; //sobe a peça
drawPiece(); //desenha a peça no lugar novo
}
}
}
document.onkeydown = function(event) { //função para detectar as setas do teclado que sao pressionadas
if(gameState == 1){
return false;
}
else{
switch (event.keyCode) {
case 37: //se for a seta <
var arrow = 37;
arrowMovimentation(arrow);
break;
case 38: //se for a seta ^
arrow = 38;
arrowMovimentation(arrow);
break;
case 39: //se for a seta >
arrow = 39;
arrowMovimentation(arrow);
break;
case 40: //se for a seta para baixo
rotatePiece();
break;
case 67:
arrow = 67;
arrowMovimentation(arrow);
break;
case 80:
arrow = 80;
arrowMovimentation(arrow);
break;
}
}
};
function arrowMovimentation(arrow){ // funcao de movimentaçao horizontal da peça
if(arrow == 37)
{
if(checkColision(0, -1, mainPiece.GoTetramino)){
return false;
}
else{
deletePiece();
mainPiece.col--;
drawPiece();
}
}
else
if(arrow == 39)
{
if(checkColision(0, 1, mainPiece.GoTetramino)){
return false;
}
else{
deletePiece();
mainPiece.col++;
drawPiece();
}
}
else
if(arrow == 38)
{
if(checkColision(-1, 0, mainPiece.GoTetramino)){
drawPieceOnBoard();
mainPiece = nextPiece;
nextPiece = generatePiece((Math.floor(Math.random()*6)+1));
if(checkGameOver()){
return false;
}
else{
drawNextPiece(nextPiece);
}
return false;
}
else{
deletePiece();
mainPiece.row--;
drawPiece();
}
}
if(arrow == 40) //Funcao para girar a peca
{
if (mainPiece.TetraminoN > 3)//reseta o vetor
{
mainPiece.TetraminoN =0;
}
else //se nao for a ultima posicao da peca
{
deletePiece();
mainPiece.GoTetramino = mainPiece.Tetramino;
mainPiece.Tetramino[mainPiece.TetraminoN++];
drawPiece();
}
}
else
if(arrow == 67){
if(checkHoldedPiece == true){
deleteHoldedPiece(holdedPiece);
deletePiece();
mainPiece= holdedPiece;
drawPiece(mainPiece);
checkHoldedPiece = false;
}
else{
holdedPiece = mainPiece;
deletePiece();
drawHoldedPiece(holdedPiece);
mainPiece = nextPiece;
pieceCode = (Math.floor(Math.random()*6)+1);
nextPiece = generatePiece(pieceCode);
drawNextPiece(nextPiece);
drawPiece(mainPiece);
checkHoldedPiece = true;
}
}
else
if(arrow == 80){
pauseGame();
}
}
function checkColision(r, c, futurePiece){
for(row = 0 ; row < mainPiece.GoTetramino.length ; row++){
for(col = 0 ; col < mainPiece.GoTetramino.length ; col++){
if(futurePiece[row][col] != 0){
let nextRow;
let nextCol;
nextRow = row + r + mainPiece.row;
nextCol = col + c + mainPiece.col;
if(nextRow < 0 || nextCol < 0 || nextCol > NCOL){
return true;
}
if(main[nextRow][nextCol] != EMPTY_SQ){
return true;
}
else{
continue;
}
}
else{
continue;
}
}
}
return false;
}
function drawPieceOnBoard(){
for(row = 0 ; row < mainPiece.GoTetramino.length ; row++){
for(col = 0 ; col < mainPiece.GoTetramino.length ; col++){
if(mainPiece.GoTetramino[row][col] == 1){
main[row+mainPiece.row][col+mainPiece.col] = mainPiece.color;
mainBlocks.fillStyle = mainPiece.color ; //Define a cor do bloco gerado
mainBlocks.fillRect((mainPiece.col+col)*width_pixel, (row+mainPiece.row)*height_pixel, width_pixel, height_pixel);//Linha*tamDoBloco,Coluna*TamDoBloco, TamDoBloco,TamDoBloco
mainBlocks.strokeRect((mainPiece.col+col)*width_pixel, (row+mainPiece.row)*height_pixel, width_pixel, height_pixel);
}
}
}
checkRow();
}
function checkRow(){
var count = 0;
var rowsSequence=0;
for(row = 0 ; row < NROW ; row++){
for(col = 0 ; col < NCOL ; col++){ //percorre a matriz base inteira
if(main[row][col] != EMPTY_SQ){ //verifica se é diferente de vazio
count++;
}
}
if(count == NCOL){ // compara se a linha inteira está preenchida
eliminatedRows();
rowsSequence++;
for(lin = row; lin < NROW-1; lin++){
for(col = 0; col < NCOL; col ++){ // se foi preenchida
main[lin][col] = main[lin+1][col]; // coloca as colunas em branco
mainBlocks.fillStyle = main[lin][col]; //Define a cor do bloco gerado
mainBlocks.fillRect(col*width_pixel, lin*height_pixel, width_pixel, height_pixel);//Linha*tamDoBloco,Coluna*TamDoBloco, TamDoBloco,TamDoBloco
mainBlocks.strokeRect(col*width_pixel, lin*height_pixel, width_pixel, height_pixel);
}
}
count = 0;
row--;
}
else{
count = 0;
}
}
if(rowsSequence > 0){
points += (rowsSequence*10)*rowsSequence;
var display = "Points: " + points.toString();
document.getElementById("points").innerHTML = display;
controlSpeed += (rowsSequence*10)*rowsSequence;
if(controlSpeed/200 >= 1){
continueMusic.pause();
continueMusic.currentTime = 0;
levelUp.play();
up.pause();//pausar o up
up.currentTime = 0; //setar o up para 0
level++;
var display = "Level: " + level.toString();
document.getElementById("level").innerHTML = display;
gameSpeed = Math.floor(gameSpeed*0.5);
controlSpeed -= 200;
clearInterval(interval);
interval = setInterval(tickMovimentation, gameSpeed);
continueMusic.pause();
continueMusic.currentTime = 0;
}
}
}
function rotatePiece(){
let futureN = mainPiece.TetraminoN;
let futureTetramino = mainPiece.GoTetramino;
if(futureN == 3){
futureN = 0;
futureTetramino = mainPiece.Tetramino[futureN];
}
else{
futureN++;
futureTetramino = mainPiece.Tetramino[futureN];
}
if(checkColision(0, 0, futureTetramino)){
return false;
}
else{
deletePiece();
mainPiece.TetraminoN = futureN;
mainPiece.GoTetramino = futureTetramino;
drawPiece();
}
}
function gameTime()
{
if (gameState == 1 || paused == 1)
return false;
seconds++;
if (seconds%2){
time =seconds;
}
var display = "Time: " + seconds.toString() + " seconds";
document.getElementById("time").innerHTML = display;
return true;
}
function eliminatedRows(){
continueMusic.pause();
up.play();
rowscount++;
var display = "Eliminated rows: " + rowscount.toString();
document.getElementById("rows").innerHTML = display;
continueMusic.play();
}
/*Funcao para o ranking */
var name;
var time;
var cont=0;
//Criando arrow function como objeto pessoa
class Pessoa {
constructor() {
this.name = setName(name);
this.points = points;
this.level = level;
this.time = seconds;
}
};
//Funções para setar o valor dos atributos
function setName(){
name = prompt("Game Over !!! \nRegister to Rank: ");
return name;
}
function printData(){
continueMusic.pause();
gameOver.play();
continueMusic.currentTime = 0;
players.push (new Pessoa());//Adicionando Pessoas ao array Jogadores
//Funcao para ordenar o Vetor de jogadores a partir da maior pontução
players.sort((a, b) => (a.points < b.points) ? 1 : -1)
document.getElementById("dados").innerHTML = ""; //Limpa o campo dados antes de imprimir a lista
//Item Percorre a quantidade de jogadores imprimindo no html
players.forEach(item => {
document.getElementById("dados").innerHTML +=
'<li><b>Name: </b>'+item.name +
'<b> points: </b>'+item.points+
'<br><b> level: </b>'+item.level +
'<b> Time: </b>'+item.time+
'</li>';//Adicionar ponto aqu
});
return false;
}
function printData(){
continueMusic.pause();
gameOver.play();
continueMusic.currentTime = 0;
players.push (new Pessoa());//Adicionando Pessoas ao array Jogadores
//Funcao para ordenar o Vetor de jogadores a partir da maior pontução
players.sort((a, b) => (a.points < b.points) ? 1 : -1)
document.getElementById("dados").innerHTML = ""; //Limpa o campo dados antes de imprimir a lista
//Item Percorre a quantidade de jogadores imprimindo no html
players.forEach(item => {
document.getElementById("dados").innerHTML +=
'<li><b>Name: </b>'+item.name +
'<b> points: </b>'+item.points+
'<br><b> level: </b>'+item.level +
'<b> Time: </b>'+item.time+
'</li>';//Adicionar ponto aqu
});
return false;
}
function pauseGame(){
if(paused == 1){
paused = 0;
document.getElementById("button").innerHTML = "Pause game";
}
else{
paused = 1;
document.getElementById("button").innerHTML = "Continue game";
}
}
function instructWindow(){
if(activeInstruction == false){
document.getElementById("instructions").style.display = "block";
document.getElementById("button").disabled = true;
document.getElementById("button").style.cursor = "not-allowed";
activeInstruction = true;
if(paused == 1){
return true;
}
else{
paused = 1;
}
}
else{
document.getElementById("instructions").style.display = "none";
document.getElementById("button").disabled = false;
document.getElementById("button").style.cursor = "pointer";
activeInstruction = false;
if(document.getElementById("button").textContent == "Continue game"){
return false;
}
else{
paused = 0;
}
}
}