简体   繁体   English

如何在剪刀石头布游戏中添加重置按钮?

[英]How do add a reset button to a Rock Paper Scissors game?

I'm learning JavaScript and having an issue trying to make a reset button for my game.我正在学习 JavaScript 并且在尝试为我的游戏制作重置按钮时遇到问题。 Im assuming the way I have coded it has scope tripping me up.我假设我编码它的方式有 scope 让我绊倒。 any pointers would help immensely.任何指针都会有很大帮助。 I have tried but the best i managed was to make the current score say 0 upon clicking the button, but if rock, paper or scissors were selected again, the score would jump straight back to where it was before + the result of the current round.我已经尝试过,但我做到的最好的方法是在单击按钮时使当前分数为 0,但如果再次选择石头、纸或剪刀,分数将直接跳回到之前的位置+当前回合的结果.

here is my code:这是我的代码:

let userScore = 0;
let computerScore = 0;

let choice = document.querySelector("#selections");
choice.addEventListener("click", event => {
    if (event.target.nodeName == "BUTTON") {
      let playerSelecion = (event.target.textContent);
      let computerSelection = getCompChoice();
      document.getElementById("player").innerHTML = playerSelecion;
      document.getElementById("computer").innerHTML = computerSelection;
      playRound(playerSelecion, computerSelection); 
      }
    });
  
function playRound(playerSelecion, computerSelection) {
    
    if (playerSelecion === computerSelection) {
        document.getElementById("currentRound").innerHTML = ("tie");
    } else if (playerSelecion === "rock" && computerSelection === "scissors") {
        userScore++;
        document.getElementById("currentRound").innerHTML = ("you win rock beats scissors");
    } else if (playerSelecion === "paper" && computerSelection === "rock") {
        userScore++
        document.getElementById("currentRound").innerHTML = ("you win paper beats rock")
    } else if (playerSelecion === "scissors" && computerSelection === "paper") {
        userScore++
        document.getElementById("currentRound").innerHTML = ("you win scissors beats paper")
    } else if (playerSelecion === "paper" && computerSelection === "scissors") {
        computerScore++
        document.getElementById("currentRound").innerHTML = ("you lose scissors beats paper")
    } else if (playerSelecion === "rock" && computerSelection === "paper") {
        computerScore++ 
        document.getElementById("currentRound").innerHTML = ("you lose paper beats rock")
    } else if (playerSelecion === "scissors" && computerSelection === "rock") {
        computerScore++
        document.getElementById("currentRound").innerHTML = ("you lose rock beats scissors")
    } if (userScore >= 5) {
        document.getElementById("result").innerHTML = "You win";
    } else if (computerScore >= 5) {
        document.getElementById("result").innerHTML = "Computer wins";
        }
    document.getElementById("userScore").innerHTML = userScore;
    document.getElementById("compScore").innerHTML = computerScore;
}


function getCompChoice() {
    let a = Math.random();
    if (a < 0.34) {
        return "rock";
    } else if (a <= 0.67) {
        return "paper";
    } else {
        return "scissors";
    }
}```
//so i think you want a reset button to your game . and that button should make your game to user score and comp score to "0".

//so you can make a button and add some reset code to that so that when someone click on the button the game will reset the user and comp score to "0".

//adding a reset button
<button onclick="reset()">Reset</button>//remember to put () here
//normal function
function reset(){
    document.getElementById("userScore").innerHTML = 0;
    document.getElementById("compScore").innerHTML = 0;
}
//with arrow function
let reset=()=>{
    document.getElementById("userScore").innerHTML = 0;
    document.getElementById("compScore").innerHTML = 0;
}

To avoid the previous results to be added with the new score, the scores should be set to 0 in your reset button too.为避免以前的结果与新分数一起添加,您的重置按钮中的分数也应设置为 0。

//create a reset button
 <button onclick="resetScore()"> Reset The Game </button>

//reset function

const reset = () => {
   useScore = 0;
   computerScore = 0;
   document.querySelector("#userScore").innerHTML = userScore;
   document.querySelector("#compScore").innerHTML = computerScore;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM