-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.js
392 lines (339 loc) · 11.7 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
const canvas = document.getElementById('tetris-board');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const congratulationsBanner = document.getElementById('congratulations-banner');
let BLOCK_SIZE;
let ROWS;
let COLS;
let isPaused = false;
let currentTheme = 'rgb';
const pauseMenu = document.getElementById('pause-menu');
const settingsMenu = document.getElementById('settings-menu');
const resumeBtn = document.getElementById('resume-btn');
const settingsBtn = document.getElementById('settings-btn');
const backBtn = document.getElementById('back-btn');
const rgbTheme = document.getElementById('rgb-theme');
const darkTheme = document.getElementById('dark-theme');
const lightTheme = document.getElementById('light-theme');
function togglePause() {
isPaused = !isPaused;
if (isPaused) {
pauseMenu.style.display = 'block';
} else {
pauseMenu.style.display = 'none';
settingsMenu.style.display = 'none';
}
}
function showSettings() {
pauseMenu.style.display = 'none';
settingsMenu.style.display = 'block';
}
function hideSettings() {
settingsMenu.style.display = 'none';
pauseMenu.style.display = 'block';
}
function changeTheme(theme) {
currentTheme = theme;
document.body.className = theme === 'rgb' ? 'rgb-glow' : `${theme}-theme`;
}
resumeBtn.addEventListener('click', togglePause);
settingsBtn.addEventListener('click', showSettings);
backBtn.addEventListener('click', hideSettings);
rgbTheme.addEventListener('click', () => changeTheme('rgb'));
darkTheme.addEventListener('click', () => changeTheme('dark'));
lightTheme.addEventListener('click', () => changeTheme('light'));
function resizeGame() {
const gameContainer = document.getElementById('game-container');
const containerWidth = gameContainer.clientWidth;
const containerHeight = gameContainer.clientHeight;
// Ajustar el tamaño del tablero para que ocupe el 80% de la altura disponible
const boardHeight = Math.floor(containerHeight * 0.8);
const boardWidth = Math.floor(boardHeight / 2);
canvas.width = boardWidth;
canvas.height = boardHeight;
BLOCK_SIZE = Math.floor(boardHeight / 20);
ROWS = Math.floor(boardHeight / BLOCK_SIZE);
COLS = Math.floor(boardWidth / BLOCK_SIZE);
// Reiniciar el juego con las nuevas dimensiones
resetGame();
}
// Llamar a resizeGame cuando la ventana cambie de tamaño
window.addEventListener('resize', resizeGame);
const nextPieceCanvas = document.getElementById('next-piece-canvas');
const nextPieceCtx = nextPieceCanvas.getContext('2d');
let score = 0;
let board = Array(ROWS).fill().map(() => Array(COLS).fill(0));
let currentFigure = null;
let currentColor = '';
let currentX = 0;
let currentY = 0;
let nextFigure = null;
let nextColor = '';
// Definimos los pesos para cada pieza
const PIECE_WEIGHTS = [
1.11, // Pieza I (11% más probable)
1, // Pieza O
1, // Pieza Z
1, // Pieza S
1, // Pieza J
1, // Pieza L
1 // Pieza T
];
// Calculamos la suma total de los pesos
const TOTAL_WEIGHT = PIECE_WEIGHTS.reduce((sum, weight) => sum + weight, 0);
function newFigure() {
if (nextFigure === null) {
// Si es la primera vez, generamos tanto la figura actual como la siguiente
[currentFigure, currentColor] = getRandomFigure();
[nextFigure, nextColor] = getRandomFigure();
} else {
// Si ya teníamos una figura siguiente, la usamos y generamos una nueva siguiente
[currentFigure, currentColor] = [nextFigure, nextColor];
[nextFigure, nextColor] = getRandomFigure();
}
currentX = Math.floor(COLS / 2) - Math.floor(currentFigure[0].length / 2);
currentY = 0;
drawNextPiece();
}
function getRandomFigure() {
// Generamos un número aleatorio entre 0 y el peso total
const randomWeight = Math.random() * TOTAL_WEIGHT;
let weightSum = 0;
let chosenIndex = 0;
// Recorremos los pesos hasta encontrar la pieza seleccionada
for (let i = 0; i < PIECE_WEIGHTS.length; i++) {
weightSum += PIECE_WEIGHTS[i];
if (randomWeight <= weightSum) {
chosenIndex = i;
break;
}
}
return [FIGURES[chosenIndex].shape, FIGURES[chosenIndex].color];
}
function drawNextPiece() {
nextPieceCtx.clearRect(0, 0, nextPieceCanvas.width, nextPieceCanvas.height);
const blockSize = 30;
const offsetX = (nextPieceCanvas.width - nextFigure[0].length * blockSize) / 2;
const offsetY = (nextPieceCanvas.height - nextFigure.length * blockSize) / 2;
nextPieceCtx.fillStyle = nextColor;
for (let y = 0; y < nextFigure.length; y++) {
for (let x = 0; x < nextFigure[y].length; x++) {
if (nextFigure[y][x]) {
nextPieceCtx.fillRect(offsetX + x * blockSize, offsetY + y * blockSize, blockSize, blockSize);
nextPieceCtx.strokeStyle = 'black';
nextPieceCtx.strokeRect(offsetX + x * blockSize, offsetY + y * blockSize, blockSize, blockSize);
}
}
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Dibujar el tablero
for (let y = 0; y < ROWS; y++) {
for (let x = 0; x < COLS; x++) {
if (board[y][x]) {
ctx.fillStyle = board[y][x]; // Usar el color almacenado en el tablero
ctx.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
ctx.strokeStyle = 'black';
ctx.strokeRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
// Dibujar la figura actual
if (currentFigure) {
ctx.fillStyle = currentColor;
for (let y = 0; y < currentFigure.length; y++) {
for (let x = 0; x < currentFigure[y].length; x++) {
if (currentFigure[y][x]) {
ctx.fillRect((currentX + x) * BLOCK_SIZE, (currentY + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
ctx.strokeStyle = 'black';
ctx.strokeRect((currentX + x) * BLOCK_SIZE, (currentY + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
}
}
}
}
}
function moveDown() {
if (canMove(currentX, currentY + 1)) {
currentY++;
} else {
freeze();
clearLines();
newFigure();
if (!canMove(currentX, currentY)) {
// Juego terminado
alert('¡Juego terminado! Tu puntuación: ' + score);
resetGame();
}
}
draw();
}
function canMove(newX, newY, piece = currentFigure) {
for (let y = 0; y < piece.length; y++) {
for (let x = 0; x < piece[y].length; x++) {
if (piece[y][x]) {
if (newY + y >= ROWS || newX + x < 0 || newX + x >= COLS ||
(newY + y >= 0 && board[newY + y][newX + x])) {
return false;
}
}
}
}
return true;
}
function freeze() {
for (let y = 0; y < currentFigure.length; y++) {
for (let x = 0; x < currentFigure[y].length; x++) {
if (currentFigure[y][x]) {
if (currentY + y >= 0 && currentY + y < ROWS && currentX + x >= 0 && currentX + x < COLS) {
board[currentY + y][currentX + x] = currentColor;
}
}
}
}
}
function clearLines() {
let linesCleared = 0;
for (let y = ROWS - 1; y >= 0; y--) {
if (board[y].every(cell => cell !== 0)) {
// Eliminar la línea completa
board.splice(y, 1);
// Añadir una nueva línea vacía en la parte superior
board.unshift(Array(COLS).fill(0));
// Incrementar el contador de líneas eliminadas
linesCleared++;
// Como hemos eliminado una línea, necesitamos revisar la misma y de nuevo
y++;
}
}
// Actualizar la puntuación basada en el número de líneas eliminadas
if (linesCleared > 0) {
updateScore(linesCleared * 100);
}
}
function updateScore(points) {
score += points;
scoreElement.textContent = score;
// Mostrar el banner cada 1000 puntos
if (score % 1000 === 0 && score > 0) {
showCongratulationsBanner();
}
}
function showCongratulationsBanner() {
congratulationsBanner.style.display = 'block';
congratulationsBanner.style.opacity = '1';
congratulationsBanner.style.transition = 'opacity 0.5s ease-in-out';
setTimeout(() => {
congratulationsBanner.style.opacity = '0';
setTimeout(() => {
congratulationsBanner.style.display = 'none';
}, 500);
}, 2500);
}
function resetGame() {
board = Array(ROWS).fill().map(() => Array(COLS).fill(0));
score = 0;
scoreElement.textContent = score;
newFigure();
}
document.addEventListener('keydown', (e) => {
if (!isPaused) {
switch (e.key) {
case 'ArrowLeft':
if (canMove(currentX - 1, currentY)) {
currentX--;
draw();
}
break;
case 'ArrowRight':
if (canMove(currentX + 1, currentY)) {
currentX++;
draw();
}
break;
case 'ArrowDown':
moveDown();
break;
case 'ArrowUp':
tryRotate(1); // Rotación en sentido horario
break;
case 'z':
case 'Z':
tryRotate(-1); // Rotación en sentido antihorario
break;
}
}
if (e.key === 'Escape') {
if (settingsMenu.style.display === 'block') {
hideSettings();
} else {
togglePause();
}
}
});
function tryRotate(dir) {
const rotated = rotate(currentFigure, dir);
const kicks = getWallKicks(currentFigure, rotated);
for (let [dx, dy] of kicks) {
if (canMove(currentX + dx, currentY + dy, rotated)) {
currentFigure = rotated;
currentX += dx;
currentY += dy;
draw();
return;
}
}
}
function rotate(matrix, dir) {
const N = matrix.length;
const M = matrix[0].length;
const rotated = Array(M).fill().map(() => Array(N).fill(0));
if (dir > 0) { // Rotación en sentido horario
for (let y = 0; y < N; y++) {
for (let x = 0; x < M; x++) {
rotated[x][N - 1 - y] = matrix[y][x];
}
}
} else { // Rotación en sentido antihorario
for (let y = 0; y < N; y++) {
for (let x = 0; x < M; x++) {
rotated[M - 1 - x][y] = matrix[y][x];
}
}
}
return rotated;
}
function getWallKicks(oldPiece, newPiece) {
// Implementa aquí la lógica de "wall kicks" según las reglas del Tetris moderno
// Por ahora, usaremos una versión simplificada
return [[0, 0], [-1, 0], [1, 0], [0, -1]];
}
function gameLoop() {
if (!isPaused) {
moveDown();
}
setTimeout(gameLoop, 1000);
}
// Asegúrate de llamar a resizeGame al inicio del juego
resizeGame();
gameLoop();
draw();
// Almacena el puntaje en una cookie
function setHighScore(score) {
document.cookie = `highScore=${score}; expires=Fri, 31 Dec 9999 23:59:59 GMT`;
}
// Obtiene el puntaje más alto almacenado en la cookie
function getHighScore() {
const cookie = document.cookie.split(';').find(cookie => cookie.includes('highScore'));
if (cookie) {
return parseInt(cookie.split('=')[1]);
}
return 0;
}
// Actualiza el puntaje más alto si es necesario
function updateHighScore(score) {
const highScore = getHighScore();
if (score > highScore) {
setHighScore(score);
}
}