-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
57 lines (50 loc) · 1.47 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
let food = 5;
let happiness = 5;
let energy = 5;
function saveToLocalStorage() {
const tamagotchiData = {
food: food,
happiness: happiness,
energy: energy,
};
localStorage.setItem("tamagotchiData", JSON.stringify(tamagotchiData));
}
function loadFromLocalStorage() {
const savedData = localStorage.getItem("tamagotchiData");
if (savedData) {
const tamagotchiData = JSON.parse(savedData);
food = tamagotchiData.food;
happiness = tamagotchiData.happiness;
energy = tamagotchiData.energy;
updateStats();
}
}
function updateStats() {
document.getElementById(
"stats"
).innerText = `Food: ${food}, Happiness: ${happiness}, Energy: ${energy}`;
saveToLocalStorage();
}
function feed() {
food = Math.min(10, food + 1);
happiness = Math.max(0, happiness - 1);
updateStats();
document.getElementById("status").innerText = "Status: Eating";
}
function play() {
happiness = Math.min(10, happiness + 1);
energy = Math.max(0, energy - 1);
updateStats();
document.getElementById("status").innerText = "Status: Playing";
}
function sleep() {
energy = Math.min(10, energy + 1);
food = Math.max(0, food - 1);
updateStats();
document.getElementById("status").innerText = "Status: Sleeping";
}
document.addEventListener("DOMContentLoaded", (event) => {
const statsElement = document.getElementById("stats");
loadFromLocalStorage();
updateStats();
});