Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个非常基本的Javascript游戏中,在哪里声明变量是个难题_Javascript_Variables_Methods - Fatal编程技术网

在一个非常基本的Javascript游戏中,在哪里声明变量是个难题

在一个非常基本的Javascript游戏中,在哪里声明变量是个难题,javascript,variables,methods,Javascript,Variables,Methods,我一直在学习一门基本的Pluralsight Javascript课程,最后我需要使用以下工具制作一个简单的基于文本的游戏: confirm() prompt() alert() If/else math.round() math.random() Operators like === etc. 我想出了一个场景(这就是我试图实现的),玩家面对狼,通过提示符()可以选择聊天、攻击或奔跑,并在背景中生成一个四舍五入的随机数。根据提示符中返回的内容,将运行以下命令: a) 如果响应为run,则会弹

我一直在学习一门基本的Pluralsight Javascript课程,最后我需要使用以下工具制作一个简单的基于文本的游戏:

confirm()
prompt()
alert()
If/else
math.round()
math.random()
Operators like === etc.
我想出了一个场景(这就是我试图实现的),玩家面对狼,通过提示符()可以选择聊天、攻击或奔跑,并在背景中生成一个四舍五入的随机数。根据提示符中返回的内容,将运行以下命令:

a) 如果响应为run,则会弹出一个confirm()窗口,询问播放机是否确定要执行此操作

  • 如果返回的值为真,且随机数等于0,则警告玩家他逃走(幸存)
  • 如果返回的值为真,但随机数等于q,则玩家会收到死亡警告(被狼吃掉)。
    • 如果该值为false,则提醒玩家他已死亡
b) 如果对提示的响应是攻击,则提示玩家生存

c) 如果响应为chat,则会提醒玩家他已死亡

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.")
    }
confirm()
是一个函数,因此它一出现就运行并向
response
返回一个值。在本例中,它出现在您为响应播放机输入而编写的关键条件语句之前

由于您希望确认对话框仅在玩家选择“运行”选项时出现,因此您对该函数的调用应在该点出现。在您的情况下,这基本上意味着只需将其向下移动到
if(choice===run)
行的正下方,以便在您知道播放器已决定运行之后,但在您知道确认对话框的结果之前


需要注意的一点是,
choice
变量将是一个字符串,因为
prompt()
返回的是字符串。因此,当您使用
==
检查条件中的等价性时,请确保运算符的右侧也是字符串。这意味着您可能打算编写
(choice==“run”)
,而不是
(choice==run)
。否则,当计算机在代码中看到
(choice==run)
时,它会看到
run
,并认为您所说的是名为
run
的变量,而不是run这个词。只有在代码中的其他地方声明了
var run=“run”
时,它才会起作用。

实际上不必用任何东西初始化变量

所以你可以这样做:

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;
}

太好了,谢谢你。好的观点。我还没学会关于“跑”的弦乐。我现在进入switch语句,所以这是有意义的。