Skip to content

Commit

Permalink
all done
Browse files Browse the repository at this point in the history
  • Loading branch information
Hritiksarraf committed Nov 1, 2024
1 parent d3f59cc commit 961b3d3
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 110 deletions.
8 changes: 4 additions & 4 deletions age-as-a-lesn.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
@media (orientation: portrait) {
#bg-video {
width: auto;
height: 100%;
height: 100vh;
max-width: 100%;
}
}
Expand All @@ -86,7 +86,7 @@
/* Tablets */
@media (max-width: 768px) {
.video-section {
height: 80vh;
height: 100vh;
}

.overlay-content {
Expand All @@ -103,7 +103,7 @@
/* Large phones */
@media (max-width: 576px) {
.video-section {
height: 70vh;
height: 90vh;
}

.overlay-content {
Expand All @@ -121,7 +121,7 @@
/* Small phones */
@media (max-width: 375px) {
.video-section {
height: 60vh;
height: 80vh;
}

.overlay-content {
Expand Down
213 changes: 109 additions & 104 deletions cards-age.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,126 @@
var errors = 0;
var score = 0;
var matchedPairs = 0; // Track matched pairs
var totalPairs = 8; // Total pairs in the game
var startTime, timerInterval;
var cardList = ["2", "3", "4", "5", "6", "7", "8", "9"];
var cardSet, board = [];
var rows = 4, columns = 4;
var card1Selected, card2Selected;
var flipTimeout;
const flipSound = new Audio("flipcard.mp3");
const matchSound = new Audio("match.mp3");
const winSound = new Audio("win.mp3");

window.onload = function() {
shuffleCards();
startGame();
document.getElementById("timer").innerText = "Time: 0s"; // Initialize timer display
}
var errors = 0;
var score = 0;
var matchedPairs = 0;
var totalPairs = 8;
var startTime, timerInterval;
var cardList = ["2", "3", "4", "5", "6", "7", "8", "9"];
var cardSet, board = [];
var rows = 4, columns = 4;
var card1Selected, card2Selected;
var flipTimeout;

function shuffleCards() {
cardSet = cardList.concat(cardList);
for (let i = 0; i < cardSet.length; i++) {
let j = Math.floor(Math.random() * cardSet.length);
[cardSet[i], cardSet[j]] = [cardSet[j], cardSet[i]]; // Swap
}
}
window.onload = function() {
shuffleCards();
startGame();
document.getElementById("timer").innerText = "Time: 0s";
}

function startGame() {
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < columns; c++) {
let cardImg = cardSet.pop();
row.push(cardImg);
let card = document.createElement("img");
card.id = `${r}-${c}`;
card.src = cardImg + ".png";
card.classList.add("card");
card.addEventListener("click", selectCard);
document.getElementById("board").append(card);
function shuffleCards() {
cardSet = cardList.concat(cardList);
for (let i = 0; i < cardSet.length; i++) {
let j = Math.floor(Math.random() * cardSet.length);
[cardSet[i], cardSet[j]] = [cardSet[j], cardSet[i]];
}
}
board.push(row);
}
setTimeout(hideCards, 10);
}

function hideCards() {
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
document.getElementById(`${r}-${c}`).src = "back.png";
function startGame() {
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < columns; c++) {
let cardImg = cardSet.pop();
row.push(cardImg);
let card = document.createElement("img");
card.id = `${r}-${c}`;
card.src = cardImg + ".png";
card.classList.add("card");
card.addEventListener("click", selectCard);
document.getElementById("board").append(card);
}
board.push(row);
}
setTimeout(hideCards, 10);
}
}
}

function startTimer() {
startTime = new Date();
timerInterval = setInterval(() => {
let timeElapsed = Math.floor((new Date() - startTime) / 1000);
document.getElementById("timer").innerText = `Time: ${timeElapsed}s`;
}, 1000);
}
function hideCards() {
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
document.getElementById(`${r}-${c}`).src = "back.png";
}
}
}

function stopTimer() {
clearInterval(timerInterval);
}
function startTimer() {
startTime = new Date();
timerInterval = setInterval(() => {
let timeElapsed = Math.floor((new Date() - startTime) / 1000);
document.getElementById("timer").innerText = `Time: ${timeElapsed}s`;
}, 1000);
}

function selectCard() {
if (!startTime) startTimer(); // Start timer only on first card click

if (this.src.includes("back") && !card2Selected) { // Only proceed if a card pair isn’t being processed
if (flipTimeout) clearTimeout(flipTimeout); // Clear any pending timeout to avoid freezing

if (!card1Selected) {
card1Selected = this;
revealCard(card1Selected);
} else if (this != card1Selected) {
card2Selected = this;
revealCard(card2Selected);
flipTimeout = setTimeout(checkMatch, 500); // Check for match after a short delay
function stopTimer() {
clearInterval(timerInterval);
}
}
}

function revealCard(card) {
let [r, c] = card.id.split("-").map(Number);
card.src = board[r][c] + ".png";
}
function selectCard() {
if (!startTime) startTimer(); // Start timer only on first card click

if (this.src.includes("back") && !card2Selected) { // Only proceed if a card pair isn’t being processed
if (flipTimeout) clearTimeout(flipTimeout); // Clear any pending timeout to avoid freezing

if (!card1Selected) {
card1Selected = this;
revealCard(card1Selected);
matchSound.play(); // Play flip sound
} else if (this != card1Selected) {
card2Selected = this;
revealCard(card2Selected);
matchSound.play(); // Play flip sound
flipTimeout = setTimeout(checkMatch, 500); // Check for match after a short delay
}
}
}

function revealCard(card) {
let [r, c] = card.id.split("-").map(Number);
card.src = board[r][c] + ".png";
}

function checkMatch() {
if (card1Selected && card2Selected) { // Proceed only if both cards are selected
if (card1Selected.src === card2Selected.src) {
let timeTaken = Math.floor((new Date() - startTime) / 1000);
let tempScore = 50 - (errors * 5) - (timeTaken * 0.2);
score += tempScore < 5 ? 5 : tempScore;
errors = 0;
document.getElementById("score").innerText = score;
matchedPairs++; // Increment matched pairs
function checkMatch() {
if (card1Selected && card2Selected) {
if (card1Selected.src === card2Selected.src) {
let timeTaken = Math.floor((new Date() - startTime) / 1000);
let tempScore = 50 - (errors * 5) - (timeTaken * 0.2);
score += tempScore < 5 ? 5 : tempScore;
errors = 0;
document.getElementById("score").innerText = score;
matchedPairs++;

if (matchedPairs === totalPairs) { // Check if all pairs are matched
endGame();
matchSound.play(); // Play match sound

if (matchedPairs === totalPairs) {
endGame();
}
} else {
card1Selected.src = "back.png";
card2Selected.src = "back.png";
errors++;
}
card1Selected = null;
card2Selected = null;
}
} else {
card1Selected.src = "back.png";
card2Selected.src = "back.png";
errors++;
}
card1Selected = null;
card2Selected = null;
}
}

function endGame() {
stopTimer();
document.getElementById("finalScore").innerText = score;
document.getElementById("endModal").style.display = "flex"; // Show modal
}

function restartGame() {
location.reload(); // Reload the page to restart the game
}
function endGame() {
stopTimer();
document.getElementById("finalScore").innerText = score;
document.getElementById("endModal").style.display = "flex";
winSound.play(); // Play win sound
}

function revealTheme() {
alert("The theme is 'Age as a Lens' – exploring how different age groups perceive life differently!");
}
function restartGame() {
location.reload();
}
Binary file added flipcard.mp3
Binary file not shown.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
<div class="content">
<h5 class="box_title">DATE</h5>
<p>
5:30 PM, 8th Nov, 2024
6:00 PM, 8th Nov, 2024
</p>
</div>
</div>
Expand Down Expand Up @@ -627,7 +627,7 @@ <h4 class="footer_title">
time: 3000
});

$('.countdown').countdown(new Date("2024-11-08T17:30:00.000+05:30"), function (event) {
$('.countdown').countdown(new Date("2024-11-08T18:00:00.000+05:30"), function (event) {
$(this).html(
event.strftime("<div>%D <span>Days</span></div><div>%H <span>Hours</span></div><div>%M <span>Minutes</span></div><div>%S <span>Seconds</span></div>")
);
Expand Down
Binary file added match.mp3
Binary file not shown.
Binary file added win.mp3
Binary file not shown.

0 comments on commit 961b3d3

Please sign in to comment.