2015-10-20 26 views
0

我試圖用嚴格的提示來創建一個簡單的遊戲。遊戲將通過提示onclick向您提問。提示會問你一個問題,你會在空白處輸入你的'猜測'。如果你猜對了,它會再次提示「你贏了!」,如果你猜錯了,它會提示「再試一次!」在同一個提示符下有下列問題。有五個猜測,如果你最終沒有猜測,提示會出現「你輸了!」正確使用var x = prompt()?

這就是要點。 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 ++) 
 
}

回答

0

你有東西,看起來像在後提示功能代碼塊的嘗試,即不應該存在。它被解釋爲內聯代碼塊,因此return將退出gameQ1函數。

var guess=prompt("I'm going on a camping trip, and I'm bringing an Apple.\n\nAnswer:"); { 
return prompt(guess); 
} 

應該只是:

var guess=prompt("I'm going on a camping trip, and I'm bringing an Apple.\n\nAnswer:"); 

當你檢查你使用的是可變的,就好像它是prompt函數對象的屬性的結果。這只是一個變量。

if (prompt.guess == answer) { 

應該是:

if (guess == answer) { 

當您嘗試重新加載您剛剛訪問功能的頁面,而不是調用它。

document.refresh; 

應該是:

document.refresh(); 

這應該讓你開始。我可以告訴你,你需要一個循環來繼續在gamePlay函數中運行代碼,以便可以遍歷遊戲狀態。

+0

非常感謝Guffa。實際上,我是第一個五年的媒體專業學生。我確信在可預見的將來我的代碼中會出現漏洞,但大多數情況下,我覺得我已經能夠通過試用找出我的大部分問題,錯誤。這一個沒那麼多哈哈。到達那裏! –