我試圖教自己使用codeacademy.com編碼。我決定嘗試應用我學到的一些東西來構建基於網絡的岩石,紙張,剪刀,蜥蜴,spock遊戲。遊戲的代碼運行良好,我知道可能有更好的方法來做到這一點,但我後來想到了這一點,但我想爲玩家和計算機加上一個圓形計數器的分數。任何人都可以告訴我如何讓計數器工作,我已經嘗試了一些事情,但無法弄清楚。我需要一個JavaScript的圓形計數器和我的遊戲的得分計數器
一旦我有JavaScript的工作,我要建立一個單一的網站,練習HTML和CSS。
<!DOCTYPE html>
<html>
<head>
<script>
var rock = "rock"
var paper = "paper"
var scissors = "scissors"
var lizard = "lizard"
var spock = "spock"
var compScore = 1
var playerScore = 1
function roundUp() {
round += 1
}
function compare(choice1, choice2) {
var round = 1
var computerChoice = Math.random()
if (computerChoice <= 0.2) {
choice2 = "rock";
} else if(computerChoice <= 0.4) {
choice2 = "paper";
} else if (computerChoice <= 0.6) {
choice2 = "scissors";
} else if (computerChoice <= 0.8) {
choice2 = "lizard";
}
else {
choice2 = "spock";
}
if (choice1 === choice2) {
document.getElementById("winner").innerHTML = "Round tied!";
} else if (choice1 === rock) {
if (choice2 === paper) {
compScore += 1
document.getElementById("winner").innerHTML =
"Paper covers rock, computer wins";
}
else if (choice2 === scissors) {
playerScore += 1
document.getElementById("winner").innerHTML =
"Rock breaks scissors, you win";
}
else if (choice2 === lizard) {
playerScore += 1
document.getElementById("winner").innerHTML =
"Rock crushes lizzard, you win";
}
else {
compScore += 1
document.getElementById("winner").innerHTML =
"Spock vaporises rock, computer wins";
}
} else if (choice1 === paper) {
if (choice2 === rock) {
document.getElementById("winner").innerHTML =
"Paper covers rock, you win";
playerScore += 1
} else if (choice2 === scissors) {
document.getElementById("winner").innerHTML =
"Scissors cut paper, computer wins";
compScore += 1
} else if (choice2 === lizard) {
document.getElementById("winner").innerHTML =
"lizard eats paper, computer wins";
compScore += 1
} else {
document.getElementById("winner").innerHTML =
"Paper disproves spock, you win";
playerScore += 1
}
} else if (choice1 === scissors) {
if (choice2 === rock) {
document.getElementById("winner").innerHTML =
"Rock breaks scissors, computer wins";
compScore += 1
} else if (choice2 === paper) {
document.getElementById("winner").innerHTML =
"Scissors cut paper, you win";
playerScore += 1
}else if (choice2 === lizard) {
document.getElementById("winner").innerHTML =
"Scissors decapitate lizard, you win";
playerScore += 1
} else {
document.getElementById("winner").innerHTML =
"Spock smashes scissors, computer wins";
compScore += 1
}
} else if (choice1 === lizard) {
if (choice2 === rock) {
document.getElementById("winner").innerHTML =
"Rock crushes lizzard, computer wins";
compScore += 1
} else if (choice2 === scissors) {
document.getElementById("winner").innerHTML =
"Scissors decapitate lizard, computer wins";
compScore += 1
} else if (choice2 === paper) {
document.getElementById("winner").innerHTML =
"Lizard eats paper, you win";
playerScore += 1
} else {
document.getElementById("winner").innerHTML =
"Lizard poisons spock, you win";
playerScore += 1
}
} else {
if (choice2 === rock) {
document.getElementById("winner").innerHTML =
"Spock vaporises rock, you win";
playerScore += 1
} else if (choice2 === scissors) {
document.getElementById("winner").innerHTML =
"Spock smashes scissors, you win";
playerScore += 1
} else if (choice2 === paper) {
document.getElementById("winner").innerHTML =
"Paper disproves spock, computer wins";
compScore += 1
} else {
document.getElementById("winner").innerHTML =
"Lizard poisons spock, computer wins";
compScore += 1
}
}
document.getElementById("choice1").innerHTML = choice1
document.getElementById("choice2").innerHTML = choice2
document.getElementById("computerChoice").innerHTML = computerChoice
document.getElementById("playerScore").innerHTML = playerScore
document.getElementById("compScore").innerHTML = compScore
document.getElementById("round").innerHTML = round
roundUp()
}
</script>
</head>
<body>
<h1>My Web Page</h1>
<h3>Round</h3>
<p id="round"></p>
<h4>Player Choice</h4>
<p id="choice1"></p>
<h4>Player Score</h4>
<p id="playerScore"></p>
<h4>Computer Choice</h4>
<p id="choice2"></p>
<h4>Computer Score</h4>
<p id="compScore"></p>
<h4>And the winner is</h4>
<p id="winner"></p>
<button type="button" onclick="compare(rock, choice2)">Rock</button>
<button type="button" onclick="compare(paper, choice2)">Paper</button>
<button type="button" onclick="compare(scissors, choice2)">scissors</button>
<button type="button" onclick="compare(lizard, choice2)">lizard</button>
<button type="button" onclick="compare(spock, choice2)">Spock</button>
</body>
</html>
至少你錯過了一個分號。 – Roope 2014-09-30 16:36:49
如果你使用[jQuery](http://jquery.com/),這將會少得多的代碼。使用'document.getElementById()'是非常黑客。至少編寫你自己的包裝函數來讓這些減少冗長。你也迫切需要一個'switch'語句而不是整個'if's堆。 – tadman 2014-10-01 16:08:44
嗨tadman,感謝您的反饋。我對JavaScript非常陌生,上個星期六我只開始了codeacademy.com java模塊,並且在創建大部分遊戲後纔開始瞭解交換機。我應該在4到5天內到達jquery模塊,所以一旦我學到更多東西,我可能會重寫它。 – Marc 2014-10-01 19:47:48