帮助: 我对JS有点新,需要一个函数的帮助。 我需要将此功能修改为仅在得分< 20, 我还想对你进行四次变换,然后再不能滚动。
<html>
<head>
<font face="verdana">
<title>The Game Of Pig</title>
<body background="h.jpg">
<center>
<table border="1">
<script>
alert("Welcome to the Game of Pig!");
function roll() {
return Math.floor(Math.random() * 6) + 1;
}
function game() {
var die = document.getElementById("die"),
score = document.getElementById("score"),
d,
d = roll();
die.innerHTML = d;
if (d !== 1) {
score.innerHTML = parseInt(score.innerHTML, 10) + d;
}
if (d === 1) {
score.innerHTML = 0;
}
}
</script>
</head>
<body>
<form>
<h1>Dice Roll</h1>
<br>
<br>
<br>
<br>
<div style="width:200px;height:108px;border:4px solid black;">
<h2>Total: <span id="score">0</span></h2>
<div>
<span id="die">0</span>
</div>
<input type="button" value="Roll" id="roll" onclick="game();">
</center>
</form>
</body>
</font>
</html>
答案 0 :(得分:0)
不需要解析dom元素的内容。在全局范围内使用变量。他们将在游戏调用之间保持价值。
var die, score = 0, attempts = 0;
var die_div = document.getElementById("die");
var score_div = document.getElementById("score");
function game() {
var d = roll();
if(score + d > 20) return;
if(attempts > 3) return;
die_div.innerHTML = d;
score = (d=== 1)?0:(score+d);
score_div.innerHTML = score;
attempts++;
}
答案 1 :(得分:0)
while (score > 20) {
// do something
}