猿问

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

我创建了一个非常基本的剪刀石头布射击游戏,您可以在其中使用 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;

    

我注释掉了我的提示功能,因为每当程序运行时,它都会启动无法关闭的提示。

如果没有输入,我需要一种关闭提示窗口的方法。


素胚勾勒不出你
浏览 77回答 1
1回答

慕标琳琳

您可能希望在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() {&nbsp; function computerPlay() {&nbsp; &nbsp; let arr = ["Rock", "Paper", "Scissors"];&nbsp; &nbsp; let compChoice = arr[Math.floor(Math.random() * arr.length)];&nbsp; &nbsp; return compChoice;&nbsp; }&nbsp; function getPlayerChoice() {&nbsp; &nbsp; var str = prompt("What is your selection for this round?");&nbsp; &nbsp; if (!str) {&nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; let newStr = str.toLowerCase();&nbsp; &nbsp; let capStr = newStr[0].toUpperCase() + newStr.slice(1);&nbsp; &nbsp; if (capStr !== "Rock" && capStr !== "Paper" && capStr !== "Scissors") {&nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; return capStr;&nbsp; &nbsp; }&nbsp; }&nbsp; //&nbsp; let playerSelection = getPlayerChoice();&nbsp; let computerSelection = computerPlay();&nbsp; if (playerSelection === computerSelection) {&nbsp; &nbsp; alert("Try again... You both chose " + playerSelection + "!");&nbsp; &nbsp; return;&nbsp; } else {&nbsp; &nbsp; if (playerSelection === "Rock") {&nbsp; &nbsp; &nbsp; if (computerSelection === "Scissors") {&nbsp; &nbsp; &nbsp; &nbsp; alert("You win! Rock beats Scissors!");&nbsp; &nbsp; &nbsp; &nbsp; playerWinCount++;&nbsp; &nbsp; &nbsp; &nbsp; roundCount++;&nbsp; &nbsp; &nbsp; &nbsp; console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; } else if (computerSelection === "Paper") {&nbsp; &nbsp; &nbsp; &nbsp; alert("You lose! Paper beats Rock!");&nbsp; &nbsp; &nbsp; &nbsp; computerWinCount++;&nbsp; &nbsp; &nbsp; &nbsp; roundCount++;&nbsp; &nbsp; &nbsp; &nbsp; console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else if (playerSelection === "Paper") {&nbsp; &nbsp; &nbsp; if (computerSelection === "Rock") {&nbsp; &nbsp; &nbsp; &nbsp; alert("You win! Paper beats Rock!");&nbsp; &nbsp; &nbsp; &nbsp; playerWinCount++;&nbsp; &nbsp; &nbsp; &nbsp; roundCount++;&nbsp; &nbsp; &nbsp; &nbsp; console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; } else if (computerSelection === "Scissors") {&nbsp; &nbsp; &nbsp; &nbsp; alert("You lose! Scissors beats Paper!");&nbsp; &nbsp; &nbsp; &nbsp; computerWinCount++;&nbsp; &nbsp; &nbsp; &nbsp; roundCount++;&nbsp; &nbsp; &nbsp; &nbsp; console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else if (playerSelection === "Scissors") {&nbsp; &nbsp; &nbsp; if (computerSelection === "Rock") {&nbsp; &nbsp; &nbsp; &nbsp; alert("You lose! Rock beats Scissors!");&nbsp; &nbsp; &nbsp; &nbsp; computerWinCount++;&nbsp; &nbsp; &nbsp; &nbsp; roundCount++;&nbsp; &nbsp; &nbsp; &nbsp; console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; } else if (computerSelection === "Paper") {&nbsp; &nbsp; &nbsp; &nbsp; alert("You win! Scissors beats Paper!");&nbsp; &nbsp; &nbsp; &nbsp; playerWinCount++;&nbsp; &nbsp; &nbsp; &nbsp; roundCount++;&nbsp; &nbsp; &nbsp; &nbsp; console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }}//function game() {&nbsp; while (roundCount < 5) {&nbsp; &nbsp; playRound();&nbsp; }&nbsp; if (playerWinCount > computerWinCount) {&nbsp; &nbsp; alert("Player wins! The score was " + playerWinCount + " - " + computerWinCount);&nbsp; } else if (computerWinCount > playerWinCount) {&nbsp; &nbsp; alert("Computer wins! The score was " + computerWinCount + " - " + playerWinCount);&nbsp; } else {&nbsp; &nbsp; alert("Something crazy happened and I have no idea who won!");&nbsp; }}<input id="Start Game" type="button" value="Start Game" onclick="game()" />
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答