-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
130 lines (109 loc) ยท 4.07 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
const bgsound = new Audio("./sounds/bg.mp3");
// Function to start the game
function startGame() {
// Close the popup
closePopup();
bgsound.play();
const places = document.querySelectorAll('.place');
const svgPaths = document.querySelectorAll('path');
const scoreElement = document.getElementById('score');
const timerElement = document.getElementById('timer');
let draggedPlace = null;
let score = 0;
let timer;
// Set the time limit in seconds
const timeLimit = 60; // Change this to the desired time limit in seconds
// Function to start the timer
function startTimer() {
let timeLeft = timeLimit;
timerElement.textContent = `Time: ${timeLeft}s`;
timer = setInterval(() => {
timeLeft--;
if (timeLeft < 0) {
clearInterval(timer);
showPopup(); // Call a function to show the popup when time is over
} else {
timerElement.textContent = `Time: ${timeLeft}s`;
}
}, 1000);
}
// Function to show the popup when time is over
function showPopup() {
alert('Time is over!'); // You can customize the popup content and styling
// You can also add code here to reset the game or perform other actions
}
places.forEach(place => {
place.addEventListener('dragstart', dragStart);
place.addEventListener('dragend', dragEnd);
});
svgPaths.forEach(path => {
path.addEventListener('dragover', dragOver);
path.addEventListener('dragenter', dragEnter);
path.addEventListener('dragleave', dragLeave);
path.addEventListener('drop', dragDrop);
});
// Start the timer when the page loads
startTimer();
function dragStart() {
this.className += ' hold';
setTimeout(() => (this.className = 'invisible'), 0);
draggedPlace = this;
}
function dragEnd() {
this.className = 'place';
draggedPlace = null;
}
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
this.className += ' hovered';
}
function dragLeave() {
this.className = '';
}
function dragDrop() {
const correctPlace = this.getAttribute('title');
const placeDataCorrect = draggedPlace.getAttribute('data-correct');
const badsound = new Audio("./sounds/bad.wav");
const goodsound = new Audio("./sounds/good.wav");
var good = document.getElementById('good');
var bad = document.getElementById('bad');
if (!draggedPlace.classList.contains('placed')) {
if (placeDataCorrect.toLowerCase() === correctPlace.toLowerCase()) {
// Change the color to green if it's the correct place
draggedPlace.style.backgroundColor = 'green';
good.style.display = "block";
goodsound.play();
setTimeout(() => {
good.style.display = 'none';
}, 2000); // 3000 milliseconds (3 seconds)
// Update the score
score++;
scoreElement.textContent = `Score: ${score}`;
} else {
// Change the color to red if it's not the correct place
draggedPlace.style.backgroundColor = 'red';
bad.style.display = "block";
badsound.play();
setTimeout(() => {
bad.style.display = 'none';
}, 2000); // 3000 milliseconds (3 seconds)
}
// Mark the place as "placed" and make it undraggable
draggedPlace.classList.add('placed');
draggedPlace.draggable = false;
}
}
}
// Function to open the popup
function openPopup() {
document.getElementById('popup').style.display = 'block';
}
// Function to close the popup
function closePopup() {
document.getElementById('popup').style.display = 'none';
}
// Call the openPopup function when the page loads to show the confirmation dialog
window.onload = openPopup;