为了账号安全,请及时绑定邮箱和手机立即绑定

将开始按钮从html链接到javascript函数以执行开始游戏的提示,提示在页面开始时开始

将开始按钮从html链接到javascript函数以执行开始游戏的提示,提示在页面开始时开始

素胚勾勒不出你 2022-12-29 15:08:44
我创建了一个非常基本的剪刀石头布射击游戏,您可以在其中使用 JavaScript 函数与计算机对战。我有一个非常基本的 HTML 框架,其中有一个按钮。我试图在按下时启动提示(游戏)。提示按我想要的方式工作,但一旦我打开窗口它就会启动脚本。我试图仅在按下 html 上的按钮时启动脚本。当我按下按钮时,控制台出现错误getPlayerChoice 未在 HTMLButtonElement.onclick 中定义这是我的代码:let playerWinCount = 0;let computerWinCount = 0;let roundCount = 0;function playRound() {function computerPlay() {    let arr = ["Rock", "Paper", "Scissors"];    let compChoice = arr[Math.floor(Math.random() * arr.length)];        return compChoice;}//function getPlayerChoice() {    var str = prompt("What is your selection for this round?");    if (!str) {        return;}    let newStr = str.toLowerCase();    let capStr = newStr[0].toUpperCase() + newStr.slice(1);        if (capStr !== "Rock" && capStr!== "Paper" && capStr !== "Scissors") {        return;} else {        return capStr;    }}//    let playerSelection = getPlayerChoice();    let computerSelection = computerPlay();                    if (playerSelection === computerSelection) {        alert("Try again... You both chose " + playerSelection + "!");        return;} else {        if (playerSelection === "Rock") {        if (computerSelection === "Scissors") {        alert("You win! Rock beats Scissors!");        playerWinCount++;        roundCount++;        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);        return;} else if (computerSelection === "Paper") {        alert("You lose! Paper beats Rock!");        computerWinCount++;        roundCount++;        console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);        return;    }    } else  if (playerSelection === "Paper") {        if (computerSelection === "Rock") {        alert("You win! Paper beats Rock!");        playerWinCount++;        roundCount++;        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);        return;    } 我注释掉了我的提示功能,因为每当程序运行时,它都会启动无法关闭的提示。如果没有输入,我需要一种关闭提示窗口的方法。
查看完整描述

1 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

您可能希望在game单击 Start Game 按钮时运行该函数,而不是在页面加载时运行它。


<input id="Start Game" type="button" value="Start Game" onclick="game();" />

现场示例:


let playerWinCount = 0;

let computerWinCount = 0;

let roundCount = 0;


function playRound() {

  function computerPlay() {

    let arr = ["Rock", "Paper", "Scissors"];

    let compChoice = arr[Math.floor(Math.random() * arr.length)];

    return compChoice;

  }


  function getPlayerChoice() {

    var str = prompt("What is your selection for this round?");

    if (!str) {

      return;

    }

    let newStr = str.toLowerCase();

    let capStr = newStr[0].toUpperCase() + newStr.slice(1);

    if (capStr !== "Rock" && capStr !== "Paper" && capStr !== "Scissors") {

      return;

    } else {

      return capStr;

    }

  }

  //

  let playerSelection = getPlayerChoice();

  let computerSelection = computerPlay();

  if (playerSelection === computerSelection) {

    alert("Try again... You both chose " + playerSelection + "!");

    return;

  } else {

    if (playerSelection === "Rock") {

      if (computerSelection === "Scissors") {

        alert("You win! Rock beats Scissors!");

        playerWinCount++;

        roundCount++;

        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;


      } else if (computerSelection === "Paper") {

        alert("You lose! Paper beats Rock!");

        computerWinCount++;

        roundCount++;

        console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;

      }


    } else if (playerSelection === "Paper") {

      if (computerSelection === "Rock") {

        alert("You win! Paper beats Rock!");

        playerWinCount++;

        roundCount++;

        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;


      } else if (computerSelection === "Scissors") {

        alert("You lose! Scissors beats Paper!");

        computerWinCount++;

        roundCount++;

        console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;

      }

    } else if (playerSelection === "Scissors") {

      if (computerSelection === "Rock") {

        alert("You lose! Rock beats Scissors!");

        computerWinCount++;

        roundCount++;

        console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;

      } else if (computerSelection === "Paper") {

        alert("You win! Scissors beats Paper!");

        playerWinCount++;

        roundCount++;

        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;

      }

    }

  }

}

//


function game() {

  while (roundCount < 5) {

    playRound();

  }

  if (playerWinCount > computerWinCount) {

    alert("Player wins! The score was " + playerWinCount + " - " + computerWinCount);

  } else if (computerWinCount > playerWinCount) {

    alert("Computer wins! The score was " + computerWinCount + " - " + playerWinCount);

  } else {

    alert("Something crazy happened and I have no idea who won!");

  }

}

<input id="Start Game" type="button" value="Start Game" onclick="game()" />


查看完整回答
反对 回复 2022-12-29
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信