-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (108 loc) · 3.99 KB
/
script.js
File metadata and controls
122 lines (108 loc) · 3.99 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
document.addEventListener('DOMContentLoaded', () => {
const holes = document.querySelectorAll('.hole');
const scoreBoard = document.getElementById('score');
const highScoreBoard = document.getElementById('highScore');
const startButton = document.getElementById('startButton');
const resetButton = document.getElementById('resetButton');
const timer = document.getElementById('timer');
let score = 0;
let highScore = localStorage.getItem('highScore') || 0;
highScoreBoard.textContent = highScore;
let moleInterval;
let gameDuration = 30; // 30 seconds
let moleSpeed = 1000; // Interval for mole appearance in milliseconds
startButton.addEventListener('click', startGame);
resetButton.addEventListener('click', resetGame);
let timerInterval;
function startGame() {
score = 0;
scoreBoard.textContent = score;
timer.textContent = gameDuration;
clearInterval(moleInterval); // Ensure no previous game is running
moleInterval = setInterval(randomMole, moleSpeed);
// Start the timer countdown
startTimer();
}
function startTimer() {
let countdown = gameDuration;
timerInterval = setInterval(() => {
countdown--;
timer.textContent = countdown;
if (countdown <= 0) {
clearInterval(timerInterval);
endGame();
}
}, 1000); // Update timer every second (1000 milliseconds)
}
function endGame() {
clearInterval(moleInterval);
alert('Game Over! Your score is: ' + score);
if (score > highScore) {
highScore = score;
localStorage.setItem('highScore', highScore);
highScoreBoard.textContent = highScore;
}
}
function resetGame() {
clearInterval(moleInterval);
clearInterval(timerInterval); // Clear any running timer
holes.forEach(hole => {
hole.innerHTML = '';
});
score = 0;
scoreBoard.textContent = score;
gameDuration = 30; // Reset game duration to 30 seconds
timer.textContent = gameDuration;
}
function randomMole() {
holes.forEach(hole => {
hole.innerHTML = '';
});
const randomHole = holes[Math.floor(Math.random() * holes.length)];
const mouse = document.createElement('img');
mouse.src = 'mouse.png'; // Path to your mouse image
mouse.classList.add('mouse');
randomHole.appendChild(mouse);
setTimeout(() => {
mouse.style.bottom = '0';
}, 10);
// Add bomb with a certain probability
if (Math.random() < 0.3) { // 30% chance to add a bomb
const bomb = document.createElement('img');
bomb.src = 'bomb.png'; // Path to your bomb image
bomb.classList.add('mouse'); // Reuse mouse class for animation
randomHole.appendChild(bomb);
setTimeout(() => {
bomb.style.bottom = '0';
}, 10);
bomb.addEventListener('click', () => {
endGameWithBomb();
});
}
mouse.addEventListener('click', () => {
score++;
scoreBoard.textContent = score;
mouse.style.bottom = '-50%';
setTimeout(() => randomHole.innerHTML = '', 300);
});
setTimeout(() => {
if (mouse.style.bottom === '0') {
mouse.style.bottom = '-50%';
setTimeout(() => randomHole.innerHTML = '', 300);
}
}, moleSpeed);
}
function endGameWithBomb() {
clearInterval(moleInterval);
clearInterval(timerInterval);
alert('Game Over! You clicked the bomb.');
holes.forEach(hole => {
hole.innerHTML = '';
});
if (score > highScore) {
highScore = score;
localStorage.setItem('highScore', highScore);
highScoreBoard.textContent = highScore;
}
}
});