quizEngine/js/game.js

42 lines
918 B
JavaScript
Raw Normal View History

2021-07-04 21:37:21 +03:00
// функции игры
export function imagePreloader(images, callback) {
let counter = 0;
2021-07-06 05:27:07 +03:00
// norm
function onLoad() {
counter += 1;
if (counter == images.length) callback();
}
let assetsDiv = document.getElementById("assets");
for (let i of images) {
let img = document.createElement('img');
img.onload = img.onerror = onLoad;
img.src = `assets/images/${i}`;
}
2021-07-06 05:27:07 +03:00
}
export function checkAnswer(quest, answer) {
if (quest.rightAnswer.toLowerCase() == answer.toLowerCase()) {
quest.status = true;
2021-07-04 23:24:27 +03:00
}
2021-07-04 21:37:21 +03:00
else
quest.status = false;
2021-07-04 22:12:04 +03:00
}
2021-07-04 23:07:38 +03:00
2021-07-06 05:27:07 +03:00
export function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
2021-07-04 23:07:38 +03:00
const j = Math.floor(Math.random() * (i + 1));
2021-07-06 05:27:07 +03:00
[array[i], array[j]] = [array[j], array[i]];
2021-07-04 23:07:38 +03:00
}
}
2021-07-06 06:10:22 +03:00
export function restartGame(game, quests) {
shuffle(quests);
quests.forEach(element => element.status = null);
game.finish = false;
game.currentQuest = 0;
}