我做了一个摇滚,纸张,剪刀游戏,我已经添加了蜥蜴和Spock部分。但是,很明显。我添加了可以使用Math.random与计算机相媲美的按钮,但我想要做的就是这样我可以让它自动提示你没有某种按钮。我想这样做,所以我可以添加一个密码,让你赢得每场比赛。这是我目前的代码。
<!DOCTYPE html>
<html>
<head>
<title>Rock, Paper, Scissors!</title>
<script src="main.js"></script>
<head>
<body>
<button onclick="play(0);">Rock</button>
<button onclick="play(1);">Paper</button>
<button onclick="play(2);">Scissors</button>
<button onclick="play(3);">Lizard</button>
<button onclick="play(4);">Spock</button>
</body>
</html>
以下是Javascript代码。
var gestures = ["rock", "paper", "scissors", "lizard", "spock"];
var rules = {
rock: { scissors: "breaks", lizard: "crushes" },
paper: { rock: "covers", spock: "disproves" },
scissors: { paper: "cuts", lizard: "decapitates" },
lizard: { paper: "eats", spock: "poisons" },
spock: { scissors: "smashes", rock: "vaporizes" }
};
function play(index) {
var your = gestures[index];
var mine = gestures[Math.floor(5 * Math.random())];
if (your === mine) return alert("Draw. We both played " + your + ".");
var win = rules[your].hasOwnProperty(mine);
var result = win ? "win" : "lose";
var a = win ? your : mine;
var b = win ? mine : your;
alert("You " + result + ": " + a + " " + rules[a][b] + " " + b + ".");
}
您可以在https://8bitguys.github.io/rock-paper-scissors/
看到游戏网站PS。我是Javascript的新手
答案 0 :(得分:0)
为什么不在html页面的body标签上使用onkeypress
事件?因此,您将检查是否按下某个键(例如,“p”表示提示,并且将是按键事件的ascii代码 112 )。一旦按下弹出弹出窗口,这样它就会“隐藏”给用户,但仍然存在。在此处查找ascii代码:http://www.theasciicode.com.ar/ascii-printable-characters/capital-letter-p-uppercase-ascii-code-80.html
OR
如果您只是在页面加载时启动它:只需在play(prompt("do you want to win?"));
函数
play()