From 6aaad0c45d6e12be92bf36016c5485d2743d6a57 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Tue, 6 Jul 2021 06:10:22 +0300 Subject: [PATCH] restart game realize --- app/styles.css | 8 ++++---- js/game.js | 7 +++++++ js/index.js | 26 ++++++++++++++++++++------ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/styles.css b/app/styles.css index 7e0a316..9b2e64d 100644 --- a/app/styles.css +++ b/app/styles.css @@ -1,7 +1,7 @@ @font-face { font-family: 'Yanone Kaffeesatz'; - src: url('YanoneKaffeesatz-Bold.woff2') format('woff2'), - url('YanoneKaffeesatz-Bold.woff') format('woff'); + src: url('assets/fonts/YanoneKaffeesatz-Bold.woff2') format('woff2'), + url('assets/fonts/YanoneKaffeesatz-Bold.woff') format('woff'); font-weight: bold; font-style: normal; font-display: swap; @@ -9,8 +9,8 @@ @font-face { font-family: 'Yanone Kaffeesatz'; - src: url('YanoneKaffeesatz-Regular.woff2') format('woff2'), - url('YanoneKaffeesatz-Regular.woff') format('woff'); + src: url('assets/fonts/YanoneKaffeesatz-Regular.woff2') format('woff2'), + url('assets/fonts/YanoneKaffeesatz-Regular.woff') format('woff'); font-weight: normal; font-style: normal; font-display: swap; diff --git a/js/game.js b/js/game.js index edcf7d8..43150e6 100644 --- a/js/game.js +++ b/js/game.js @@ -22,3 +22,10 @@ export function shuffle(array) { [array[i], array[j]] = [array[j], array[i]]; } } + +export function restartGame(game, quests) { + shuffle(quests); + quests.forEach(element => element.status = null); + game.finish = false; + game.currentQuest = 0; +} diff --git a/js/index.js b/js/index.js index 7e0c815..77bddcb 100644 --- a/js/index.js +++ b/js/index.js @@ -5,7 +5,7 @@ import gameData from '../app/gameData.json'; // game data import { getMousePos, isInside } from './buttons.js'; import { clearContext, getCenterH, getCenterV } from './draw.js'; -import { loadingLogo, checkAnswer, shuffle } from './game.js'; +import { loadingLogo, checkAnswer, shuffle, restartGame } from './game.js'; // Engine variables ------------------------------------- const DEBUG = config.debug; @@ -74,13 +74,22 @@ window.onload = function() { let mousePos = getMousePos(canvas, evt); for (const [key, value] of Object.entries(buttons)) { - if (isInside(mousePos, value)) { - checkAnswer(gameData.questions[game.currentQuest], value.data); + if (!game.finish && key != undefined) { + if (isInside(mousePos, value)) { + checkAnswer(gameData.questions[game.currentQuest], value.data); - if (game.currentQuest < gameData.questions.length - 1) { - game.currentQuest += 1; + if (game.currentQuest < gameData.questions.length - 1) { + game.currentQuest += 1; + } + else game.finish = true; } - else game.finish = true; + } + } + + if (game.finish && buttons.btnRestart != undefined) { + if (isInside(mousePos, buttons.btnRestart)) { + restartGame(game, gameData.questions); + delete buttons.btnRestart; } } }, false); @@ -132,6 +141,11 @@ function update() { } if (game.finish) { + delete buttons.answerButton0; + delete buttons.answerButton1; + delete buttons.answerButton2; + delete buttons.answerButton3; + buttons.btnRestart = { x: getCenterH(cW, cW / 1.5), y: areas.finish.labelTotalInfo.y + areas.finish.labelTotalInfo.h + 20, w: cW / 1.5, h: 70, data: "Ответить на вопросы заново" }; } }