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.
HTML code to display Pairing Game
Now we insert a new HTML Page on this pairing game. The code is given below.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Pairing Game</title>
- <link href="https://fonts.googleapis.com/css?family=Indie+Flower&display=swap" rel="stylesheet">
- <link href="css/bootstrap.css" rel="stylesheet">
- <link href="css/style.css" rel="stylesheet">
- </head>
- <body>
- <div class='main'>
- <div class='top'>
- <div class='board score'>
- <h5>Score</h5>
- <p class='sc'>0</p>
- </div>
- <div class='mes'>Start</div>
- <div class='board time'>
- <h5>Seconds</h5>
- <p class='second'>30</p>
- </div>
- </div>
- <div class='bottom'>
- <div class='box'></div>
- </div>
- </div>
- <audio data-key='select' src='libs/sounds/bounce.mp3'></audio>
- <audio data-key='wrong' src='libs/sounds/wrong.mp3'></audio>
- <audio data-key='correct' src='libs/sounds/correct.mp3'></audio>
- <audio data-key='seconds' src='libs/sounds/seconds.mp3'></audio>
- <audio data-key='yay' src='libs/sounds/yay.mp3'></audio>
- <audio data-key='lose' src='libs/sounds/lose.mp3'></audio>
- </body>
- <script src='./js/script.js'></script>
-
- </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.
- const fruits = ['carrot', 'cherry', 'apple', 'pineapple', 'pumpkin', 'strawberry']
- const box = document.querySelector('.box')
- const mes = document.querySelector('.mes')
- const score = document.querySelector('.sc')
- const second = document.querySelector('.second')
- const sec = document.querySelector('audio[data-key="seconds"]')
- const select = document.querySelector('audio[data-key="select"]')
- const wrong = document.querySelector('audio[data-key="wrong"]')
- const correct = document.querySelector('audio[data-key="correct"]')
- const yay = document.querySelector('audio[data-key="yay"]')
- const lose = document.querySelector('audio[data-key="lose"]')
- let tim;
- let timer;
-
- fruits.push(...fruits)
-
- function shuffle(arr) {
- var val1 = arr.length, temp, index;
- while (val1 > 0) {
- index = Math.floor(Math.random() * val1);
- val1--;
- temp = arr[val1];
- arr[val1] = arr[index];
- arr[index] = temp;
- }
- return arr;
- }
-
- window.onload = init;
-
- function init() {
- var divs = (shuffle(fruits))
- for (var x = 0; x < divs.length; x++) {
- box.innerHTML += `
- <div class='placer ${x}'>
- <div class='holder ${divs[x]}'>
- <div class='front'></div>
- <img class='back' src='libs/img/${divs[x]}.png'>
- </div>
- </div>
- `
- }
- showBox();
- }
-
- function showBox() {
- mes.addEventListener('click', ready)
- }
-
- function ready() {
- var a = box.children;
- this.innerHTML = 'Ready!'
- tim = new Date().getTime() + 30000
- timer = setInterval(setDate, 1000)
- for (var x = 0; x < a.length; x++) {
- a[x].addEventListener('click', click);
- }
- }
-
-
- function click() {
- var b = this.firstElementChild;
- b.classList.add('flip');
- evaluator(this);
- }
-
- var selectedBox = [];
- function evaluator(a) {
- select.play()
-
- if (a.classList[1] === undefined) {
- return
- }
-
- selectedBox.push(a);
-
- if (selectedBox.length === 1) {
- mes.innerHTML = "Be sure!";
- }
-
- if (selectedBox.length === 2) {
- if (selectedBox[0].firstElementChild.classList[1] == selectedBox[1].firstElementChild.classList[1]) {
- removePic(selectedBox)
- selectedBox = [];
- } else {
- hidePic(selectedBox);
- selectedBox = [];
- }
- }
- }
-
- var scoreCard = 1;
- function scoreBoard() {
- score.innerHTML = scoreCard++;
- score.classList.add('addSc')
- if (scoreCard === 7) {
- endGame()
- }
- removeCss(score, 'addSc')
- }
-
- function endGame() {
- setTimeout(function () {
- mes.innerHTML = `
- <div>You win!</div>
- <div>Start again.</div>
- `
- clearInterval(timer)
- var a = box.children
- for (var x = 0; x < a.length; x++) {
- a[x].removeEventListener('click', click);
- }
- mes.addEventListener('click', reset)
- yay.play()
- }, 700)
- }
-
- function reset() {
- window.location.reload(true)
- }
-
- function removeCss(element, cssName) {
- setTimeout(function () {
-
- element.classList.remove(cssName)
- }, 700)
- }
-
-
- function removePic(a) {
- a[0].classList.remove(a[0].classList[1])
- a[1].classList.remove(a[1].classList[1])
- setTimeout(function () {
- correct.play()
- mes.innerHTML = 'Great Job!';
- scoreBoard();
- }, 700)
- }
-
- function hidePic(a) {
- setTimeout(function () {
- for (var x = 0; x < 2; x++) {
- a[x].firstElementChild.classList.remove('flip')
- }
- mes.innerHTML = "Wrong!";
- wrong.play()
- }, 700)
- }
-
-
- function setDate() {
- var newTime = new Date().getTime()
- var remainingTime = tim - newTime
- var remTime = Math.ceil(remainingTime / 1000)
- second.innerHTML = remTime
- second.classList.add('timeStyle')
- mes.removeEventListener('click', ready)
- sec.play()
- if (remTime === 0) {
- mes.innerHTML = `
- <div>You lose!</div>
- <div>Play again.</div>
- `
- clearInterval(timer)
- var a = box.children
- for (var x = 0; x < a.length; x++) {
- a[x].removeEventListener('click', click);
- }
- lose.play()
- mes.addEventListener('click', reset)
- }
- removeCss(second, 'timeStyle')
- }
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.