-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogic.js
114 lines (94 loc) · 3.29 KB
/
logic.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
const fruits = ['🍍', '🍏', '🍌', '🍊', '🍉', '🍈', '🍇'];
let score = 0;
let timeLeft = 30;
let gameInterval, timerInterval;
function randomPosition() {
const gameArea = document.getElementById('gameArea');
const x = Math.random() * (gameArea.clientWidth - 50);
const y = Math.random() * (gameArea.clientHeight - 50);
return { x, y };
}
function createFruit() {
const fruitElement = document.createElement('div');
const { x, y } = randomPosition();
const randomFruit = fruits[Math.floor(Math.random() * fruits.length)];
fruitElement.innerText = randomFruit;
fruitElement.classList.add('fruit');
fruitElement.style.left = `${x}px`;
fruitElement.style.top = `${y}px`;
fruitElement.addEventListener('click', () => {
score++;
document.getElementById('score').innerText = `Score: ${score}`;
playSliceSound();
createFruitPieces(fruitElement, x, y);
setTimeout(() => {
fruitElement.remove();
createFruit();
}, 500);
});
document.getElementById('fruits').appendChild(fruitElement);
}
function createFruitPieces(fruitElement, x, y) {
const pieceCount = 3;
for (let i = 0; i < pieceCount; i++) {
const piece = document.createElement('div');
piece.innerText = fruitElement.innerText;
piece.classList.add('fruit-piece');
piece.style.left = `${x + (Math.random() * 40 - 20)}px`;
piece.style.top = `${y + (Math.random() * 40 - 20)}px`;
document.getElementById('fruits').appendChild(piece);
setTimeout(() => {
piece.style.transform = `translateY(${Math.random() * 100}px) rotate(${Math.random() * 360}deg)`;
piece.style.opacity = '0';
}, 10);
setTimeout(() => {
piece.remove();
}, 1000);
}
}
function startGame() {
score = 0;
timeLeft = 30;
document.getElementById('score').innerText = `Score: ${score}`;
document.getElementById('timer').innerText = `Time: ${timeLeft}`;
document.getElementById('endGame').classList.add('hidden');
createFruit();
gameInterval = setInterval(createFruit, 2000);
timerInterval = setInterval(updateTimer, 1000);
playBackgroundMusic();
}
function updateTimer() {
timeLeft--;
document.getElementById('timer').innerText = `Time: ${timeLeft}`;
if (timeLeft <= 0) {
endGame();
}
}
function endGame() {
clearInterval(gameInterval);
clearInterval(timerInterval);
document.getElementById('finalScore').innerText = score;
document.getElementById('endGame').classList.remove('hidden');
stopBackgroundMusic();
}
function restartGame() {
const fruitsContainer = document.getElementById('fruits');
while (fruitsContainer.firstChild) {
fruitsContainer.removeChild(fruitsContainer.firstChild);
}
startGame();
}
function playSliceSound() {
const sliceSound = document.getElementById('sliceSound');
sliceSound.currentTime = 0;
sliceSound.play();
}
function playBackgroundMusic() {
const backgroundMusic = document.getElementById('backgroundMusic');
backgroundMusic.play();
}
function stopBackgroundMusic() {
const backgroundMusic = document.getElementById('backgroundMusic');
backgroundMusic.pause();
}
startGame();