Introduction

In this blog, you will learn how to create a pairing game in JavaScript.
Step 1
First, open a Sublime Text editor,
  • Open start -> Sublime Text Editor.
  • Go to file -> File -> New File
  • Name of File -> Weather app.html.
Now let's follow the steps for our game project.
Step 2
Now, I have add css styles inside the <style> tag.
  1. body,
  2. p,
  3. h5 {
  4. padding: 0;
  5. margin: 0;
  6. font-family: 'indie flower'
  7. }
  8. body {
  9. width: 100hw;
  10. height: 100vh;
  11. background: url('../libs//img//background.png') no-repeat center
  12. }
  13. .main {
  14. position: absolute;
  15. width: 400px;
  16. height: 600px;
  17. top: 50%;
  18. left: 50%;
  19. margin: -300px -200px;
  20. }
  21. .top {
  22. width: 100%;
  23. height: 18%;
  24. background: #9f392d;
  25. position: absolute;
  26. color: #520b03
  27. }
  28. .bottom {
  29. position: absolute;
  30. width: 100%;
  31. height: 82%;
  32. top: 18%;
  33. background: #9f392d;
  34. }
  35. .board {
  36. width: 26%;
  37. height: 90%;
  38. margin: 2% 0 0 2%;
  39. background: #f9bbb4;
  40. float: left;
  41. }
  42. .board h5 {
  43. width: 100%;
  44. height: 25%;
  45. font-size: 23px;
  46. text-align: center;
  47. }
  48. .board p {
  49. width: 100%;
  50. height: 75%;
  51. font-size: 4.5em;
  52. text-align: center
  53. }
  54. .mes {
  55. width: 44.1%;
  56. height: 90%;
  57. background: #f9bbb4;
  58. margin: 2% 0 0 0;
  59. float: left;
  60. font-size: 2.5em;
  61. padding-top: 15px;
  62. text-align: center;
  63. }
  64. .mes div {
  65. font-size: 0.70em;
  66. color: #008a00
  67. }
  68. .time {
  69. margin: 2% 0 0 0;
  70. }
  71. .box {
  72. width: 96%;
  73. height: 98.5%;
  74. background: #f9bbb4;
  75. margin: 0.3% 0 0 2%;
  76. }
  77. .placer {
  78. width: 33.3343%;
  79. height: 25%;
  80. float: left;
  81. }
  82. .holder {
  83. width: 98%;
  84. height: 97.8%;
  85. margin: 1.5%;
  86. border: 1px solid #9f392d;
  87. transform-style: preserve-3d;
  88. transition: all 0.5s ease;
  89. border-radius: 3px;
  90. }
  91. .flip {
  92. transform: rotateY(180deg)
  93. }
  94. .front {
  95. position: absolute;
  96. width: 100%;
  97. height: 100%;
  98. background: #fed4d1;
  99. transition: all 0.5s ease;
  100. }
  101. .back {
  102. position: absolute;
  103. width: 100%;
  104. height: 100%;
  105. transform: rotateY(180deg);
  106. transition: all 0.5s ease;
  107. backface-visibility: hidden;
  108. }
  109. .addSc {
  110. animation: enable 0.3s linear
  111. }
  112. .timeStyle {
  113. animation: timer 0.3s linear
  114. }
  115. @keyframes enable {
  116. 0% {
  117. transform: scale(1);
  118. }
  119. 60% {
  120. color: #00cc03;
  121. transform: scale(1.2);
  122. }
  123. 100% {
  124. transform: scale(1);
  125. }
  126. }
  127. @keyframes timer {
  128. 0% {
  129. transform: scale(1);
  130. }
  131. 60% {
  132. color: #0062c4;
  133. transform: scale(1.2);
  134. }
  135. 100% {
  136. transform: scale(1);
  137. }
  138. }
HTML code to display Pairing Game
Now we insert a new HTML Page on this pairing game. The code is given below.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Pairing Game</title>
  8. <link href="https://fonts.googleapis.com/css?family=Indie+Flower&display=swap" rel="stylesheet">
  9. <link href="css/bootstrap.css" rel="stylesheet">
  10. <link href="css/style.css" rel="stylesheet">
  11. </head>
  12. <body>
  13. <div class='main'>
  14. <div class='top'>
  15. <div class='board score'>
  16. <h5>Score</h5>
  17. <p class='sc'>0</p>
  18. </div>
  19. <div class='mes'>Start</div>
  20. <div class='board time'>
  21. <h5>Seconds</h5>
  22. <p class='second'>30</p>
  23. </div>
  24. </div>
  25. <div class='bottom'>
  26. <div class='box'></div>
  27. </div>
  28. </div>
  29. <audio data-key='select' src='libs/sounds/bounce.mp3'></audio>
  30. <audio data-key='wrong' src='libs/sounds/wrong.mp3'></audio>
  31. <audio data-key='correct' src='libs/sounds/correct.mp3'></audio>
  32. <audio data-key='seconds' src='libs/sounds/seconds.mp3'></audio>
  33. <audio data-key='yay' src='libs/sounds/yay.mp3'></audio>
  34. <audio data-key='lose' src='libs/sounds/lose.mp3'></audio>
  35. </body>
  36. <script src='./js/script.js'></script>
  37. </html>
Step 3
Here you can use some JavaScript along with HTML code. Just follow the steps to see how to create this pairing game.
  1. const fruits = ['carrot', 'cherry', 'apple', 'pineapple', 'pumpkin', 'strawberry']
  2. const box = document.querySelector('.box')
  3. const mes = document.querySelector('.mes')
  4. const score = document.querySelector('.sc')
  5. const second = document.querySelector('.second')
  6. const sec = document.querySelector('audio[data-key="seconds"]')
  7. const select = document.querySelector('audio[data-key="select"]')
  8. const wrong = document.querySelector('audio[data-key="wrong"]')
  9. const correct = document.querySelector('audio[data-key="correct"]')
  10. const yay = document.querySelector('audio[data-key="yay"]')
  11. const lose = document.querySelector('audio[data-key="lose"]')
  12. let tim;
  13. let timer;
  14. fruits.push(...fruits)
  15. function shuffle(arr) {
  16. var val1 = arr.length, temp, index;
  17. while (val1 > 0) {
  18. index = Math.floor(Math.random() * val1);
  19. val1--;
  20. temp = arr[val1];
  21. arr[val1] = arr[index];
  22. arr[index] = temp;
  23. }
  24. return arr;
  25. }
  26. window.onload = init;
  27. function init() {
  28. var divs = (shuffle(fruits))
  29. for (var x = 0; x < divs.length; x++) {
  30. box.innerHTML += `
  31. <div class='placer ${x}'>
  32. <div class='holder ${divs[x]}'>
  33. <div class='front'></div>
  34. <img class='back' src='libs/img/${divs[x]}.png'>
  35. </div>
  36. </div>
  37. `
  38. }
  39. showBox();
  40. }
  41. function showBox() {
  42. mes.addEventListener('click', ready)
  43. }
  44. function ready() {
  45. var a = box.children;
  46. this.innerHTML = 'Ready!'
  47. tim = new Date().getTime() + 30000
  48. timer = setInterval(setDate, 1000)
  49. for (var x = 0; x < a.length; x++) {
  50. a[x].addEventListener('click', click);
  51. }
  52. }
  53. function click() {
  54. var b = this.firstElementChild;
  55. b.classList.add('flip');
  56. evaluator(this);
  57. }
  58. var selectedBox = [];
  59. function evaluator(a) {
  60. select.play()
  61. if (a.classList[1] === undefined) {
  62. return
  63. }
  64. selectedBox.push(a);
  65. if (selectedBox.length === 1) {
  66. mes.innerHTML = "Be sure!";
  67. }
  68. if (selectedBox.length === 2) {
  69. if (selectedBox[0].firstElementChild.classList[1] == selectedBox[1].firstElementChild.classList[1]) {
  70. removePic(selectedBox)
  71. selectedBox = [];
  72. } else {
  73. hidePic(selectedBox);
  74. selectedBox = [];
  75. }
  76. }
  77. }
  78. var scoreCard = 1;
  79. function scoreBoard() {
  80. score.innerHTML = scoreCard++;
  81. score.classList.add('addSc')
  82. if (scoreCard === 7) {
  83. endGame()
  84. }
  85. removeCss(score, 'addSc')
  86. }
  87. function endGame() {
  88. setTimeout(function () {
  89. mes.innerHTML = `
  90. <div>You win!</div>
  91. <div>Start again.</div>
  92. `
  93. clearInterval(timer)
  94. var a = box.children
  95. for (var x = 0; x < a.length; x++) {
  96. a[x].removeEventListener('click', click);
  97. }
  98. mes.addEventListener('click', reset)
  99. yay.play()
  100. }, 700)
  101. }
  102. function reset() {
  103. window.location.reload(true)
  104. }
  105. function removeCss(element, cssName) {
  106. setTimeout(function () {
  107. element.classList.remove(cssName)
  108. }, 700)
  109. }
  110. function removePic(a) {
  111. a[0].classList.remove(a[0].classList[1])
  112. a[1].classList.remove(a[1].classList[1])
  113. setTimeout(function () {
  114. correct.play()
  115. mes.innerHTML = 'Great Job!';
  116. scoreBoard();
  117. }, 700)
  118. }
  119. function hidePic(a) {
  120. setTimeout(function () {
  121. for (var x = 0; x < 2; x++) {
  122. a[x].firstElementChild.classList.remove('flip')
  123. }
  124. mes.innerHTML = "Wrong!";
  125. wrong.play()
  126. }, 700)
  127. }
  128. function setDate() {
  129. var newTime = new Date().getTime()
  130. var remainingTime = tim - newTime
  131. var remTime = Math.ceil(remainingTime / 1000)
  132. second.innerHTML = remTime
  133. second.classList.add('timeStyle')
  134. mes.removeEventListener('click', ready)
  135. sec.play()
  136. if (remTime === 0) {
  137. mes.innerHTML = `
  138. <div>You lose!</div>
  139. <div>Play again.</div>
  140. `
  141. clearInterval(timer)
  142. var a = box.children
  143. for (var x = 0; x < a.length; x++) {
  144. a[x].removeEventListener('click', click);
  145. }
  146. lose.play()
  147. mes.addEventListener('click', reset)
  148. }
  149. removeCss(second, 'timeStyle')
  150. }

Output

Now, you can right-click on the sublime text window stage. A dialog box appears, select->open a new browser.

Conclusion

Hope you found this blog interesting. We have created a pairing game using JavaScript. Thanks for reading.