-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
88 lines (77 loc) · 3.59 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
document.addEventListener('DOMContentLoaded', () => {
const hamster = document.getElementById('hamster');
const scoreElement = document.getElementById('score');
const energyElement = document.getElementById('energy');
const dailyRewardTimerElement = document.getElementById('daily-reward-timer');
const dailyCodeTimerElement = document.getElementById('daily-code-timer');
const comboTimerElement = document.getElementById('combo-timer');
// Получение сохраненного значения очков из Local Storage
let score = parseInt(localStorage.getItem('score')) || 0; // Установка начального значения очков
let currentEnergy = 3000;
const totalEnergy = 3000;
// Функция форматирования числа с пробелами
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}
// Обновление отображения начального значения очков
scoreElement.textContent = formatNumber(score);
function updateEnergyDisplay() {
energyElement.textContent = `${currentEnergy}/${totalEnergy}`;
}
function createFloatingScore(x, y) {
const floatingScore = document.createElement('div');
floatingScore.id = 'floating-score';
floatingScore.style.left = `${x}px`;
floatingScore.style.top = `${y}px`;
floatingScore.textContent = '+1';
document.body.appendChild(floatingScore);
setTimeout(() => {
floatingScore.remove();
}, 1000);
}
hamster.addEventListener('click', (event) => {
if (currentEnergy > 0) {
score++;
currentEnergy--;
scoreElement.textContent = formatNumber(score);
updateEnergyDisplay();
createFloatingScore(event.clientX, event.clientY);
localStorage.setItem('score', score); // Сохранение очков в Local Storage
}
});
setInterval(() => {
if (currentEnergy < totalEnergy) {
currentEnergy++;
updateEnergyDisplay();
}
}, 5000);
setInterval(() => {
score += 2000;
scoreElement.textContent = formatNumber(score);
localStorage.setItem('score', score); // Сохранение очков в Local Storage
}, 60000);
function startTimer(timerElement, initialTimeInSeconds, callback) {
let time = initialTimeInSeconds;
const interval = setInterval(() => {
if (time > 0) {
time--;
const minutes = Math.floor(time / 60);
const seconds = time % 60;
timerElement.textContent = `${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
} else {
callback();
clearInterval(interval);
startTimer(timerElement, initialTimeInSeconds, callback);
}
}, 1000);
}
function rewardCallback() {
const randomPoints = Math.floor(Math.random() * 10001) + 5000;
score += randomPoints;
scoreElement.textContent = formatNumber(score);
localStorage.setItem('score', score); // Сохранение очков в Local Storage
}
startTimer(dailyRewardTimerElement, 750, rewardCallback); // 12:30 in seconds
startTimer(dailyCodeTimerElement, 450, rewardCallback); // 07:30 in seconds
startTimer(comboTimerElement, 30, rewardCallback); // 00:30 in seconds
});