200922__JS Flipping card game ์ •๋ฆฌ (1/2)

Positive Koยท2020๋…„ 9์›” 22์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
4/28

๐ŸŒบPositive Ko๐Ÿฆ„๐Ÿ’—

ํ•ด๋‹น ํŽ˜์ด์ง€ ๊ฒŒ์ž„ ์ฝ”๋“œ ๋ฆฌ๋ทฐ

์ œ๋กœ์ดˆ - ์นด๋“œ ๋’ค์ง‘๊ธฐ ๊ฒŒ์ž„ ์ฐธ๊ณ .

  1. CSS๋กœ ์นด๋“œ ๋’ค์ง‘๋Š” ๊ฒƒ์„ ๊ตฌํ˜„
.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
}
.card.flipped .card-inner {
  transform: rotateY(180deg);
}
.card-front,
.card-back {
  border: 1px solid #585858;
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
}
.card-back {
  transform: rotateY(180deg);
}

http://uxuiz.cafe24.com/wp/archives/1212 : ๊ฐ๊ฐ์˜ css ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์„ค๋ช…

  1. card setting
let cardWidth = 4;
let cardHeight = 3;
function cardSet(cardWidth, cardHeight) {
  clickFlag = false;
  for (let i = 0; i < cardWidth * cardHeight; i += 1) {
    let card = document.createElement('div');
    card.className = 'card';
    let cardInner = document.createElement('div');
    cardInner.className = 'card-inner';
    let cardFront = document.createElement('div');
    cardFront.className = 'card-front';
    let cardBack = document.createElement('div');
    cardBack.className = 'card-back';
    cardBack.style.backgroundColor = cardColor[i];
    cardInner.appendChild(cardFront);
    cardInner.appendChild(cardBack);
    card.appendChild(cardInner);

๊ฐ๊ฐ์˜ card div ์•ˆ์— inner์ด ๋“ค์–ด๊ฐ€๊ณ  front, back์ด ๋ณ‘๋ ฌ์ ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  for๋ฌธ์œผ๋กœ 12๊ฐœ์˜ ์นด๋“œ๊ฐ€ ์ƒ์„ฑ๋˜๋„๋ก ํ–ˆ๋‹ค.

  1. ํ† ๊ธ€ ๊ธฐ๋Šฅ์œผ๋กœ ํด๋ฆญ ์‹œ ์นด๋“œ๊ฐ€ ๋’ค์ง‘ํžˆ๋„๋ก ์„ค์ •
(function (c) {
      card.addEventListener('click', function () {
        if (clickFlag && !matchedCard.includes(c)) {
          c.classList.toggle('flipped');
          clickedCard.push(c);
          if (clickedCard.length === 2) {
            if (
              clickedCard[0].querySelector('.card-back').style
                .backgroundColor ===
              clickedCard[1].querySelector('.card-back').style.backgroundColor
            ) {
              matchedCard.push(clickedCard[0]);
              matchedCard.push(clickedCard[1]);
              clickedCard = [];
              if (matchedCard.length === cardWidth * cardHeight) {
                let gameFinish = new Date();
                alert(
                  `Congratulations๐Ÿ‘ Time record: ${parseInt(
                    (gameFinish - gameStart) / 1000
                  )} seconds`
                );
                document.querySelector('#gameTable').innerHTML = '';
                cardColorset = colorPool.slice();
                cardColor = [];
                matchedCard = [];
                gameStart = null;
                shuffle();
                cardSet(cardWidth, cardHeight);
              }
            }

eventlistener๋ฅผ ์ฆ‰์‹œ์‹คํ–‰ํ•จ์ˆ˜ function(c)๋กœ ๊ฐ์‹ธ์„œ ํด๋กœ์ € ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

profile
๋‚ด ์ด๋ฆ„ ๊ณ ์€์ •, ์€์„ 180deg ๋Œ๋ ค ๊ณ ๊ธ์ • ๐Ÿคญ

0๊ฐœ์˜ ๋Œ“๊ธ€