亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Hangman 游戲圖像僅限 JavaScript

Hangman 游戲圖像僅限 JavaScript

浮云間 2022-12-29 10:31:55
每次玩家未能猜出字母時,我該如何實現這些圖像?我應該把它放在哪里?當玩家猜不到1個字母時,會顯示劊子手1,然后是劊子手2,然后是劊子手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 stringwhil (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;          }        }       
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我的建議是將您的各種圖形存儲在一個數組中,并存儲當前圖形的索引 - 從零開始。


每次用戶得到錯誤答案時,您都會獲取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++]);

}

// ... //


查看完整回答
反對 回復 2022-12-29
?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

使用發電機!

要更好地了解什么是生成器,您可以訪問此處: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...

//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")

    }


  }


查看完整回答
反對 回復 2022-12-29
  • 2 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號