- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在可汗学院编写一个剪刀石头布游戏,这样我就可以看到视觉效果,但是 var Compare = function(choice1, choice2)
无法正常工作。 html 它工作得很好。插入了我的其余代码(请记住,视觉效果和按钮不是
background(0, 0, 0);
var userChoice = text("Do you choose rock, paper or scissors? Refresh to play again!", 25, 50); //starting text
var choice1 = userChoice;
var choice2 = computerChoice;
var winner = 25;
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} // Abover: computers choice, randomly chooses rock, paper or scissors
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
text("It is a draw!!! Try again!", winner, 50); // If both choices are the same it is a draw
}
};
if (choice1 === "rock") {
if (choice2 === "scissors") {
text("rock Wins!!!", winner, 50);
} else {
text("paper Wins!!!", winner, 50); // If the choices are rock and scissors, rock wins, if not then paper wins
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
text("paper Wins!!!", winner, 50);
} else {
if (choice2 === "scissors") {
text("scissors Wins!!!", winner, 50); // If the choices are rock and paper, paper wins, if not then scissors
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
text("rock Wins!!!", winner, 50);
} else {
if (choice2 === "paper") {
text("paper Wins!!!", winner, 50); // If the choices are scissors and rock, rock wins, if not then paper
}
}
}
}
}
// Above: compares the two choices to determine the winner, winner is rock, paper or scissors, not computer or user
text("User Choice: ", 20, 80 + userChoice);
text("Computer Choice: ", 20, 70 + computerChoice);
compare(userChoice, computerChoice); // Above: the message that tells the user who won
//Below: everything to do with the buttons
var squareW = 50;
var squareH = 50;
draw();
rect(75, 200, squareW, squareH); //left square
rect(175, 200, squareW, squareH); //middle square
rect(275, 200, squareW, squareH); //right square
最佳答案
我可以看到你现在已经弄清楚了,但出于好奇我还是尝试了一下。我做了一些更改,如下所示。我必须重新排列一些函数并进行一些嵌套,以便用户有机会在计算结果之前做出选择。我还删除了一些代码以避免文本重叠。我将让您进一步清理它,但您的游戏现在可以针对用户和计算机选择的每种可能的组合正常运行。
background(0, 0, 0);
var userChoice;
var computerChoice;
var winner = 200;
text("Do you choose rock, paper or scissors?", 25, 22);
var squareW = 50;
var squareH = 50;
draw();
text("ROCK", 75, 180, squareW, squareH);
rect(75, 200, squareW, squareH); //Choose rock by clicking anywhere to the left of this square's right edge.
text("PAPER", 175, 180, squareW, squareH);
rect(175, 200, squareW, squareH); //Choose paper by clicking anywhere between the left square's right edge and the right square's left edge.
text("SCISSORS", 275, 180, squareW + 10, squareH);
rect(275, 200, squareW, squareH); //Choose scissors by clicking anywhere to the right of this square's left edge.
mousePressed = function() {
//I wasn't sure how to keep the above background, squares and text from disappearing without repeating the code within this function.
background(0, 0, 0);
text("Do you choose rock, paper or scissors?", 25, 22);
draw();
text("ROCK", 75, 180, squareW, squareH);
rect(75, 200, squareW, squareH); //left square
text("PAPER", 175, 180, squareW, squareH);
rect(175, 200, squareW, squareH); //middle square
text("SCISSORS", 275, 180, squareW + 10, squareH);
rect(275, 200, squareW, squareH); //right square
if(mouseX < 125) {
userChoice = "rock";
} else if(mouseX < 225) {
userChoice = "paper";
} else {
userChoice = "scissors";
}
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
text("YOU BOTH WIN! YAY!", winner, 50);
}
};
if (userChoice === "rock") {
if (computerChoice === "scissors") {
text("YOU'RE A WINNER!!!", winner, 50);
} else if (computerChoice === "paper") {
text("YOU'RE A LOSER!!!", winner, 50); // I don't include a final else statement here because it would cause overlapping text and draws are already accounted for.
}
}
if (userChoice === "paper") {
if (computerChoice === "rock") {
text("YOU'RE A WINNER!!!", winner, 50);
} else if (computerChoice === "scissors") {
text("YOU'RE A LOSER!!!", winner, 50);
}
}
if (userChoice === "scissors") {
if (computerChoice === "paper") {
text("YOU'RE A WINNER!!!", winner, 50);
} else if (computerChoice === "rock") {
text("YOU'RE A LOSER!!!", winner, 50);
}
}
// I figured it would make more sense to specify if the user won rather than rock, paper or scissors.
text("Your Choice: " + userChoice, 50, 50);
text("Computer's Choice: " + computerChoice, 50, 100);
compare(userChoice, computerChoice);
};
关于javascript - 石头、剪刀、布、可汗学院,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38755074/
显然是在学习java。我能够让游戏正常运行。但我需要这样做,以便如果用户放置 R/P/S 以外的东西,那么它默认为 Rock。我不需要循环。如果我放石头的话,游戏就完美了。如果我放置 RPS 以外的任
我刚刚开始使用 python,需要一些帮助!我当时在做剪刀石头布游戏,我想在人或计算机赢得 3 场胜利后添加一个重启选项。 我已经四处寻找一些答案,但从我看到的所有其他代码来看,似乎超出了我的范围,或
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我的类(class)被分配了一个石头剪刀布游戏。以下是作业说明: 学习目标: 练习枚举的使用 创建一个包含构造函数、字段和方法的枚举 设计并实现您自己的 GUI 创建一个可运行的 jar 描述:编写一
我正在用Python创建一个石头剪刀布游戏。我的方法如下。然而,我不想打印玩家 1 和玩家 2,而是想让它们成为真实的名字。因此,可以说 John Wins 或 Joe Wins。如何实现每次打印玩家
我正在尝试完成类里面的石头、剪刀、布作业。 我收到“UnboundLocalError:赋值前引用的局部变量“绑定(bind)”” 错误。 有人可以告诉我为什么会收到此错误吗? import rand
我在 C++ 上学习石头剪刀布游戏时有一个游戏实例类。我想创建整数常量,它表示游戏中允许的符号数。对于经典的 rsp 游戏,它是 3(石头、剪刀和布),但是有一些有趣的 rcs 游戏扩展带有额外的符号
我正在上 Codecademy 类(class)(找到 here ),但一直告诉我“当输入是纸和石头时,您的代码返回‘石头获胜’而不是‘纸获胜’”,为什么?应该是正确的。既然它在谈论“石头获胜”,那么
我正在可汗学院编写一个剪刀石头布游戏,这样我就可以看到视觉效果,但是 var Compare = function(choice1, choice2) 无法正常工作。 html 它工作得很好。插入了我
对于我的类(class)项目,我们要制作一个“石头剪刀布”游戏,使用一个函数来显示菜单和验证输入,以及一个函数来确定谁赢了。当我绕过第一个函数时,我的代码将编译并正常运行。但是,当我同时使用这两个函数
我目前受困于我的 RPS 程序,因为它无法正确存储用户丢失的次数或与计算机的关联。例如,当我运行程序并输入“q”退出时,我得到以下输出: Enter R, P, S, or Q (for quit)
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 7 年前。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic在这里
我一直在尝试解决石头剪刀布项目,但我不知道如何做出 if/else 语句。我这样做了很多次,最后我认为我已经接近解决问题了,但问题是,每次运行程序时我都会得到错误的输出。 例如,我使用“Paper”,
以下是我在 BlueJ 中编写的石头剪刀布游戏的代码。当我编译并且用户输入输入时,计算机立即从playerWins()打印大量输出。当用户输入“退出”时游戏结束。有人可以帮助我,这样我的屏幕就不会被淹
如果之前曾问过此问题的某个版本,我们深表歉意。我查了一下,但找不到真正能解决我的问题的东西。 我今天开始学习Python,并尝试构建一个简单的石头剪刀布游戏。 我有以下代码,它运行得很好: impor
我今天一直在开发这个应用程序,但我似乎无法弄清楚代码有什么问题。 当我检查时,没有语法错误。但是当我尝试运行它并调试它时,它说 "Source not found" on "ActivityThrea
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我有 JS 中的基本“石头、剪刀、布”游戏的代码。它可以与提示一起使用,但我希望能够通过按钮做出选择。我想使用“getElementById”和“addEventListener(“click”)”。
有没有一种方法可以用大于号和小于号来做剪刀石头布游戏? 我知道执行 RPS 有不同的方法,但我想具体了解大于和小于。 这是我的代码: "Rock" > "Scissors" and 'Rock' "
我是一名优秀的程序员,十分优秀!