在html中去掉先前选中的单词,并在javascript的Hangman游戏中替换为新单词
我目前正在用javascript制作一个刽子手游戏,我正在努力用一个新词来删除和替换之前选择的词。目前,我已经设置了我的代码,这样当玩家猜对单词时,就会弹出一条消息,询问用户是否想再次玩。如果他们按Y键,我将调用该函数,该函数从and数组中随机选择一个单词,并使用push方法用与所选单词长度相同的空行填充和清空数组。但当我在按下Y键后调用此函数时,前面的单词不会消失,按键也不会注册在html中去掉先前选中的单词,并在javascript的Hangman游戏中替换为新单词,javascript,html,Javascript,Html,我目前正在用javascript制作一个刽子手游戏,我正在努力用一个新词来删除和替换之前选择的词。目前,我已经设置了我的代码,这样当玩家猜对单词时,就会弹出一条消息,询问用户是否想再次玩。如果他们按Y键,我将调用该函数,该函数从and数组中随机选择一个单词,并使用push方法用与所选单词长度相同的空行填充和清空数组。但当我在按下Y键后调用此函数时,前面的单词不会消失,按键也不会注册 var hangmanObject = { randomWords: ['rock','paper
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";
}
}
var hangmanObject={
随机词:['rock'、'paper'、'modular synthesizer'、'led齐柏林飞艇'],
chosenWord:“,
numWins:0,
努姆利夫:10,
空:[],
不正确:[],
拆分数组:[]
}
函数startFunction()
{
document.getElementById(“numLives”).innerHTML=hangmanObject.numLives;
displayChosenWord();
}
函数displayChosenWord()
{
hangmanObject.chosenWord=hangmanObject.randomWords[Math.floor(Math.random()*hangmanObject.randomWords.length)]
hangmanObject.splitArray=hangmanObject.chosenWord.split(“”);
对于(x=0;x 如果(hangmanObject.numLives单词连接-您需要清除数组
无键控-您将其替换为“y/n”键控,您需要重置它
也值得清除错误的字母和生活在一个新的游戏
请参见下面的工作示例:-
var hangmanObject={
随机词:['rock','paper','modular synthesizer','led齐柏林飞艇'],
chosenWord:“,
numWins:0,
努姆利夫:10,
空:[],
不正确:[],
拆分数组:[]
}
函数startFunction(){
document.getElementById(“numLives”).innerHTML=hangmanObject.numLives;
displayChosenWord();
}
函数displayChosenWord(){
hangmanObject.empty=[];//清空数组
hangmanObject.Error=[];
hangmanObject.chosenWord=hangmanObject.randomWords[Math.floor(Math.random()*hangmanObject.randomWords.length)]
hangmanObject.splitArray=hangmanObject.chosenWord.split(“”);
对于(x=0;x 如果(hangmanObject.numLives您正在回调内部设置document.onkeyup
回调,则有效地禁用它以进行字母猜测
另外,empty
数组永远不会清空