-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
225 lines (191 loc) · 7.38 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
// Axel Cotón Gutiérrez Copyright 2024
document.addEventListener('DOMContentLoaded', (event) => {
initPuzzle();
document.getElementById('check-button').addEventListener('click', checkPuzzle);
document.getElementById('next-question').addEventListener('click', resetGame);
});
let remainingPieces = []; // Almacenar las piezas restantes
let draggedElement = null; // Variable global para almacenar el elemento arrastrado
function initPuzzle() {
const puzzlePieces = document.getElementById('puzzle-pieces');
const puzzleBoard = document.getElementById('puzzle-board');
const imageSrc = 'calendario.jpg';
const numRows = 3;
const numCols = 3;
let pieces = [];
// Crear piezas del puzzle y almacenarlas en un array
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
let piece = document.createElement('div');
piece.classList.add('puzzle-piece');
piece.id = 'piece-' + row + '-' + col;
piece.style.backgroundImage = `url(${imageSrc})`;
pieces.push(piece);
}
}
// Añadir temporalmente una pieza al DOM para calcular su tamaño
let tempPiece = pieces[0].cloneNode();
puzzlePieces.appendChild(tempPiece);
let rect = tempPiece.getBoundingClientRect();
puzzlePieces.removeChild(tempPiece);
const pieceWidth = rect.width;
const pieceHeight = rect.height;
// Configurar piezas del puzzle
pieces.forEach(piece => {
const col = parseInt(piece.id.split('-')[2], 10);
const row = parseInt(piece.id.split('-')[1], 10);
piece.style.backgroundPosition = `-${col * pieceWidth}px -${row * pieceHeight}px`;
piece.style.width = `${pieceWidth}px`;
piece.style.height = `${pieceHeight}px`;
piece.draggable = true;
piece.addEventListener('dragstart', dragStart);
});
// Desordenar y mostrar las primeras piezas
shuffleArray(pieces);
for (let i = 0; i < pieces.length; i++) {
if (i < 3) {
puzzlePieces.appendChild(pieces[i]);
} else {
remainingPieces.push(pieces[i]);
}
}
// Crear slots para el puzzle
for (let i = 0; i < numRows * numCols; i++) {
let slot = document.createElement('div');
slot.classList.add('puzzle-slot');
slot.id = 'slot-' + Math.floor(i / numCols) + '-' + (i % numCols);
slot.style.width = `${pieceWidth}px`;
slot.style.height = `${pieceHeight}px`;
slot.addEventListener('dragover', dragOver);
slot.addEventListener('drop', drop);
puzzleBoard.appendChild(slot);
}
// Añadir eventos táctiles
addTouchEventsToPieces();
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function dragStart(event) {
draggedElement = event.target; // Almacenar la referencia al elemento arrastrado
event.dataTransfer.setData('text/plain', event.target.id); // Usar el ID como dato de transferencia
}
function dragOver(event) {
event.preventDefault();
}
function drop(event) {
event.preventDefault();
const slot = event.target.closest('.puzzle-slot');
const draggableId = event.dataTransfer.getData('text/plain');
const draggableElement = document.getElementById(draggableId);
if (slot && !slot.firstChild) {
slot.appendChild(draggableElement);
if (remainingPieces.length > 0) {
addNextPiece();
}
}
}
function addNextPiece() {
const nextPiece = remainingPieces.shift();
document.getElementById('puzzle-pieces').appendChild(nextPiece);
}
function checkPuzzle() {
let correctCount = 0;
const numRows = 3;
const numCols = 3;
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
let slot = document.getElementById('slot-' + row + '-' + col);
let piece = slot.firstChild;
if (piece && piece.id === 'piece-' + row + '-' + col) {
correctCount++;
}
}
}
const resultDisplay = document.getElementById('result');
if (correctCount === numRows * numCols) {
resultDisplay.textContent = '¡Correcto! Todas las piezas están en el lugar adecuado.';
resultDisplay.style.color = 'green';
} else {
resultDisplay.textContent = `Algunas piezas están en el lugar incorrecto. Intenta de nuevo. (${correctCount} de ${numRows * numCols} correctas)`;
resultDisplay.style.color = 'red';
}
}
function resetGame() {
const puzzleBoard = document.getElementById('puzzle-board');
const puzzlePieces = document.getElementById('puzzle-pieces');
puzzleBoard.innerHTML = '';
puzzlePieces.innerHTML = '';
const resultDisplay = document.getElementById('result');
resultDisplay.textContent = '';
initPuzzle();
}
function touchStart(e) {
draggedElement = e.target;
const touchLocation = e.targetTouches[0];
offsetX = touchLocation.pageX - draggedElement.offsetLeft;
offsetY = touchLocation.pageY - draggedElement.offsetTop;
e.preventDefault();
}
function touchMove(e) {
if (draggedElement) {
const touchLocation = e.targetTouches[0];
draggedElement.style.position = 'absolute';
draggedElement.style.left = `${touchLocation.pageX - offsetX}px`;
draggedElement.style.top = `${touchLocation.pageY - offsetY}px`;
e.preventDefault();
}
}
function touchEnd(e) {
if (draggedElement) {
const slots = document.querySelectorAll('.puzzle-slot');
let targetSlot = null;
slots.forEach(slot => {
if (isElementOverlapping(draggedElement, slot)) {
targetSlot = slot;
}
});
if (targetSlot && !targetSlot.firstChild) {
targetSlot.appendChild(draggedElement);
draggedElement.style.position = 'static';
if (remainingPieces.length > 0) {
addNextPiece();
}
} else {
draggedElement.style.position = 'static';
}
draggedElement = null;
}
}
function isElementOverlapping(element1, element2) {
const rect1 = element1.getBoundingClientRect();
const rect2 = element2.getBoundingClientRect();
const centerX = rect1.left + rect1.width / 2;
const centerY = rect1.top + rect1.height / 2;
return centerX > rect2.left && centerX < rect2.right && centerY > rect2.top && centerY < rect2.bottom;
}
function addNextPiece() {
const nextPiece = remainingPieces.shift();
document.getElementById('puzzle-pieces').appendChild(nextPiece);
addTouchEventsToPiece(nextPiece); // Añadir eventos táctiles a la nueva pieza
}
function addTouchEventsToPiece(piece) {
piece.addEventListener('touchstart', touchStart, { passive: false });
piece.addEventListener('touchmove', touchMove, { passive: false });
piece.addEventListener('touchend', touchEnd);
}
function addTouchEventsToPieces() {
const pieces = document.querySelectorAll('.puzzle-piece');
pieces.forEach(addTouchEventsToPiece);
}
// Navegaciòn"
document.addEventListener('DOMContentLoaded', function() {
var menuToggle = document.querySelector('.menu-toggle');
var menu = document.querySelector('.menu');
menuToggle.addEventListener('click', function() {
menu.classList.toggle('active');
});
});