Skip to content

Commit

Permalink
Refactor game logic and timer
Browse files Browse the repository at this point in the history
  • Loading branch information
MA-Abahmane committed Mar 14, 2024
1 parent 2589b65 commit 0c36df8
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ let board = document.querySelector(".outer");
let table = document.querySelector(".table");


let Game0N = false
let TIME = 5 * 60
let onTable = 0

//pre-loader
window.addEventListener("load", () => {
const preloader = document.querySelector(".preloader");
Expand All @@ -12,15 +16,14 @@ window.addEventListener("load", () => {
}, 2000)
});

let onTable = 0

//this function triggers when you click play
function StartGame() {
if (parseInt(points.innerHTML) <= 0) {
if (parseInt(points.innerHTML) <= 0) { // if no spades left
score.innerHTML = 'No spades left '
board.style.display = "flex";
clearInterval(idItr);
} else if (onTable == 0) {
} else if (onTable == 0) { // if no spades on table
// blinking border
table.style.border = '5px dashed red'

Expand All @@ -36,25 +39,26 @@ function StartGame() {
table.style.border = '5px dashed transparent'
}, 1500)
} else {
Game0N = true
Showball();
// setTimeout(Showball,);
setTimeout(shuffling, 7000);
}
}


//this function lifts the thimbles
function thimbleup(x) {
x.classList.add("thimbleup");
}

//this function puts the thimble down
function thimbledown(x) {
x.classList.remove("thimbleup");
}

//this function selects one thimble at random and positions the ball under it and lifts it at the beginning
let timeLeft = 1 * 60; // 5 minutes in seconds
//this function selects one thimble at random and positions the ball
// under it and lifts it at the beginning
let idItr
let timeLeft = TIME ; // 5 minutes in seconds
function Showball() {
document.getElementById("Playbutton").style.pointerEvents = "none";
let rand = getRandNum();
Expand All @@ -79,22 +83,25 @@ function Showball() {
// Start Timer
if (countdownEl.innerHTML == '05:00') {
idItr = setInterval(() => {

if (timeLeft <= 0) {
clearInterval(idItr);
board.style.display = "flex";
score.innerHTML = 'Time\'s up! You Won ' + points.innerHTML + ' '
if (!Game0N) {
setTimeout(() => {
clearInterval(idItr);
board.style.display = "flex";
score.innerHTML = 'Time\'s up! You Won ' + points.innerHTML + ' '
clearInterval(idItr);
}, 2000)
}
} else {
let minutes = Math.floor(timeLeft / 60);
let seconds = timeLeft % 60;

minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;

countdownEl.innerHTML = `${minutes}:${seconds}`;
timeLeft--;
}

let minutes = Math.floor(timeLeft / 60);
let seconds = timeLeft % 60;

minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;

countdownEl.innerHTML = `${minutes}:${seconds}`;
timeLeft--;

}, 1000);
}
}
Expand Down Expand Up @@ -234,13 +241,14 @@ function selectthimble(x) {
}, 2500); //bring the winning thimble down after 2.5secs
document.getElementById("Playbutton").style.pointerEvents = "all"; //make the play button clickable again
}
Game0N = false
}, 2000);
}


function setCup(id) {
let elements = document.querySelectorAll(".sewing_thimble");
for (element of elements) {
for (let element of elements) {
element.style.background = `url("Img/c${id}.png") no-repeat fixed center`;
element.style.backgroundSize = `contain`;
}
Expand Down Expand Up @@ -270,4 +278,11 @@ function init() {
points.innerHTML = 200;
board.style.display = "none";
countdownEl.innerHTML = '05:00'
// Clear all intervals and timeouts
InID = setInterval(() => {}, 10)
TiID = setTimeout(() => {}, 10)
while (InID--) clearInterval(InID)
while (TiID--) clearTimeout(TiID)
timeLeft = TIME

}

0 comments on commit 0c36df8

Please sign in to comment.