Skip to content

Commit

Permalink
Add music,timer,localstorag
Browse files Browse the repository at this point in the history
  • Loading branch information
naikmubashir committed Oct 8, 2024
1 parent 1990f2d commit 4b398e0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 47 deletions.
Binary file added alexander-nakarada-chase(chosic.com).mp3
Binary file not shown.
145 changes: 98 additions & 47 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,109 @@
const holes = document.querySelectorAll('.hole');
const scoreBoard = document.querySelector('.score');
const startBtn = document.querySelector('.start-btn');
const moles = document.querySelectorAll('.mole');
let lastHole;
let timeUp = false;
let score = 0;
let countdownInterval;

const holes = document.querySelectorAll('.hole');
const scoreBoard = document.querySelector('.score');
const startBtn = document.querySelector('.start-btn');
const moles = document.querySelectorAll('.mole');
let lastHole;
let timeUp = false;
let flag=false;
let score = 0;

function randomTime(min, max) {
return Math.round(Math.random() * (max - min) + min);
// Create audio element for background music
const music = new Audio('alexander-nakarada-chase(chosic.com).mp3'); // Replace with your actual music file
music.loop = true; // Loop the music

function randomTime(min, max) {
return Math.round(Math.random() * (max - min) + min);
}

function randomHole(holes) {
const idx = Math.floor(Math.random() * holes.length);
const hole = holes[idx];
if (hole === lastHole) {
return randomHole(holes);
}
lastHole = hole;
return hole;
}

function peep() {
const time = randomTime(200, 1000);
const hole = randomHole(holes);
hole.classList.add('up');
setTimeout(() => {
hole.classList.remove('up');
if (!timeUp) peep();
}, time);
}

function startGame() {
startBtn.disabled = true;
scoreBoard.textContent = 0;
timeUp = false;
score = 0;

// Start the music
music.currentTime = 3; // Set the music start time
music.play();

function randomHole(holes) {
const idx = Math.floor(Math.random() * holes.length);
const hole = holes[idx];
if (hole === lastHole) {
console.log('Ah nah thats the same one bud');
return randomHole(holes);
let countdown = 30; // 30 seconds countdown
startBtn.textContent = `Time Left: ${countdown}s`;
startBtn.style.color = ''; // Reset color in case of restart

peep();

// Countdown logic
countdownInterval = setInterval(() => {
countdown--;

// Change text color to red for the last 10 seconds
if (countdown <= 10) {
startBtn.style.color = 'red';
} else {
startBtn.style.color = ''; // Default color
}
lastHole = hole;
return hole;
}

function peep() {
const time = randomTime(200, 1000);
const hole = randomHole(holes);
hole.classList.add('up');
setTimeout(() => {
hole.classList.remove('up');
if (!timeUp) peep();
}, time);
}
startBtn.textContent = `Time Left: ${countdown}s`;

function startGame() {
startBtn.disabled=true;
scoreBoard.textContent = 0;
timeUp = false;
score = 0;
peep();
setTimeout(() => {
startBtn.disabled=false;
timeUp = true}, 10000)
}
if (countdown <= 0) {
clearInterval(countdownInterval);
endGame();
}
}, 1000);

function bonk(e) {
if(!e.isTrusted) return; // cheater!
score++;
this.parentNode.classList.remove('up');
scoreBoard.textContent = score;
}
setTimeout(() => {
clearInterval(countdownInterval);
endGame();
}, 30000); // Game runs for 30 seconds
}


function endGame() {
startBtn.disabled = false;
timeUp = true;

// Stop the music
music.pause();

// Get the current high score from local storage
let highScore = localStorage.getItem('highScore') || 0;

// Check if the current score is higher than the high score
if (score > highScore) {
localStorage.setItem('highScore', score); // Update the high score
highScore = score; // Set high score to current score
}

// Display the final score and high score in the alert
alert(`Game ended! Your final score is: ${score}\nYour highest score is: ${highScore}`);

startBtn.textContent = 'Start Game';
startBtn.style.color = ''; // Reset the button color back to normal
}

moles.forEach(mole => mole.addEventListener('click', bonk));
function bonk(e) {
if (!e.isTrusted) return; // cheater!
score++;
this.parentNode.classList.remove('up');
scoreBoard.textContent = score;
}

moles.forEach(mole => mole.addEventListener('click', bonk));

0 comments on commit 4b398e0

Please sign in to comment.