我一直在做一个基本的Pluralsight Javascript课程,最后我需要使用以下方法制作一个简单的基于文本的游戏:
confirm()
prompt()
alert()
If/else
math.round()
math.random()
Operators like === etc.
我想出了一个场景(这就是我想要实现的),其中玩家面对狼,并通过提示符()可以选择聊天,攻击或运行,并在后台生成一个舍入的随机数。根据提示中返回的内容,以下内容将运行:
a)如果响应是运行,则会弹出一个confirm()窗口,询问玩家是否确定要这样做。
b)如果对提示的响应为攻击,则会提醒玩家他幸存下来。
c)如果回复是聊天,则会提醒玩家他已经死亡。
d)如果回复是其他,他会被警告他已经死亡。
我的问题是如何构建代码。我理解在我的脚本开头声明所有变量是一种好习惯。但是我该如何宣布:
var response = confirm ("Are you sure you want to do that? It's risky");
但当时没有运行它(我只想在玩家返回' run')时运行它。
我的问题可能会更清楚,你看我的代码发布在我的代码下面。请帮忙!
alert("You're off to see Grandma and you're wandering through a deep dark forest. Suddenly you feel hot breath on the back of your neck and smell the unmistakeable stench of canine. You turn around - it's the wolf!");
var choice = prompt("His piercing red eyes stare through you. He steps closer. What do you do - chat, attack or run?");
var randomNumber = Math.round(Math.random());
//The line below I only want to run if "run" is the value returned on the prompt() above.
var response = confirm ("Are you sure you want to do that? It's risky");
if (choice === run)
if (response === true && randomNumber === 0){
alert("Wow - you made it to Grandma's. Well done - you're safe!");
}
else if (response === true && randomNumber === 1){
alert("Oh - too bad! You couldn't out run the wolf - he catches you and eats you!")
}
else{
alert("Oh no - indecisive! While you're standing there trying to choose what to do, the wolf pounces and - BAM! You're dinner.");
}}
else if (choice === attack){
alert("Brave move - you hit the wolf over the head with a stick and run to Grandma's. You're out of trouble!")
}
else if (choice === chat){
alert("The wolf invites you in for a cup of tea and some biscuits. After a nice chat about the weather, he eat you. Bad move.")
}
else {
alert("You choose to" + " " + choice + "?" + " " + "- what fairytale are you in? Weird. The wolf eats you. You're dead.")
}
答案 0 :(得分:0)
confirm()
是一个函数,因此只要它出现就会运行并返回值response
。在这种情况下,在之前出现您为了响应播放器输入而编写的关键条件语句。
由于您希望仅在玩家选择“运行”选项时才显示确认对话框,因此您应该在该点显示对该功能的调用。在你的情况下,这基本上意味着只是将其向下移动到if (choice === run)
线以下,以便在之后你知道玩家决定在之前知道确认对话框的结果。
要注意的一件事 - 变量choice
将是一个String,因为prompt()
返回的是。因此,当您使用===
检查条件中的等效性时,请确保运算符的右侧也是字符串。这意味着您可能需要写(choice === "run")
而不是(choice === run)
。否则,当计算机在您的代码中看到(choice === run)
时,它会查看run
并认为您正在谈论名为run
的变量而不是< em> word 运行。它只有在你的代码中的其他地方声明var run = "run"
时才有效。
答案 1 :(得分:0)
您实际上不必使用任何内容初始化变量。
所以你可以这样做:
var response, choice, randomNumber;
randomNumber = Math.round(Math.random());
// introduce the game
alert("You're off to see Grandma and you're wandering through a deep dark forest. Suddenly you feel hot breath on the back of your neck and smell the unmistakeable stench of canine. You turn around - it's the wolf!");
// first player choice
choice = prompt("His piercing red eyes stare through you. He steps closer. What do you do - chat, attack or run?");
// game logic
if (choice === 'run') {
response = confirm ("Are you sure you want to do that? It's risky");
if (response === true && randomNumber === 0){
alert("Wow - you made it to Grandma's. Well done - you're safe!");
}
// the rest of your code as-is
}
另外两点:
您很可能想要检查choice
是否为特定字符串。字符串必须用引号括起来,否则它被视为变量。
因此if (choice === run)
应更改为if (choice === 'run')
。
另一点是你可以使用一个switch语句(这不是必需的,你完全没问题的方式,只是认为我会给你提供其他选项),如下所示:
switch (choice) {
case 'run':
// code to handle run
break;
case 'attack':
// code to handle break
break;
case 'chat':
// code to handle chat
break;
}