Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. class soundEffects
  2. {
  3. constructor()
  4. {
  5. this.backGroundMusic = new Audio('Sounds/BackGround.mp3');
  6. this.backGroundMusic.volume=0.5;
  7. this.backGroundMusic.loop= true;
  8. this.flipSoundvoice = new Audio('Sounds/flip.wav');
  9. this.introMusic = new Audio ('Sounds/Intro.mp3');
  10. this.matchedVoice = new Audio ('Sounds/Matched.mp3');
  11. this.victoRy= new Audio('Sounds/Victory.mp3');
  12. }
  13. flipSound()
  14. {
  15. this.flipSoundvoice.play();
  16. }
  17. intro()
  18. {
  19. this.introMusic.play();
  20. }
  21. back()
  22. {
  23. this.backGroundMusic.play();
  24. }
  25. matchedS()
  26. {
  27. this.matchedVoice.play();
  28. }
  29. victory()
  30. {
  31. this.victoRy.play();
  32. }
  33. }
  34. function startGame(nOofPairs){
  35. //let sound = new soundEffects();
  36. sound.intro();
  37. //sound.back();
  38. const cards= document.querySelectorAll('.memory-card');
  39. cards.forEach(card => {
  40. card.classList.remove('flip');
  41. });
  42.  
  43.  
  44. //for (var i =0 ; i<nOofPairs ; i++)
  45. //{
  46. var div = document.createElement('div');
  47. div.className = 'row';
  48. div.innerHTML =
  49. '<img class="front-face" src="18.png" >\
  50. <img class="back-face" src="back.png">';
  51. document.getElementById('content').appendChild(div);
  52. //}
  53.  
  54.  
  55. let hasFlippedCard=false;
  56. let firstCard, secondCard;
  57. let lockBoard=false;
  58. var flips=0;
  59. var counter=0;
  60. var hit=0;
  61.  
  62.  
  63. function flipCard(){
  64. if(lockBoard){return;}
  65. if(this===firstCard){return;}
  66.  
  67. flips++;
  68. sound.flipSound();
  69. document.getElementById('flips').innerText=flips;
  70. this.classList.toggle('flip');
  71.  
  72. if(!hasFlippedCard){
  73.  
  74. hasFlippedCard=true;
  75. firstCard=this;
  76. }
  77. else{
  78. hasFlippedCard=false;
  79. secondCard=this;
  80.  
  81. if(firstCard.dataset.framework===secondCard.dataset.framework){
  82. firstCard.removeEventListener('click',flipCard);
  83. secondCard.removeEventListener('click',flipCard);
  84. hit++;
  85. sound.matchedS();
  86. }
  87. else{
  88. lockBoard=true;
  89. setTimeout(() => {
  90. firstCard.classList.remove('flip');
  91. secondCard.classList.remove('flip');
  92. lockBoard=false;
  93. firstCard= null;
  94. },1500);
  95. }
  96. }
  97. }
  98. (function shuffle(){
  99. cards.forEach(card => {
  100. let randomPos = Math.floor(Math.random()*36);
  101. card.style.order = randomPos;
  102. });
  103. })();
  104. cards.forEach(card => card.addEventListener('click',flipCard));
  105.  
  106. function time(){
  107. document.getElementById('time-remaining').innerText=counter;
  108. counter++;
  109. if(hit==18){clearInterval(t);
  110. document.getElementById('victory-text').classList.add('visible');
  111. sound.victory();
  112. document.getElementById('flips').innerText=0;
  113. }
  114. }
  115.  
  116. var t=setInterval(time,1000);
  117.  
  118. }
  119.  
  120.  
  121.  
  122.  
  123. let sound = new soundEffects();
  124.  
  125. /*let e = document.getElementById("keke");
  126. let strUser = e.options[e.selectedIndex].text;
  127. startGame(strUser);*/
  128.  
  129. let overlays = Array.from(document.getElementsByClassName('overlay-text'));
  130. overlays.forEach( overlay => {
  131. overlay.addEventListener('click',() => {
  132. overlay.classList.remove('visible');
  133. sound.back();
  134. startGame(0);
  135. });
  136. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement