Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                    
  
<div id="land" class="land">
  <div id="land-msg" class="land-msg">
    <div class="memorama-flip">
      <span class="memorama" >Memoria</span>
      <button class="btn-play" onclick="javascript:playNow();">Jugar ahora &#9658;</button>
    </div>
  </div>    
</div>

<div id="result" class="land land-hide"> 
    <div id="result-content" class="land-msg">
      <span id="result-msg" class="result-msg"></span>
      <button class="btn-play btn-play-again" onclick="javascript:playAgain();">Jugar nuevamente! &#9658;</button>    
    </div>
</div>  
    
<div id="game"><ul class="flex-container" id="cards"></ul>
</div>
              
            
!

CSS

              
                /* Land & repeat game*/

.land {
  background-color:#0c3757;
  width:100%;
  height: 100%;
  min-height: 500px;
  position:absolute;
  top:0px;
  left:0px;
  z-index:1;
  display: flex;
  
  transform: scale(1);
  transition: all .2s ease-in-out;
  transition-duration: 700ms, 0s;
  transition-delay: 0s, 1s;   
}

.land-hide{
  transform: scale(0);
  transition: all .2s ease-in-out;
  transition-duration: 700ms, 0s;
  transition-delay: 0s, 1s;  
}

.land-msg {
  background-color:#0c3757;
  width:50%;
  height:100%;
  min-height: 500px;
  margin: auto auto;
}

.memorama-flip{
  -webkit-transform: rotateY(0deg);  
          transform: rotateY(0deg);
  
  animation-name: memorama-flip;
  animation-duration: 1200ms;
  animation-delay: 800ms;
  animation-timing-function: ease-in-out;
  animation-iteration-count: 1;    
  animation-fill-mode: forwards;    
}

@keyframes memorama-flip {     
      50% {transform: rotateY(180deg) ;}    
} 

.memorama{
  color:white;
  font-size:11vw;
  font-family: 'Concert One', cursive;
  text-align: center;
}

/* button style by: http://callmenick.com/post/stylish-css-buttons*/
.btn-play{
  position:absolute;
  left:35%;
  top:95%;
  font-size:2vw;
  margin: 0 10px 0 0;
  padding: 15px 45px;  
  color: #fff;
  background-color: #6496c8;
  border: none;
  border-radius: 15px;
  box-shadow: 0 10px #27496d;
  outline:none;
  
  visibility:hidden;
  opacity:0; 
  animation-name: btn-play-show;
  animation-duration: 800ms;
  animation-delay: 2s;
  animation-timing-function: linear;
  animation-iteration-count: 1;    
  animation-fill-mode: forwards;  
}

@keyframes btn-play-show { 
  100% {    
         visibility:visible;
         opacity:1;
       }
}

.btn-play:hover,
.btn-play.hover {
  background-color: #417cb8
}

.btn-play:active,
.btn-play.active {
  background-color: #417cb8;
  box-shadow: 0 5px #27496d;
  transform: translateY(5px);
}

.btn-play-again{
  position:absolute;
  left:35%;
  top:95%;
	font-size:2vw;
  margin: 0 10px 0 0;
  padding: 15px 45px;  
  color: #fff;
  background-color: #6496c8;
  border: none;
  border-radius: 15px;
  box-shadow: 0 10px #27496d;
  outline:none;
}

.result-msg{
  color:#0c3757;
  font-size:4vw;
  font-family: 'Concert One', cursive;
}

/* Game*/
.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

.flex-item {
  border-radius: 11px;
  background: #0c3757;
  padding: 3px;
  width: 100px;
  height:100px;
  margin-top: 5px;
  line-height: 100px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center; 
}

.card
{
  width:100%;  
  height: 100%;
  position:relative;
  border-radius: 11px;
}
.card-front, 
.card-back
{
  position:absolute;
  top:0px;
  left:0px;  
  width:100%;  
  height: 100%;
  border-radius: 10px;

    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
  
    -webkit-transition: -webkit-transform 300ms;
    transition: transform 300ms;
  
    -webkit-transition-timing-function: linear;
            transition-timing-function: linear;    
}

.card-back {
-webkit-transform: rotateY(0deg);  
    transform: rotateY(0deg);
}

.card-back-flip 
{
    -webkit-transform: rotateY(270deg);
            transform: rotateY(270deg);
	scale(-1, 1){}
}

.card-front {    
    -webkit-transform: rotateY(-270deg);
            transform: rotateY(-270deg);
}


.card-front-flip
{
    -webkit-transform: rotateY(0deg);
            transform: rotateY(0deg);
    scale(-1, 1)
}

.card-front-no
{
  animation-name: card-front-no;
  animation-duration: 200ms;
  animation-timing-function: lineal;
  animation-iteration-count: 15;    
  animation-fill-mode: forwards;  
}

@keyframes card-front-no {   
  0%  { transform: translateX(0px); }    
  33% { transform: translateX(4px); }  
  70% { transform: translateX(-7px);}
  100%{ transform: translateX(3px); }  
} 

.card-front-yes
{
  animation-name: card-front-yes;
  animation-duration: 200ms;
  animation-timing-function: ease;
  animation-iteration-count: 5;    
  animation-fill-mode: forwards;  
}

@keyframes card-front-yes {     
  0%  { transform: translateY(0px);}    
  33% { transform: translateY(3px);}  
  70% { transform: translateY(-7px);}
  100%{ transform: translateY(4px);}  
} 
              
            
!

JS

              
                const NUM_MAX_CARDS = 36;
const NUM_TUPLE = 2;

const Cards = [
  [
    1,
    {
      id: 1,
      source:
        "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C30-BOMBERO.jpg"
    }
  ],
  [
    2,

    {
      id: 2,
      source:
        "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C32-BANDERA.jpg"
    }
  ],
  [
    3,

    {
      id: 3,
      source:
        "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C18-HACER-EJERCICIO.jpg"
    }
  ],
  [
    4,

    {
      id: 4,
      source:
        "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C19-FAMILIA.jpg"
    }
  ],
  [
    5,

    {
      id: 5,
      source:
        "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C20-ESTUDIAR-EN-CASA.jpg"
    }
  ],
  [
    6,

    {
      id: 6,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C21-ESTORNUDAR.jpg"
    }
  ],
	[
    7,

    {
      id: 7,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C22-ENFERMERA.jpg"
    }
  ],
	[
    8,

    {
      id: 8,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C23-DORMIR-BIEN.jpg"
    }
  ],
	[
    9,

    {
      id: 9,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C24-DOCTOR.jpg"
    }
  ],
	[
    10,

    {
      id: 10,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C25-COVID.jpg"
    }
  ],
	[
    11,

    {
      id: 11,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C29-CANTAR.jpg"
    }
  ],
	[
    12,

    {
      id: 12,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C33-BACTERIA.jpg"
    }
  ],
	[
    13,

    {
      id: 13,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C2-VITAMINAS.jpg"
    }
  ],
	[
    14,

    {
      id: 14,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C3-VIRUS.jpg"
    }
  ],
	[
    15,

    {
      id: 15,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C8-PRESIDENTE.jpg"
    }
  ],
	[
    16,

    {
      id: 16,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C11-PELOTA.jpg"
    }
  ],
	[
    17,

    {
      id: 17,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C14-MAMA.jpg"
    }
  ],
	[
    18,

    {
      id: 18,
      source: "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C15-LEER.jpg"
    }
  ]
  
];

class Card {
  constructor(id, sourceFront) {
    this.liCard = this.renderCard(id);

    this.back = this.liCard.querySelector(".card-back");
    this.front = this.liCard.querySelector(".card-front");
    this.sourceFront = sourceFront;

    this.isBack = true;
    this.enabled = true;
  }

  flip() {
    if (this.enabled == true) {
      this.front.src = this.sourceFront;
      this.back.classList.toggle("card-back-flip");
      this.front.classList.toggle("card-front-flip");
      this.enabled = false;
      this.isBack = false;
      return true;
    }
    return false;
  }

  cover() {
    this.enabled = true;
    this.isBack = true;
    this.back.classList.toggle("card-back-flip");
    this.front.classList.toggle("card-front-flip");
    this.front.src = this.back.src;
  }

  say(say) {
    this.front.classList.toggle("card-front-" + say);
  }

  renderCard(id) {
    const src =
      "http://mcd.gob.gt/inicio/wp-content/uploads/2020/04/C0-PORTADA.jpg";
    let cardBack = document.createElement("img");
    cardBack.className = "card-back";
    cardBack.src = src;

    let cardFront = document.createElement("img");
    cardFront.className = "card-front";
    cardFront.src = src;

    let divCard = document.createElement("div");
    divCard.className = "card";
    divCard.appendChild(cardBack);
    divCard.insertBefore(cardFront, cardBack);

    let li = document.createElement("li");
    li.id = "card" + id;
    li.className = "flex-item";
    li.appendChild(divCard);

    return li;
  }
}

class Game {
  constructor(onEndGame, elCanvas) {
    this.cards = new Map();
    this.cardsSelected = new Array();
    this.onEndGame = onEndGame.bind(this);
    this.numTuplesOK = 0;
    this.numClicks = 0;
    this.resultArray = ["ʘ‿ʘ Excelent!", "(´・_・`) Not bad", "¯\\_(ツ)_/¯ Bad "];
    this.elCanvas = elCanvas;
    this.shuffleNum = [];
  }

  init() {
    let cardsMap = new Map(Cards);
    this.shuffle();
    this.cleanCanvas();

    for (let i = 0; i < this.shuffleNum.length; i++) {
      let num = (this.shuffleNum[i] % (NUM_MAX_CARDS / NUM_TUPLE)) + 1;
      let card = cardsMap.get(num);
      let cardg = new Card(i, card.source);
      this.addCard(i, cardg);
    }
  }

  addCard(id, card) {
    card.liCard.addEventListener("click", this.onClick.bind(this, card));
    this.cards.set(id, card);
    this.elCanvas.appendChild(card.liCard);
  }

  onClick(card) {
    let flip = false;
    if (this.cardsSelected.length < NUM_TUPLE) {
      if (card.flip()) {
        this.cardsSelected.push(card);
        this.numClicks++;
      }
      if (this.cardsSelected.length == NUM_TUPLE) {
        this.enabledCards(false);
        if (!this.checkTupla()) {
          setTimeout(() => this.coverCards(), 1700);
          this.cardsSay("no", 1500);
          setTimeout(() => this.enabledCards(true), 2000);
          setTimeout(() => {
            this.cardsSelected = new Array();
          }, 2000);
        } else {
          setTimeout(() => this.enabledCards(true), 800);
          this.cardsSay("yes", 600);
          setTimeout(() => {
            this.cardsSelected = new Array();
          }, 700);

          this.numTuplesOK++;
          if (this.numTuplesOK === NUM_MAX_CARDS / NUM_TUPLE)
            setTimeout(() => {
              this.endGame();
            }, 1000);
        }
      }
    }
  }

  checkTupla() {
    if (this.cardsSelected.length > 0) {
      let srcFilter = this.cardsSelected[0].sourceFront;
      let ce = this.cardsSelected.filter(
        (card) => card.sourceFront === srcFilter
      );
      return ce.length === this.cardsSelected.length;
    }
    return false;
  }

  cardsSay(say, delay) {
    this.cardsSelected.forEach(function (card) {
      setTimeout(() => card.say(say), delay - 350);
      setTimeout(() => card.say(say), delay);
    });
  }

  coverCards() {
    this.cardsSelected.forEach(function (card) {
      card.cover();
    });
  }

  enabledCards(value) {
    this.cards.forEach(function (card) {
      if (card.isBack) card.enabled = value;
    });
  }

  endGame() {
    let numClicksMin = NUM_MAX_CARDS;
    let resultIndex = this.resultArray.length - 1;

    if (this.numClicks === numClicksMin) resultIndex = 0;

    if (this.numClicks > numClicksMin && this.numClicks <= numClicksMin * 2)
      resultIndex = 1;

    if (this.numClicks >= numClicksMin * 2) resultIndex = 2;

    this.onEndGame(this.resultArray[resultIndex], this.numClicks);
  }

  cleanCanvas() {
    while (this.elCanvas.firstChild) {
      this.elCanvas.removeChild(this.elCanvas.firstChild);
    }
  }

  randomNum(min, max) {
    return Math.round(Math.random() * (max - min) + min);
  }

  shuffle() {
    this.shuffleNum = [];
    for (var i = 0; i < NUM_MAX_CARDS; i++) {
      var num = this.randomNum(1, NUM_MAX_CARDS);
      console.log(num);
      if (this.shuffleNum.indexOf(num) >= 0) {
        i = i - 1;
      } else {
        this.shuffleNum[i] = num;
      }
    }
  }
}

function startGame() {
  let cards = document.getElementById("cards");
  let game = new Game(endGame, cards);
  game.init();
}

function playNow() {
  land.classList.toggle("land-hide");
  startGame();
}

function playAgain() {
  result.classList.toggle("land-hide");
  startGame();
}

function endGame(resultGame, score) {
  document.getElementById("result-msg").innerHTML =
    resultGame + " - " + score + " Clicks";
  result.classList.toggle("land-hide");
}

              
            
!
999px

Console