用户丢失时如何重置游戏

时间:2019-06-11 20:42:20

标签: javascript

我已经上了一个月的学校课程,所以我对此很陌生。我正在制作一个游戏,其中计算机收到一个随机字母,并且用户有10个猜测可以正确回答。用户剩下0个猜测后,将显示一条Confirm()消息。我想将猜测值重置为10,并消除猜测的字母,同时保持得失。有没有一种方法可以不刷新页面?

var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", 
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", 
"z"];
var wins = 0;
var losses = 0;
var guessesLeft = 10;
var guessesLetter = "";
var letter = []

var winsText = document.getElementById("wins-text");
var lossesText = document.getElementById("losses-text");
var guessesLeftText = document.getElementById("guesses-left-text");
var guessesLetterText = document.getElementById("guesses-letter-text");

 document.onkeyup = function(event) {

  var userGuess = event.key;
    letter.push(userGuess)
  var computerGuess = letters[Math.floor(Math.random() * letters.length)];

    if (userGuess === computerGuess) {
        wins++;
    }else {
        guessesLeft--;
    }
    if (guessesLeft === 0) {
        losses++;
        confirm("GAME OVER, SUCKA! Try again?")
    }

3 个答案:

答案 0 :(得分:0)

制作一个具有逻辑名称的方法(例如,reset_game())。在此方法中,我们可以通过说letter = []清除数组字母,并通过说guessesLeft = 10重设剩余的猜测。就像ps2goat所说的,应该在确认后再试一次。

答案 1 :(得分:0)

function game() {
    var letters = [
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
        "z"
    ];
    var wins = 0;
    var losses = 0;
    var guessesLeft = 10;
    var guessesLetter = "";
    var letter = [];

    var winsText = document.getElementById("wins-text");
    var lossesText = document.getElementById("losses-text");
    var guessesLeftText = document.getElementById("guesses-left-text");
    var guessesLetterText = document.getElementById("guesses-letter-text");

    document.onkeyup = function(event) {

        var userGuess = event.key;
        letter.push(userGuess);
        var computerGuess = letters[Math.floor(Math.random() * letters.length)];

        if (userGuess === computerGuess) {
            wins++;
        } else {
            guessesLeft--;
        }
        if (guessesLeft === 0) {
            losses++;
            confirm("GAME OVER, SUCKA! Try again?");
        }
    };

    function start()
    {
        // Set all elements to default values here
    }
}

然后,您可以根据需要创建游戏实例。

var game1 = new game();
game1.start();

// on start game button
game1 = new game();

别忘了在任何用户界面中重置您的值。

答案 2 :(得分:0)

// Countdown function. After timer hits 0, the 'counter turns red, and gong sounds through timerSound(), and options() runs
const countDown = ({minutes, seconds}) => {
    if (!minutes && seconds === '00'){
        const timer = document.getElementById('counter');

        options();
        timerSound();
        return;
}
// Sets timer. Subtracts the minutes set by 1
const myTimer = setTimeout(() => {
    if (seconds === '00'){
       minutes = minutes - 1;
     seconds = 60;
   }
// Subtracts the seconds
 seconds = seconds - 1;
 if (seconds < 10){
     seconds = '0' + seconds;
 }
 document.getElementById('minutes').innerHTML = minutes + " :";
 document.getElementById('seconds').innerHTML = seconds;

   countDown({minutes, seconds});
}, 1000);
};
// Function that allows the CountDown to run
const initApp = () => {
countDown({
 minutes: 25,
 seconds: 01
    });
};

// Gets rid of the pausePlay class and displays the middlebuttons
const options = () => {
    const $pausePlay = document.getElementsByClassName("pausePlay");
    const $middleButtons = document.getElementsByClassName("middleButtons");

    for (var i = 0; i < $pausePlay.length; i++) {
        $pausePlay[i].style.display = "none";
    }
    for(var i = 0; i < $middleButtons.length; i++){
        $middleButtons[i].style.display = "block";
    }

    const fiveMinButton = document.getElementById('fiveMin');
    fiveMinButton.addEventListener('click', fiveMinBreak);

    const restartButton = document.getElementById('redo');
    redo.addEventListener('click', restartTimer);

    const endSession = document.getElementById('endSess');
    endSession.addEventListener('click', endSess);


}

//  Pause button
 const handleClickPauseButton = () => {
    clearInterval(timer);
    const $pauseButton = document.getElementById("pause");


 };
// Play button 
 const handleClickPlayButton = () => {

    const $playButton = document.getElementById("play");

 };