Guest User

Untitled

a guest
Sep 27th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class AudioController {
  2.     constructor() {
  3.         this.bgMusic = new Audio('Assets/Audio/xxxxx.mp3');
  4.         this.flipSound = new Audio('Assets/Audio/flip.wav');
  5.         this.matchSound = new Audio('Assets/Audio/match.wav');
  6.         this.victorySound = new Audio('Assets/Audio/01.mp3');
  7.         this.gameOverSound = new Audio('Assets/Audio/18.mp3');
  8.         this.bgMusic.volume = 0.5;
  9.         this.bgMusic.loop = true;
  10.     }
  11.     startMusic() {
  12.         this.bgMusic.play();
  13.     }
  14.     stopMusic() {
  15.         this.bgMusic.pause();
  16.         this.bgMusic.currentTime = 0;
  17.     }
  18.     flip() {
  19.         this.flipSound.play();
  20.     }
  21.     match() {
  22.         this.matchSound.play();
  23.     }
  24.     victory() {
  25.         this.stopMusic();
  26.         this.victorySound.play();
  27.     }
  28.     gameOver() {
  29.         this.stopMusic();
  30.         this.gameOverSound.play();
  31.     }
  32. }
  33.  
  34. class MixOrMatch {
  35.     constructor(totalTime, cards) {
  36.         this.cardsArray = cards;
  37.         this.totalTime = totalTime;
  38.         this.timeRemaining = totalTime;
  39.         this.timer = document.getElementById('time-remaining')
  40.         this.ticker = document.getElementById('flips');
  41.         this.audioController = new AudioController();
  42.     }
  43.  
  44.     startGame() {
  45.         this.totalClicks = 0;
  46.         this.timeRemaining = this.totalTime;
  47.         this.cardToCheck = null;
  48.         this.matchedCards = [];
  49.         this.busy = true;
  50.         setTimeout(() => {
  51.             this.audioController.startMusic();
  52.             this.shuffleCards(this.cardsArray);
  53.             this.countdown = this.startCountdown();
  54.             this.busy = false;
  55.         }, 500)
  56.         this.hideCards();
  57.         this.timer.innerText = this.timeRemaining;
  58.         this.ticker.innerText = this.totalClicks;
  59.     }
  60.     startCountdown() {
  61.         return setInterval(() => {
  62.             this.timeRemaining--;
  63.             this.timer.innerText = this.timeRemaining;
  64.             if(this.timeRemaining === 0)
  65.                 this.gameOver();
  66.         }, 1000);
  67.     }
  68.     gameOver() {
  69.         clearInterval(this.countdown);
  70.         this.audioController.gameOver();
  71.         document.getElementById('game-over-text').classList.add('visible');
  72.     }
  73.     victory() {
  74.         clearInterval(this.countdown);
  75.         this.audioController.victory();
  76.         document.getElementById('victory-text').classList.add('visible');
  77.     }
  78.     hideCards() {
  79.         this.cardsArray.forEach(card => {
  80.             card.classList.remove('visible');
  81.             card.classList.remove('matched');
  82.         });
  83.     }
  84.     flipCard(card) {
  85.         if(this.canFlipCard(card)) {
  86.             this.audioController.flip();
  87.             this.totalClicks++;
  88.             this.ticker.innerText = this.totalClicks;
  89.             card.classList.add('visible');
  90.  
  91.             if(this.cardToCheck) {
  92.                 this.checkForCardMatch(card);
  93.             } else {
  94.                 this.cardToCheck = card;
  95.             }
  96.         }
  97.     }
  98.     checkForCardMatch(card) {
  99.         if(this.getCardType(card) === this.getCardType(this.cardToCheck))
  100.             this.cardMatch(card, this.cardToCheck);
  101.         else
  102.             this.cardMismatch(card, this.cardToCheck);
  103.  
  104.         this.cardToCheck = null;
  105.     }
  106.     cardMatch(card1, card2) {
  107.         this.matchedCards.push(card1);
  108.         this.matchedCards.push(card2);
  109.         card1.classList.add('matched');
  110.         card2.classList.add('matched');
  111.         this.audioController.match();
  112.         if(this.matchedCards.length === this.cardsArray.length)
  113.             this.victory();
  114.     }
  115.     cardMismatch(card1, card2) {
  116.         this.busy = true;
  117.         setTimeout(() => {
  118.             card1.classList.remove('visible');
  119.             card2.classList.remove('visible');
  120.             this.busy = false;
  121.  
  122.         }, 1000);
  123.     }
  124.     shuffleCards(cardsArray) { // Fisher-Yates Shuffle Algorithm.
  125.         for (let i = cardsArray.length - 1; i > 0; i--) {
  126.             let randIndex = Math.floor(Math.random() * (i + 1));
  127.             cardsArray[randIndex].style.order = i;
  128.             cardsArray[i].style.order = randIndex;
  129.         }
  130.     }
  131.     getCardType(card) {
  132.         return card.getElementsByClassName('card-value')[0].src;
  133.     }
  134.     canFlipCard(card) {
  135.         return !this.busy && !this.matchedCards.includes(card) && card !== this.cardToCheck;
  136.     }
  137. }
  138.  
  139. if (document.readyState == 'loading') {
  140.     document.addEventListener('DOMContentLoaded', ready);
  141. } else {
  142.     ready();
  143. }
  144.  
  145.  
  146.  
  147. function ready() {
  148.     let overlays = Array.from(document.getElementsByClassName('overlay-text'));
  149.     let cards = Array.from(document.getElementsByClassName('card'));
  150.     let game = new MixOrMatch(100, cards);
  151.  
  152.     overlays.forEach(overlay => {
  153.         overlay.addEventListener('click', () => {
  154.             overlay.classList.remove('visible');
  155.             game.startGame();
  156.         });
  157.     });
  158.  
  159.     cards.forEach(card => {
  160.         card.addEventListener('click', () => {
  161.             game.flipCard(card);
  162.         });
  163.     });
  164. }
Add Comment
Please, Sign In to add comment