javascript - 刽子手游戏图像仅 javascript
问题描述
每次玩家猜不出字母时,我该如何实现这些图像,我应该把它放在哪里?当玩家猜出1个字母失败时,会显示hangman 1,然后是hangman 2,然后是hangman 3,以此类推?这是我的代码。图像在 console.log 中
/* hangman 1
console.log(" _|_\n| |_____\n| |\n|_________|");
hangman 2
console.log(" _____\n | |\n |\n |\n _|_\n| |_____\n| |\n|_________|\n");
hangman 3
console.log(" _____\n | |\n | o\n | | \n _|_ \ \n| |_____\n| |\n|_________|\n");
hangman 4
console.log(" _____\n | |\n | o\n | /|\\ \n _|_ \ \n| |_____\n| |\n|_________|\n");
hangman 5
console.log(" _____\n | |\n | o\n | /|\\ \n _|_ / \\ \n| |_____\n| |\n|_________|\n");
*/
// Show player their progress | .join returned answer as a string
while (remainingLetters > 0 && lives > 0) {
(answerArray.join(""));
guess = readline.question(name + "'s guess (Enter 9 for lifelines or 0 to pass): ");
guess = guess.toUpperCase();
//if guess is more than 1 letter or no letter, alert player to guess 1 letter only
if (guess.length !== 1) {
console.log("Please enter 1 letter only.");
}
//if valid guess
else {
if (guesses.includes(guess)) {
console.log("\nYou have already made this guess, please try another letter!\n");
} else {
guesses.push(guess);
correctGuess = 0;
for (var j = 0; j < Word.length; j++) {
if (Word[j] == guess) {
answerArray[j] = guess;
remainingLetters--;
correctGuess = 1;
}
}
if (correctGuess == 1) {
console.log("\nGood job! " + guess + " is one of the letters!\n");
console.log(JSON.stringify(answerArray) + "\n");
console.log(JSON.stringify(alphabets) + "\n");
} else {
lives -= 1;
console.log("\nSorry. " + guess + " is not a part of the word.\n");
console.log(JSON.stringify(answerArray) + "\n");
console.log(JSON.stringify(alphabets) + "\n");
console.log("You have " + lives + " lives remaining.\n");
}
}
}
if (remainingLetters == 0) {
console.log("Congratulation! You managed to guess the word!\n");
break;
}
if (lives == 0) {
console.log("Game Over... You failed to guess the word. The word is " + Word + ".\n")
}
}
解决方案
我建议将您的各种图形存储在一个数组中,并存储一个索引,该索引是当前图形 - 从零开始。
每次用户得到错误答案时,您都会console.log
使用当前索引,然后增加索引:
console.log(graphicsArray[graphicsIndex++]);
下面有一个演示,使用按钮来模拟错误的答案。试试看。
var graphicsArray = [];
graphicsArray.push(" _|_\n| |_____\n| |\n|_________|");
graphicsArray.push(" _____\n | |\n |\n |\n _|_\n| |_____\n| |\n|_________|\n");
graphicsArray.push(" _____\n | |\n | o\n | | \n _|_ \ \n| |_____\n| |\n|_________|\n");
graphicsArray.push(" _____\n | |\n | o\n | /|\\ \n _|_ \ \n| |_____\n| |\n|_________|\n");
graphicsArray.push(" _____\n | |\n | o\n | /|\\ \n _|_ / \\ \n| |_____\n| |\n|_________|\n");
var graphicsIndex = 0;
document.querySelector("#demo").onclick = () => {
console.log(graphicsArray[graphicsIndex++]);
}
<button id="demo">press me</button>
您可以在减少生命数量的代码部分执行此操作。
// ... //
if (correctGuess == 1) {
console.log("\nGood job! " + guess + " is one of the letters!\n");
console.log(JSON.stringify(answerArray) + "\n");
console.log(JSON.stringify(alphabets) + "\n");
} else {
lives -= 1;
console.log("\nSorry. " + guess + " is not a part of the word.\n");
console.log(JSON.stringify(answerArray) + "\n");
console.log(JSON.stringify(alphabets) + "\n");
console.log("You have " + lives + " lives remaining.\n");
console.log(graphicsArray[graphicsIndex++]);
}
// ... //
推荐阅读
- java - 如何在Java中将元素添加到对象数组中
- python - 深度学习/Keras:我应该对非常小的数据(输入和输出值)使用非常小的学习率吗?
- javascript - Vuex ReferenceError 未定义。如何正确设置值?
- flutter - Flutter:如何从ongenerate Route注销?
- r - R中的链接分配
- c# - 连接未正确关闭 ASP.NET C#
- cmake - 从 yocto SDK 生成 aarch64 RPM 包
- web - 删除 URL 转发 - Freenom
- google-sheets - Google 表格/Excel:将 IMAGE() 公式转换为纯 URL
- react-native - 在对齐模式下反应原生和阿拉伯文本