Skip to content

Commit

Permalink
1.0 - basic card mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadCreator committed Aug 6, 2024
0 parents commit 7328e9d
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/FindPair.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Find The Pair</title>
</head>
<body>
<header>

</header>
<main>
<section class="field">
</section>
</main>
<footer>

</footer>
<script src="main.js"></script>
</body>
</html>
82 changes: 82 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const x = 4
const y = 6

const delay = ms => new Promise(res => setTimeout(res, ms))

const checkMatched = () => {
const matchedCards = document.querySelectorAll(".matched")
if (matchedCards.length === x * y) {
console.log("GameOver!!")
}
}
async function lol() {
this.classList.add('active')
const activeCards = document.querySelectorAll('.active')

await delay(500)

if (activeCards.length === 2) {
if (activeCards[0].classList[1] === activeCards[1].classList[1]) {
console.log("Great!")
activeCards[0].classList.add('matched')
activeCards[1].classList.add('matched')

await delay(500)

activeCards[0].removeEventListener("click", lol)
activeCards[1].removeEventListener("click", lol)
} else {
console.log("Lose!")
}
activeCards[0].classList.remove('active')
activeCards[1].classList.remove('active')

checkMatched()
}
}
const makeField = (x, y) => {
const field = document.querySelector('.field')

while (field.hasChildNodes()) {
field.removeChild(field.firstChild)
}

const size = 72 / x
const pairs = []
for (let k = 0; k < x * y / 2; k++) {
pairs[k] = 2
}

for (let i = 0; i < x; i++) {
for (let j = 0; j < y; j++) {
const card = document.createElement('div')
const front = document.createElement('div')
const back = document.createElement('div')

card.style.width = `${size}vw`
card.style.height = `${size}vw`
card.classList.add('card')

let num = false
while (!num) {
let k = Math.ceil(Math.random() * pairs.length) - 1
if (pairs[k] !== 0) {
pairs[k]--
num = k + 1
}
}

back.innerHTML = `${num}`

card.classList.add(`pair-${num}`)
card.addEventListener("click", lol)

card.appendChild(front)
card.appendChild(back)
field.appendChild(card)
}
}
}

window.onload = () => {makeField(x, y)}

72 changes: 72 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
body, html {
margin: 0;

}

main, header {
display: flex;
justify-content: center;
}

header, footer {
height: 12vh;
width: 100%;
background-color: lightgreen;
margin-bottom: 5vw;
}

main {
background-color: lightgoldenrodyellow;
aspect-ratio: 9 / 14;
}

footer {
margin-top: 5vw;
margin-bottom: 5vw;
}

.field {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: center;
background-color: lightblue;
width: 90vw;
padding: 5vw;
gap: 4vw;
}

.card {
aspect-ratio: 1;
transition: all 0.6s ease-in;
position: relative;
transform-style: preserve-3d;
}

.card div {
position: absolute;
background-color: coral;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
backface-visibility: hidden;
}

.card div:last-child {
background-color: yellow;
transform: rotateY(180deg);
}

.active, .matched {
transform: rotateY(180deg);
transition: all 0.5s;
}

.card.matched div {
font-size: 0;
background-color: transparent;
transition: background-color 0.3s;
}

0 comments on commit 7328e9d

Please sign in to comment.