如何解决为什么window.addEventListener'keypress',functione不起作用
我创建了一个简单的Hangman游戏,可以在控制台上运行。一切正常,但是当我尝试使用window.addEventListener('keypress,function(e)时,我无法按游戏角色。
const Hangman = function(word,remainguess) {
this.word = word.toLowerCase().split('')
this.remainguess = remainguess
this.guessedLetters = []
}
Hangman.prototype.getPuzzle = function() {
let puzzle = ' '
this.word.forEach(element => {
if (this.guessedLetters.includes(element) || element === ' ') {
puzzle += element
} else {
puzzle += '*'
}
});
return puzzle
}
Hangman.prototype.guessing = function(guess) {
guess = guess.toLowerCase()
const isUnique = !this.guessedLetters.includes(guess)
const isBaddGuess = !this.word.includes(guess)
if (isUnique) {
this.guessedLetters.push(guess)
}
if (isBaddGuess) {
this.remainguess--
}
// return this.remainguess
}
const game1 = new Hangman('cat',1)
window.addEventListener('keypress',function(e) {
const guess = String.fromCharCode(e.charCode)
game1.guessing(guess)
console.log(game1.getPuzzle())
console.log(game1.remainguess)
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。