【发布时间】:2015-10-20 00:55:36
【问题描述】:
我正在尝试使用严格的提示创建一个简单的游戏。游戏将通过提示 onclick 向您提问。提示会问您一个问题,您将在空白字段中输入您的“猜测”。如果你猜对了,它会再次提示“你赢了!”,如果你猜错了,它会提示“再试一次!”在同一提示中包含以下问题。共有五种猜测,如果你最后没有猜到,会出现提示“你输了!”
这就是要点。 if else 语句和这里的大部分函数代码直接来自我的导师,但我不确定我需要什么才能让提示显示“你是对的!”。我尝试了各种方法,但每次我在提示中输入正确答案以进行测试时,都会出现 Null。
如果我能弄清楚如何通过得到正确答案来结束游戏,那么我想我应该能够弄清楚如何重复问题提示功能。
<!doctype html>
<html>
<head>
<title>Camping Trip!</title>
<meta charset="utf-8">
<link type="style" src="style.css">
</head>
<body>
<br><br><br><br><br><br><br>
<center><h1>"I'm going on a camping trip..."</h1></center>
<br>
<!-- This is the Start Game button that begins the game-->
<center>
<button onclick="gamePlay()">Start Game!</button>
</center>
<!-- This is the Tutorial button that shows the rules-->
<br>
<center>
<button id="show"onclick="startTutorial()">Show Tutorial</button>
<p id="rules"></p>
</center>
<!-- This is the Hide Tutorial Button-->
<center>
<button id="hide">Hide Tutorial</button>
</center>
<!-- This is the Camping Trip picture-->
<center>
<img class="irc_mi" style="margin-top: 141px;" src="http://i10.photobucket.com/albums/a146/dizzybint78/tent.gif" width="600" height="333">
</center>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
alert("Welcome to the Caming Trip game!");
function startTutorial() {
document.getElementById("rules").innerHTML = "<strong>Rules:</strong><br><br>The rules are simple! Initially, you will be given a clue, and an attempt to answer. There will be five clues. All five clues will follow a certain pattern, trait, or category. It's up to you to find out what that is!";
}
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
var gameState = 0;
function gamePlay() {
if (gameState == 0) {
gameQ1();
}
else if (gameState == 1) {
gameQ2();
}
else if (gameState == 2) {
gameQ3();
}
else if (gameState == 3) {
gameQ4();
}
else if (gameState == 4) {
gameQ5();
}
else if (gameState == 5) {
prompt("Sorry! Try Again!");
}
}
function gameQ1() {
var answer = "fruit"
var guess=prompt("I'm going on a camping trip, and I'm bringing an Apple.\n\nAnswer:"); {
return prompt(guess);
}
if (prompt.guess == answer) {
alert("You've Won!");
document.refresh;
}
else (gameState ++)
}
【问题讨论】:
标签: javascript variables if-statement var prompt