-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgame2.js
More file actions
42 lines (35 loc) · 1.12 KB
/
Copy pathgame2.js
File metadata and controls
42 lines (35 loc) · 1.12 KB
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
let score = 0;
// Add drag events to blocks
const blocks = document.querySelectorAll('.block');
blocks.forEach(block => {
block.addEventListener('dragstart', dragStart);
});
function dragStart(event) {
event.dataTransfer.setData('text', event.target.id);
}
// Add drop events to test tubes
const testTubes = document.querySelectorAll('.test-tube');
testTubes.forEach(tube => {
tube.addEventListener('dragover', dragOver);
tube.addEventListener('drop', drop);
});
function dragOver(event) {
event.preventDefault(); // Allow dropping
}
function drop(event) {
const draggedBlockId = event.dataTransfer.getData('text');
const draggedBlock = document.getElementById(draggedBlockId);
const correctTubeId = draggedBlockId + '-tube';
if (event.target.id === correctTubeId) {
// Correct drop
event.target.appendChild(draggedBlock);
score += 10;
updateScore();
} else {
// Incorrect drop
alert('Incorrect! Try again.');
}
}
function updateScore() {
document.getElementById('score').innerText = `Score: ${score}`;
}