Skip to content

Commit fb0e9ac

Browse files
Merge pull request #201 from Dhureen7/main
Added Hard Core mode. Also added time bonus to the final score to make it more engaging.
2 parents 86e8e0b + 49af6c9 commit fb0e9ac

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

projects/gravity-flip-pinball/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ <h1>Gravity Flip Pinball</h1>
3636
<input type="checkbox" id="challengeMode" />
3737
<span>Challenge Mode (60s)</span>
3838
</label>
39+
40+
<br>
41+
42+
<label>
43+
<input type="checkbox" id="hardCoreMode" />
44+
<span>Hardcore Mode (60s & you get <br> penalised every 50 collisions)</span>
45+
</label>
3946
</div>
4047

4148
<div class="timer-display" id="timer">60</div>

projects/gravity-flip-pinball/script.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ let status = "ready";
88
let challengeMode = false;
99
let timeRemaining = 60;
1010
let timerInterval = null;
11+
let hardCoreMode = false;
12+
let collisions = 0;
1113

1214
let walls = [];
1315
let gates = [];
@@ -164,6 +166,14 @@ function ballRectCollision(b, rect) {
164166
const distSq = dx * dx + dy * dy;
165167

166168
if (distSq < b.radius * b.radius) {
169+
collisions++;
170+
if(hardCoreMode && collisions%50 == 0) {
171+
score--;
172+
document.getElementById("score").textContent = score;
173+
if(score < 0) {
174+
showVictoryModal();
175+
}
176+
}
167177
const dist = Math.sqrt(distSq) || 1;
168178
const overlap = b.radius - dist;
169179

@@ -244,13 +254,30 @@ function showVictoryModal() {
244254
timerInterval = null;
245255
}
246256

257+
collisions = 0;
258+
247259
const modal = document.getElementById("victoryModal");
248260
const title = document.getElementById("victoryTitle");
249261
const message = document.getElementById("victoryMessage");
250262

251-
if (challengeMode) {
263+
if(hardCoreMode) {
264+
265+
if(score <= 0) {
266+
title.textContent = "Better luck next time";
267+
message.textContent = `You had too many collisions! Tip: Do not let the ball remain idle`;
268+
} else {
269+
const timeTaken = 60 - timeRemaining;
270+
score += timeRemaining;
271+
document.getElementById("score").textContent = score;
272+
title.textContent = "Impeccable Victory!";
273+
message.textContent = `You finished Hard Core mode in ${timeTaken} seconds and ${collisions} collisions with a final score of ${score}!`;
274+
}
275+
276+
} else if (challengeMode) {
252277
const timeTaken = 60 - timeRemaining;
253278
title.textContent = "Challenge Complete!";
279+
score += timeRemaining;
280+
document.getElementById("score").textContent = score;
254281
message.textContent = `You finished in ${timeTaken} seconds with a score of ${score}!`;
255282
} else {
256283
title.textContent = "Victory!";
@@ -432,18 +459,32 @@ document.querySelectorAll(".gravity-btn").forEach((btn) => {
432459

433460
window.addEventListener("resize", resizeCanvas);
434461

462+
const challengeCheckbox = document.getElementById("challengeMode");
463+
const hardCoreCheckbox = document.getElementById("hardCoreMode");
464+
465+
challengeCheckbox.addEventListener("change", () => {
466+
if (challengeCheckbox.checked) hardCoreCheckbox.checked = false;
467+
});
468+
469+
hardCoreCheckbox.addEventListener("change", () => {
470+
if (hardCoreCheckbox.checked) challengeCheckbox.checked = false;
471+
});
472+
435473
document.getElementById("startBtn").addEventListener("click", () => {
436-
challengeMode = document.getElementById("challengeMode").checked;
474+
challengeMode = challengeCheckbox.checked;
475+
hardCoreMode = hardCoreCheckbox.checked;
437476
status = "playing";
477+
438478
document.getElementById("status").textContent = "Playing";
439479
document.getElementById("startBtn").style.display = "none";
440480

441-
if (challengeMode) {
481+
if (challengeMode || hardCoreMode) {
442482
document.getElementById("timer").classList.add("active");
443-
startTimer();
483+
startTimer();
444484
}
445485
});
446486

487+
447488
function replay() {
448489
document.getElementById("victoryModal").classList.remove("show");
449490
resizeCanvas();
@@ -457,7 +498,8 @@ function replay() {
457498
setGravity(0, 0.5, "down");
458499

459500
challengeMode = document.getElementById("challengeMode").checked;
460-
if (challengeMode) {
501+
hardCoreMode = document.getElementById("hardCoreMode").checked;
502+
if (challengeMode || hardCoreMode) {
461503
document.getElementById("timer").classList.add("active");
462504
startTimer();
463505
} else {
@@ -485,7 +527,7 @@ canvas.addEventListener("dblclick", () => {
485527
ball.vy = 0;
486528
setGravity(0, 0.5, "down");
487529

488-
if (challengeMode) {
530+
if (challengeMode || hardCoreMode) {
489531
startTimer();
490532
}
491533
});

0 commit comments

Comments
 (0)