使以前选择的单词消失在html上,并在javascript的Hangman游戏中替换新单词

Make previous selected word go away on html and replace with new word in Hangman game for javascript

本文关键字:Hangman javascript 并在 游戏 替换 新单词 html 选择 单词 消失      更新时间:2023-09-26

我目前正在用javascript制作一个绞刑游戏,我正在努力如何删除和替换以前选择的单词,用一个新词。目前我的代码设置是,当玩家猜对单词时,就会弹出一条消息,询问用户是否想再玩一次。如果他们按下Y,我调用从数组中随机选择一个单词的函数,并使用push方法用与所选单词长度相同的空行填充和清空数组。但是当我在按下Y之后调用这个函数时,前面的单词不会消失,并且按键也不会注册。

    var hangmanObject = {
    randomWords: ['rock','paper','modular synthesizer', 'led zeppelin'],
    chosenWord: "",
    numWins: 0,
    numLives: 10,
    empty: [],
    incorrect: [],
    splitArray: []
    }
   function startFunction() 
   {
    document.getElementById("numLives").innerHTML = hangmanObject.numLives;
    displayChosenWord();
   }
   function displayChosenWord()
   {
   hangmanObject.chosenWord =  hangmanObject.randomWords[Math.floor(Math.random()*hangmanObject.randomWords.length)]
   hangmanObject.splitArray = hangmanObject.chosenWord.split("");
   for (x = 0; x < hangmanObject.chosenWord.length; x++)
   {
    if (hangmanObject.chosenWord.charAt(x) === " ")
        hangmanObject.empty.push(" ");
    else
        hangmanObject.empty.push(" _ ");
    }
        document.getElementById("blanks").innerHTML = hangmanObject.empty.join("");
    }

    document.onkeyup = function(event) 
    {      
   var userGuess = String.fromCharCode(event.keyCode).toLowerCase();

   for (x = 0; x < hangmanObject.chosenWord.length; x++)
        {
            if (hangmanObject.chosenWord.charAt(x) === userGuess)
            {
                hangmanObject.empty[x] = userGuess;
                document.getElementById("blanks").innerHTML = hangmanObject.empty.join("");
            }
        }
        if (hangmanObject.splitArray.indexOf(userGuess) === -1) //checking to see if wrong letter chosen
        {
                hangmanObject.numLives--;
                document.getElementById("numLives").innerHTML = hangmanObject.numLives;
                hangmanObject.incorrect.push(userGuess);
                document.getElementById("LettersGuessed").innerHTML = hangmanObject.incorrect;
        }
        console.log(hangmanObject.empty);
        if (hangmanObject.empty.indexOf(" _ ") === -1) 
        {
            hangmanObject.numWins++;
            // console.log("i won");
            document.getElementById("wins").innerHTML = hangmanObject.numWins;
            document.getElementById("Play").innerHTML = "Play Again? Y/N";
            document.onkeyup = function(event) 
            {      
               // Determines which exact key was selected. Make it lowercase
               var Choice = String.fromCharCode(event.keyCode).toLowerCase();
               if (Choice === 'y')
               {
                    hangmanObject.numLives = 10;
                    displayChosenWord();
               }
            }
        }
        if (hangmanObject.numLives <= 0)
        {
            document.getElementById("lose").innerHTML = "You Lose";
        }
        }

您正在回调中设置document.onkeyup回调,有效地禁用了字母猜测。

同样,empty数组永远不会被清空,所以下一个单词被附加到前一个单词的空字母数组中。这里有一个更简单的方法,通过使用gameState标志,你可以决定用户是输入字母进行猜测,还是决定再玩一次。此外,可以使用单个div表示status;)

var hangmanObject = {
  gameState: 'playing',
  randomWords: [
    'rock',
    'paper',
    'modular synthesizer',
    'led zeppelin'
  ],
  chosenWord: "",
  numWins: 0,
  numLives: 10,
  empty: [],
  incorrect: [],
  splitArray: []
}
function startFunction() {
  document.getElementById("numLives").innerHTML = hangmanObject.numLives;
  chooseNewWord();
}
function chooseNewWord() {
  hangmanObject.chosenWord = hangmanObject.randomWords[Math.floor(Math.random() * hangmanObject.randomWords.length)]
  hangmanObject.splitArray = hangmanObject.chosenWord.split("");
  // Reset guesses and misses
  hangmanObject.empty = [];
  hangmanObject.incorrect = [];
  for (x = 0; x < hangmanObject.chosenWord.length; x++) {
    if (hangmanObject.chosenWord.charAt(x) === " ")
      hangmanObject.empty.push(" ");
    else
      hangmanObject.empty.push("_");
  }
  document.getElementById("blanks").innerHTML = hangmanObject.empty.join(" ");
}
document.onkeyup = function(event) {
  var userGuess = String.fromCharCode(event.keyCode).toLowerCase();
  // Game status is "playing"
  if (hangmanObject.gameState === 'playing') {
    for (x = 0; x < hangmanObject.chosenWord.length; x++) {
      if (hangmanObject.chosenWord.charAt(x) === userGuess) {
        hangmanObject.empty[x] = userGuess;
        document.getElementById("blanks").innerHTML = hangmanObject.empty.join(" ");
      }
    }
    // checking to see if wrong letter chosen
    if (hangmanObject.splitArray.indexOf(userGuess) === -1) {
      hangmanObject.numLives--;
      document.getElementById("numLives").innerHTML = hangmanObject.numLives;
      hangmanObject.incorrect.push(userGuess);
      document.getElementById("LettersGuessed").innerHTML = hangmanObject.incorrect.join(",");
    }
    // Some debug
    console.log(hangmanObject.empty);
    // WIN situation
    if (hangmanObject.empty.indexOf("_") === -1) {
      hangmanObject.numWins++;
      // Set status message and game state
      document.getElementById("status").innerHTML = "You won " + hangmanObject.numWins + " times";
      hangmanObject.gameState = 'finished';
    }
    // LOSE situation
    if (hangmanObject.numLives <= 0) {
      // Set status message and game state
      document.getElementById("status").innerHTML = "You Lose";
      hangmanObject.gameState = 'finished';
    }
    // Set message if game finished
    if (hangmanObject.gameState === 'finished') {
      document.getElementById("Play").innerHTML = "Play Again? Y/N";
    }
    // Game status is "finished"
  } else {
    // If user selects play again
    if (userGuess === 'y') {
      // Set status back to "playing"
      hangmanObject.gameState = 'playing';
      // Reset lives and messages
      hangmanObject.numLives = 10;
      document.getElementById("status").innerHTML = "";
      document.getElementById("LettersGuessed").innerHTML = "";
      document.getElementById("Play").innerHTML = "";
      // Choose new word
      chooseNewWord();
    } else {
      // Set message
      document.getElementById("status").innerHTML = "Goodbye!";
      // Disable key handler
      document.onkeyup = null;
    }
  }
}
startFunction();
<div id="numLives"></div>
<div id="blanks"></div>
<div id="LettersGuessed"></div>
<div id="status"></div>
<div id="Play"></div>

words joining -需要清除数组

no keyup -你用'y/n' keyup替换了它,你需要重置它。

在新游戏中也值得清除错误的字母和生命。

见下面的工作示例:-

var hangmanObject = {
  randomWords: ['rock', 'paper', 'modular synthesizer', 'led zeppelin'],
  chosenWord: "",
  numWins: 0,
  numLives: 10,
  empty: [],
  incorrect: [],
  splitArray: []
}
function startFunction() {
  document.getElementById("numLives").innerHTML = hangmanObject.numLives;
  displayChosenWord();
}
function displayChosenWord() {
  hangmanObject.empty = []; // empty the array
  hangmanObject.incorrect = [];
  hangmanObject.chosenWord = hangmanObject.randomWords[Math.floor(Math.random() * hangmanObject.randomWords.length)]
  hangmanObject.splitArray = hangmanObject.chosenWord.split("");
  for (x = 0; x < hangmanObject.chosenWord.length; x++) {
    if (hangmanObject.chosenWord.charAt(x) === " ")
      hangmanObject.empty.push(" ");
    else
      hangmanObject.empty.push(" _ ");
  }
  document.getElementById("blanks").innerHTML = hangmanObject.empty.join("");
  document.getElementById("LettersGuessed").innerHTML = '';
  document.getElementById("numLives").innerHTML = hangmanObject.numLives;
  document.onkeyup = gameKeyUp;
}
function gameKeyUp(event) {
  var userGuess = String.fromCharCode(event.keyCode).toLowerCase();
  for (x = 0; x < hangmanObject.chosenWord.length; x++) {
    if (hangmanObject.chosenWord.charAt(x) === userGuess) {
      hangmanObject.empty[x] = userGuess;
      document.getElementById("blanks").innerHTML = hangmanObject.empty.join("");
    }
  }
  if (hangmanObject.splitArray.indexOf(userGuess) === -1) //checking to see if wrong letter chosen
  {
    hangmanObject.numLives--;
    document.getElementById("numLives").innerHTML = hangmanObject.numLives;
    hangmanObject.incorrect.push(userGuess);
    document.getElementById("LettersGuessed").innerHTML = hangmanObject.incorrect;
  }
  console.log(hangmanObject.empty);
  if (hangmanObject.empty.indexOf(" _ ") === -1) {
    hangmanObject.numWins++;
    // console.log("i won");
    document.getElementById("wins").innerHTML = hangmanObject.numWins;
    document.getElementById("Play").innerHTML = "Play Again? Y/N";
    document.onkeyup = function(event) {
    // Determines which exact key was selected. Make it lowercase
      var Choice = String.fromCharCode(event.keyCode).toLowerCase();
      if (Choice === 'y') {
        hangmanObject.numLives = 10;
        displayChosenWord();
      }
    }
  }
  if (hangmanObject.numLives <= 0) {
    document.getElementById("lose").innerHTML = "You Lose";
  }
} 
displayChosenWord();
document.onkeyup = gameKeyUp;
<div id="numLives"></div>
<div id="blanks"></div>
<div id="LettersGuessed"></div>
<div id="wins"></div>
<div id="Play"></div>
<div id="lose"></div>