-
Notifications
You must be signed in to change notification settings - Fork 41.8k
Expand file tree
/
Copy pathindex-START.html
More file actions
195 lines (171 loc) · 4.49 KB
/
index-START.html
File metadata and controls
195 lines (171 loc) · 4.49 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Whack A Mole!</title>
<link href='https://fonts.googleapis.com/css?family=Amatic+SC:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="style.css">
<link rel="icon" href="https://fav.farm/🔥" />
</head>
<body>
<h1>Whack-a-mole! <span class="score">0</span></h1>
<button onClick="startGame()">Start!</button>
<button id="reset-score-btn">Reset Score</button>
<div class="game">
<div class="hole hole1">
<div class="mole"></div>
</div>
<div class="hole hole2">
<div class="mole"></div>
</div>
<div class="hole hole3">
<div class="mole"></div>
</div>
<div class="hole hole4">
<div class="mole"></div>
</div>
<div class="hole hole5">
<div class="mole"></div>
</div>
<div class="hole hole6">
<div class="mole"></div>
</div>
</div>
<style>
/* Basic game styles - no changes needed here */
html {
box-sizing: border-box;
font-size: 10px;
background: #ffc600;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
padding: 0;
margin: 0;
font-family: 'Amatic SC', cursive;
}
h1 {
text-align: center;
font-size: 10rem;
line-height: 1;
margin-bottom: 0;
}
.score {
background: rgba(255,255,255,0.2);
padding: 0 3rem;
line-height: 1;
border-radius: 1rem;
}
.game {
width: 600px;
height: 400px;
display: flex;
flex-wrap: wrap;
margin: 0 auto;
}
.hole {
flex: 1 0 33.33%;
overflow: hidden;
position: relative;
}
.hole:after {
display: block;
background: url(dirt.svg) bottom center no-repeat;
background-size: contain;
content: '';
width: 100%;
height: 70px;
position: absolute;
z-index: 2;
bottom: -30px;
}
.mole {
background: url('mole.svg') bottom center no-repeat;
background-size: 60%;
position: absolute;
top: 100%;
width: 100%;
height: 100%;
transition: all 0.4s;
}
.hole.up .mole {
top: 0;
}
button {
margin: 10px auto;
display: block;
padding: 10px 20px;
font-size: 2rem;
background: rgba(255,255,255,0.2);
border: 0;
border-radius: 5px;
font-family: 'Amatic SC', cursive;
cursor: pointer;
}
/* 2. CSS: ADDED BASIC STYLING FOR THE RESET BUTTON */
#reset-score-btn {
margin-left: 10px;
padding: 5px 10px;
font-size: 1.5rem; /* Make it slightly smaller */
display: inline-block; /* Place it next to Start button */
vertical-align: middle; /* Align vertically with Start button */
}
</style>
<script>
const holes = document.querySelectorAll('.hole');
const scoreBoard = document.querySelector('.score');
const moles = document.querySelectorAll('.mole');
const resetScoreBtn = document.getElementById('reset-score-btn'); // 3. JS: SELECT THE NEW BUTTON
let lastHole;
let timeUp = false;
// 4. JS: LOAD SCORE FROM LOCALSTORAGE OR DEFAULT TO 0
let score = localStorage.getItem('whackAMoleScore') || 0;
scoreBoard.textContent = score; // Display initial score
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) {
console.log('Ah nah thats the same one bud');
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() {
scoreBoard.textContent = score; // Ensure score is displayed correctly at start
timeUp = false;
peep();
setTimeout(() => timeUp = true, 10000) // Game duration 10 seconds
}
function bonk(e) {
if(!e.isTrusted) return; // cheater!
score++;
this.parentNode.classList.remove('up');
scoreBoard.textContent = score;
// 5. JS: SAVE UPDATED SCORE TO LOCALSTORAGE
localStorage.setItem('whackAMoleScore', score);
}
moles.forEach(mole => mole.addEventListener('click', bonk));
// 6. JS: ADD EVENT LISTENER FOR RESET BUTTON
resetScoreBtn.addEventListener('click', () => {
score = 0; // Reset score variable
scoreBoard.textContent = score; // Update display
localStorage.removeItem('whackAMoleScore'); // Remove score from storage
});
</script>
</body>
</html>