Skip to content

Game #5447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
samihtmlcss opened this issue Apr 20, 2025 · 0 comments
Open

Game #5447

samihtmlcss opened this issue Apr 20, 2025 · 0 comments

Comments

@samihtmlcss
Copy link

<title>Code Battle - Puzzle Game</title>

Code Battle - Memory Puzzle

Score: 0

Save Score
<script src="script.js"></script>

const emojis = ['💻','⚡','🔥','🚀','🧠','🖱️','⌨️','🧩'];
let cards = [...emojis, ...emojis];
cards.sort(() => 0.5 - Math.random());

const grid = document.getElementById("gameGrid");
let first = null, second = null, lock = false, score = 0;

cards.forEach((emoji, index) => {
const card = document.createElement("div");
card.className = "card";
card.dataset.emoji = emoji;
card.innerText = "?";
card.onclick = () => flip(card);
grid.appendChild(card);
});

function flip(card) {
if (lock || card.innerText !== "?") return;
card.innerText = card.dataset.emoji;
if (!first) {
first = card;
} else {
second = card;
check();
}
}

function check() {
lock = true;
if (first.dataset.emoji === second.dataset.emoji) {
score += 10;
document.getElementById("score").innerText = score;
first = second = null;
lock = false;
} else {
setTimeout(() => {
first.innerText = "?";
second.innerText = "?";
first = second = null;
lock = false;
}, 1000);
}
}

function saveScore() {
alert("Score saved locally: " + score);
}
body {
margin: 0;
font-family: Arial;
background: linear-gradient(to right, #1e3c72, #2a5298);
color: white;
text-align: center;
}

h1 {
margin-top: 20px;
}

.grid {
display: grid;
grid-template-columns: repeat(4, 100px);
gap: 10px;
justify-content: center;
margin-top: 30px;
}

.card {
width: 100px;
height: 100px;
background: white;
color: black;
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
border-radius: 8px;
user-select: none;
}

#scoreBox {
margin-top: 20px;
}

button {
padding: 10px 20px;
background: gold;
border: none;
font-weight: bold;
cursor: pointer;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant