gpt4 book ai didi

javascript - 多按钮帮助。某些功能上出现的按钮

转载 作者:行者123 更新时间:2023-12-03 10:19:26 25 4
gpt4 key购买 nike

如何让某个功能执行后出现一个按钮?

我正在尝试创建一个简单的游戏,它有 2 个按钮,当程序首次运行时,它们会导致功能 2 和 3。

按下两个按钮之一并执行其中一项功能后,该按钮消失。然后我想在第二个和第三个功能上创建另一个按钮,允许玩家重玩游戏并退出游戏。问题是我不知道如何让按钮只出现在第二个功能或第三个功能上。

  <HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE="JavaScript">

var score;
score = 0


function mainmenu() {
var start


name = prompt("What is your name?")
document.write("Welcome " + name + " for playing game" + "<BR>")

}


function game() {
var ask
var repeat
var again

again = "yes"
while (again == "yes") {

document.write("Hello " + name + " Welcome to the game, you will decide on what action you will take depending on the situation. You will either gain or loose score depending on your decision made. GLHF!" + "<br>" + "<BR>")
document.write(" You enter the dungeon and its pitch black, you can barely see your hand infront of your face. You have a pair of flint inside your pocket and you can attempt to make a torch by ripping a piece of your cloth. What do you do?" + "<BR>")
decision1 = (" Option 1) You brave the darkness" + "<br>")
document.write(decision1)
/* dont need to use all these's variables for decision 1-16*/
decision2 = ("Option 2) You attempt to make a torch" + "<BR>")
document.write(decision2)
decision3 = ("Option 3) You attempt to find a wall and let it guide you" + "<BR>")
document.write(decision3)
decision4 = ("Option 4) You run like an animal, screaming like an animal" + "<BR>")
document.write(decision3)
ask = prompt("Which decision do you choose? Choose from 1,2,3 and 4" + "<BR>")
if (ask == "1") {
document.write("You walk for a few minute without any event but you suddenly step onto a pot-hole and loose your grip. You injured your right knee. You'll live but its gonna hurt for a while" + "<BR>")
score = score + 20
}
if (ask == "2") {
document.write("You tore off a piece of cloth from your sholder and you write it around a stick you found nearby but ask you reach into your pocket to grab your flint, you dropped it. As you attempt to find it, you accidentally felt something sharp and you started to bleed from your left hand. Tought luck m8" + "<BR>")
score = score - 30
}
if (ask == "3") {
document.write("You glide your hand agaist the wall, after half and hour of nothing happening. You finally found a ray of light at the end of the tunnel" + "<BR>")
score = score + 50
}
if (ask == "4") {
document.write("You started running and you scream your lungs out. You took 3 steps and you fell down face first. What the hell were you thinking?" + "<BR>")
score = score - 50
}
document.write("Your current score is " + score + "<BR>")


document.write(" As you finally walk past the darkness, you stumble upon a bright litted room. In the middle of the room, filled a chest. You took a survey around the room and you notice there's nothing in the room except the chest. What to do you?" + "<BR>" + "<BR>")
decision5 = ("Option 1) You rush towards the treasure chest and open it. Gotta get dat bootie!" + "<br>")
document.write(decision5)
/* dont need to use all these's variables for decision 1-16*/
decision6 = ("Option 2) You take a more carefull look around the room." + "<BR>")
document.write(decision6)
decision7 = ("Option 3) You leave the chest alone and walk straight past it and into the next room" + "<BR>")
document.write(decision7)
decision8 = ("Option 4) You kick the chest" + "<BR>")
document.write(decision8)
ask = prompt("Which decision do you choose? Choose from 5,6,7 and 8" + "<BR>")
if (ask == "1") {
document.write("You rush towards the chest with full speed and suddenly, half a dozen arrow wizzez past you. Unfortunatly, you've been hit around the sholder. You decided its too dangerous and you cut your losses and enter the next room" + "<BR>")
score = score - 50
}
if (ask == "2") {
document.write("As you walk to the side of the room, you notice there are dozens of small holes placed infront of the the treasure chest. You also notice there are pressure plates suinfront of the chest. You walk behind the chest and opened it and you fond that its completely empty!. So much for bootie." + "<BR>")
score = score + 50
}
if (ask == "3") {
document.write("You decided to play it safe and walk past the treasure chest. Nothing happens" + "<BR>")
score = score + 0
}
if (ask == "4") {
document.write("You decided to kick the chest for whatever reason. You hurt your foot during the process. Why would you do that?" + "<BR>")
score = score - 20
}
document.write("Your current score is " + score + "<BR>")


document.write(" As you continue along your journey, you stumble upon an injured adventurer! You rush to his side and he claims that his right leg is broken. He's seems to be carrying a rather heavy rucksack. You cant carry him with you because he's simply too heavy. " + "<BR>" + "<BR>")
decision9 = (" Option 1) You perform some medical treatment on him. Though you dont have much experience on medicine." + "<br>")
document.write(decision9)
/* dont need to use all these's variables for decision 1-16*/
decision10 = ("Option 2) You ignore him." + "<BR>")
document.write(decision10)
decision11 = ("Option 3) You search for his bag and see if there is anything useful to help him" + "<BR>")
document.write(decision11)
decision12 = ("Option 4) You end his misery. He's not gonna last long here anyway." + "<BR>")
document.write(decision12)
ask = prompt("Which decision do you choose? Choose from 9,10,11 and 12" + "<BR>")
if (ask == "1") {
document.write("you attempt to fix the adventurer's leg by applying some ointment onto his leg. It wont help much but at least it's something" + "<BR>")
score = score + 10
}
if (ask == "2") {
document.write("You walk past him and ignore his plea of him. There's nothing you can do to help him" + "<BR>")
score = score + 0
}
if (ask == "3") {
document.write("As you run through his bad you found a stick! Looks like he's in a sticky situation :D, you tied the stick to the adventurer's leg. It seems to be sturdy enough to hold him. He thanks you and he goes to his merry ways." + "<BR>")
score = score + 40
}
if (ask == "4") {
document.write("You decide to end his misery by knocking him in the back. He falls dead on the ground the moment your hand make contact. How this work i will nover know. Just roll with it" + "<BR>")
score = score - 20
}


document.write(" At the end of the room a giant lion statue gazes upon you. He ask you a quiz. What walks on 4 legs, then 2 and finally 3. " + "<BR>" + "<BR>")
decision13 = (" Option 1) AN ANIMAL!" + "<br>")
document.write(decision13)
/* dont need to use all these's variables for decision 1-16*/
decision14 = ("Option 2) A CHAIR!" + "<BR>")
document.write(decision14)
decision15 = ("Option 3) A MAN!" + "<BR>")
document.write(decision15)
decision16 = ("Option 4) I dunno" + "<BR>")
document.write(decision16)
ask = prompt("Which decision do you choose? Choose from 13,14,15 and 16" + "<BR>")
if (ask == "1") {
document.write("NOPE, the lion statue slowly disapears and behind it leads to the outside world" + "<BR>")
score = score - 10
}
if (ask == "2") {
document.write("NOPE, the lion statue slowly disapears and behind it leads to the outside world" + "<BR>")
score = score - 10
}
if (ask == "3") {
document.write("Correct!, the lion statue slowly disapears and behind it leads to the outside world." + "<BR>")
score = score + 10
}
if (ask == "4") {
document.write("Your not even gonna try? The lion's head shook in disapointment and slowly disapears. Behind it leads to the outside world" + "<BR>")
score = score - 20
}
document.write("Your current score is " + score)
again = prompt("would you like to play again?")
}

leaderboard()

}
function leaderboard() {

var n, temp;
ok = false;

var leaderboardarray = new Array(5);

leaderboardarray[0] = 50;
leaderboardarray[1] = 60;
leaderboardarray[2] = 130;
leaderboardarray[3] = 120;
leaderboardarray[4] = score;

leaderboardarray.sort(function (a, b) {
return b - a
});
var myContent = '';
for (var n = 0; n < 5; n++)
myContent += "score: " + leaderboardarray[n] + "<br>";

document.getElementById("leaderBoard").innerHTML = myContent;
document.write("Your current score is " + score)
}
leaderboard()

</SCRIPT>
</HEAD>
<BODY style="background-color:#cccccc;">
<INPUT NAME="dobutton" TYPE="button" value="Start game" onclick=game()>
<INPUT NAME="dobutton" TYPE="button" value="Leaderboard" onclick=leaderboard()>

<H2>The Foundation Page </H2>
<HR>
<div id="leaderBoard">
<script type="text/javascript">mainmenu();</script>
</div>

<BR>
</BODY>
</HTML>

最佳答案

您可以为这两个按钮分配广告 ID,并隐藏第二个按钮:

<INPUT ID="btn1" NAME="dobutton" TYPE="button" value="Start game" onclick="game()">
<INPUT ID="btn2" NAME="dobutton" TYPE="button" value="Leaderboard" onclick="leaderboard()" style="display:none;">

然后您可以通过以下方式显示或隐藏它们:

document.getElementById("btn1").style.display = "none"; /// first button disappear
document.getElementById("btn2").style.display = "inline"; /// second button appear

关于javascript - 多按钮帮助。某些功能上出现的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29715039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com