我正在做一个简单的游戏。基本上玩家必须选择正确的数学等式,该等式等于价格。游戏开始时会出现三种随机价格,玩家必须选择正确数学方程旁边的按钮。我正在努力的是添加分数并在选择按钮时弹出CORRECT或FALSE横幅。我尝试了一些不同的东西,但似乎没有任何工作,除非有一点javascript丢失。
这是HTML
<!DOCTYPE html>
<html>
<head>
<title>How much are the tickets?</title>
<!-- These are the links for The script and CSS-->
<script src="scripting.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styling.css"/>
</head>
<!--This is the body with the default background I chose-->
<body onload="disablebuttons()" background="background3.gif">
<!--The game and the timer will not begin until the user clicks the button-->
<div><input type = "button" value = "Begin the Game" onClick = "start();" id="button3"></div>
<!--The Winner/Loser block that displays if you win/lose-->
<div id="blockparty"></div>
<!--This is the ticketman-->
<div id = "TM">
<img src="tm.gif"> </img>
</div>
<!--This is the speech bubble where the price of the ticket will appear when the game begins-->
<div id = "speechbubble">
<img src = "bubble1.gif" id="speechbubble1">
</div>
<!--These are the tickets where the maths equation will be-->
<div id = "ticket1">
<img src = "ticket1.gif" id="equation1">
</div>
<div id = "ticket2">
<img src = "ticket2.gif" id="equation2">
</div>
<div id = "ticket3">
<img src = "ticket3.gif" id="equation3">
</div>
<!--These are the tickets where the maths equation will be-->
<div><input type = "button" value = "1" onClick = "one();" id="tktbtn1"></div>
<div><input type = "button" value = "2" onClick = "two();" id="tktbtn2"></div>
<div><input type = "button" value = "3" onClick = "three();" id="tktbtn3"></div>
<!--This is the score, I set it to 0 so it displays when the page loads, it's a placeholder until the draw button is hit, the draw function is then targeted and it writes the score. It will either add 5 or take away 10 depending on if the player wins or loses.-->
<div id="scored">0</div>
<!--This is the reset score button. I made it a p tag and in my css I made it so that when you hover over it the cursor will appear as a pointer-->
<p id="resetscore" onclick="resetscore();"><u>Reset</u></p>
<!--The Winner/Loser block that displays if you win/lose-->
<div id="blockparty"></div>
</div>
</div>
</body>
这是我的javascript - 我已经包含了我在javascript中尝试做的事情的描述。
/*These are the vars that I used in my project.
User = The Player
Comp = The Computer
Chosen = The ticket selected
Score = Used to add/minus the score
*/
var user;
var comp;
var chosen;
var score = 0;
//* This function disables the buttons at the beginning*/
function disablebuttons(){
document.getElementById("tktbtn1").disabled= true;
document.getElementById("tktbtn2").disabled= true;
document.getElementById("tktbtn3").disabled= true;
document.getElementById("resetscore").disabled= true;
}
/* This is my start button function, it calls on the computer once the start button is selected*/
function start(){
comp = Math.floor(Math.random()*3)+1;
document.getElementById('speechbubble1').src = 'bubble/price' + comp + '.gif';
document.getElementById("tktbtn1").disabled= false;
document.getElementById("tktbtn2").disabled= false;
document.getElementById("tktbtn3").disabled= false;
document.getElementById("resetscore").disabled= false;
document.getElementById('equation1').src = 'tickets/maths1.gif';
document.getElementById('equation2').src = 'tickets/maths2.gif';
document.getElementById('equation3').src = 'tickets/maths3.gif';
}
/* This function puts everything back to normal 5 seconds after the player selected their answer*/
function reset(){
document.getElementById('blockparty').style.display = "none";
document.getElementById('speechbubble1').src = "bubble/price" + comp + '.gif';
document.getElementById("tktbtn1").disabled= true;
document.getElementById("tktbtn2").disabled= true;
document.getElementById("tktbtn3").disabled= true;
document.getElementById("resetscore").disabled= true;
document.getElementById('equation1').src = 'tickets/maths1.gif';
document.getElementById('equation2').src = 'tickets/maths2.gif';
document.getElementById('equation3').src = 'tickets/maths3.gif';
}
/*
This add either 5 or 0 to the score depending on the result,
It displays a block saying: Correct/False
并且它还会在5秒后设置超时,然后将所有内容恢复正常,如上所示* /
function begin(){
user = Math.floor(Math.random()*3)+1;
document.getElementById('player').src = 'bubble/price' +user+ '.gif';
document.getElementById('blockparty').style.display="block";
document.getElementById("tktbtn1").disabled= true;
document.getElementById("tktbtn2").disabled= true;
document.getElementById("tktbtn3").disabled= true;
document.getElementById("resetscore").disabled= true;
document.getElementById('equation1').src = 'tickets/maths1.gif';
document.getElementById('equation2').src = 'tickets/maths2.gif';
document.getElementById('equation3').src = 'tickets/maths3.gif'; }
if (chosen == "correct1;" && "tktbtn1" + "price1"){
document.getElementById('blockparty').innerHTML= "CORRECT!";
document.getElementById("blockparty").style.backgroundColor="green";
document.getElementById("scored").innerHTML = score = score + 5;}
else if (chosen == "false1;" && "tktbtn3" + "price1"){
document.getElementById('blockparty').innerHTML= "FALSE!";
document.getElementById("blockparty").style.backgroundColor="red";
document.getElementById("scored").innerHTML = score = score + 0;}
else if (chosen == "false2;" && "tktbtn2" + "price1"){
document.getElementById('blockparty').innerHTML= "FALSE!";
document.getElementById("blockparty").style.backgroundColor="red";
document.getElementById("scored").innerHTML = score = score + 0;}
else if (chosen == "correct2;" && "tktbtn2" + "price2"){
document.getElementById('blockparty').innerHTML= "CORRECT!";
document.getElementById("blockparty").style.backgroundColor="green";
document.getElementById("scored").innerHTML = score = score + 5;}
else if (chosen == "false3;" && "tktbtn3" + "price2"){
document.getElementById('blockparty').innerHTML= "FALSE!";
document.getElementById("blockparty").style.backgroundColor="red";
document.getElementById("scored").innerHTML = score = score + 0;}
else if (chosen == "false4;" && "tktbtn1" + "price2"){
document.getElementById('blockparty').innerHTML= "FALSE!";
document.getElementById("blockparty").style.backgroundColor="red";
document.getElementById("scored").innerHTML = score = score + 0;}
else if (chosen == "correct3;" && "tktbtn3" + "price3"){
document.getElementById('blockparty').innerHTML= "CORRECT!";
document.getElementById("blockparty").style.backgroundColor="green";
document.getElementById("scored").innerHTML = score = score + 5;}
else if (chosen == "false5;" && "tktbtn1" + "price3"){
document.getElementById('blockparty').innerHTML= "FALSE!";
document.getElementById("blockparty").style.backgroundColor="red";
document.getElementById("scored").innerHTML = score = score + 0;}
else if (chosen == "false6;" && "tktbtn2" + "price3"){
document.getElementById('blockparty').innerHTML= "FALSE!";
document.getElementById("blockparty").style.backgroundColor="red";
document.getElementById("scored").innerHTML = score = score + 0;}
else {
document.getElementById("blockparty").innerHTML= "Loser!";
document.getElementById("blockparty").style.backgroundColor="red";
document.getElementById("scored").innerHTML = score = score + -10;}
setTimeout('reset()', 5000);
}
/*This sets the score back to 0 once the reset p tag is clicked. This prevents the score from adding up once the game is reset.*/
function resetscore(){
document.getElementById("scored").innerHTML = score = score = 0;
}
答案 0 :(得分:0)
您正在调用一个不存在的函数。你刚才没有加入它们吗?功能&#39;一个&#39;,&#39;两个&#39;和&#39;三个&#39;。例如:
<div><input type = "button" value = "1" onClick = "one();" id="tktbtn1"></div>
另外,你实际上从来没有打电话给'开始&#39;功能。即使你这样做,看起来在条件语句开始之前有一个大括号关闭它。