-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (59 loc) · 2.17 KB
/
app.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
const square = document.querySelectorAll('.square'); // Zbieranie wszystkich kwadratów
const score = document.getElementById('score'); // Element do wyświetlania wyniku
const timeLeft = document.getElementById('timeLeft'); // Element do wyświetlania pozostałego czasu
const emojis = ['🐞', '🐛', '🦗', '🐜']; // Array of bug emojis
let result = 0;
let currentTime = parseInt(timeLeft.textContent); // Ensure it's a number
let clickable = true;
let lastSqr;
let timerId;
// Uzyskiwanie dostępu do dźwięków
const hitSound = document.getElementById('hitSound');
const backgroundMusic = document.getElementById('backgroundMusic');
// Odtwarzanie muzyki w tle
backgroundMusic.play();
function randomSquare() {
square.forEach(className => {
className.textContent = ''; // Clear previous bugs
});
let randomPosition = square[Math.floor(Math.random() * square.length)];
randomPosition.textContent = emojis[Math.floor(Math.random() * emojis.length)]; // Randomly select an emoji
clickable = true;
hitPosition = randomPosition.id;
if (hitPosition === lastSqr) {
console.log("How dare you!");
return randomSquare();
}
lastSqr = hitPosition;
return hitPosition;
}
// Function to handle clicks on squares
function handleClick(id) {
if (id.id === hitPosition && clickable) {
id.textContent = ''; // Clear the emoji
result++;
score.textContent = result;
clickable = false;
hitSound.currentTime = 0; // Reset dźwięku do początku
hitSound.play(); // Odtwórz dźwięk przy trafieniu
}
}
// Event listeners for mouse and touch events
square.forEach(id => {
id.addEventListener('mouseup', () => handleClick(id));
id.addEventListener('touchstart', () => handleClick(id)); // Added touch event
});
function moveBug() {
timerId = setInterval(randomSquare, 1000);
}
moveBug();
function countDown() {
currentTime--;
timeLeft.textContent = currentTime;
if (currentTime === 0) {
clearInterval(timerId);
backgroundMusic.pause(); // Zatrzymaj muzykę w tle
alert(`GAME OVER! You crushed ${result} bugs.`);
}
}
timerId = setInterval(countDown, 1000);