Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. // Those are global variables, they stay alive and reflect the state of the game
  2. var elPreviousCard = null;
  3. var flippedCouplesCount = 0;
  4.  
  5. // This is a constant that we dont change during the game (we mark those with CAPITAL letters)
  6. var TOTAL_COUPLES_COUNT = 3;
  7.  
  8. // Load an audio file
  9. var audioWin = new Audio('sound/win.mp3');
  10. var right = new Audio('sound/right.mp3');
  11. var wrong = new Audio('sound/wrong.mp3');
  12.  
  13. // Select all cards
  14. //var flip = document.querySelectorAll('.card');
  15.  
  16.  
  17. // This function is called whenever the user click a card
  18. function cardClicked(elCard) {
  19.  
  20. // If the user clicked an already flipped card - do nothing and return from the function
  21. if (elCard.classList.contains('flipped')) {
  22. return;
  23. }
  24.  
  25. // Flip it
  26. elCard.classList.add('flipped');
  27.  
  28. // This is a first card, only keep it in the global variable
  29. if (elPreviousCard === null) {
  30. elPreviousCard = elCard;
  31. } else {
  32. // get the data-card attribute's value from both cards
  33. var card1 = elPreviousCard.getAttribute('data-card');
  34. var card2 = elCard.getAttribute('data-card');
  35.  
  36. // No match, schedule to flip them back in 1 second
  37. if (card1 !== card2){
  38. wrong.play();
  39. setTimeout(function () {
  40. elCard.classList.remove('flipped');
  41. elPreviousCard.classList.remove('flipped');
  42. elPreviousCard = null;
  43. }, 1000)
  44.  
  45. } else {
  46. // Yes! a match!
  47. right.play();
  48. flippedCouplesCount++;
  49. elPreviousCard = null;
  50.  
  51. // All cards flipped!
  52. if (TOTAL_COUPLES_COUNT === flippedCouplesCount) {
  53. audioWin.play();
  54. }
  55. var el = document.querySelectorAll("div.card");
  56. if (el.classList.contain('flipped') == 3)[
  57. el.classList.remove('flipped')
  58. ]
  59. elPreviousCard = null;
  60. }
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement