简体   繁体   English

刽子手游戏映像仅 javascript

[英]Hangman game image javascript only

How do I implement these images each time player failed to guess a letter and where should I put it at?每次玩家猜不出字母时,我该如何实现这些图像,我应该把它放在哪里? When player failed to guess 1 letter, hangman 1 will display, then followed with hangman 2 then hangman 3 and so on?当玩家猜出1个字母失败时,会显示hangman 1,然后是hangman 2,然后是hangman 3,以此类推? Here is my code.这是我的代码。 images is in the console.log图像在 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")
    }

  }

What I would suggest is storing your various graphics in an array, and storing an index to which is the current graphics - start at zero.我建议将您的各种图形存储在一个数组中,并存储一个索引,该索引是当前图形 - 从零开始。

Every time the user gets a wrong answer, you console.log the current index and then increment the index:每次用户得到错误答案时,您都console.log当前索引,然后增加索引:

 console.log(graphicsArray[graphicsIndex++]);

There is a demo of this below, using a button press to simulate a wrong answer.下面有一个演示,使用按钮来模拟错误的答案。 try it out.试试看。

 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>

You would do this in the part of your code which decrements the number of lives.您可以在减少生命数量的代码部分执行此操作。

// ... //
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++]);
}
// ... //

Use a Generator !使用发电机!
To better understand what a generator is you can go here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*为了更好地了解生成器是什么,您可以在此处查看 go: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*

 /* This function generates your images */ function* hangmanGenerator() { yield console.log(" _|_\n| |_____\n| |\n|_________|"); yield console.log(" _____\n | |\n |\n |\n _|_\n| |_____\n| |\n|_________|\n"); yield console.log(" _____\n | |\n | o\n | | \n _|_ \ \n| |_____\n| |\n|_________|\n"); yield console.log(" _____\n | |\n | o\n | /|\\ \n _|_ \ \n| |_____\n| |\n|_________|\n"); yield console.log(" _____\n | |\n | o\n | /|\\ \n _|_ / \\ \n| |_____\n| |\n|_________|\n"); } /* This is the istance of your image generator */ const generator = hangmanGenerator(); /* This is how you get the images out of your generator */ generator.next().value /* Hangman 1*/ generator.next().value /* Hangman 2*/ generator.next().value /* Hangman 3*/ generator.next().value /* Hangman 4*/ generator.next().value /* Hangman 5*/ generator.next().value /* No value because you're out of yields -> game over */

Your code should look like this:您的代码应如下所示:

 //Your code... //The function definition can go wherever you want in your code as long as it's before the while loop function* hangmanGenerator() { yield console.log(" _|_\n| |_____\n| |\n|_________|"); yield console.log(" _____\n | |\n |\n |\n _|_\n| |_____\n| |\n|_________|\n"); yield console.log(" _____\n | |\n | o\n | | \n _|_ \ \n| |_____\n| |\n|_________|\n"); yield console.log(" _____\n | |\n | o\n | /|\\ \n _|_ \ \n| |_____\n| |\n|_________|\n"); yield console.log(" _____\n | |\n | o\n | /|\\ \n _|_ / \\ \n| |_____\n| |\n|_________|\n"); } //As the generator definition you can istanciate the generator object wherever you need //as long as it's visible in the while loop const generator = hangmanGenerator(); // 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"). //HERE you show the hangman in the console generator;next();value. } } } 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") } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM