startit/team/oppgaver/uke4/script.js

83 lines
2 KiB
JavaScript
Raw Normal View History

2024-08-28 12:17:11 +02:00
// Model
let foodTime = 10;
let petTime = 10;
let foodElement = document.getElementById("foodId");
let petElement = document.getElementById('petId');
let frogImage = document.getElementById('tamaFrog').src = "img/default.jpg";
2024-08-28 14:31:18 +02:00
let foodCounter = setInterval(foodFunction, 1000);
let petCounter = setInterval(petFunction, 1000);
2024-08-28 12:17:11 +02:00
// View
2024-08-28 14:31:18 +02:00
function reset() {
if (foodTime < 0){
defaultImg();
foodTime = 50;
setInterval(foodFunction, 1000);
petTime = 10;
setInterval(petFunction, 1000);
}
}
2024-08-28 12:17:11 +02:00
function foodFunction() {
if (foodTime < 0) {
//game over???
document.getElementById('tamaFrog').src = "img/dead.jpg";
2024-08-28 14:31:18 +02:00
clearInterval(foodCounter);
setTimeout(afterLife, 5000);
2024-08-28 12:17:11 +02:00
} else {
foodElement.innerHTML = foodTime + " seconds remaining"
foodTime--
2024-08-28 14:12:05 +02:00
if (foodTime < 15) {
2024-08-28 12:17:11 +02:00
document.getElementById('tamaFrog').src = "img/need_food.jpg";
}
}
}
// Controller
function feed() {
2024-08-28 14:12:05 +02:00
if (foodTime > 0 && petTime > 5) {
2024-08-28 12:17:11 +02:00
foodTime += 10;
2024-08-28 14:31:18 +02:00
petElement.innerHTML = petTime + " seconds remaining." + " Pet the frog";
2024-08-28 12:17:11 +02:00
document.getElementById('tamaFrog').src = "img/eating.jpg";
setTimeout(defaultImg, 500)
}
}
function defaultImg() {
document.getElementById('tamaFrog').src = "img/default.jpg";
}
function petFunction() {
2024-08-28 14:31:18 +02:00
if (petTime < 0) {
clearInterval(petCounter);
} else if (petTime < 5) {
2024-08-28 14:12:05 +02:00
document.getElementById('tamaFrog').src = "img/angry.jpg";
2024-08-28 14:31:18 +02:00
petElement.innerHTML = petTime + " seconds remaining"
petTime--;
2024-08-28 14:12:05 +02:00
}
else {
petElement.innerHTML = petTime + " seconds remaining"
2024-08-28 14:31:18 +02:00
petTime--
2024-08-28 12:17:11 +02:00
}
}
2024-08-28 14:31:18 +02:00
2024-08-28 12:17:11 +02:00
function pet() {
2024-08-28 14:12:05 +02:00
if (foodTime > 0) {
2024-08-28 12:17:11 +02:00
petTime += 10;
2024-08-28 14:12:05 +02:00
document.getElementById('tamaFrog').src = "img/need_love.jpg";
2024-08-28 12:17:11 +02:00
setTimeout(defaultImg, 500)
}
2024-08-28 14:12:05 +02:00
2024-08-28 12:17:11 +02:00
}
function defaultImg() {
document.getElementById('tamaFrog').src = "img/default.jpg";
}
function afterLife() {
document.getElementById('tamaFrog').src = "img/ghost.jpg";
}