Answer the question
In order to leave comments, you need to log in
How to draw human body parts every time the player enters a wrong letter?
let canvas = document.querySelector('canvas')
let ctx = canvas.getContext('2d')
function pickWord() {
// Возвращает случайно выбранное слово
var words = [
"программа",
"макака",
"прекрасный",
"оладушек",
"телевизор",
"монитор",
"ручка",
"тапочек",
"карандаш",
"плита"
];
var word = words[Math.floor(Math.random() * words.length)];
return word;
};
function setupAnswerArray(word) {
let answerArray = [];
for (let i = 0; i < word.length; i++){answerArray[i] = '_';}
return answerArray;
};
function showPlayerProgress(answerArray) {
alert(answerArray.join(' '));
}
function getGuess() {
let guess = prompt('Угадайте букву или нажмите отмена для выхода из игры'.toLowerCase());
return guess;
}
function updateGame (guess, word, answerArray) {
// Обновляем answerArray по ответу игрока (guess)
let current = 0; // поставил счетчик
for (let j = 0; j < word.length; j++){
if (word[j] === guess && answerArray[j].includes('_')) {
answerArray[j] = guess;
current++;
}
}
return current;
// current возвращает число = сколько раз было встречена буква guess в слове
// с помощью этого обновляем remainingLetters которое = word.length
}
let showAnswerCongrat = function(answerArray) {
alert(answerArray.join(' '));
if (count === 0) {alert('GAME OVER')}
else if (remainingLetters === 0) {alert('well done! you now know secret word' + word)}
else {alert('learn JS')}
}
function happyMan(counts) {
if (count === 6) {ctx.lineWidth = 4;ctx.strokeRect(10, 10, 20, 20);ctx.stroke();}
else if (count === 5) {ctx.lineWidth = 4;ctx.beginPath();ctx.moveTo(20, 30);ctx.lineTo(20, 60);ctx.stroke();}
else if (count === 4) {ctx.lineWidth = 4;ctx.moveTo(20, 40);ctx.lineTo(50, 30);ctx.stroke();}
else if (count === 3) {ctx.lineWidth = 4;ctx.moveTo(20, 40);ctx.lineTo(0, 30);ctx.stroke();}
else if (count === 2) {ctx.lineWidth = 4;ctx.moveTo(20, 60);ctx.lineTo(40, 70);ctx.stroke();}
}
let word = pickWord();
let answerArray = setupAnswerArray(word)
let remainingLetters = word.length;
let count = 6;
let totalRemainigLetters = remainingLetters;
//игровой цикл
while (remainingLetters > 0 && count !== 0) {
showPlayerProgress(answerArray);
//запрос варианта ответа
let guess = getGuess();
if (guess === null){break;}
else if (guess.length !== 1) {
alert('Вводить нужно только одну букву!и пишите ее не с заглавной');
}
else {
let correctGuesses = updateGame(guess, word, answerArray);
remainingLetters -= correctGuesses;
totalRemainigLetters === remainingLetters
count--; // условие для ограниченного количества попыток
}
if(count > 0){
let showManParts = happyMan(count, word);
}
}
showAnswerCongrat(answerArray)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question